feat: Add support for message attachments

- Introduced Attachment model to handle file attachments associated with messages.
- Updated ModerationController to manage message deletions and attachment cleanup.
- Enhanced ChatService to include attachments in message retrieval.
- Implemented migration for legacy single-attachment messages to the new Attachments model.
- Added unit tests for attachment handling in message formatting and parsing.
- Updated database context and migrations to support new Attachments table.
This commit is contained in:
HueByte
2026-07-16 05:02:12 +02:00
parent e05b420ce9
commit 2d33773c24
33 changed files with 1843 additions and 454 deletions
+20 -8
View File
@@ -214,7 +214,6 @@ public class ChatService : IChatService
{
Id = Guid.NewGuid(),
Content = dbContent,
Type = MessageType.Text,
SentAt = DateTimeOffset.UtcNow,
ChannelId = channel.Id,
SenderUserId = userId,
@@ -233,9 +232,6 @@ public class ChatService : IChatService
message.SenderUsername,
sender?.NicknameColor,
channelName,
MessageType.Text,
null,
null,
message.SentAt,
Embeds: embeds);
@@ -386,6 +382,13 @@ public class ChatService : IChatService
raw.Reverse();
var messageIds = raw.Select(x => x.m.Id).ToList();
var attachmentsByMessage = (await db.Attachments
.Where(a => messageIds.Contains(a.MessageId))
.ToListAsync())
.GroupBy(a => a.MessageId)
.ToDictionary(g => g.Key, g => g.ToList());
return raw.Select(x =>
{
// Decrypt DB content (handles both encrypted and plaintext via prefix detection)
@@ -399,6 +402,18 @@ public class ChatService : IChatService
catch { /* ignore malformed JSON */ }
}
List<AttachmentDto>? attachments = null;
if (attachmentsByMessage.TryGetValue(x.m.Id, out var atts) && atts.Count > 0)
{
attachments = atts.Select(a => new AttachmentDto(
a.Kind,
a.Url,
a.FileName,
a.FileSize,
// Preview re-encrypted for transport; client decrypts (and room-decrypts for E2E)
_encryption.EncryptNullable(_encryption.DecryptNullable(a.AsciiPreview)))).ToList();
}
// Encrypt for transport — client decrypts
return new MessageDto(
x.m.Id,
@@ -406,11 +421,8 @@ public class ChatService : IChatService
x.m.SenderUsername,
x.NicknameColor,
channelName,
x.m.Type,
x.m.AttachmentUrl,
x.m.AttachmentFileName,
x.m.SentAt,
x.m.AttachmentFileSize,
attachments,
embeds);
}).ToList();
}