Skip to content

Commit

Permalink
Implement support for webhook messages with files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Apr 5, 2021
1 parent c05608b commit 6fadaca
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .idea/.idea.Remora.Discord/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ Task<Result<IMessage>> ExecuteWebhookAsync
/// <param name="content">The new content, if any.</param>
/// <param name="embeds">The new embeds, if any.</param>
/// <param name="allowedMentions">The new allowed mentions, if any.</param>
/// <param name="file">The new file, if any.</param>
/// <param name="ct">The cancellation token for this operation.</param>
/// <returns>A result which may or may not have succeeded.</returns>
Task<Result<IMessage>> EditWebhookMessageAsync
Expand All @@ -221,6 +222,7 @@ Task<Result<IMessage>> EditWebhookMessageAsync
Optional<string?> content = default,
Optional<IReadOnlyList<IEmbed>?> embeds = default,
Optional<IAllowedMentions?> allowedMentions = default,
Optional<FileData?> file = default,
CancellationToken ct = default
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ public virtual async Task<Result<IMessage>> EditWebhookMessageAsync
Optional<string?> content = default,
Optional<IReadOnlyList<IEmbed>?> embeds = default,
Optional<IAllowedMentions?> allowedMentions = default,
Optional<FileData?> file = default,
CancellationToken ct = default
)
{
Expand All @@ -322,6 +323,11 @@ public virtual async Task<Result<IMessage>> EditWebhookMessageAsync
$"webhooks/{webhookID}/{token}/messages/{messageID}",
b =>
{
if (file.HasValue)
{
b.AddContent(new StreamContent(file.Value.Content), "file", file.Value.Name);
}

b.WithJson
(
json =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ public async Task PerformsRequestCorrectly()
/// </summary>
public class ExecuteWebhookAsync : RestAPITestBase<IDiscordRestWebhookAPI>
{
/// <summary>
/// <summary>
/// Tests whether the API method performs its request correctly.
/// </summary>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
Expand Down Expand Up @@ -799,20 +799,65 @@ public async Task PerformsFileUploadRequestCorrectly()
/// </summary>
public class EditWebhookMessageAsync : RestAPITestBase<IDiscordRestWebhookAPI>
{
/// <summary>
/// Tests whether the API method performs its request correctly.
/// </summary>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
[Fact]
public async Task PerformsNormalRequestCorrectly()
{
var webhookID = new Snowflake(0);
var token = "aa";
var messageID = new Snowflake(1);

var content = "booga";
var allowedMentions = new AllowedMentions();

var api = CreateAPI
(
b => b
.Expect
(
HttpMethod.Patch,
$"{Constants.BaseURL}webhooks/{webhookID}/{token}/messages/{messageID}"
)
.WithJson
(
json => json.IsObject
(
o => o
.WithProperty("content", p => p.Is(content))
.WithProperty("allowed_mentions", p => p.IsObject())
)
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
);

var result = await api.EditWebhookMessageAsync
(
webhookID,
token,
messageID,
content,
allowedMentions: allowedMentions
);

ResultAssert.Successful(result);
}

/// <summary>
/// Tests whether the API method performs its request correctly.
/// </summary>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
[Fact]
public async Task PerformsRequestCorrectly()
public async Task PerformsEmbedRequestCorrectly()
{
var webhookID = new Snowflake(0);
var token = "aa";
var messageID = new Snowflake(1);

var content = "booga";
var embeds = new List<IEmbed>();
var allowedMentions = new AllowedMentions(default, default, default, default);

var api = CreateAPI
(
Expand All @@ -829,7 +874,6 @@ public async Task PerformsRequestCorrectly()
o => o
.WithProperty("content", p => p.Is(content))
.WithProperty("embeds", p => p.IsArray(a => a.WithCount(0)))
.WithProperty("allowed_mentions", p => p.IsObject())
)
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
Expand All @@ -841,8 +885,74 @@ public async Task PerformsRequestCorrectly()
token,
messageID,
content,
embeds,
allowedMentions
embeds
);

ResultAssert.Successful(result);
}

/// <summary>
/// Tests whether the API method performs its request correctly.
/// </summary>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
[Fact]
public async Task PerformsFileUploadRequestCorrectly()
{
var webhookID = new Snowflake(0);
var token = "aa";
var messageID = new Snowflake(1);

var content = "booga";

await using var file = new MemoryStream();
var fileName = "file.bin";

var api = CreateAPI
(
b => b
.Expect
(
HttpMethod.Patch,
$"{Constants.BaseURL}webhooks/{webhookID}/{token}/messages/{messageID}"
)
.With
(
m =>
{
if (!(m.Content is MultipartFormDataContent multipart))
{
return false;
}

var streamContent = multipart.FirstOrDefault(x => x is StreamContent);
if (streamContent?.Headers.ContentDisposition is null)
{
return false;
}

if (streamContent.Headers.ContentDisposition.FileName != fileName)
{
return false;
}

if (!multipart.Any(c => c is StringContent))
{
return false;
}

return true;
}
)
.Respond("application/json", SampleRepository.Samples[typeof(IMessage)])
);

var result = await api.EditWebhookMessageAsync
(
webhookID,
token,
messageID,
content,
file: new FileData(fileName, file)
);

ResultAssert.Successful(result);
Expand Down

0 comments on commit 6fadaca

Please sign in to comment.