refactor: remove unused in-memory store
This commit is contained in:
@@ -1,236 +0,0 @@
|
|||||||
package store
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"slices"
|
|
||||||
"sort"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
|
|
||||||
"mtusi2026-messenger/internal/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrNotFound = errors.New("not found")
|
|
||||||
ErrConflict = errors.New("conflict")
|
|
||||||
ErrDuplicateMember = errors.New("duplicate member id")
|
|
||||||
ErrUnknownMember = errors.New("unknown member id")
|
|
||||||
ErrInvalidCursor = errors.New("invalid cursor")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Memory struct {
|
|
||||||
mu sync.RWMutex
|
|
||||||
|
|
||||||
users map[uuid.UUID]model.User
|
|
||||||
usersByUsername map[string]uuid.UUID
|
|
||||||
chats map[uuid.UUID]model.Chat
|
|
||||||
messages map[uuid.UUID]model.Message
|
|
||||||
chatMessages map[uuid.UUID][]uuid.UUID
|
|
||||||
}
|
|
||||||
|
|
||||||
func New() *Memory {
|
|
||||||
return &Memory{
|
|
||||||
users: make(map[uuid.UUID]model.User),
|
|
||||||
usersByUsername: make(map[string]uuid.UUID),
|
|
||||||
chats: make(map[uuid.UUID]model.Chat),
|
|
||||||
messages: make(map[uuid.UUID]model.Message),
|
|
||||||
chatMessages: make(map[uuid.UUID][]uuid.UUID),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Memory) CreateUser(username, displayName string) (model.User, error) {
|
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
|
|
||||||
if _, exists := m.usersByUsername[username]; exists {
|
|
||||||
return model.User{}, ErrConflict
|
|
||||||
}
|
|
||||||
|
|
||||||
user := model.User{
|
|
||||||
ID: uuid.New(),
|
|
||||||
Username: username,
|
|
||||||
DisplayName: displayName,
|
|
||||||
CreatedAt: time.Now().UTC(),
|
|
||||||
}
|
|
||||||
|
|
||||||
m.users[user.ID] = user
|
|
||||||
m.usersByUsername[user.Username] = user.ID
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Memory) GetUser(id uuid.UUID) (model.User, error) {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
|
|
||||||
user, exists := m.users[id]
|
|
||||||
if !exists {
|
|
||||||
return model.User{}, ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Memory) CreateChat(title string, memberIDs []uuid.UUID) (model.Chat, error) {
|
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
|
|
||||||
seen := make(map[uuid.UUID]struct{}, len(memberIDs))
|
|
||||||
for _, id := range memberIDs {
|
|
||||||
if _, dup := seen[id]; dup {
|
|
||||||
return model.Chat{}, ErrDuplicateMember
|
|
||||||
}
|
|
||||||
seen[id] = struct{}{}
|
|
||||||
|
|
||||||
if _, exists := m.users[id]; !exists {
|
|
||||||
return model.Chat{}, ErrUnknownMember
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
chat := model.Chat{
|
|
||||||
ID: uuid.New(),
|
|
||||||
Title: title,
|
|
||||||
MemberIDs: append([]uuid.UUID(nil), memberIDs...),
|
|
||||||
CreatedAt: time.Now().UTC(),
|
|
||||||
}
|
|
||||||
|
|
||||||
m.chats[chat.ID] = chat
|
|
||||||
|
|
||||||
return chat, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Memory) GetChat(id uuid.UUID, userID uuid.UUID) (model.Chat, error) {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
|
|
||||||
chat, exists := m.chats[id]
|
|
||||||
if !exists || !slices.Contains(chat.MemberIDs, userID) {
|
|
||||||
return model.Chat{}, ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
chat.MemberIDs = append([]uuid.UUID(nil), chat.MemberIDs...)
|
|
||||||
|
|
||||||
return chat, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Memory) ListChats(
|
|
||||||
userID uuid.UUID,
|
|
||||||
afterCreatedAt *time.Time,
|
|
||||||
afterID *uuid.UUID,
|
|
||||||
limit int,
|
|
||||||
) ([]model.Chat, bool, error) {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
|
|
||||||
chats := make([]model.Chat, 0)
|
|
||||||
for _, c := range m.chats {
|
|
||||||
if slices.Contains(c.MemberIDs, userID) {
|
|
||||||
c.MemberIDs = append([]uuid.UUID(nil), c.MemberIDs...)
|
|
||||||
chats = append(chats, c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(chats, func(i, j int) bool {
|
|
||||||
if !chats[i].CreatedAt.Equal(chats[j].CreatedAt) {
|
|
||||||
return chats[i].CreatedAt.Before(chats[j].CreatedAt)
|
|
||||||
}
|
|
||||||
return chats[i].ID.String() < chats[j].ID.String()
|
|
||||||
})
|
|
||||||
|
|
||||||
start := 0
|
|
||||||
if afterCreatedAt != nil && afterID != nil {
|
|
||||||
start = sort.Search(len(chats), func(i int) bool {
|
|
||||||
chat := chats[i]
|
|
||||||
|
|
||||||
return chat.CreatedAt.After(*afterCreatedAt) ||
|
|
||||||
(chat.CreatedAt.Equal(*afterCreatedAt) &&
|
|
||||||
chat.ID.String() > afterID.String())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
end := start + limit
|
|
||||||
hasMore := end < len(chats)
|
|
||||||
if end > len(chats) {
|
|
||||||
end = len(chats)
|
|
||||||
}
|
|
||||||
|
|
||||||
return chats[start:end], hasMore, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Memory) CreateMessage(
|
|
||||||
chatID uuid.UUID,
|
|
||||||
senderID uuid.UUID,
|
|
||||||
text string,
|
|
||||||
) (model.Message, error) {
|
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
|
|
||||||
chat, exists := m.chats[chatID]
|
|
||||||
if !exists || !slices.Contains(chat.MemberIDs, senderID) {
|
|
||||||
return model.Message{}, ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
message := model.Message{
|
|
||||||
ID: uuid.New(),
|
|
||||||
ChatID: chatID,
|
|
||||||
SenderID: senderID,
|
|
||||||
Text: text,
|
|
||||||
CreatedAt: time.Now().UTC(),
|
|
||||||
}
|
|
||||||
|
|
||||||
m.messages[message.ID] = message
|
|
||||||
m.chatMessages[chatID] = append(m.chatMessages[chatID], message.ID)
|
|
||||||
|
|
||||||
return message, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Memory) ListMessages(
|
|
||||||
chatID uuid.UUID,
|
|
||||||
userID uuid.UUID,
|
|
||||||
afterCreatedAt *time.Time,
|
|
||||||
afterID *uuid.UUID,
|
|
||||||
limit int,
|
|
||||||
) ([]model.Message, bool, error) {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
|
|
||||||
chat, exists := m.chats[chatID]
|
|
||||||
if !exists || !slices.Contains(chat.MemberIDs, userID) {
|
|
||||||
return nil, false, ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
ids := m.chatMessages[chatID]
|
|
||||||
messages := make([]model.Message, 0, len(ids))
|
|
||||||
|
|
||||||
for _, id := range ids {
|
|
||||||
messages = append(messages, m.messages[id])
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(messages, func(i, j int) bool {
|
|
||||||
if !messages[i].CreatedAt.Equal(messages[j].CreatedAt) {
|
|
||||||
return messages[i].CreatedAt.Before(messages[j].CreatedAt)
|
|
||||||
}
|
|
||||||
return messages[i].ID.String() < messages[j].ID.String()
|
|
||||||
})
|
|
||||||
|
|
||||||
start := 0
|
|
||||||
if afterCreatedAt != nil && afterID != nil {
|
|
||||||
start = sort.Search(len(messages), func(i int) bool {
|
|
||||||
msg := messages[i]
|
|
||||||
|
|
||||||
return msg.CreatedAt.After(*afterCreatedAt) ||
|
|
||||||
(msg.CreatedAt.Equal(*afterCreatedAt) &&
|
|
||||||
msg.ID.String() > afterID.String())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
end := start + limit
|
|
||||||
hasMore := end < len(messages)
|
|
||||||
if end > len(messages) {
|
|
||||||
end = len(messages)
|
|
||||||
}
|
|
||||||
|
|
||||||
return messages[start:end], hasMore, nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user