OSDN Git Service

関連発言取得時にPostIdの型に応じて使用するアカウントを選択する
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 9 Jun 2024 12:32:16 +0000 (21:32 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Mon, 10 Jun 2024 13:26:28 +0000 (22:26 +0900)
OpenTween/SocialProtocol/AccountCollection.cs
OpenTween/SocialProtocol/ISocialAccount.cs
OpenTween/SocialProtocol/InvalidAccount.cs
OpenTween/SocialProtocol/Twitter/TwitterAccount.cs
OpenTween/Tween.cs
OpenTween/TweetDetailsView.cs

index c667d55..8ff6004 100644 (file)
@@ -107,5 +107,26 @@ namespace OpenTween.SocialProtocol
 
             return this.Primary;
         }
+
+        public ISocialAccount? GetAccountForPostId(PostId postId, AccountKey? preferedAccountKey)
+        {
+            if (preferedAccountKey != null && this.accounts.TryGetValue(preferedAccountKey.Value, out var preferedAccount))
+            {
+                if (preferedAccount.CanUsePostId(postId))
+                    return preferedAccount;
+            }
+
+            var primaryAccount = this.Primary;
+            if (primaryAccount.CanUsePostId(postId))
+                return primaryAccount;
+
+            foreach (var account in this.SecondaryAccounts)
+            {
+                if (account.CanUsePostId(postId))
+                    return account;
+            }
+
+            return null;
+        }
     }
 }
index 7656c00..5f5a0f3 100644 (file)
@@ -46,5 +46,7 @@ namespace OpenTween.SocialProtocol
         public bool IsDisposed { get; }
 
         public void Initialize(UserAccount accountSettings, SettingCommon settingCommon);
+
+        public bool CanUsePostId(PostId postId);
     }
 }
index 594be08..802b046 100644 (file)
@@ -57,6 +57,9 @@ namespace OpenTween.SocialProtocol
         {
         }
 
+        public bool CanUsePostId(PostId postId)
+            => false;
+
         public void Dispose()
             => this.IsDisposed = true;
 
index 78cf6e0..aac7607 100644 (file)
@@ -89,6 +89,9 @@ namespace OpenTween.SocialProtocol.Twitter
             this.twLegacy.RestrictFavCheck = settingCommon.RestrictFavCheck;
         }
 
+        public bool CanUsePostId(PostId postId)
+            => postId is TwitterStatusId or TwitterDirectMessageId;
+
         public void Dispose()
         {
             if (this.IsDisposed)
index 9a2093b..e4a93e7 100644 (file)
@@ -8984,9 +8984,13 @@ namespace OpenTween
             var post = this.statuses[statusId];
             if (post == null)
             {
+                var account = this.GetAccountForPostId(statusId);
+                if (account == null)
+                    return;
+
                 try
                 {
-                    post = await this.CurrentTabAccount.Client.GetPostById(statusId, firstLoad: false);
+                    post = await account.Client.GetPostById(statusId, firstLoad: false);
                 }
                 catch (WebApiException ex)
                 {
@@ -8998,6 +9002,12 @@ namespace OpenTween
             await this.OpenRelatedTab(post);
         }
 
+        public ISocialAccount? GetAccountForPostId(PostId postId)
+        {
+            var preferedAccountKey = this.CurrentTab.SourceAccountKey;
+            return this.accounts.GetAccountForPostId(postId, preferedAccountKey);
+        }
+
         /// <summary>
         /// 指定されたツイートに対する関連発言タブを開きます
         /// </summary>
@@ -9013,7 +9023,11 @@ namespace OpenTween
 
             var tabName = this.statuses.MakeTabName("Related Tweets");
 
-            tabRelated = new RelatedPostsTabModel(tabName, this.CurrentTabAccount.UniqueKey, post)
+            var account = this.GetAccountForPostId(post.StatusId);
+            if (account == null)
+                return;
+
+            tabRelated = new RelatedPostsTabModel(tabName, account.UniqueKey, post)
             {
                 UnreadManage = false,
                 Notify = false,
index 37c914a..1572878 100644 (file)
@@ -365,9 +365,13 @@ namespace OpenTween
             var post = TabInformations.GetInstance()[statusId];
             if (post == null)
             {
+                var account = this.Owner.GetAccountForPostId(statusId);
+                if (account == null)
+                    return FormatQuoteTweetHtml(statusId, "This post is unavailable.", isReply);
+
                 try
                 {
-                    post = await this.Owner.CurrentTabAccount.Client.GetPostById(statusId, firstLoad: false)
+                    post = await account.Client.GetPostById(statusId, firstLoad: false)
                         .ConfigureAwait(false);
                 }
                 catch (WebApiException ex)
@@ -375,7 +379,7 @@ namespace OpenTween
                     return FormatQuoteTweetHtml(statusId, WebUtility.HtmlEncode($"Err:{ex.Message}(GetStatus)"), isReply);
                 }
 
-                if (this.Owner.CurrentTabAccount.AccountState.BlockedUserIds.Contains(post.UserId))
+                if (account.AccountState.BlockedUserIds.Contains(post.UserId))
                     return FormatQuoteTweetHtml(statusId, "This Tweet is unavailable.", isReply);
 
                 if (!TabInformations.GetInstance().AddQuoteTweet(post))