package safew import ( "context" "sync" "time" "aiaa-notification-service/internal/adapter" ) type ChatStore interface { MergeSafewChats(ctx context.Context, token string, chats []adapter.SafewChat) error ListSafewChats(ctx context.Context, token string) ([]adapter.SafewChat, error) GetSafewOffset(ctx context.Context, token string) (int64, error) SetSafewOffset(ctx context.Context, token string, offset int64) error TrySafewPollLock(ctx context.Context, token string, ttl time.Duration) (bool, error) UnlockSafewPoll(ctx context.Context, token string) error } type MemStore struct { mu sync.Mutex chats map[string]map[string]adapter.SafewChat offset map[string]int64 locks map[string]bool } func NewMemStore() *MemStore { return &MemStore{ chats: map[string]map[string]adapter.SafewChat{}, offset: map[string]int64{}, locks: map[string]bool{}, } } func (m *MemStore) MergeSafewChats(_ context.Context, token string, chats []adapter.SafewChat) error { m.mu.Lock() defer m.mu.Unlock() if m.chats[token] == nil { m.chats[token] = map[string]adapter.SafewChat{} } for _, c := range chats { m.chats[token][c.ID] = c } return nil } func (m *MemStore) ListSafewChats(_ context.Context, token string) ([]adapter.SafewChat, error) { m.mu.Lock() defer m.mu.Unlock() var out []adapter.SafewChat for _, c := range m.chats[token] { out = append(out, c) } if out == nil { out = []adapter.SafewChat{} } return out, nil } func (m *MemStore) GetSafewOffset(_ context.Context, token string) (int64, error) { m.mu.Lock() defer m.mu.Unlock() return m.offset[token], nil } func (m *MemStore) SetSafewOffset(_ context.Context, token string, offset int64) error { m.mu.Lock() defer m.mu.Unlock() m.offset[token] = offset return nil } func (m *MemStore) TrySafewPollLock(_ context.Context, token string, _ time.Duration) (bool, error) { m.mu.Lock() defer m.mu.Unlock() if m.locks[token] { return false, nil } m.locks[token] = true return true, nil } func (m *MemStore) UnlockSafewPoll(_ context.Context, token string) error { m.mu.Lock() defer m.mu.Unlock() delete(m.locks, token) return nil }