From: Kimura Youichi Date: Tue, 14 May 2024 18:35:46 +0000 (+0900) Subject: ISocialProtocolClient.GetSearchTimelineメソッドを追加 X-Git-Tag: OpenTween_v3.14.0^2~31^2~2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=0230bc6fbf74545e2878b5ecd9a2ec9c032e355c;p=opentween%2Fopen-tween.git ISocialProtocolClient.GetSearchTimelineメソッドを追加 --- diff --git a/OpenTween/Models/PublicSearchTabModel.cs b/OpenTween/Models/PublicSearchTabModel.cs index 269f91be..ce13bc3d 100644 --- a/OpenTween/Models/PublicSearchTabModel.cs +++ b/OpenTween/Models/PublicSearchTabModel.cs @@ -28,13 +28,8 @@ #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 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; diff --git a/OpenTween/SocialProtocol/ISocialProtocolClient.cs b/OpenTween/SocialProtocol/ISocialProtocolClient.cs index 2af6ecde..70da72a0 100644 --- a/OpenTween/SocialProtocol/ISocialProtocolClient.cs +++ b/OpenTween/SocialProtocol/ISocialProtocolClient.cs @@ -32,6 +32,8 @@ namespace OpenTween.SocialProtocol public Task GetHomeTimeline(int count, IQueryCursor? cursor, bool firstLoad); + public Task GetSearchTimeline(string query, string lang, int count, IQueryCursor? cursor, bool firstLoad); + public Task DeletePost(PostId postId); public Task FavoritePost(PostId postId); diff --git a/OpenTween/SocialProtocol/Twitter/TwitterGraphqlClient.cs b/OpenTween/SocialProtocol/Twitter/TwitterGraphqlClient.cs index f57a8ca6..e1c49286 100644 --- a/OpenTween/SocialProtocol/Twitter/TwitterGraphqlClient.cs +++ b/OpenTween/SocialProtocol/Twitter/TwitterGraphqlClient.cs @@ -88,6 +88,32 @@ namespace OpenTween.SocialProtocol.Twitter return new(posts, cursorTop, cursorBottom); } + public async Task 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(), + }; + + 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); diff --git a/OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs b/OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs index 0f8a343d..423097e7 100644 --- a/OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs +++ b/OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs @@ -90,6 +90,40 @@ namespace OpenTween.SocialProtocol.Twitter return new(posts, cursorTop, cursorBottom); } + public async Task GetSearchTimeline(string query, string lang, int count, IQueryCursor? cursor, bool firstLoad) + { + this.account.Legacy.CheckAccountState(); + + TwitterStatusId? maxId = null, sinceId = null; + + if (cursor is QueryCursor 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(CursorType.Top, max); + cursorBottom = new QueryCursor(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); diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index d1038d75..d1c021b9 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -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(), - }; - 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(); - } - else - { - sinceId = tab.CursorTop?.As(); - } - - 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(CursorType.Bottom, min); - - if (!more) - tab.CursorTop = new QueryCursor(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();