#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
{
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;
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);
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);
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);
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();