OSDN Git Service

BlockedUserIdsによるフィルタをTimelineResponseFilterで行う
authorKimura Youichi <kim.upsilon@bucyou.net>
Mon, 20 May 2024 19:35:00 +0000 (04:35 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Mon, 20 May 2024 19:56:54 +0000 (04:56 +0900)
OpenTween.Tests/SocialProtocol/Twitter/TimelineResponseFilterTest.cs
OpenTween/Models/TabInformations.cs
OpenTween/SocialProtocol/Twitter/TimelineResponseFilter.cs
OpenTween/SocialProtocol/Twitter/TwitterAccountState.cs
OpenTween/SocialProtocol/Twitter/TwitterGraphqlClient.cs
OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs
OpenTween/TweetDetailsView.cs
OpenTween/Twitter.cs

index a3e9b62..d24047a 100644 (file)
@@ -108,5 +108,91 @@ namespace OpenTween.SocialProtocol.Twitter
             Assert.Single(filteredPosts);
             Assert.Equal(new TwitterStatusId("100"), filteredPosts[0].StatusId);
         }
+
+        [Fact]
+        public void Run_FilterByBlockedUser_FilteredTest()
+        {
+            var accountState = new TwitterAccountState
+            {
+                BlockedUserIds = new HashSet<PersonId>
+                {
+                    new TwitterUserId("111"),
+                },
+            };
+            var posts = new[]
+            {
+                new PostClass
+                {
+                    StatusId = new TwitterStatusId("100"),
+                    UserId = new TwitterUserId("111"),
+                },
+            };
+
+            var filter = new TimelineResponseFilter(accountState)
+            {
+                IsHomeTimeline = true,
+            };
+            var filteredPosts = filter.Run(posts);
+
+            Assert.Empty(filteredPosts);
+        }
+
+        [Fact]
+        public void Run_FilterByBlockedUser_NotFilteredTest()
+        {
+            var accountState = new TwitterAccountState
+            {
+                BlockedUserIds = new HashSet<PersonId>
+                {
+                    new TwitterUserId("111"),
+                },
+            };
+            var posts = new[]
+            {
+                new PostClass
+                {
+                    StatusId = new TwitterStatusId("100"),
+                    UserId = new TwitterUserId("222"), // ブロックされたユーザーではない
+                },
+            };
+
+            var filter = new TimelineResponseFilter(accountState)
+            {
+                IsHomeTimeline = true,
+            };
+            var filteredPosts = filter.Run(posts);
+
+            Assert.Single(filteredPosts);
+            Assert.Equal(new TwitterStatusId("100"), filteredPosts[0].StatusId);
+        }
+
+        [Fact]
+        public void Run_FilterByBlockedUser_NotHomeTimelineTest()
+        {
+            var accountState = new TwitterAccountState
+            {
+                BlockedUserIds = new HashSet<PersonId>
+                {
+                    new TwitterUserId("111"),
+                },
+            };
+            var posts = new[]
+            {
+                new PostClass
+                {
+                    StatusId = new TwitterStatusId("100"),
+                    UserId = new TwitterUserId("111"),
+                },
+            };
+
+            var filter = new TimelineResponseFilter(accountState)
+            {
+                IsHomeTimeline = false, // ホームタイムライン以外では BlockedUserIds によるフィルタを行わない
+            };
+            var filteredPosts = filter.Run(posts);
+
+            Assert.Single(filteredPosts);
+            Assert.Equal(new TwitterStatusId("100"), filteredPosts[0].StatusId);
+        }
     }
 }
index 28619c3..345dad8 100644 (file)
@@ -54,8 +54,6 @@ namespace OpenTween.Models
 
         public Stack<TabModel> RemovedTab { get; } = new();
 
-        public ISet<PersonId> BlockIds { get; set; } = new HashSet<PersonId>();
-
         public ISet<PersonId> MuteUserIds { get; set; } = new HashSet<PersonId>();
 
         // 発言の追加
@@ -610,9 +608,6 @@ namespace OpenTween.Models
                             return;
                     }
 
-                    if (this.BlockIds.Contains(item.UserId))
-                        return;
-
                     this.Posts.TryAdd(item.StatusId, item);
                 }
                 if (item.IsFav && this.retweetsCount.ContainsKey(item.StatusId))
