From 28a8ca6e1aee639d6f59fc7a13d2b022803bcbb0 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Wed, 8 May 2024 00:12:59 +0900 Subject: [PATCH] =?utf8?q?Twitter.GetHomeTimelineApi=E3=83=A1=E3=82=BD?= =?utf8?q?=E3=83=83=E3=83=89=E3=81=8B=E3=82=89TabModel=E3=81=B8=E3=81=AE?= =?utf8?q?=E4=BE=9D=E5=AD=98=E3=82=92=E9=99=A4=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- OpenTween/Models/HomeTabModel.cs | 13 ++++++++++++- OpenTween/Models/TimelineResponse.cs | 31 +++++++++++++++++++++++++++++++ OpenTween/Twitter.cs | 33 +++++++++++++++++++-------------- 3 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 OpenTween/Models/TimelineResponse.cs diff --git a/OpenTween/Models/HomeTabModel.cs b/OpenTween/Models/HomeTabModel.cs index bb9651e2..53705ce0 100644 --- a/OpenTween/Models/HomeTabModel.cs +++ b/OpenTween/Models/HomeTabModel.cs @@ -75,16 +75,27 @@ namespace OpenTween.Models progress.Report(string.Format(Properties.Resources.GetTimelineWorker_RunWorkerCompletedText5, backward ? -1 : 1)); var firstLoad = !this.IsFirstLoadCompleted; + var count = Twitter.GetApiResultCount(MyCommon.WORKERTYPE.Timeline, backward, firstLoad); + var cursor = backward ? this.CursorBottom : this.CursorTop; - await twAccount.Legacy.GetHomeTimelineApi(this, backward, firstLoad) + var response = await twAccount.Legacy.GetHomeTimelineApi(count, cursor, firstLoad) .ConfigureAwait(false); + foreach (var post in response.Posts) + TabInformations.GetInstance().AddPost(post); + // 新着時未読クリア if (SettingManager.Instance.Common.ReadOldPosts) TabInformations.GetInstance().SetReadHomeTab(); 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/Models/TimelineResponse.cs b/OpenTween/Models/TimelineResponse.cs new file mode 100644 index 00000000..57475879 --- /dev/null +++ b/OpenTween/Models/TimelineResponse.cs @@ -0,0 +1,31 @@ +// OpenTween - Client of Twitter +// Copyright (c) 2024 kim_upsilon (@kim_upsilon) +// All rights reserved. +// +// This file is part of OpenTween. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program. If not, see , or write to +// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +// Boston, MA 02110-1301, USA. + +#nullable enable + +namespace OpenTween.Models +{ + public record TimelineResponse( + PostClass[] Posts, + IQueryCursor? CursorTop, + IQueryCursor? CursorBottom + ); +} diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index ca7373f4..b1e424ee 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -623,16 +623,15 @@ namespace OpenTween return Math.Min(count, GetMaxApiResultCount(type)); } - public async Task GetHomeTimelineApi(HomeTabModel tab, bool more, bool firstLoad) + public async Task GetHomeTimelineApi(int count, IQueryCursor? cursor, bool firstLoad) { this.CheckAccountState(); - var count = GetApiResultCount(MyCommon.WORKERTYPE.Timeline, more, firstLoad); - TwitterStatus[] statuses; + IQueryCursor? cursorTop = null, cursorBottom = null; + if (this.Api.AuthType == APIAuthType.TwitterComCookie) { - var cursor = more ? tab.CursorBottom : tab.CursorTop; var request = new HomeLatestTimelineRequest { Count = count, @@ -643,30 +642,36 @@ namespace OpenTween statuses = response.ToTwitterStatuses(); - tab.CursorBottom = response.CursorBottom; - - if (!more) - tab.CursorTop = response.CursorTop; + cursorTop = response.CursorTop; + cursorBottom = response.CursorBottom; } else { - var maxId = more ? tab.CursorBottom?.As() : null; + TwitterStatusId? maxId = null, sinceId = null; - statuses = await this.Api.StatusesHomeTimeline(count, maxId) + if (cursor is QueryCursor statusIdCursor) + { + if (statusIdCursor.Type == CursorType.Top) + sinceId = statusIdCursor.Value; + else if (statusIdCursor.Type == CursorType.Bottom) + maxId = statusIdCursor.Value; + } + + statuses = await this.Api.StatusesHomeTimeline(count, maxId, sinceId) .ConfigureAwait(false); if (statuses.Length > 0) { - var min = statuses.Select(x => new TwitterStatusId(x.IdStr)).Min(); - tab.CursorBottom = new QueryCursor(CursorType.Bottom, min); + 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.CreatePostsFromJson(statuses, firstLoad); posts = this.FilterNoRetweetUserPosts(posts); - foreach (var post in posts) - TabInformations.GetInstance().AddPost(post); + return new(posts, cursorTop, cursorBottom); } public async Task GetMentionsTimelineApi(MentionsTabModel tab, bool more, bool firstLoad) -- 2.11.0