Skip to content

Commit

Permalink
TwitterUser.ProfileImageUrlHttpsをnullableに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed Jun 22, 2024
1 parent ebcba50 commit 483aadf
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
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
2 changes: 1 addition & 1 deletion OpenTween/Models/PostClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ double Latitude
/// <summary>スクリーンリーダーでの読み上げを考慮したテキスト</summary>
public string AccessibleText { get; init; } = "";

public string ImageUrl { get; init; } = "";
public string? ImageUrl { get; init; }

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

Expand Down
8 changes: 6 additions & 2 deletions OpenTween/SocialProtocol/Twitter/TwitterPostFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public PostClass CreateFromStatus(
// 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
var screenName = string.Intern(originalStatusUser.ScreenName);
var nickname = string.Intern(originalStatusUser.Name);
var imageUrl = string.Intern(originalStatusUser.ProfileImageUrlHttps);
var imageUrl = originalStatusUser.ProfileImageUrlHttps is { } profileImageUrl
? string.Intern(profileImageUrl)
: null;

// Source整形
var (sourceText, sourceUri) = ParseSource(originalStatus.Source);
Expand Down Expand Up @@ -265,7 +267,9 @@ bool firstLoad
// 他の発言と重複しやすい (共通化できる) 文字列は string.Intern を通す
var screenName = string.Intern(displayUser.ScreenName);
var nickname = string.Intern(displayUser.Name);
var imageUrl = string.Intern(displayUser.ProfileImageUrlHttps);
var imageUrl = displayUser.ProfileImageUrlHttps is { } imageUrlStr
? string.Intern(imageUrlStr)
: null;

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

var image = this.iconCache.TryGetFromCache(post.ImageUrl);
this.gh.Notify(nt, post.StatusId.Id, title.ToString(), bText, image?.Image, post.ImageUrl);
var image = post.ImageUrl is { } imageUrl
? this.iconCache.TryGetFromCache(imageUrl)
: null;

this.gh.Notify(nt, post.StatusId.Id, title.ToString(), bText, image?.Image);
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions OpenTween/TweetDetailsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public HtmlElement[] GetLinkElements()
.ToArray();
}

private async Task SetUserPictureAsync(string normalImageUrl, bool force = false)
private async Task SetUserPictureAsync(string? normalImageUrl, bool force = false)
{
if (MyCommon.IsNullOrEmpty(normalImageUrl))
return;
Expand Down Expand Up @@ -627,7 +627,7 @@ private void ContextMenuUserPicture_Opening(object sender, CancelEventArgs e)

this.ReloadIconToolStripMenuItem.Enabled = true;

if (this.IconCache.TryGetFromCache(this.CurrentPost.ImageUrl) != null)
if (this.CurrentPost.ImageUrl is { } imageUrl && this.IconCache.TryGetFromCache(imageUrl) != null)
{
this.SaveIconPictureToolStripMenuItem.Enabled = true;
}
Expand Down
11 changes: 3 additions & 8 deletions OpenTween/UserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,9 @@ public UserInfo(TwitterUser user)
this.ScreenName = user.ScreenName;
this.Location = WebUtility.HtmlDecode(user.Location);
this.Description = WebUtility.HtmlDecode(user.Description);
try
{
this.ImageUrl = new Uri(user.ProfileImageUrlHttps);
}
catch (Exception)
{
this.ImageUrl = null;
}
this.ImageUrl = user.ProfileImageUrlHttps is { } imageUrlStr
? new Uri(imageUrlStr)
: null;
this.Url = user.Url ?? "";
this.Protect = user.Protected;
this.FriendsCount = user.FriendsCount;
Expand Down
7 changes: 4 additions & 3 deletions OpenTween/UserInfoDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,12 @@ private async Task SetDescriptionAsync(string? descriptionText, TwitterEntities?
}
}

private async Task SetUserImageAsync(string imageUri, CancellationToken cancellationToken)
private async Task SetUserImageAsync(string? imageUri, CancellationToken cancellationToken)
{
var oldImage = this.UserPicture.Image;
this.UserPicture.Image = null;
oldImage?.Dispose();

// ProfileImageUrlHttps が null になる場合があるらしい
// 参照: https://sourceforge.jp/ticket/browse.php?group_id=6526&tid=33871
if (imageUri == null)
return;

Expand Down Expand Up @@ -468,6 +466,9 @@ private async void ButtonSearchPosts_Click(object sender, EventArgs e)
private async void UserPicture_Click(object sender, EventArgs e)
{
var imageUrl = this.displayUser.ProfileImageUrlHttps;
if (imageUrl == null)
return;

imageUrl = TwitterLegacy.CreateProfileImageUrl(imageUrl, "original");

await MyCommon.OpenInBrowserAsync(this, imageUrl);
Expand Down

0 comments on commit 483aadf

Please sign in to comment.