feat: store message metadata

This commit is contained in:
sam 2024-08-15 01:12:34 +02:00
parent 4db09346e2
commit 5585ffd6ea
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
2 changed files with 27 additions and 5 deletions

View file

@ -58,7 +58,7 @@ public class MessageDeleteResponder(
return Result.Success;
}
logChannel = webhookExecutor.GetLogChannel(guild, LogChannelType.MessageDelete, ev.ChannelID, msg.UserId);
if (logChannel == null) return Result.Success;
@ -96,6 +96,14 @@ public class MessageDeleteResponder(
builder.AddField("Member ID", msg.Member, true);
}
if (msg.Metadata != null)
{
var attachmentInfo = string.Join("\n",
msg.Metadata.Attachments.Select(a =>
$"{a.Filename} ({a.ContentType}, {a.Size.Bytes().Humanize()})"));
if (!string.IsNullOrWhiteSpace(attachmentInfo)) builder.AddField("Attachments", attachmentInfo, false);
}
await webhookExecutor.QueueLogAsync(logChannel.Value, builder.Build().GetOrThrow());
return Result.Success;
}

View file

@ -1,4 +1,4 @@
using Catalogger.Backend.Database.Models;
using System.Text.Json;
using Catalogger.Backend.Extensions;
using Microsoft.EntityFrameworkCore;
using Remora.Discord.API.Abstractions.Gateway.Events;
@ -14,6 +14,9 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
{
_logger.Debug("Saving message {MessageId}", msg.ID);
var metadata = new Metadata(IsWebhook: msg.WebhookID.HasValue,
msg.Attachments.Select(a => new Attachment(a.Filename, a.Size, a.ContentType.Value)));
var dbMessage = new DbMessage
{
Id = msg.ID.ToUlong(),
@ -25,6 +28,7 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
await Task.Run(
() => encryptionService.Encrypt(string.IsNullOrWhiteSpace(msg.Content) ? "None" : msg.Content), ct),
EncryptedUsername = await Task.Run(() => encryptionService.Encrypt(msg.Author.Tag()), ct),
EncryptedMetadata = await Task.Run(() => encryptionService.Encrypt(JsonSerializer.Serialize(metadata)), ct),
AttachmentSize = msg.Attachments.Select(a => a.Size).Sum()
};
@ -40,8 +44,14 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
if (dbMsg == null) return null;
return new Message(dbMsg.Id, dbMsg.OriginalId, dbMsg.UserId, dbMsg.ChannelId, dbMsg.GuildId, dbMsg.Member,
dbMsg.System, await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedUsername), ct),
await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedContent), ct), null, dbMsg.AttachmentSize);
dbMsg.System,
Username: await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedUsername), ct),
Content: await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedContent), ct),
Metadata: dbMsg.EncryptedMetadata != null
? JsonSerializer.Deserialize<Metadata>(
await Task.Run(() => encryptionService.Decrypt(dbMsg.EncryptedMetadata), ct))
: null,
dbMsg.AttachmentSize);
}
/// <summary>
@ -95,7 +105,11 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
string? System,
string Username,
string Content,
string? Metadata,
Metadata? Metadata,
int AttachmentSize
);
public record Metadata(bool IsWebhook, IEnumerable<Attachment> Attachments);
public record Attachment(string Filename, int Size, string ContentType);
}