@@ -663,7 +658,7 @@ namespace OpenTween.Models
         {
             lock (this.lockObj)
             {
-                if (this.IsMuted(item, isHomeTimeline: false) || this.BlockIds.Contains(item.UserId))
+                if (this.IsMuted(item, isHomeTimeline: false))
                     return false;
 
                 this.quotes[item.StatusId] = item;
index b99ca40..45c202b 100644 (file)
@@ -37,6 +37,8 @@ namespace OpenTween.SocialProtocol.Twitter
     {
         private readonly TwitterAccountState accountState;
 
+        public bool IsHomeTimeline { get; set; }
+
         public TimelineResponseFilter(TwitterAccountState accountState)
         {
             this.accountState = accountState;
@@ -48,10 +50,16 @@ namespace OpenTween.SocialProtocol.Twitter
 
             filteredPosts = this.FilterNoRetweetUserPosts(filteredPosts);
 
+            if (this.IsHomeTimeline)
+                filteredPosts = this.FilterBlockedUserPosts(filteredPosts);
+
             return filteredPosts.ToArray();
         }
 
         private IEnumerable<PostClass> FilterNoRetweetUserPosts(IEnumerable<PostClass> posts)
             => posts.Where(x => x.RetweetedByUserId == null || !this.accountState.NoRetweetUserIds.Contains(x.RetweetedByUserId));
+
+        private IEnumerable<PostClass> FilterBlockedUserPosts(IEnumerable<PostClass> posts)
+            => posts.Where(x => !this.accountState.BlockedUserIds.Contains(x.UserId));
     }
 }
index 3e61027..d8cb153 100644 (file)
@@ -41,6 +41,8 @@ namespace OpenTween.SocialProtocol.Twitter
 
         public ISet<PersonId> FollowerIds { get; set; } = new HashSet<PersonId>();
 
+        public ISet<PersonId> BlockedUserIds { get; set; } = new HashSet<PersonId>();
+
         public ISet<TwitterUserId> NoRetweetUserIds { get; set; } = new HashSet<TwitterUserId>();
 
         public bool HasUnrecoverableError { get; set; } = true;
index cd0050c..2cea509 100644 (file)
@@ -98,7 +98,10 @@ namespace OpenTween.SocialProtocol.Twitter
 
             var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad);
 
-            var filter = new TimelineResponseFilter(this.account.AccountState);
+            var filter = new TimelineResponseFilter(this.account.AccountState)
+            {
+                IsHomeTimeline = true,
+            };
             posts = filter.Run(posts);
 
             return new(posts, cursorTop, cursorBottom);
index 726a841..4e6e5c0 100644 (file)
@@ -81,7 +81,10 @@ namespace OpenTween.SocialProtocol.Twitter
             var (cursorTop, cursorBottom) = GetCursorFromResponse(statuses);
             var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad);
 
-            var filter = new TimelineResponseFilter(this.account.AccountState);
+            var filter = new TimelineResponseFilter(this.account.AccountState)
+            {
+                IsHomeTimeline = true,
+            };
             posts = filter.Run(posts);
 
             return new(posts, cursorTop, cursorBottom);
index 4e59fa5..c4f709f 100644 (file)
@@ -42,6 +42,7 @@ using System.Threading.Tasks;
 using System.Windows.Forms;
 using OpenTween.Models;
 using OpenTween.Setting;
+using OpenTween.SocialProtocol.Twitter;
 
 namespace OpenTween
 {
@@ -371,6 +372,12 @@ namespace OpenTween
                     return FormatQuoteTweetHtml(statusId, WebUtility.HtmlEncode($"Err:{ex.Message}(GetStatus)"), isReply);
                 }
 
+                if (this.Owner.CurrentTabAccount is TwitterAccount twAccount)
+                {
+                    if (twAccount.AccountState.BlockedUserIds.Contains(post.UserId))
+                        return FormatQuoteTweetHtml(statusId, "This Tweet is unavailable.", isReply);
+                }
+
                 if (!TabInformations.GetInstance().AddQuoteTweet(post))
                     return FormatQuoteTweetHtml(statusId, "This Tweet is unavailable.", isReply);
             }
index 5badd24..c5bd5fb 100644 (file)
@@ -812,7 +812,7 @@ namespace OpenTween
             var blockIdsSet = newBlockIds.ToHashSet();
             blockIdsSet.Remove(this.UserId); // 元のソースにあったので一応残しておく
 
-            TabInformations.GetInstance().BlockIds = blockIdsSet;
+            this.AccountState.BlockedUserIds = blockIdsSet;
         }
 
         /// <summary>