From 59c8094828e5bd9b207b690ca3ba3313a73f022a Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Wed, 15 May 2024 04:24:05 +0900 Subject: [PATCH] =?utf8?q?TwitterV1Client=E3=81=A7=E3=81=AEcursor=E3=81=AB?= =?utf8?q?=E5=AF=BE=E3=81=99=E3=82=8B=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?utf8?q?=E3=83=BC=E3=83=89=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../SocialProtocol/Twitter/TwitterV1ClientTest.cs | 92 ++++++++++++++++++++++ .../SocialProtocol/Twitter/TwitterV1Client.cs | 69 ++++++++-------- 2 files changed, 125 insertions(+), 36 deletions(-) create mode 100644 OpenTween.Tests/SocialProtocol/Twitter/TwitterV1ClientTest.cs diff --git a/OpenTween.Tests/SocialProtocol/Twitter/TwitterV1ClientTest.cs b/OpenTween.Tests/SocialProtocol/Twitter/TwitterV1ClientTest.cs new file mode 100644 index 00000000..ac91fdc6 --- /dev/null +++ b/OpenTween.Tests/SocialProtocol/Twitter/TwitterV1ClientTest.cs @@ -0,0 +1,92 @@ +// 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. + +using System; +using OpenTween.Api.DataModel; +using OpenTween.Models; +using Xunit; + +namespace OpenTween.SocialProtocol.Twitter +{ + public class TwitterV1ClientTest + { + [Fact] + public void GetCursorParams_NoneTest() + { + var cursor = (IQueryCursor?)null; + var (sinceId, maxId) = TwitterV1Client.GetCursorParams(cursor); + Assert.Null(sinceId); + Assert.Null(maxId); + } + + [Fact] + public void GetCursorParams_TopTest() + { + var cursor = new QueryCursor(CursorType.Top, new("11111")); + var (sinceId, maxId) = TwitterV1Client.GetCursorParams(cursor); + Assert.Equal(new("11111"), sinceId); + Assert.Null(maxId); + } + + [Fact] + public void GetCursorParams_BottomTest() + { + var cursor = new QueryCursor(CursorType.Bottom, new("11111")); + var (sinceId, maxId) = TwitterV1Client.GetCursorParams(cursor); + Assert.Null(sinceId); + Assert.Equal(new("11111"), maxId); + } + + [Fact] + public void GetCursorFromResponse_EmptyTest() + { + var statuses = Array.Empty(); + var (cursorTop, cursorBottom) = TwitterV1Client.GetCursorFromResponse(statuses); + Assert.Null(cursorTop); + Assert.Null(cursorBottom); + } + + [Fact] + public void GetCursorFromResponse_MinMaxTest() + { + var statuses = new[] + { + new TwitterStatus + { + IdStr = "11111", + }, + new TwitterStatus + { + IdStr = "22222", + }, + }; + var (cursorTop, cursorBottom) = TwitterV1Client.GetCursorFromResponse(statuses); + + var statusIdCursorTop = Assert.IsType>(cursorTop); + Assert.Equal(CursorType.Top, statusIdCursorTop.Type); + Assert.Equal(new("22222"), statusIdCursorTop.Value); + + var statusIdCursorBottom = Assert.IsType>(cursorBottom); + Assert.Equal(CursorType.Bottom, statusIdCursorBottom.Type); + Assert.Equal(new("11111"), statusIdCursorBottom.Value); + } + } +} diff --git a/OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs b/OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs index d0e7db9b..75858393 100644 --- a/OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs +++ b/OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs @@ -62,28 +62,12 @@ namespace OpenTween.SocialProtocol.Twitter { 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 (sinceId, maxId) = GetCursorParams(cursor); var statuses = await this.account.Legacy.Api.StatusesHomeTimeline(count, maxId, sinceId) .ConfigureAwait(false); - 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 (cursorTop, cursorBottom) = GetCursorFromResponse(statuses); var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad); posts = this.account.Legacy.FilterNoRetweetUserPosts(posts); @@ -94,30 +78,14 @@ namespace OpenTween.SocialProtocol.Twitter { 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 (sinceId, maxId) = GetCursorParams(cursor); 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 (cursorTop, cursorBottom) = GetCursorFromResponse(statuses); var posts = this.account.Legacy.CreatePostsFromJson(statuses, firstLoad); posts = this.account.Legacy.FilterNoRetweetUserPosts(posts); @@ -202,5 +170,34 @@ namespace OpenTween.SocialProtocol.Twitter private PostClass CreatePostFromJson(TwitterStatus status) => this.account.Legacy.CreatePostsFromStatusData(status, firstLoad: false, favTweet: false); + + public static (TwitterStatusId? SinceId, TwitterStatusId? MaxId) GetCursorParams(IQueryCursor? cursor) + { + TwitterStatusId? sinceId = null, maxId = null; + + if (cursor is QueryCursor statusIdCursor) + { + if (statusIdCursor.Type == CursorType.Top) + sinceId = statusIdCursor.Value; + else if (statusIdCursor.Type == CursorType.Bottom) + maxId = statusIdCursor.Value; + } + + return (sinceId, maxId); + } + + public static (IQueryCursor? CursorTop, IQueryCursor? CursorBottom) GetCursorFromResponse(TwitterStatus[] 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); + } + + return (cursorTop, cursorBottom); + } } } -- 2.11.0