Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TwitterProfileImageUriクラスを追加 #376

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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.

using System;
using Xunit;

namespace OpenTween.SocialProtocol.Twitter
{
public class TwitterProfileImageUriTest
{
[Theory]
[InlineData(24, "mini")]
[InlineData(25, "normal")]
[InlineData(48, "normal")]
[InlineData(49, "bigger")]
[InlineData(73, "bigger")]
[InlineData(74, "original")]
public void SizeName_GetPreferredSize_Test(int sizePx, string expected)
{
var size = TwitterProfileImageUri.SizeName.GetPreferredSize(sizePx);
Assert.Equal(expected, size.Name);
}

[Fact]
public void SizeName_GetLargerOrSameSize_Test()
{
var expected = new[]
{
TwitterProfileImageUri.SizeName.Normal,
TwitterProfileImageUri.SizeName.Bigger,
TwitterProfileImageUri.SizeName.Original,
};
Assert.Equal(expected, TwitterProfileImageUri.SizeName.GetLargerOrSameSize(minSizePx: 48));
}

[Theory]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", 48, "https://pbs.twimg.com/profile_images/00000/foo_normal.jpg")]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", 73, "https://pbs.twimg.com/profile_images/00000/foo_bigger.jpg")]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", 24, "https://pbs.twimg.com/profile_images/00000/foo_mini.jpg")]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg", 100, "https://pbs.twimg.com/profile_images/00000/foo.jpg")]
[InlineData("https://pbs.twimg.com/profile_images/00000/foo_normal_bar_normal.jpg", 100, "https://pbs.twimg.com/profile_images/00000/foo_normal_bar.jpg")]
public void GetImageUri_Test(string normalUrl, int sizePx, string expected)
{
var responsiveImageUri = new TwitterProfileImageUri(normalUrl);
Assert.Equal(expected, responsiveImageUri.GetImageUri(sizePx).AbsoluteUri);
}

[Fact]
public void GetImageUriLargerOrSameSize_Test()
{
var responsiveImageUri = new TwitterProfileImageUri("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg");
var expected = new Uri[]
{
new("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg"),
new("https://pbs.twimg.com/profile_images/00000/foo_bigger.jpg"),
new("https://pbs.twimg.com/profile_images/00000/foo.jpg"),
};
Assert.Equal(expected, responsiveImageUri.GetImageUriLargerOrSameSize(minSizePx: 48));
}

[Fact]
public void GetOriginalImageUri_Test()
{
var responsiveImageUri = new TwitterProfileImageUri("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg");
Assert.Equal(new("https://pbs.twimg.com/profile_images/00000/foo.jpg"), responsiveImageUri.GetOriginalImageUri());
}

[Fact]
public void GetFileName_Test()
{
var responsiveImageUri = new TwitterProfileImageUri("https://pbs.twimg.com/profile_images/00000/foo_normal.jpg");
Assert.Equal("foo.jpg", responsiveImageUri.GetFilename());
}
}
}
6 changes: 4 additions & 2 deletions OpenTween/Api/DataModel/TwitterUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ public class TwitterUserEntity
[DataMember(Name = "profile_banner_url")]
public string ProfileBannerUrl { get; set; }

// ProfileImageUrlHttps が null になる場合があるらしい
// 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=33871
[DataMember(Name = "profile_image_url_https")]
public string ProfileImageUrlHttps { get; set; }
public string? ProfileImageUrlHttps { get; set; }

[DataMember(Name = "protected")]
public bool Protected { get; set; }
Expand Down Expand Up @@ -120,7 +122,7 @@ public static TwitterUser CreateUnknownUser()
IdStr = "0",
ScreenName = "?????",
Name = "Unknown User",
ProfileImageUrlHttps = "",
ProfileImageUrlHttps = null,
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion OpenTween/Api/GraphQL/TwitterGraphqlUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static string GetText(XElement elm, string name)
{
IdStr = GetText(userElm, "rest_id"),
Name = GetText(userLegacyElm, "name"),
ProfileImageUrlHttps = GetText(userLegacyElm, "profile_image_url_https"),
ProfileImageUrlHttps = GetTextOrNull(userLegacyElm, "profile_image_url_https"),
ScreenName = GetText(userLegacyElm, "screen_name"),
Protected = GetTextOrNull(userLegacyElm, "protected") == "true",
Verified = GetTextOrNull(userLegacyElm, "verified") == "true",
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
38 changes: 38 additions & 0 deletions OpenTween/Models/IResponsiveImageUri.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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;

namespace OpenTween.Models
{
public interface IResponsiveImageUri
{
public Uri GetImageUri(int sizePx);

public Uri[] GetImageUriLargerOrSameSize(int minSizePx);

public Uri GetOriginalImageUri();

public string GetFilename();
}
}
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
Loading