Skip to content

Commit

Permalink
Merge pull request #59 from upsilon/post-dm-event
Browse files Browse the repository at this point in the history
DM送信に新エンドポイントを使用する
  • Loading branch information
upsilon authored Jun 8, 2018
2 parents c78e997 + b47248c commit b52d5da
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 111 deletions.
135 changes: 116 additions & 19 deletions OpenTween.Tests/Api/TwitterApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,24 +724,21 @@ await twitterApi.DirectMessagesSent(count: 200, maxId: 900L, sinceId: 100L)
}

[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);

Expand All @@ -750,22 +747,39 @@ await twitterApi.DirectMessagesNew("hogehoge", "opentween")
}

[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"",
""attachment"": {
""type"": ""media"",
""media"": {
""id"": ""67890""
}
}
}
}
}
}")
)
.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", mediaId: 67890L)
.ConfigureAwait(false);

mock.VerifyAll();
Expand Down Expand Up @@ -1262,31 +1276,114 @@ await twitterApi.Configuration()
}

[Fact]
public async Task MediaUpload_Test()
public async Task MediaUploadInit_Test()
{
using (var twitterApi = new TwitterApi())
{
var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.PostLazyAsync<TwitterUploadMediaInit>(
new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
new Dictionary<string, string> {
{ "command", "INIT" },
{ "total_bytes", "123456" },
{ "media_type", "image/png" },
{ "media_category", "dm_image" },
})
)
.ReturnsAsync(LazyJson.Create(new TwitterUploadMediaInit()));

twitterApi.apiConnection = mock.Object;

await twitterApi.MediaUploadInit(totalBytes: 123456L, mediaType: "image/png", mediaCategory: "dm_image")
.IgnoreResponse()
.ConfigureAwait(false);

mock.VerifyAll();
}
}

[Fact]
public async Task MediaUploadAppend_Test()
{
using (var twitterApi = new TwitterApi())
using (var image = TestUtils.CreateDummyImage())
using (var media = new MemoryImageMediaItem(image))
{
var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.PostLazyAsync<TwitterUploadMediaResult>(
x.PostAsync(
new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
null,
new Dictionary<string, string> {
{ "command", "APPEND" },
{ "media_id", "11111" },
{ "segment_index", "1" },
},
new Dictionary<string, IMediaItem> { { "media", media } })
)
.Returns(Task.FromResult(0));

twitterApi.apiConnection = mock.Object;

await twitterApi.MediaUploadAppend(mediaId: 11111L, segmentIndex: 1, media: media)
.ConfigureAwait(false);

mock.VerifyAll();
}
}

[Fact]
public async Task MediaUploadFinalize_Test()
{
using (var twitterApi = new TwitterApi())
{
var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.PostLazyAsync<TwitterUploadMediaResult>(
new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
new Dictionary<string, string> {
{ "command", "FINALIZE" },
{ "media_id", "11111" },
})
)
.ReturnsAsync(LazyJson.Create(new TwitterUploadMediaResult()));

twitterApi.apiConnection = mock.Object;

await twitterApi.MediaUpload(media)
await twitterApi.MediaUploadFinalize(mediaId: 11111L)
.IgnoreResponse()
.ConfigureAwait(false);

mock.VerifyAll();
}
}

[Fact]
public async Task MediaUploadStatus_Test()
{
using (var twitterApi = new TwitterApi())
{
var mock = new Mock<IApiConnection>();
mock.Setup(x =>
x.GetAsync<TwitterUploadMediaResult>(
new Uri("https://upload.twitter.com/1.1/media/upload.json", UriKind.Absolute),
new Dictionary<string, string> {
{ "command", "STATUS" },
{ "media_id", "11111" },
},
null)
)
.ReturnsAsync(new TwitterUploadMediaResult());

twitterApi.apiConnection = mock.Object;

await twitterApi.MediaUploadStatus(mediaId: 11111L)
.ConfigureAwait(false);

mock.VerifyAll();
}
}

[Fact]
public async Task MediaMetadataCreate_Test()
{
Expand Down
74 changes: 67 additions & 7 deletions OpenTween/Api/DataModel/TwitterUploadMediaResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,29 @@ namespace OpenTween.Api.DataModel
// 参照: https://dev.twitter.com/docs/platform-objects/entities
// https://dev.twitter.com/docs/api/multiple-media-extended-entities

[DataContract]
public class TwitterUploadMediaInit
{
[DataMember(Name = "expires_after_secs")]
public int ExpiresAfterSecs { get; set; }

[DataMember(Name = "media_id")]
public long MediaId { get; set; }

[DataMember(Name = "media_id_string")]
public string MediaIdStr { get; set; }

/// <exception cref="SerializationException"/>
public static TwitterUploadMediaInit ParseJson(string json)
=> MyCommon.CreateDataFromJson<TwitterUploadMediaInit>(json);
}

[DataContract]
public class TwitterUploadMediaResult
{
[DataMember(Name = "expires_after_secs")]
public int ExpiresAfterSecs { get; set; }

[DataContract]
public class ImageInfo
{
Expand All @@ -47,22 +67,62 @@ public class ImageInfo
public string ImageType { get; set; }
}

[DataMember(Name = "image")]
public TwitterUploadMediaResult.ImageInfo MediaInfo { get; set; }
[DataMember(Name = "image", IsRequired = false)]
public ImageInfo Image { get; set; }

[DataMember(Name = "media_id")]
public long MediaId { get; set; }

[DataMember(Name = "media_id_string")]
public string MediaIdStr { get; set; }

[DataMember(Name = "size")]
public long Size { get; set; }
[DataContract]
public class MediaProcessingInfo
{
[DataMember(Name = "check_after_secs", IsRequired = false)]
public int? CheckAfterSecs { get; set; }

/// <exception cref="SerializationException"/>
public static TwitterUploadMediaResult ParseJson(string json)
[DataContract]
public class MediaProcessingError
{
[DataMember(Name = "code")]
public int Code { get; set; }

[DataMember(Name = "name")]
public string Name { get; set; }

[DataMember(Name = "message")]
public string Message { get; set; }
}

[DataMember(Name = "error", IsRequired = false)]
public MediaProcessingError Error { get; set; }

[DataMember(Name = "progress_percent")]
public int ProgressPercent { get; set; }

[DataMember(Name = "state")]
public string State { get; set; }
}

[DataMember(Name = "processing_info", IsRequired = false)]
public MediaProcessingInfo ProcessingInfo { get; set; }

[DataMember(Name = "size", IsRequired = false)]
public long? Size { get; set; }

[DataContract]
public class VideoInfo
{
return MyCommon.CreateDataFromJson<TwitterUploadMediaResult>(json);
[DataMember(Name = "video_type")]
public string VideoType { get; set; }
}

[DataMember(Name = "video", IsRequired = false)]
public VideoInfo Video { get; set; }

/// <exception cref="SerializationException"/>
public static TwitterUploadMediaResult ParseJson(string json)
=> MyCommon.CreateDataFromJson<TwitterUploadMediaResult>(json);
}
}
Loading

0 comments on commit b52d5da

Please sign in to comment.