OSDN Git Service

DM送信に新エンドポイントを使用する
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 3 Jun 2018 15:42:42 +0000 (00:42 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sun, 3 Jun 2018 15:42:42 +0000 (00:42 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/OpenTween.csproj
OpenTween/Twitter.cs

index ab909e3..15990b5 100644 (file)
@@ -724,24 +724,21 @@ namespace OpenTween.Api
         }
 
         [Fact]
-        public async Task DirectMessagesNew_Test()
+        public async Task DirectMessagesDestroy_Test()
         {
             using (var twitterApi = new TwitterApi())
             {
                 var mock = new Mock<IApiConnection>();
                 mock.Setup(x =>
                     x.PostLazyAsync<TwitterDirectMessage>(
-                        new Uri("direct_messages/new.json", UriKind.Relative),
-                        new Dictionary<string, string> {
-                            { "text", "hogehoge" },
-                            { "screen_name", "opentween" },
-                        })
+                        new Uri("direct_messages/destroy.json", UriKind.Relative),
+                        new Dictionary<string, string> { { "id", "100" } })
                 )
-                .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage()));
+                .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage { Id = 100L }));
 
                 twitterApi.apiConnection = mock.Object;
 
-                await twitterApi.DirectMessagesNew("hogehoge", "opentween")
+                await twitterApi.DirectMessagesDestroy(statusId: 100L)
                     .IgnoreResponse()
                     .ConfigureAwait(false);
 
@@ -750,22 +747,33 @@ namespace OpenTween.Api
         }
 
         [Fact]
-        public async Task DirectMessagesDestroy_Test()
+        public async Task DirectMessagesEventsNew_Test()
         {
             using (var twitterApi = new TwitterApi())
             {
                 var mock = new Mock<IApiConnection>();
                 mock.Setup(x =>
-                    x.PostLazyAsync<TwitterDirectMessage>(
-                        new Uri("direct_messages/destroy.json", UriKind.Relative),
-                        new Dictionary<string, string> { { "id", "100" } })
+                    x.PostJsonAsync(
+                        new Uri("direct_messages/events/new.json", UriKind.Relative),
+                        @"{
+  ""event"": {
+    ""type"": ""message_create"",
+    ""message_create"": {
+      ""target"": {
+        ""recipient_id"": ""12345""
+      },
+      ""message_data"": {
+        ""text"": ""hogehoge""
+      }
+    }
+  }
+}")
                 )
-                .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage { Id = 100L }));
+                .Returns(Task.FromResult(0));
 
                 twitterApi.apiConnection = mock.Object;
 
-                await twitterApi.DirectMessagesDestroy(statusId: 100L)
-                    .IgnoreResponse()
+                await twitterApi.DirectMessagesEventsNew(recipientId: 12345L, text: "hogehoge")
                     .ConfigureAwait(false);
 
                 mock.VerifyAll();
index 693cd0c..80c551a 100644 (file)
@@ -421,27 +421,36 @@ namespace OpenTween.Api
             return this.apiConnection.GetAsync<TwitterDirectMessage[]>(endpoint, param, "/direct_messages/sent");
         }
 
-        public Task<LazyJson<TwitterDirectMessage>> DirectMessagesNew(string status, string sendTo)
+        public Task<LazyJson<TwitterDirectMessage>> DirectMessagesDestroy(long statusId)
         {
-            var endpoint = new Uri("direct_messages/new.json", UriKind.Relative);
+            var endpoint = new Uri("direct_messages/destroy.json", UriKind.Relative);
             var param = new Dictionary<string, string>
             {
-                ["text"] = status,
-                ["screen_name"] = sendTo,
+                ["id"] = statusId.ToString(),
             };
 
             return this.apiConnection.PostLazyAsync<TwitterDirectMessage>(endpoint, param);
         }
 
-        public Task<LazyJson<TwitterDirectMessage>> DirectMessagesDestroy(long statusId)
+        public Task DirectMessagesEventsNew(long recipientId, string text)
         {
-            var endpoint = new Uri("direct_messages/destroy.json", UriKind.Relative);
-            var param = new Dictionary<string, string>
-            {
-                ["id"] = statusId.ToString(),
-            };
+            var endpoint = new Uri("direct_messages/events/new.json", UriKind.Relative);
 
-            return this.apiConnection.PostLazyAsync<TwitterDirectMessage>(endpoint, param);
+            var json = $@"{{
+  ""event"": {{
+    ""type"": ""message_create"",
+    ""message_create"": {{
+      ""target"": {{
+        ""recipient_id"": ""{EscapeJsonString(recipientId.ToString())}""
+      }},
+      ""message_data"": {{
+        ""text"": ""{EscapeJsonString(text)}""
+      }}
+    }}
+  }}
+}}";
+
+            return this.apiConnection.PostJsonAsync(endpoint, json);
         }
 
         public Task<TwitterUser> UsersShow(string screenName)
index 64aa0dd..1058adf 100644 (file)
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
index bacf23f..41cbe37 100644 (file)
@@ -383,13 +383,14 @@ namespace OpenTween
 
             var mc = Twitter.DMSendTextRegex.Match(postStr);
 
-            var response = await this.Api.DirectMessagesNew(mc.Groups["body"].Value, mc.Groups["id"].Value)
-                .ConfigureAwait(false);
+            var body = mc.Groups["body"].Value;
+            var recipientName = mc.Groups["id"].Value;
 
-            var dm = await response.LoadJsonAsync()
+            var recipient = await this.Api.UsersShow(recipientName)
                 .ConfigureAwait(false);
 
-            this.UpdateUserStats(dm.Sender);
+            await this.Api.DirectMessagesEventsNew(recipient.Id, body)
+                .ConfigureAwait(false);
         }
 
         public async Task PostRetweet(long id, bool read)