forked from mtusi2026/mtusi2026lab4
feat(attachments): add attachment metadata and message links
This commit is contained in:
+92
-16
@@ -6,6 +6,8 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -18,14 +20,20 @@ import (
|
||||
|
||||
var (
|
||||
ErrMessageChatNotFound = errors.New("chat not found")
|
||||
ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
ErrAttachmentNotReady = errors.New("attachment not ready")
|
||||
ErrIdempotencyKeyConflict = errors.New("idempotency key reused with different request")
|
||||
)
|
||||
|
||||
type messageQueries interface {
|
||||
CreateIdempotencyKey(ctx context.Context, arg db.CreateIdempotencyKeyParams) (int64, error)
|
||||
CreateMessage(ctx context.Context, arg db.CreateMessageParams) (db.Message, error)
|
||||
CreateMessageAttachment(ctx context.Context, arg db.CreateMessageAttachmentParams) error
|
||||
GetIdempotentMessage(ctx context.Context, arg db.GetIdempotentMessageParams) (db.GetIdempotentMessageRow, error)
|
||||
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
|
||||
ListAttachmentsByIDs(ctx context.Context, arg db.ListAttachmentsByIDsParams) ([]string, error)
|
||||
ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error)
|
||||
ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIDs []uuid.UUID) ([]db.ListMessageAttachmentsByMessageIDsRow, error)
|
||||
ListMessagesAfter(ctx context.Context, arg db.ListMessagesAfterParams) ([]db.Message, error)
|
||||
ListMessagesFirstPage(ctx context.Context, arg db.ListMessagesFirstPageParams) ([]db.Message, error)
|
||||
}
|
||||
@@ -52,13 +60,33 @@ func (s *MessageService) Create(
|
||||
chatID uuid.UUID,
|
||||
key string,
|
||||
text string,
|
||||
attachmentIDs []uuid.UUID,
|
||||
) (model.Message, error) {
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
sum := sha256.Sum256([]byte(text))
|
||||
if len(attachmentIDs) > 0 {
|
||||
statuses, err := s.q.ListAttachmentsByIDs(ctx, db.ListAttachmentsByIDsParams{
|
||||
ChatID: chatID,
|
||||
Ids: attachmentIDs,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
if len(statuses) != len(attachmentIDs) {
|
||||
return model.Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if status != "ready" {
|
||||
return model.Message{}, ErrAttachmentNotReady
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hashInput := text + "|" + normalizeAttachmentIDs(attachmentIDs)
|
||||
sum := sha256.Sum256([]byte(hashInput))
|
||||
hash := hex.EncodeToString(sum[:])
|
||||
path := "/api/v1/chats/" + chatID.String() + "/messages"
|
||||
|
||||
@@ -107,12 +135,18 @@ func (s *MessageService) Create(
|
||||
return model.Message{}, ErrIdempotencyKeyConflict
|
||||
}
|
||||
|
||||
storedAttachmentIDs, err := q.ListMessageAttachmentIDs(ctx, stored.ID)
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
return model.Message{
|
||||
ID: stored.ID,
|
||||
ChatID: stored.ChatID,
|
||||
SenderID: stored.SenderID,
|
||||
Text: stored.Text,
|
||||
CreatedAt: stored.CreatedAt,
|
||||
ID: stored.ID,
|
||||
ChatID: stored.ChatID,
|
||||
SenderID: stored.SenderID,
|
||||
Text: stored.Text,
|
||||
AttachmentIDs: storedAttachmentIDs,
|
||||
CreatedAt: stored.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -129,16 +163,27 @@ func (s *MessageService) Create(
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
for i, attachmentID := range attachmentIDs {
|
||||
if err := q.CreateMessageAttachment(ctx, db.CreateMessageAttachmentParams{
|
||||
MessageID: id,
|
||||
AttachmentID: attachmentID,
|
||||
Position: int32(i),
|
||||
}); err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
return model.Message{
|
||||
ID: row.ID,
|
||||
ChatID: row.ChatID,
|
||||
SenderID: row.SenderID,
|
||||
Text: row.Text,
|
||||
CreatedAt: row.CreatedAt,
|
||||
ID: row.ID,
|
||||
ChatID: row.ChatID,
|
||||
SenderID: row.SenderID,
|
||||
Text: row.Text,
|
||||
AttachmentIDs: attachmentIDs,
|
||||
CreatedAt: row.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -189,14 +234,31 @@ func (s *MessageService) List(
|
||||
rows = rows[:limit]
|
||||
}
|
||||
|
||||
attachmentsByMessage := make(map[uuid.UUID][]uuid.UUID, len(rows))
|
||||
if len(rows) > 0 {
|
||||
messageIDs := make([]uuid.UUID, len(rows))
|
||||
for i, r := range rows {
|
||||
messageIDs[i] = r.ID
|
||||
}
|
||||
|
||||
links, err := s.q.ListMessageAttachmentsByMessageIDs(ctx, messageIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, l := range links {
|
||||
attachmentsByMessage[l.MessageID] = append(attachmentsByMessage[l.MessageID], l.AttachmentID)
|
||||
}
|
||||
}
|
||||
|
||||
messages := make([]model.Message, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
messages = append(messages, model.Message{
|
||||
ID: r.ID,
|
||||
ChatID: r.ChatID,
|
||||
SenderID: r.SenderID,
|
||||
Text: r.Text,
|
||||
CreatedAt: r.CreatedAt,
|
||||
ID: r.ID,
|
||||
ChatID: r.ChatID,
|
||||
SenderID: r.SenderID,
|
||||
Text: r.Text,
|
||||
AttachmentIDs: attachmentsByMessage[r.ID],
|
||||
CreatedAt: r.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -213,3 +275,17 @@ func (s *MessageService) List(
|
||||
|
||||
return messages, next, nil
|
||||
}
|
||||
|
||||
func normalizeAttachmentIDs(ids []uuid.UUID) string {
|
||||
if len(ids) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
parts := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
parts[i] = id.String()
|
||||
}
|
||||
sort.Strings(parts)
|
||||
|
||||
return strings.Join(parts, ",")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user