Skip to content

Commit

Permalink
PostClass.ImageUrlにIResponsiveImageUriを使用する
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed Jun 22, 2024
1 parent 9e85e2f commit 5661b56
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void CreateFromNote_LocalNoteTest()
Assert.Equal(new MisskeyUserId("ghijkl"), post.UserId);
Assert.Equal("bar", post.ScreenName);
Assert.Equal("bar", post.Nickname);
Assert.Equal("", post.ImageUrl);
Assert.Null(post.ImageUrl);
Assert.Null(post.RetweetedId);
Assert.Null(post.RetweetedBy);
Assert.Null(post.RetweetedByUserId);
Expand Down
23 changes: 0 additions & 23 deletions OpenTween.Tests/SocialProtocol/Twitter/TwitterLegacyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,28 +302,5 @@ public void GetTextLengthRemain_BrokenSurrogateTest()
Assert.Equal(278, twitter.GetTextLengthRemain("\ud83d"));
Assert.Equal(9999, twitter.GetTextLengthRemain("D twitter \ud83d"));
}

[Theory]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", "normal", "https://pbs.twimg.com/profile_images/00000/foo_normal.jpg")]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", "bigger", "https://pbs.twimg.com/profile_images/00000/foo_bigger.jpg")]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", "mini", "https://pbs.twimg.com/profile_images/00000/foo_mini.jpg")]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", "original", "https://pbs.twimg.com/profile_images/00000/foo.jpg")]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal_bar_normal.jpg", "original", "https://pbs.twimg.com/profile_images/00000/foo_normal_bar.jpg")]
public void CreateProfileImageUrl_Test(string normalUrl, string size, string expected)
=> Assert.Equal(expected, TwitterLegacy.CreateProfileImageUrl(normalUrl, size));

[Fact]
public void CreateProfileImageUrl_InvalidSizeTest()
=> Assert.Throws<ArgumentException>(() => TwitterLegacy.CreateProfileImageUrl("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", "INVALID"));

[Theory]
[InlineData(24, "mini")]
[InlineData(25, "normal")]
[InlineData(48, "normal")]
[InlineData(49, "bigger")]
[InlineData(73, "bigger")]
[InlineData(74, "original")]
public void DecideProfileImageSize_Test(int sizePx, string expected)
=> Assert.Equal(expected, TwitterLegacy.DecideProfileImageSize(sizePx));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void CreateFromStatus_Test()
Assert.Equal(new TwitterUserId(status.User.IdStr), post.UserId);
Assert.Equal("tetete", post.ScreenName);
Assert.Equal("ててて", post.Nickname);
Assert.Equal("https://example.com/profile.png", post.ImageUrl);
Assert.Equal(new TwitterProfileImageUri("https://example.com/profile.png"), post.ImageUrl);
Assert.False(post.IsProtect);
Assert.False(post.IsOwl);
Assert.False(post.IsMe);
Expand Down Expand Up @@ -324,7 +324,7 @@ public void CreateFromDirectMessageEvent_Test()
Assert.Equal(new TwitterUserId(otherUser.IdStr), post.UserId);
Assert.Equal("tetete", post.ScreenName);
Assert.Equal("ててて", post.Nickname);
Assert.Equal("https://example.com/profile.png", post.ImageUrl);
Assert.Equal(new TwitterProfileImageUri("https://example.com/profile.png"), post.ImageUrl);
Assert.False(post.IsProtect);
Assert.True(post.IsOwl);
Assert.False(post.IsMe);
Expand Down
30 changes: 14 additions & 16 deletions OpenTween/ImageCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
using OpenTween.Connection;
using OpenTween.Models;
using OpenTween.SocialProtocol.Twitter;

namespace OpenTween
Expand Down Expand Up @@ -85,24 +82,25 @@ public long CacheCount
/// <param name="address">取得先の URL</param>
/// <param name="force">キャッシュを使用せずに取得する場合は true</param>
/// <returns>非同期に画像を取得するタスク</returns>
public Task<MemoryImage> DownloadImageAsync(string address, bool force = false)
public Task<MemoryImage> DownloadImageAsync(Uri address, bool force = false)
{
var cancelToken = this.cancelTokenSource.Token;
var addressStr = address.AbsoluteUri;

this.InnerDictionary.TryGetValue(address, out var cachedImageTask);
this.InnerDictionary.TryGetValue(addressStr, out var cachedImageTask);

if (cachedImageTask != null && !force)
return cachedImageTask;

cancelToken.ThrowIfCancellationRequested();

var imageTask = Task.Run(() => this.FetchImageAsync(address, cancelToken));
this.InnerDictionary[address] = imageTask;
this.InnerDictionary[addressStr] = imageTask;

return imageTask;
}

