forked from mtusi2026/mtusi2026lab4
лаба 4 на оценку 5
This commit is contained in:
@@ -166,6 +166,15 @@ func (r *MemoryRepository) CreateMessageIdempotently(
|
||||
if _, err := r.legacy.GetChatByID(write.ChatID, write.ActorID); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
for _, attachmentID := range write.AttachmentIDs {
|
||||
attachment, err := r.legacy.GetAttachmentByID(attachmentID)
|
||||
if err != nil || attachment.ChatID != write.ChatID {
|
||||
return domain.Message{}, ErrNotFound
|
||||
}
|
||||
if attachment.Status != domain.AttachmentStatusReady {
|
||||
return domain.Message{}, application.ErrAttachmentNotReady
|
||||
}
|
||||
}
|
||||
|
||||
if stored, exists := r.idempotencyByScope[scope]; exists {
|
||||
if stored.expiresAt.After(now) {
|
||||
@@ -177,7 +186,12 @@ func (r *MemoryRepository) CreateMessageIdempotently(
|
||||
delete(r.idempotencyByScope, scope)
|
||||
}
|
||||
|
||||
message, err := r.legacy.CreateMessage(write.ChatID, write.ActorID, write.Text)
|
||||
message, err := r.legacy.CreateMessage(
|
||||
write.ChatID,
|
||||
write.ActorID,
|
||||
write.Text,
|
||||
write.AttachmentIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
@@ -277,6 +291,74 @@ func (r *MemoryRepository) DeleteSession(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) CreateAttachment(
|
||||
ctx context.Context,
|
||||
attachment domain.Attachment,
|
||||
) (domain.Attachment, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if err := r.legacy.PutAttachment(attachment); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) MarkAttachmentReady(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
updatedAt time.Time,
|
||||
) (domain.Attachment, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
return r.legacy.UpdateAttachmentStatus(
|
||||
attachmentID,
|
||||
domain.AttachmentStatusReady,
|
||||
nil,
|
||||
updatedAt,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) MarkAttachmentFailed(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
errorCode string,
|
||||
updatedAt time.Time,
|
||||
) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := r.legacy.UpdateAttachmentStatus(
|
||||
attachmentID,
|
||||
domain.AttachmentStatusFailed,
|
||||
&errorCode,
|
||||
updatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) GetAttachment(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
callerID string,
|
||||
) (domain.Attachment, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
attachment, err := r.legacy.GetAttachmentByID(attachmentID)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if _, err = r.legacy.GetChatByID(attachment.ChatID, callerID); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if attachment.Variants == nil {
|
||||
attachment.Variants = make([]domain.AttachmentVariant, 0)
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func memoryTokenHashKey(tokenHash []byte) ([32]byte, error) {
|
||||
var key [32]byte
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ type MemoryStore struct {
|
||||
chatsByID map[string]domain.Chat //key = chat UUID
|
||||
|
||||
messagesByChatID map[string][]domain.Message // key = chat UUID, value = слайс сообщений
|
||||
|
||||
attachmentsByID map[string]domain.Attachment
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
@@ -28,6 +30,7 @@ func NewMemoryStore() *MemoryStore {
|
||||
usersByUsername: make(map[string]string),
|
||||
chatsByID: make(map[string]domain.Chat),
|
||||
messagesByChatID: make(map[string][]domain.Message),
|
||||
attachmentsByID: make(map[string]domain.Attachment),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,12 +180,86 @@ func (s *MemoryStore) GetChatsForUser(
|
||||
return items, end < len(chats), nil
|
||||
}
|
||||
|
||||
// PutAttachment stores metadata for adapter and application tests. Binary
|
||||
// content intentionally does not belong to this in-memory metadata store.
|
||||
func (s *MemoryStore) PutAttachment(attachment domain.Attachment) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if _, exists := s.attachmentsByID[attachment.ID]; exists {
|
||||
return ErrConflict
|
||||
}
|
||||
|
||||
chat, exists := s.chatsByID[attachment.ChatID]
|
||||
if !exists {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
ownerIsMember := false
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == attachment.OwnerID {
|
||||
ownerIsMember = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ownerIsMember {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
s.attachmentsByID[attachment.ID] = attachment
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetAttachmentByID(
|
||||
attachmentID string,
|
||||
) (domain.Attachment, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
attachment, exists := s.attachmentsByID[attachmentID]
|
||||
if !exists {
|
||||
return domain.Attachment{}, ErrNotFound
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) UpdateAttachmentStatus(
|
||||
attachmentID string,
|
||||
status domain.AttachmentStatus,
|
||||
errorCode *string,
|
||||
updatedAt time.Time,
|
||||
) (domain.Attachment, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
attachment, exists := s.attachmentsByID[attachmentID]
|
||||
if !exists {
|
||||
return domain.Attachment{}, ErrNotFound
|
||||
}
|
||||
if attachment.Status != domain.AttachmentStatusPending {
|
||||
return domain.Attachment{}, ErrConflict
|
||||
}
|
||||
attachment.Status = status
|
||||
attachment.ErrorCode = errorCode
|
||||
attachment.UpdatedAt = updatedAt
|
||||
if attachment.Variants == nil {
|
||||
attachment.Variants = make([]domain.AttachmentVariant, 0)
|
||||
}
|
||||
s.attachmentsByID[attachmentID] = attachment
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
// ---Сообщения---
|
||||
|
||||
func (s *MemoryStore) CreateMessage(
|
||||
chatID, senderID, text string,
|
||||
attachmentLists ...[]string,
|
||||
) (domain.Message, error) {
|
||||
messageID := uuid.NewString()
|
||||
attachmentIDs := make([]string, 0)
|
||||
if len(attachmentLists) > 0 {
|
||||
attachmentIDs = append(attachmentIDs, attachmentLists[0]...)
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -216,11 +293,12 @@ func (s *MemoryStore) CreateMessage(
|
||||
}
|
||||
|
||||
message := domain.Message{
|
||||
ID: messageID,
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: createdAt,
|
||||
ID: messageID,
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
AttachmentIDs: attachmentIDs,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
|
||||
s.messagesByChatID[chatID] = append(messages, message)
|
||||
@@ -282,6 +360,12 @@ func (s *MemoryStore) GetMessages(
|
||||
|
||||
// копируем только возвращаемую страницу, а не всю историю, изменяя внутренний срез
|
||||
items := make([]domain.Message, end-start)
|
||||
copy(items, messages[start:end])
|
||||
for index, message := range messages[start:end] {
|
||||
message.AttachmentIDs = append(
|
||||
make([]string, 0, len(message.AttachmentIDs)),
|
||||
message.AttachmentIDs...,
|
||||
)
|
||||
items[index] = message
|
||||
}
|
||||
return items, end < len(messages), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user