OSDN Git Service

ISocialProtocolClient.GetSearchTimelineメソッドを追加
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 14 May 2024 18:35:46 +0000 (03:35 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Tue, 14 May 2024 19:45:15 +0000 (04:45 +0900)
OpenTween/Models/PublicSearchTabModel.cs
OpenTween/SocialProtocol/ISocialProtocolClient.cs
OpenTween/SocialProtocol/Twitter/TwitterGraphqlClient.cs
OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs
OpenTween/Twitter.cs

index 269f91b..ce13bc3 100644 (file)
 #nullable enable
 
 using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
 using System.Threading.Tasks;
-using OpenTween.Setting;
 using OpenTween.SocialProtocol;
-using OpenTween.SocialProtocol.Twitter;
 
 namespace OpenTween.Models
 {
@@ -73,21 +68,29 @@ namespace OpenTween.Models
 
         public override async Task RefreshAsync(ISocialAccount account, bool backward, IProgress<string> progress)
         {
-            if (account is not TwitterAccount twAccount)
-                throw new ArgumentException($"Invalid account type: {account.AccountType}", nameof(account));
-
             if (MyCommon.IsNullOrEmpty(this.SearchWords))
                 return;
 
             progress.Report("Search refreshing...");
 
             var firstLoad = !this.IsFirstLoadCompleted;
+            var count = Twitter.GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, backward, firstLoad);
+            var cursor = backward ? this.CursorBottom : this.CursorTop;
 
-            await twAccount.Legacy.GetSearch(this, backward, firstLoad)
+            var response = await account.Client.GetSearchTimeline(this.SearchWords, this.SearchLang, count, cursor, firstLoad)
                 .ConfigureAwait(false);
 
+            foreach (var post in response.Posts)
+                this.AddPostQueue(post);
+
             TabInformations.GetInstance().DistributePosts();
 
+            if (response.CursorTop != null && !backward)
+                this.CursorTop = response.CursorTop;
+
+            if (response.CursorBottom != null)
+                this.CursorBottom = response.CursorBottom;
+
             if (firstLoad)
                 this.IsFirstLoadCompleted = true;
 
index 2af6ecd..70da72a 100644 (file)
@@ -32,6 +32,8 @@ namespace OpenTween.SocialProtocol
 
         public Task<TimelineResponse> GetHomeTimeline(int count, IQueryCursor? cursor, bool firstLoad);
 
+        public Task<TimelineResponse> GetSearchTimeline(string query, string lang, int count, IQueryCursor? cursor, bool firstLoad);
+
         public Task DeletePost(PostId postId);
 
         public Task FavoritePost(PostId postId);
index f57a8ca..e1c4928 100644 (file)
@@ -88,6 +88,32 @@ namespace OpenTween.SocialProtocol.Twitter
             return new(posts, cursorTop, cursorBottom);
         }
 
+        public async Task<TimelineResponse> GetSearchTimeline(string query, string lang, int count, IQueryCursor? cursor, bool firstLoad)
+        {
+            this.account.Legacy.CheckAccountState();
+
+            if (!MyCommon.IsNullOrEmpty(lang))
+                query = $"({query}) lang:{lang}";
+
+            var request = new SearchTimelineRequest(query)
+            {
+                Count = count,
+                Cursor = cursor?.As<TwitterGraphqlCursor>(),
+            };
+
+            var response = await request.Send(this.account.Connection)
+                .ConfigureAwait(false);
+
+            var statuses = response.ToTwitterStatuses();
+            var cursorTop = response.CursorTop;
+            var cursorBottom = response.CursorBottom;
+
+            var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad);
+            posts = this.account.Legacy.FilterNoRetweetUserPosts(posts);
+
+            return new(posts, cursorTop, cursorBottom);
+        }
+
         public async Task DeletePost(PostId postId)
         {
             var statusId = this.AssertTwitterStatusId(postId);
index 0f8a343..423097e 100644 (file)
@@ -90,6 +90,40 @@ namespace OpenTween.SocialProtocol.Twitter
             return new(posts, cursorTop, cursorBottom);
         }
 
