OSDN Git Service

ISocialProtocolQueryとISocialProtocolMutationを統合
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 14 May 2024 17:49:23 +0000 (02:49 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Tue, 14 May 2024 17:49:23 +0000 (02:49 +0900)
OpenTween.Tests/SocialProtocol/Twitter/TwitterAccountTest.cs
OpenTween/Models/HomeTabModel.cs
OpenTween/SocialProtocol/ISocialAccount.cs
OpenTween/SocialProtocol/ISocialProtocolClient.cs [moved from OpenTween/SocialProtocol/ISocialProtocolMutation.cs with 89% similarity]
OpenTween/SocialProtocol/ISocialProtocolQuery.cs [deleted file]
OpenTween/SocialProtocol/Twitter/TwitterAccount.cs
OpenTween/SocialProtocol/Twitter/TwitterGraphqlClient.cs [moved from OpenTween/SocialProtocol/Twitter/TwitterGraphqlMutation.cs with 79% similarity]
OpenTween/SocialProtocol/Twitter/TwitterGraphqlQuery.cs [deleted file]
OpenTween/SocialProtocol/Twitter/TwitterV1Client.cs [moved from OpenTween/SocialProtocol/Twitter/TwitterV1Mutation.cs with 74% similarity]
OpenTween/SocialProtocol/Twitter/TwitterV1Query.cs [deleted file]
OpenTween/Tween.cs

index 8df1275..0bd65e1 100644 (file)
@@ -83,7 +83,7 @@ namespace OpenTween.SocialProtocol.Twitter
         }
 
         [Fact]
-        public void Query_V1_Test()
+        public void Client_V1_Test()
         {
             var accountKey = Guid.NewGuid();
             using var account = new TwitterAccount(accountKey);
@@ -100,11 +100,11 @@ namespace OpenTween.SocialProtocol.Twitter
             var settingCommon = new SettingCommon();
             account.Initialize(accountSettings, settingCommon);
 
-            Assert.IsType<TwitterV1Query>(account.Query);
+            Assert.IsType<TwitterV1Client>(account.Client);
         }
 
         [Fact]
-        public void Query_Graphql_Test()
+        public void Client_Graphql_Test()
         {
             var accountKey = Guid.NewGuid();
             using var account = new TwitterAccount(accountKey);
@@ -120,48 +120,7 @@ namespace OpenTween.SocialProtocol.Twitter
             var settingCommon = new SettingCommon();
             account.Initialize(accountSettings, settingCommon);
 
-            Assert.IsType<TwitterGraphqlQuery>(account.Query);
-        }
-
-        [Fact]
-        public void Mutation_V1_Test()
-        {
-            var accountKey = Guid.NewGuid();
-            using var account = new TwitterAccount(accountKey);
-
-            var accountSettings = new UserAccount
-            {
-                UniqueKey = accountKey,
-                TwitterAuthType = APIAuthType.OAuth1,
-                Token = "aaaaa",
-                TokenSecret = "aaaaa",
-                UserId = 11111L,
-                Username = "tetete",
-            };
-            var settingCommon = new SettingCommon();
-            account.Initialize(accountSettings, settingCommon);
-
-            Assert.IsType<TwitterV1Mutation>(account.Mutation);
-        }
-
-        [Fact]
-        public void Mutation_Graphql_Test()
-        {
-            var accountKey = Guid.NewGuid();
-            using var account = new TwitterAccount(accountKey);
-
-            var accountSettings = new UserAccount
-            {
-                UniqueKey = accountKey,
-                TwitterAuthType = APIAuthType.TwitterComCookie,
-                TwitterComCookie = "auth_token=foo; ct0=bar",
-                UserId = 11111L,
-                Username = "tetete",
-            };
-            var settingCommon = new SettingCommon();
-            account.Initialize(accountSettings, settingCommon);
-
-            Assert.IsType<TwitterGraphqlMutation>(account.Mutation);
+            Assert.IsType<TwitterGraphqlClient>(account.Client);
         }
     }
 }
index ea785d9..e8cd67b 100644 (file)
@@ -75,7 +75,7 @@ namespace OpenTween.Models
             var count = Twitter.GetApiResultCount(MyCommon.WORKERTYPE.Timeline, backward, firstLoad);
             var cursor = backward ? this.CursorBottom : this.CursorTop;
 
-            var response = await account.Query.GetHomeTimeline(count, cursor, firstLoad)
+            var response = await account.Client.GetHomeTimeline(count, cursor, firstLoad)
                 .ConfigureAwait(false);
 
             foreach (var post in response.Posts)
index a5701b8..d1a14b8 100644 (file)
@@ -36,9 +36,7 @@ namespace OpenTween.SocialProtocol
 
         public IApiConnection Connection { get; }
 
-        public ISocialProtocolQuery Query { get; }
-
-        public ISocialProtocolMutation Mutation { get; }
+        public ISocialProtocolClient Client { get; }
 
         public bool IsDisposed { get; }
 
