OSDN Git Service

DeleteRetweetを使用したリツイートの取消に対応
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 16 Jul 2023 13:35:23 +0000 (22:35 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sun, 16 Jul 2023 13:38:09 +0000 (22:38 +0900)
OpenTween.Tests/Api/GraphQL/DeleteRetweetRequestTest.cs [new file with mode: 0644]
OpenTween/Api/GraphQL/DeleteRetweetRequest.cs [new file with mode: 0644]
OpenTween/Resources/ChangeLog.txt
OpenTween/Tween.cs
OpenTween/Twitter.cs

diff --git a/OpenTween.Tests/Api/GraphQL/DeleteRetweetRequestTest.cs b/OpenTween.Tests/Api/GraphQL/DeleteRetweetRequestTest.cs
new file mode 100644 (file)
index 0000000..84f7664
--- /dev/null
@@ -0,0 +1,59 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2023 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.
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Moq;
+using OpenTween.Connection;
+using Xunit;
+
+namespace OpenTween.Api.GraphQL
+{
+    public class DeleteRetweetRequestTest
+    {
+        [Fact]
+        public async Task Send_Test()
+        {
+            var mock = new Mock<IApiConnection>();
+            mock.Setup(x =>
+                    x.PostJsonAsync(It.IsAny<Uri>(), It.IsAny<string>())
+                )
+                .Callback<Uri, string>((url, json) =>
+                {
+                    Assert.Equal(new("https://twitter.com/i/api/graphql/iQtK4dl5hBmXewYZuEOKVw/DeleteRetweet"), url);
+                    Assert.Contains(@"""source_tweet_id"":""12345""", json);
+                });
+
+            var request = new DeleteRetweetRequest
+            {
+                SourceTweetId = new("12345"),
+            };
+
+            await request.Send(mock.Object).ConfigureAwait(false);
+
+            mock.VerifyAll();
+        }
+    }
+}
diff --git a/OpenTween/Api/GraphQL/DeleteRetweetRequest.cs b/OpenTween/Api/GraphQL/DeleteRetweetRequest.cs
new file mode 100644 (file)
index 0000000..7b56679
--- /dev/null
@@ -0,0 +1,54 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2023 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;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using OpenTween.Connection;
+using OpenTween.Models;
+
+namespace OpenTween.Api.GraphQL
+{
+    public class DeleteRetweetRequest
+    {
+        private static readonly Uri EndpointUri = new("https://twitter.com/i/api/graphql/iQtK4dl5hBmXewYZuEOKVw/DeleteRetweet");
+
+        required public TwitterStatusId SourceTweetId { get; set; }
+
+        public string CreateRequestBody()
+        {
+            return $$"""
+            {"variables":{"source_tweet_id":"{{JsonUtils.EscapeJsonString(this.SourceTweetId.Id)}}","dark_request":false},"queryId":"iQtK4dl5hBmXewYZuEOKVw"}
+            """;
+        }
+
+        public async Task Send(IApiConnection apiConnection)
+        {
+            var json = this.CreateRequestBody();
+            await apiConnection.PostJsonAsync(EndpointUri, json);
+        }
+    }
+}
index 46789f2..7720ed2 100644 (file)
@@ -2,7 +2,7 @@
 
 ==== Unreleased
  * NEW: Cookie使用時のツイート投稿・削除に対応
- * NEW: Cookie使用時のリツイートに対応
+ * NEW: Cookie使ç\94¨æ\99\82ã\81®ã\83ªã\83\84ã\82¤ã\83¼ã\83\88ã\81\8aã\82\88ã\81³ã\83ªã\83\84ã\82¤ã\83¼ã\83\88ã\81®å\8f\96æ¶\88ã\81«å¯¾å¿\9c
  * NEW: graphqlエンドポイントを使用しているタブでの「前データを取得」に対応
  * CHG: Google画像検索のURLの変更に対応
 
index 155e421..e0033aa 100644 (file)
@@ -2402,8 +2402,7 @@ namespace OpenTween
                             {
                                 // 自分が RT したツイート (自分が RT した自分のツイートも含む)
                                 //   => RT を取り消し
-                                await this.tw.Api.StatusesDestroy(post.StatusId.ToTwitterStatusId())
-                                    .IgnoreResponse();
+                                await this.tw.DeleteRetweet(post);
                             }
                             else
                             {
index 7b05f13..307f029 100644 (file)
@@ -442,6 +442,26 @@ namespace OpenTween
             };
         }
 
+        public async Task DeleteRetweet(PostClass post)
+        {
+            if (post.RetweetedId == null)
+                throw new ArgumentException("post is not retweeted status", nameof(post));
+
+            if (this.Api.AppToken.AuthType == APIAuthType.TwitterComCookie)
+            {
+                var request = new DeleteRetweetRequest
+                {
+                    SourceTweetId = post.RetweetedId.ToTwitterStatusId(),
+                };
+                await request.Send(this.Api.Connection).ConfigureAwait(false);
+            }
+            else
+            {
+                await this.Api.StatusesDestroy(post.StatusId.ToTwitterStatusId())
+                    .IgnoreResponse();
+            }
+        }
+
         public string Username
             => this.Api.CurrentScreenName;