+        public async Task<TimelineResponse> GetSearchTimeline(string query, string lang, int count, IQueryCursor? cursor, bool firstLoad)
+        {
+            this.account.Legacy.CheckAccountState();
+
+            TwitterStatusId? maxId = null, sinceId = null;
+
+            if (cursor is QueryCursor<TwitterStatusId> statusIdCursor)
+            {
+                if (statusIdCursor.Type == CursorType.Top)
+                    sinceId = statusIdCursor.Value;
+                else if (statusIdCursor.Type == CursorType.Bottom)
+                    maxId = statusIdCursor.Value;
+            }
+
+            var searchResult = await this.account.Legacy.Api.SearchTweets(query, lang, count, maxId, sinceId)
+                .ConfigureAwait(false);
+
+            var statuses = searchResult.Statuses;
+
+            IQueryCursor? cursorTop = null, cursorBottom = null;
+
+            if (statuses.Length > 0)
+            {
+                var (min, max) = statuses.Select(x => new TwitterStatusId(x.IdStr)).MinMax();
+                cursorTop = new QueryCursor<TwitterStatusId>(CursorType.Top, max);
+                cursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
+            }
+
+            var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad);
+            posts = this.account.Legacy.FilterNoRetweetUserPosts(posts);
+
+            return new(posts, cursorTop, cursorBottom);
+        }
+
         public async Task DeletePost(PostId postId)
         {
             var statusId = this.AssertTwitterStatusId(postId);
index d1038d7..d1c021b 100644 (file)
@@ -867,72 +867,6 @@ namespace OpenTween
             return statuses.Select(x => this.CreatePostsFromStatusData(x, firstLoad: false)).ToArray();
         }
 
-        public async Task GetSearch(PublicSearchTabModel tab, bool more, bool firstLoad)
-        {
-            var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, firstLoad);
-
-            TwitterStatus[] statuses;
-            if (this.Api.AuthType == APIAuthType.TwitterComCookie)
-            {
-                var query = tab.SearchWords;
-
-                if (!MyCommon.IsNullOrEmpty(tab.SearchLang))
-                    query = $"({query}) lang:{tab.SearchLang}";
-
-                var cursor = more ? tab.CursorBottom : tab.CursorTop;
-                var request = new SearchTimelineRequest(query)
-                {
-                    Count = count,
-                    Cursor = cursor?.As<TwitterGraphqlCursor>(),
-                };
-                var response = await request.Send(this.Api.Connection)
-                    .ConfigureAwait(false);
-
-                statuses = response.ToTwitterStatuses();
-
-                tab.CursorBottom = response.CursorBottom;
-
-                if (!more)
-                    tab.CursorTop = response.CursorTop;
-            }
-            else
-            {
-                TwitterStatusId? maxId = null;
-                TwitterStatusId? sinceId = null;
-                if (more)
-                {
-                    maxId = tab.CursorBottom?.As<TwitterStatusId>();
-                }
-                else
-                {
-                    sinceId = tab.CursorTop?.As<TwitterStatusId>();
-                }
-
-                var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
-                    .ConfigureAwait(false);
-
-                statuses = searchResult.Statuses;
-
-                if (statuses.Length > 0)
-                {
-                    var (min, max) = statuses.Select(x => new TwitterStatusId(x.IdStr)).MinMax();
-                    tab.CursorBottom = new QueryCursor<TwitterStatusId>(CursorType.Bottom, min);
-
-                    if (!more)
-                        tab.CursorTop = new QueryCursor<TwitterStatusId>(CursorType.Top, max);
-                }
-            }
-
-            if (!TabInformations.GetInstance().ContainsTab(tab))
-                return;
-
-            var posts = this.CreatePostsFromJson(statuses, firstLoad);
-            posts = this.FilterNoRetweetUserPosts(posts);
-
-            foreach (var post in posts)
-                tab.AddPostQueue(post);
-        }
-
         public async Task GetDirectMessageEvents(DirectMessagesTabModel dmTab, bool backward, bool firstLoad)
         {
             this.CheckAccountState();