From 0ca356eec38dc36e96b36b260c98f98b80672358 Mon Sep 17 00:00:00 2001 From: spiral Date: Fri, 19 Nov 2021 09:34:52 -0500 Subject: [PATCH] feat: add support for attachment descriptions --- Myriad/Rest/BaseRestClient.cs | 4 ++-- Myriad/Rest/Types/MultipartFile.cs | 2 +- .../Types/Requests/ExecuteWebhookRequest.cs | 1 + Myriad/Types/Message.cs | 1 + PluralKit.Bot/Commands/ImportExport.cs | 2 +- PluralKit.Bot/Commands/Message.cs | 2 +- .../Services/WebhookExecutorService.cs | 20 +++++++++++++++++-- 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Myriad/Rest/BaseRestClient.cs b/Myriad/Rest/BaseRestClient.cs index f9f04000..76a32693 100644 --- a/Myriad/Rest/BaseRestClient.cs +++ b/Myriad/Rest/BaseRestClient.cs @@ -154,8 +154,8 @@ namespace Myriad.Rest { for (var i = 0; i < files.Length; i++) { - var (filename, stream) = files[i]; - mfd.Add(new StreamContent(stream), $"file{i}", filename); + var (filename, stream, _) = files[i]; + mfd.Add(new StreamContent(stream), $"files[{i}]", filename); } } diff --git a/Myriad/Rest/Types/MultipartFile.cs b/Myriad/Rest/Types/MultipartFile.cs index ba36375f..1f1d5bfd 100644 --- a/Myriad/Rest/Types/MultipartFile.cs +++ b/Myriad/Rest/Types/MultipartFile.cs @@ -2,5 +2,5 @@ using System.IO; namespace Myriad.Rest.Types { - public record MultipartFile(string Filename, Stream Data); + public record MultipartFile(string Filename, Stream Data, string? Description); } \ No newline at end of file diff --git a/Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs b/Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs index c4733fa8..c8db9e29 100644 --- a/Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs +++ b/Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs @@ -8,6 +8,7 @@ namespace Myriad.Rest.Types.Requests public string? Username { get; init; } public string? AvatarUrl { get; init; } public Embed[] Embeds { get; init; } + public Message.Attachment[] Attachments { get; set; } public AllowedMentions? AllowedMentions { get; init; } } } \ No newline at end of file diff --git a/Myriad/Types/Message.cs b/Myriad/Types/Message.cs index 7d95dd66..f60df07b 100644 --- a/Myriad/Types/Message.cs +++ b/Myriad/Types/Message.cs @@ -71,6 +71,7 @@ namespace Myriad.Types public record Attachment { public ulong Id { get; init; } + public string? Description { get; init; } public string Filename { get; init; } public int Size { get; init; } public string Url { get; init; } diff --git a/PluralKit.Bot/Commands/ImportExport.cs b/PluralKit.Bot/Commands/ImportExport.cs index ed6d53eb..f2c3c461 100644 --- a/PluralKit.Bot/Commands/ImportExport.cs +++ b/PluralKit.Bot/Commands/ImportExport.cs @@ -113,7 +113,7 @@ namespace PluralKit.Bot var msg = await ctx.Rest.CreateMessage(dm.Id, new MessageRequest { Content = $"{Emojis.Success} Here you go!" }, - new[] { new MultipartFile("system.json", stream) }); + new[] { new MultipartFile("system.json", stream, null) }); await ctx.Rest.CreateMessage(dm.Id, new MessageRequest { Content = $"<{msg.Attachments[0].Url}>" }); // If the original message wasn't posted in DMs, send a public reminder diff --git a/PluralKit.Bot/Commands/Message.cs b/PluralKit.Bot/Commands/Message.cs index 99a07be8..a767d478 100644 --- a/PluralKit.Bot/Commands/Message.cs +++ b/PluralKit.Bot/Commands/Message.cs @@ -189,7 +189,7 @@ namespace PluralKit.Bot await ctx.Rest.CreateMessage( ctx.Channel.Id, new MessageRequest { Content = $"{Emojis.Warn} Message contains codeblocks, raw source sent as an attachment." }, - new[] { new MultipartFile("message.txt", stream) }); + new[] { new MultipartFile("message.txt", stream, null) }); } return; diff --git a/PluralKit.Bot/Services/WebhookExecutorService.cs b/PluralKit.Bot/Services/WebhookExecutorService.cs index d91b6172..27990e6e 100644 --- a/PluralKit.Bot/Services/WebhookExecutorService.cs +++ b/PluralKit.Bot/Services/WebhookExecutorService.cs @@ -129,6 +129,12 @@ namespace PluralKit.Bot _logger.Information("Invoking webhook with {AttachmentCount} attachments totalling {AttachmentSize} MiB in {AttachmentChunks} chunks", req.Attachments.Length, req.Attachments.Select(a => a.Size).Sum() / 1024 / 1024, attachmentChunks.Count); files = await GetAttachmentFiles(attachmentChunks[0]); + webhookReq.Attachments = files.Select(f => new Message.Attachment() + { + Id = (ulong)Array.IndexOf(files, f), + Description = f.Description, + Filename = f.Filename + }).ToArray(); } Message webhookMessage; @@ -174,7 +180,17 @@ namespace PluralKit.Bot for (var i = 1; i < attachmentChunks.Count; i++) { var files = await GetAttachmentFiles(attachmentChunks[i]); - var req = new ExecuteWebhookRequest { Username = name, AvatarUrl = avatarUrl }; + var req = new ExecuteWebhookRequest + { + Username = name, + AvatarUrl = avatarUrl, + Attachments = files.Select(f => new Message.Attachment() + { + Id = (ulong)Array.IndexOf(files, f), + Description = f.Description, + Filename = f.Filename, + }).ToArray() + }; await _rest.ExecuteWebhook(webhook.Id, webhook.Token!, req, files, threadId); } } @@ -184,7 +200,7 @@ namespace PluralKit.Bot async Task GetStream(Message.Attachment attachment) { var attachmentResponse = await _client.GetAsync(attachment.Url, HttpCompletionOption.ResponseHeadersRead); - return new(attachment.Filename, await attachmentResponse.Content.ReadAsStreamAsync()); + return new(attachment.Filename, await attachmentResponse.Content.ReadAsStreamAsync(), attachment.Description); } return await Task.WhenAll(attachments.Select(GetStream));