private async Task<MemoryImage> FetchImageAsync(string uri, CancellationToken cancelToken)
private async Task<MemoryImage> FetchImageAsync(Uri uri, CancellationToken cancelToken)
{
try
{
Expand Down Expand Up @@ -130,24 +128,24 @@ private MemoryImage CreateBlankImage()
return MemoryImage.CopyFromImage(bitmap);
}

public MemoryImage? TryGetFromCache(string address)
public MemoryImage? TryGetFromCache(Uri address)
{
if (!this.InnerDictionary.TryGetValue(address, out var imageTask) ||
var addressStr = address.AbsoluteUri;

if (!this.InnerDictionary.TryGetValue(addressStr, out var imageTask) ||
imageTask.Status != TaskStatus.RanToCompletion)
return null;

return imageTask.Result;
}

public MemoryImage? TryGetLargerOrSameSizeFromCache(string normalUrl, string size)
public MemoryImage? TryGetLargerOrSameSizeFromCache(IResponsiveImageUri responseImageUri, int minSizePx)
{
var sizes = new[] { "mini", "normal", "bigger", "original" };
var minimumIndex = sizes.FindIndex(x => x == size);
var imageUris = responseImageUri.GetImageUriLargerOrSameSize(minSizePx);

foreach (var candidateSize in sizes.Skip(minimumIndex))
foreach (var imageUri in imageUris)
{
var imageUrl = TwitterLegacy.CreateProfileImageUrl(normalUrl, candidateSize);
var image = this.TryGetFromCache(imageUrl);
var image = this.TryGetFromCache(imageUri);
if (image != null)
return image;
}
Expand Down
5 changes: 2 additions & 3 deletions OpenTween/ListManage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,16 +342,15 @@ private async void UserList_SelectedIndexChanged(object sender, EventArgs e)
}
}

private async Task LoadUserIconAsync(Uri imageUri, PersonId userId)
private async Task LoadUserIconAsync(IResponsiveImageUri imageUri, PersonId userId)
{
var oldImage = this.UserIcon.Image;
this.UserIcon.Image = null;
oldImage?.Dispose();

await this.UserIcon.SetImageFromTask(async () =>
{
var sizeName = TwitterLegacy.DecideProfileImageSize(this.UserIcon.Width);
var uri = TwitterLegacy.CreateProfileImageUrl(imageUri.AbsoluteUri, sizeName);
var uri = imageUri.GetImageUri(this.UserIcon.Width);

using var imageStream = await Networking.Http.GetStreamAsync(uri);
var image = await MemoryImage.CopyFromStreamAsync(imageStream);
Expand Down
6 changes: 1 addition & 5 deletions OpenTween/Models/PostClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenTween.Thumbnail;

namespace OpenTween.Models
Expand All @@ -53,7 +49,7 @@ double Latitude
/// <summary>スクリーンリーダーでの読み上げを考慮したテキスト</summary>
public string AccessibleText { get; init; } = "";

public string? ImageUrl { get; init; }
public IResponsiveImageUri? ImageUrl { get; init; }

public string ScreenName { get; init; } = "";

Expand Down
46 changes: 46 additions & 0 deletions OpenTween/SocialProtocol/Misskey/MisskeyAvatarUri.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// OpenTween - Client of Twitter
// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// All rights reserved.
//
// This file is part of OpenTween.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.

#nullable enable

using System;
using System.IO;
using OpenTween.Models;

namespace OpenTween.SocialProtocol.Misskey
{
public record MisskeyAvatarUri(
string ImageUriStr
) : IResponsiveImageUri
{
public Uri GetImageUri(int sizePx)
=> new(this.ImageUriStr);

public Uri[] GetImageUriLargerOrSameSize(int minSizePx)
=> new Uri[] { new(this.ImageUriStr) };

public Uri GetOriginalImageUri()
=> new(this.ImageUriStr);

public string GetFilename()
=> Path.GetFileName(this.GetOriginalImageUri().AbsolutePath);
}
}
2 changes: 1 addition & 1 deletion OpenTween/SocialProtocol/Misskey/MisskeyPostFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public PostClass CreateFromNote(MisskeyNote note, MisskeyAccountState accountSta
UserId = originalNoteUserId,
ScreenName = originalNoteUserAcct,
Nickname = originalNoteUser.Name ?? originalNoteUser.Username,
ImageUrl = originalNoteUser.AvatarUrl ?? "",
ImageUrl = originalNoteUser.AvatarUrl is { } avatarUrl ? new MisskeyAvatarUri(avatarUrl) : null,

// renotedNote から生成
RetweetedId = renotedNote?.Id is { } renotedId ? new MisskeyNoteId(renotedId) : null,
Expand Down
28 changes: 0 additions & 28 deletions OpenTween/SocialProtocol/Twitter/TwitterLegacy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -899,34 +899,6 @@ int GetWeightFromCodepoint(int codepoint)
return remainWeight / config.Scale;
}

/// <summary>
/// プロフィール画像のサイズを指定したURLを生成
/// </summary>
/// <remarks>
/// https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/user-profile-images-and-banners を参照
/// </remarks>
public static string CreateProfileImageUrl(string normalUrl, string size)
{
return size switch
{
"original" => normalUrl.Replace("_normal.", "."),
"normal" => normalUrl,
"bigger" or "mini" => normalUrl.Replace("_normal.", $"_{size}."),
_ => throw new ArgumentException($"Invalid size: ${size}", nameof(size)),
};
}

public static string DecideProfileImageSize(int sizePx)
{
return sizePx switch
{
<= 24 => "mini",
<= 48 => "normal",
<= 73 => "bigger",
_ => "original",
};
}

public bool IsDisposed { get; private set; } = false;

protected virtual void Dispose(bool disposing)
Expand Down
4 changes: 2 additions & 2 deletions OpenTween/SocialProtocol/Twitter/TwitterPostFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public PostClass CreateFromStatus(
var screenName = string.Intern(originalStatusUser.ScreenName);
var nickname = string.Intern(originalStatusUser.Name);
var imageUrl = originalStatusUser.ProfileImageUrlHttps is { } profileImageUrl
? string.Intern(profileImageUrl)
? new TwitterProfileImageUri(string.Intern(profileImageUrl))
: null;

// Source整形
Expand Down Expand Up @@ -268,7 +268,7 @@ bool firstLoad
var screenName = string.Intern(displayUser.ScreenName);
var nickname = string.Intern(displayUser.Name);
var imageUrl = displayUser.ProfileImageUrlHttps is { } imageUrlStr
? string.Intern(imageUrlStr)
? new TwitterProfileImageUri(string.Intern(imageUrlStr))
: null;

var source = (string?)null;
Expand Down
17 changes: 8 additions & 9 deletions OpenTween/TimelineListViewDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ private void DrawListViewItemStateIcon(Graphics g, PostClass post, Rectangle sta

private void DrawListViewItemProfileImage(Graphics g, PostClass post, Size scaledIconSize, Rectangle iconRect)
{
if (scaledIconSize.Width <= 0)
var sizePx = scaledIconSize.Width;
if (sizePx <= 0)
return;

var normalImageUrl = post.ImageUrl;
if (MyCommon.IsNullOrEmpty(normalImageUrl))
var imageUri = post.ImageUrl;
if (imageUri == null)
return;

var sizeName = TwitterLegacy.DecideProfileImageSize(scaledIconSize.Width);
var cachedImage = this.iconCache.TryGetLargerOrSameSizeFromCache(normalImageUrl, sizeName);
var cachedImage = this.iconCache.TryGetLargerOrSameSizeFromCache(imageUri, minSizePx: sizePx);

if (cachedImage != null)
{
Expand All @@ -186,7 +186,7 @@ private void DrawListViewItemProfileImage(Graphics g, PostClass post, Size scale
// キャッシュにない画像の場合は読み込みが完了してから再描画する
async Task RefreshProfileImageLazy()
{
var success = await this.LoadProfileImage(normalImageUrl, sizeName);
var success = await this.LoadProfileImage(imageUri, sizePx);
if (!success)
return;

Expand All @@ -206,12 +206,11 @@ async Task RefreshProfileImageLazy()
}
}

private async Task<bool> LoadProfileImage(string normalImageUrl, string sizeName)
private async Task<bool> LoadProfileImage(IResponsiveImageUri responsiveImageUri, int sizePx)
{
try
{
var imageUrl = TwitterLegacy.CreateProfileImageUrl(normalImageUrl, sizeName);
await this.iconCache.DownloadImageAsync(imageUrl);
await this.iconCache.DownloadImageAsync(responsiveImageUri.GetImageUri(sizePx));

return true;
}
Expand Down
4 changes: 2 additions & 2 deletions OpenTween/Tween.cs
Original file line number Diff line number Diff line change
Expand Up @@ -972,8 +972,8 @@ private void NotifyNewPosts(PostClass[] notifyPosts, string soundFile, int addCo
var bText = sb.ToString();
if (MyCommon.IsNullOrEmpty(bText)) return;

var image = post.ImageUrl is { } imageUrl
? this.iconCache.TryGetFromCache(imageUrl)
var image = post.ImageUrl is { } responsiveImageUri
? this.iconCache.TryGetLargerOrSameSizeFromCache(responsiveImageUri, minSizePx: 0)
: null;

this.gh.Notify(nt, post.StatusId.Id, title.ToString(), bText, image?.Image);
Expand Down
Loading

0 comments on commit 5661b56

Please sign in to comment.