@@ -26,8 +26,10 @@ using OpenTween.Models;
 
 namespace OpenTween.SocialProtocol
 {
-    public interface ISocialProtocolMutation
+    public interface ISocialProtocolClient
     {
+        public Task<TimelineResponse> GetHomeTimeline(int count, IQueryCursor? cursor, bool firstLoad);
+
         public Task DeletePost(PostId postId);
 
         public Task FavoritePost(PostId postId);
diff --git a/OpenTween/SocialProtocol/ISocialProtocolQuery.cs b/OpenTween/SocialProtocol/ISocialProtocolQuery.cs
deleted file mode 100644 (file)
index 85e2ba4..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-// OpenTween - Client of Twitter
-// Copyright (c) 2024 kim_upsilon (@kim_upsilon) <https://upsilo.net/~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 <http://www.gnu.org/licenses/>, or write to
-// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
-// Boston, MA 02110-1301, USA.
-
-#nullable enable
-
-using System.Threading.Tasks;
-using OpenTween.Models;
-
-namespace OpenTween.SocialProtocol
-{
-    public interface ISocialProtocolQuery
-    {
-        public Task<TimelineResponse> GetHomeTimeline(int count, IQueryCursor? cursor, bool firstLoad);
-    }
-}
index e0a5f32..0f7f1d4 100644 (file)
@@ -34,9 +34,7 @@ namespace OpenTween.SocialProtocol.Twitter
 
         public Guid UniqueKey { get; }
 
-        public ISocialProtocolQuery Query { get; private set; }
-
-        public ISocialProtocolMutation Mutation { get; private set; }
+        public ISocialProtocolClient Client { get; private set; }
 
         public bool IsDisposed { get; private set; }
 
@@ -60,8 +58,7 @@ namespace OpenTween.SocialProtocol.Twitter
         public TwitterAccount(Guid uniqueKey)
         {
             this.UniqueKey = uniqueKey;
-            this.Query = this.CreateQueryInstance(APIAuthType.None);
-            this.Mutation = this.CreateMutationInstance(APIAuthType.None);
+            this.Client = this.CreateClientInstance(APIAuthType.None);
         }
 
         public void Initialize(UserAccount accountSettings, SettingCommon settingCommon)
@@ -79,8 +76,7 @@ namespace OpenTween.SocialProtocol.Twitter
             (this.apiConnection, var oldConnection) = (newConnection, this.apiConnection);
             oldConnection.Dispose();
 
-            this.Query = this.CreateQueryInstance(credential.AuthType);
-            this.Mutation = this.CreateMutationInstance(credential.AuthType);
+            this.Client = this.CreateClientInstance(credential.AuthType);
 
             this.twLegacy.Initialize(newConnection, this.AccountState);
             this.twLegacy.RestrictFavCheck = settingCommon.RestrictFavCheck;
@@ -95,21 +91,12 @@ namespace OpenTween.SocialProtocol.Twitter
             this.IsDisposed = true;
         }
 
-        private ISocialProtocolQuery CreateQueryInstance(APIAuthType authType)
-        {
-            return authType switch
-            {
-                APIAuthType.TwitterComCookie => new TwitterGraphqlQuery(this),
-                _ => new TwitterV1Query(this),
-            };
-        }
-
-        private ISocialProtocolMutation CreateMutationInstance(APIAuthType authType)
+        private ISocialProtocolClient CreateClientInstance(APIAuthType authType)
         {
             return authType switch
             {
-                APIAuthType.TwitterComCookie => new TwitterGraphqlMutation(this),
-                _ => new TwitterV1Mutation(this),
+                APIAuthType.TwitterComCookie => new TwitterGraphqlClient(this),
+                _ => new TwitterV1Client(this),
             };
         }
     }
