From 4611ea76ea1830e4f59e9ae28ead6d859aa67e61 Mon Sep 17 00:00:00 2001 From: Kimura Youichi Date: Mon, 4 Jun 2018 00:42:42 +0900 Subject: [PATCH] =?utf8?q?DM=E9=80=81=E4=BF=A1=E3=81=AB=E6=96=B0=E3=82=A8?= =?utf8?q?=E3=83=B3=E3=83=89=E3=83=9D=E3=82=A4=E3=83=B3=E3=83=88=E3=82=92?= =?utf8?q?=E4=BD=BF=E7=94=A8=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- OpenTween.Tests/Api/TwitterApiTest.cs | 38 +++++++++++++++++++++-------------- OpenTween/Api/TwitterApi.cs | 31 ++++++++++++++++++---------- OpenTween/OpenTween.csproj | 3 ++- OpenTween/Twitter.cs | 9 +++++---- 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/OpenTween.Tests/Api/TwitterApiTest.cs b/OpenTween.Tests/Api/TwitterApiTest.cs index ab909e37..15990b5e 100644 --- a/OpenTween.Tests/Api/TwitterApiTest.cs +++ b/OpenTween.Tests/Api/TwitterApiTest.cs @@ -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(); mock.Setup(x => x.PostLazyAsync( - new Uri("direct_messages/new.json", UriKind.Relative), - new Dictionary { - { "text", "hogehoge" }, - { "screen_name", "opentween" }, - }) + new Uri("direct_messages/destroy.json", UriKind.Relative), + new Dictionary { { "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(); mock.Setup(x => - x.PostLazyAsync( - new Uri("direct_messages/destroy.json", UriKind.Relative), - new Dictionary { { "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(); diff --git a/OpenTween/Api/TwitterApi.cs b/OpenTween/Api/TwitterApi.cs index 693cd0cd..80c551a2 100644 --- a/OpenTween/Api/TwitterApi.cs +++ b/OpenTween/Api/TwitterApi.cs @@ -421,27 +421,36 @@ namespace OpenTween.Api return this.apiConnection.GetAsync(endpoint, param, "/direct_messages/sent"); } - public Task> DirectMessagesNew(string status, string sendTo) + public Task> 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 { - ["text"] = status, - ["screen_name"] = sendTo, + ["id"] = statusId.ToString(), }; return this.apiConnection.PostLazyAsync(endpoint, param); } - public Task> DirectMessagesDestroy(long statusId) + public Task DirectMessagesEventsNew(long recipientId, string text) { - var endpoint = new Uri("direct_messages/destroy.json", UriKind.Relative); - var param = new Dictionary - { - ["id"] = statusId.ToString(), - }; + var endpoint = new Uri("direct_messages/events/new.json", UriKind.Relative); - return this.apiConnection.PostLazyAsync(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 UsersShow(string screenName) diff --git a/OpenTween/OpenTween.csproj b/OpenTween/OpenTween.csproj index 64aa0dd7..1058adf4 100644 --- a/OpenTween/OpenTween.csproj +++ b/OpenTween/OpenTween.csproj @@ -1,5 +1,6 @@  - + + Debug AnyCPU diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index bacf23f4..41cbe37b 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -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) -- 2.11.0