spark-server/internal/module/message/model/memory.go
zhr ae9a71034d feat: implement Spark IM V3 modular monolith
Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:52:11 +08:00

65 lines
1.3 KiB
Go

package model
import (
"context"
"sync"
"spark-server/internal/module/message/entity"
"spark-server/internal/pkg/errs"
)
// MemoryRepo is an in-process message store for tests and empty-DSN bring-up.
type MemoryRepo struct {
mu sync.Mutex
byID map[int64]*entity.Message
}
func NewMemoryRepo() *MemoryRepo {
return &MemoryRepo{byID: make(map[int64]*entity.Message)}
}
func (m *MemoryRepo) Create(_ context.Context, message *entity.Message) error {
if message == nil || message.ID <= 0 {
return errs.ErrInvalidArgument
}
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.byID[message.ID]; ok {
return errs.ErrDuplicate
}
cp := *message
if cp.Content != nil {
cp.Content = append([]byte(nil), message.Content...)
}
m.byID[message.ID] = &cp
return nil
}
func (m *MemoryRepo) GetByID(_ context.Context, id int64) (*entity.Message, error) {
m.mu.Lock()
defer m.mu.Unlock()
row, ok := m.byID[id]
if !ok {
return nil, errs.ErrNotFound
}
cp := *row
if cp.Content != nil {
cp.Content = append([]byte(nil), row.Content...)
}
return &cp, nil
}
func (m *MemoryRepo) All() []*entity.Message {
m.mu.Lock()
defer m.mu.Unlock()
out := make([]*entity.Message, 0, len(m.byID))
for _, row := range m.byID {
cp := *row
if cp.Content != nil {
cp.Content = append([]byte(nil), row.Content...)
}
out = append(out, &cp)
}
return out
}