@@ -33,15 +33,38 @@ using OpenTween.Models;
 
 namespace OpenTween.SocialProtocol.Twitter
 {
-    public class TwitterGraphqlMutation : ISocialProtocolMutation
+    public class TwitterGraphqlClient : ISocialProtocolClient
     {
         private readonly TwitterAccount account;
 
-        public TwitterGraphqlMutation(TwitterAccount account)
+        public TwitterGraphqlClient(TwitterAccount account)
         {
             this.account = account;
         }
 
+        public async Task<TimelineResponse> GetHomeTimeline(int count, IQueryCursor? cursor, bool firstLoad)
+        {
+            this.account.Legacy.CheckAccountState();
+
+            var request = new HomeLatestTimelineRequest
+            {
+                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);
diff --git a/OpenTween/SocialProtocol/Twitter/TwitterGraphqlQuery.cs b/OpenTween/SocialProtocol/Twitter/TwitterGraphqlQuery.cs
deleted file mode 100644 (file)
index aa6b6e2..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-// OpenTween - Client of Twitter
-// Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
-//           (c) 2008-2011 Moz (@syo68k)
-//           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
-//           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
-//           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
-//           (c) 2011      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
-//           (c) 2013      kim_upsilon (@kim_upsilon) <https://upsilo.net/~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 <http://www.gnu.org/licenses/>, or write to
-// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
-// Boston, MA 02110-1301, USA.
-
-#nullable enable
-
-using System.Threading.Tasks;
-using OpenTween.Api.GraphQL;
-using OpenTween.Models;
-
-namespace OpenTween.SocialProtocol.Twitter
-{
-    public class TwitterGraphqlQuery : ISocialProtocolQuery
-    {
-        private readonly TwitterAccount account;
-
-        public TwitterGraphqlQuery(TwitterAccount account)
-        {
-            this.account = account;
-        }
-
-        public async Task<TimelineResponse> GetHomeTimeline(int count, IQueryCursor? cursor, bool firstLoad)
-        {
-            this.account.Legacy.CheckAccountState();
-
-            var request = new HomeLatestTimelineRequest
-            {
-                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);
-        }
-    }
-}
@@ -36,15 +36,47 @@ using OpenTween.Models;
 
 namespace OpenTween.SocialProtocol.Twitter
 {
-    public class TwitterV1Mutation : ISocialProtocolMutation
+    public class TwitterV1Client : ISocialProtocolClient
     {
         private readonly TwitterAccount account;
 
-        public TwitterV1Mutation(TwitterAccount account)
+        public TwitterV1Client(TwitterAccount account)
         {
             this.account = account;
         }
 
+        public async Task<TimelineResponse> GetHomeTimeline(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 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<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);
diff --git a/OpenTween/SocialProtocol/Twitter/TwitterV1Query.cs b/OpenTween/SocialProtocol/Twitter/TwitterV1Query.cs
deleted file mode 100644 (file)
index 14536b1..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-// OpenTween - Client of Twitter
-// Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
-//           (c) 2008-2011 Moz (@syo68k)
-//           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
-//           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
-//           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
-//           (c) 2011      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
-//           (c) 2013      kim_upsilon (@kim_upsilon) <https://upsilo.net/~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 <http://www.gnu.org/licenses/>, or write to
-// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
-// Boston, MA 02110-1301, USA.
-
-#nullable enable
-
-using System.Linq;
-using System.Threading.Tasks;
-using OpenTween.Models;
-
-namespace OpenTween.SocialProtocol.Twitter
-{
-    public class TwitterV1Query : ISocialProtocolQuery
-    {
-        private readonly TwitterAccount account;
-
-        public TwitterV1Query(TwitterAccount account)
-        {
-            this.account = account;
-        }
-
-        public async Task<TimelineResponse> GetHomeTimeline(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 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<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);
-        }
-    }
-}
index f2004e2..2101a2d 100644 (file)
@@ -1383,7 +1383,7 @@ namespace OpenTween
                 {
                     var originalPostId = post.RetweetedId ?? post.StatusId;
 
-                    await this.CurrentTabAccount.Mutation.FavoritePost(originalPostId)
+                    await this.CurrentTabAccount.Client.FavoritePost(originalPostId)
                         .ConfigureAwait(false);
 
                     if (this.settings.Common.RestrictFavCheck)
@@ -1505,7 +1505,7 @@ namespace OpenTween
                     {
                         var originalPostId = post.RetweetedId ?? post.StatusId;
 
-                        await this.CurrentTabAccount.Mutation.UnfavoritePost(originalPostId)
+                        await this.CurrentTabAccount.Client.UnfavoritePost(originalPostId)
                             .ConfigureAwait(false);
                     }
                     catch (WebApiException)
@@ -1752,7 +1752,7 @@ namespace OpenTween
                 {
                     var statusId = post.RetweetedId ?? post.StatusId;
 
-                    var retweetedPost = await this.CurrentTabAccount.Mutation.RetweetPost(statusId)
+                    var retweetedPost = await this.CurrentTabAccount.Client.RetweetPost(statusId)
                         .ConfigureAwait(false);
 
                     if (retweetedPost != null)
@@ -2317,7 +2317,7 @@ namespace OpenTween
                             {
                                 // 自分が RT したツイート (自分が RT した自分のツイートも含む)
                                 //   => RT を取り消し
-                                await this.CurrentTabAccount.Mutation.UnretweetPost(post.RetweetedId);
+                                await this.CurrentTabAccount.Client.UnretweetPost(post.RetweetedId);
                             }
                             else
                             {
@@ -2327,13 +2327,13 @@ namespace OpenTween
                                     {
                                         // 他人に RT された自分のツイート
                                         //   => RT 元の自分のツイートを削除
-                                        await this.CurrentTabAccount.Mutation.DeletePost(post.RetweetedId);
+                                        await this.CurrentTabAccount.Client.DeletePost(post.RetweetedId);
                                     }
                                     else
                                     {
                                         // 自分のツイート
                                         //   => ツイートを削除
-                                        await this.CurrentTabAccount.Mutation.DeletePost(post.StatusId);
+                                        await this.CurrentTabAccount.Client.DeletePost(post.StatusId);
                                     }
                                 }
                             }