feat: add message update handler

This commit is contained in:
sam 2024-08-16 00:51:19 +02:00
parent 14b132e466
commit 8231c57bdf
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
3 changed files with 159 additions and 0 deletions

View file

@ -36,6 +36,37 @@ public class MessageRepository(ILogger logger, DatabaseContext db, IEncryptionSe
await db.SaveChangesAsync(ct);
}
public async Task UpdateMessageAsync(IMessageCreate msg, CancellationToken ct = default)
{
_logger.Debug("Updating message {MessageId}", msg.ID);
var tx = await db.Database.BeginTransactionAsync(ct);
var (isStored, _) = await HasProxyInfoAsync(msg.ID.Value);
if (!isStored)
{
await SaveMessageAsync(msg, ct);
}
else
{
var metadata = new Metadata(IsWebhook: msg.WebhookID.HasValue,
msg.Attachments.Select(a => new Attachment(a.Filename, a.Size, a.ContentType.Value)));
var dbMsg = await db.Messages.FindAsync(msg.ID.Value);
if (dbMsg == null) throw new CataloggerError("Message was null despite HasProxyInfoAsync returning true");
dbMsg.EncryptedContent = await Task.Run(
() => encryptionService.Encrypt(string.IsNullOrWhiteSpace(msg.Content) ? "None" : msg.Content), ct);
dbMsg.EncryptedUsername = await Task.Run(() => encryptionService.Encrypt(msg.Author.Tag()), ct);
dbMsg.EncryptedMetadata =
await Task.Run(() => encryptionService.Encrypt(JsonSerializer.Serialize(metadata)), ct);
db.Update(dbMsg);
await db.SaveChangesAsync(ct);
}
await tx.CommitAsync(ct);
}
public async Task<Message?> GetMessageAsync(ulong id, CancellationToken ct = default)
{
_logger.Debug("Retrieving message {MessageId}", id);