• R/O
  • HTTP
  • SSH
  • HTTPS

open-tween: Commit

開発に使用するリポジトリ


Commit MetaInfo

Revision8eeba116685e94a3a16fcb55ba34d8550d9a01db (tree)
Time2019-07-16 22:00:08
AuthorKimura Youichi <kim.upsilon@bucy...>
CommiterKimura Youichi

Log Message

DM送信完了時のレスポンスを即座にDirectタブに反映させる

Change Summary

Incremental Difference

--- a/OpenTween.Tests/Api/TwitterApiTest.cs
+++ b/OpenTween.Tests/Api/TwitterApiTest.cs
@@ -696,7 +696,7 @@ namespace OpenTween.Api
696696 {
697697 var mock = new Mock<IApiConnection>();
698698 mock.Setup(x =>
699- x.PostJsonAsync(
699+ x.PostJsonAsync<TwitterMessageEventSingle>(
700700 new Uri("direct_messages/events/new.json", UriKind.Relative),
701701 @"{
702702 ""event"": {
@@ -718,7 +718,7 @@ namespace OpenTween.Api
718718 }
719719 }")
720720 )
721- .Returns(Task.CompletedTask);
721+ .ReturnsAsync(LazyJson.Create(new TwitterMessageEventSingle()));
722722
723723 twitterApi.apiConnection = mock.Object;
724724
--- a/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs
+++ b/OpenTween.Tests/Connection/TwitterApiConnectionTest.cs
@@ -479,6 +479,40 @@ namespace OpenTween.Connection
479479
480480 Assert.Equal("{\"aaaa\": 1111}", body);
481481
482+ return new HttpResponseMessage(HttpStatusCode.NoContent);
483+ });
484+
485+ var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
486+
487+ await apiConnection.PostJsonAsync(endpoint, "{\"aaaa\": 1111}")
488+ .ConfigureAwait(false);
489+
490+ Assert.Equal(0, mockHandler.QueueCount);
491+ }
492+ }
493+
494+ [Fact]
495+ public async Task PostJsonAsync_T_Test()
496+ {
497+ using (var mockHandler = new HttpMessageHandlerMock())
498+ using (var http = new HttpClient(mockHandler))
499+ using (var apiConnection = new TwitterApiConnection("", ""))
500+ {
501+ apiConnection.http = http;
502+
503+ mockHandler.Enqueue(async x =>
504+ {
505+ Assert.Equal(HttpMethod.Post, x.Method);
506+ Assert.Equal("https://api.twitter.com/1.1/hoge/tetete.json",
507+ x.RequestUri.AbsoluteUri);
508+
509+ Assert.Equal("application/json; charset=utf-8", x.Content.Headers.ContentType.ToString());
510+
511+ var body = await x.Content.ReadAsStringAsync()
512+ .ConfigureAwait(false);
513+
514+ Assert.Equal("{\"aaaa\": 1111}", body);
515+
482516 return new HttpResponseMessage(HttpStatusCode.OK)
483517 {
484518 Content = new StringContent("\"hogehoge\""),
@@ -487,9 +521,14 @@ namespace OpenTween.Connection
487521
488522 var endpoint = new Uri("hoge/tetete.json", UriKind.Relative);
489523
490- await apiConnection.PostJsonAsync(endpoint, "{\"aaaa\": 1111}")
524+ var response = await apiConnection.PostJsonAsync<string>(endpoint, "{\"aaaa\": 1111}")
491525 .ConfigureAwait(false);
492526
527+ var result = await response.LoadJsonAsync()
528+ .ConfigureAwait(false);
529+
530+ Assert.Equal("hogehoge", result);
531+
493532 Assert.Equal(0, mockHandler.QueueCount);
494533 }
495534 }
--- a/OpenTween/Api/DataModel/TwitterMessageEvent.cs
+++ b/OpenTween/Api/DataModel/TwitterMessageEvent.cs
@@ -52,6 +52,13 @@ namespace OpenTween.Api.DataModel
5252 }
5353
5454 [DataContract]
55+ public class TwitterMessageEventSingle
56+ {
57+ [DataMember(Name = "event")]
58+ public TwitterMessageEvent Event { get; set; }
59+ }
60+
61+ [DataContract]
5562 public class TwitterMessageEvent
5663 {
5764 [DataMember(Name = "created_timestamp")]
--- a/OpenTween/Api/TwitterApi.cs
+++ b/OpenTween/Api/TwitterApi.cs
@@ -394,7 +394,7 @@ namespace OpenTween.Api
394394 return this.apiConnection.GetAsync<TwitterMessageEventList>(endpoint, param, "/direct_messages/events/list");
395395 }
396396
397- public Task DirectMessagesEventsNew(long recipientId, string text, long? mediaId = null)
397+ public Task<LazyJson<TwitterMessageEventSingle>> DirectMessagesEventsNew(long recipientId, string text, long? mediaId = null)
398398 {
399399 var endpoint = new Uri("direct_messages/events/new.json", UriKind.Relative);
400400
@@ -424,7 +424,7 @@ namespace OpenTween.Api
424424 }}
425425 }}";
426426
427- return this.apiConnection.PostJsonAsync(endpoint, json);
427+ return this.apiConnection.PostJsonAsync<TwitterMessageEventSingle>(endpoint, json);
428428 }
429429
430430 public Task DirectMessagesEventsDestroy(string eventId)
--- a/OpenTween/Connection/IApiConnection.cs
+++ b/OpenTween/Connection/IApiConnection.cs
@@ -44,6 +44,8 @@ namespace OpenTween.Connection
4444
4545 Task PostJsonAsync(Uri uri, string json);
4646
47+ Task<LazyJson<T>> PostJsonAsync<T>(Uri uri, string json);
48+
4749 Task DeleteAsync(Uri uri);
4850 }
4951 }
--- a/OpenTween/Connection/TwitterApiConnection.cs
+++ b/OpenTween/Connection/TwitterApiConnection.cs
@@ -327,6 +327,11 @@ namespace OpenTween.Connection
327327 }
328328
329329 public async Task PostJsonAsync(Uri uri, string json)
330+ => await this.PostJsonAsync<object>(uri, json)
331+ .IgnoreResponse()
332+ .ConfigureAwait(false);
333+
334+ public async Task<LazyJson<T>> PostJsonAsync<T>(Uri uri, string json)
330335 {
331336 var requestUri = new Uri(RestApiBase, uri);
332337 var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
@@ -337,12 +342,16 @@ namespace OpenTween.Connection
337342
338343 try
339344 {
340- using (var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
341- .ConfigureAwait(false))
342- {
343- await this.CheckStatusCode(response)
344- .ConfigureAwait(false);
345- }
345+ var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
346+ .ConfigureAwait(false);
347+
348+ await this.CheckStatusCode(response)
349+ .ConfigureAwait(false);
350+
351+ var result = new LazyJson<T>(response);
352+ response = null;
353+
354+ return result;
346355 }
347356 catch (HttpRequestException ex)
348357 {
--- a/OpenTween/Resources/ChangeLog.txt
+++ b/OpenTween/Resources/ChangeLog.txt
@@ -3,6 +3,7 @@
33 ==== Ver 2.3.2-dev(2019/xx/xx)
44 * NEW: Twemoji 12.0.0 に対応しました
55 - Unicode 12.0 で追加された絵文字が表示されるようになります
6+ * CHG: DMの送信が完了したらDirectタブに送信したDMを即座に反映する
67 * CHG: htn.to の短縮URLを展開する際に強制的にHTTPSを使用する
78 * FIX: Twemojiを有効にすると絵文字の後に余分な文字が表示される場合がある不具合を修正
89 * FIX: Tumblrのサムネイル表示時にエラーが表示される場合がある不具合を修正
--- a/OpenTween/Tween.cs
+++ b/OpenTween/Tween.cs
@@ -2734,8 +2734,8 @@ namespace OpenTween
27342734 {
27352735 this._statuses.AddPost(post);
27362736 this._statuses.DistributePosts();
2737- this.RefreshTimeline();
27382737 }
2738+ this.RefreshTimeline();
27392739 }
27402740 }
27412741 }
--- a/OpenTween/Twitter.cs
+++ b/OpenTween/Twitter.cs
@@ -381,7 +381,13 @@ namespace OpenTween
381381 var recipient = await this.Api.UsersShow(recipientName)
382382 .ConfigureAwait(false);
383383
384- await this.Api.DirectMessagesEventsNew(recipient.Id, body, mediaId)
384+ var response = await this.Api.DirectMessagesEventsNew(recipient.Id, body, mediaId)
385+ .ConfigureAwait(false);
386+
387+ var messageEventSingle = await response.LoadJsonAsync()
388+ .ConfigureAwait(false);
389+
390+ await this.CreateDirectMessagesEventFromJson(messageEventSingle, read: true)
385391 .ConfigureAwait(false);
386392 }
387393
@@ -1232,6 +1238,24 @@ namespace OpenTween
12321238
12331239 this.nextCursorDirectMessage = eventList.NextCursor;
12341240
1241+ await this.CreateDirectMessagesEventFromJson(eventList, read)
1242+ .ConfigureAwait(false);
1243+ }
1244+
1245+ private async Task CreateDirectMessagesEventFromJson(TwitterMessageEventSingle eventSingle, bool read)
1246+ {
1247+ var eventList = new TwitterMessageEventList
1248+ {
1249+ Apps = new Dictionary<string, TwitterMessageEventList.App>(),
1250+ Events = new[] { eventSingle.Event },
1251+ };
1252+
1253+ await this.CreateDirectMessagesEventFromJson(eventList, read)
1254+ .ConfigureAwait(false);
1255+ }
1256+
1257+ private async Task CreateDirectMessagesEventFromJson(TwitterMessageEventList eventList, bool read)
1258+ {
12351259 var events = eventList.Events
12361260 .Where(x => x.Type == "message_create")
12371261 .ToArray();
Show on old repository browser