187 lines
4.3 KiB
Go
187 lines
4.3 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"spark-server/internal/module/moment/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
func (m *MemoryRepo) CreatePost(_ context.Context, post *entity.Post, audience []int64) error {
|
|
if post == nil || post.ID <= 0 || post.UserID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.posts[post.ID]; ok {
|
|
return errs.ErrDuplicate
|
|
}
|
|
m.posts[post.ID] = clonePost(post)
|
|
if len(audience) > 0 {
|
|
m.audience[post.ID] = append([]int64(nil), audience...)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MemoryRepo) SoftDeletePost(_ context.Context, userID, postID int64) (*entity.Post, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
row, ok := m.posts[postID]
|
|
if !ok || row.UserID != userID || row.Status == entity.StatusDeleted {
|
|
return nil, errs.ErrNotFound
|
|
}
|
|
row.Status = entity.StatusDeleted
|
|
return clonePost(row), nil
|
|
}
|
|
|
|
func (m *MemoryRepo) AddLike(_ context.Context, like *entity.Like) (bool, error) {
|
|
if like == nil || like.PostID <= 0 || like.UserID <= 0 {
|
|
return false, errs.ErrInvalidArgument
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
post, ok := m.posts[like.PostID]
|
|
if !ok {
|
|
return false, errs.ErrNotFound
|
|
}
|
|
key := likeKey{PostID: like.PostID, UserID: like.UserID}
|
|
if _, ok := m.likes[key]; ok {
|
|
return false, nil
|
|
}
|
|
cp := *like
|
|
if cp.CreatedAtMS == 0 {
|
|
cp.CreatedAtMS = time.Now().UnixMilli()
|
|
}
|
|
m.likes[key] = &cp
|
|
post.LikeCount++
|
|
return true, nil
|
|
}
|
|
|
|
func (m *MemoryRepo) RemoveLike(_ context.Context, postID, userID int64) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
key := likeKey{PostID: postID, UserID: userID}
|
|
if _, ok := m.likes[key]; !ok {
|
|
return errs.ErrNotFound
|
|
}
|
|
delete(m.likes, key)
|
|
if post, ok := m.posts[postID]; ok && post.LikeCount > 0 {
|
|
post.LikeCount--
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MemoryRepo) AddComment(_ context.Context, c *entity.Comment) error {
|
|
if c == nil || c.ID <= 0 || c.PostID <= 0 || c.UserID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
post, ok := m.posts[c.PostID]
|
|
if !ok {
|
|
return errs.ErrNotFound
|
|
}
|
|
if _, ok := m.comments[c.ID]; ok {
|
|
return errs.ErrDuplicate
|
|
}
|
|
m.comments[c.ID] = cloneComment(c)
|
|
post.CommentCount++
|
|
return nil
|
|
}
|
|
|
|
func (m *MemoryRepo) SoftDeleteComment(_ context.Context, id int64) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
row, ok := m.comments[id]
|
|
if !ok || row.Status == entity.StatusDeleted {
|
|
return errs.ErrNotFound
|
|
}
|
|
row.Status = entity.StatusDeleted
|
|
if post, ok := m.posts[row.PostID]; ok && post.CommentCount > 0 {
|
|
post.CommentCount--
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MemoryRepo) CreateNotices(_ context.Context, rows []*entity.Notice) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for _, n := range rows {
|
|
if n == nil || n.ID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
if _, ok := m.notices[n.ID]; ok {
|
|
return errs.ErrDuplicate
|
|
}
|
|
cp := *n
|
|
m.notices[n.ID] = &cp
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MemoryRepo) MarkNoticesRead(_ context.Context, userID, cursor int64) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
now := time.Now().UnixMilli()
|
|
for _, n := range m.notices {
|
|
if n.ToUserID != userID || n.ReadAtMS > 0 {
|
|
continue
|
|
}
|
|
if cursor > 0 && n.ID > cursor {
|
|
continue
|
|
}
|
|
n.ReadAtMS = now
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MemoryRepo) UpsertSettings(_ context.Context, s *entity.Settings) error {
|
|
if s == nil || s.UserID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
cp := *s
|
|
if cp.UpdatedAtMS == 0 {
|
|
cp.UpdatedAtMS = time.Now().UnixMilli()
|
|
}
|
|
m.settings[s.UserID] = &cp
|
|
return nil
|
|
}
|
|
|
|
func (m *MemoryRepo) AdvanceRead(ctx context.Context, userID, postID int64) error {
|
|
cur, err := m.GetSettings(ctx, userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if cur.LastReadPostID >= postID {
|
|
return nil
|
|
}
|
|
cur.LastReadPostID = postID
|
|
cur.UpdatedAtMS = time.Now().UnixMilli()
|
|
return m.UpsertSettings(ctx, cur)
|
|
}
|
|
|
|
func (m *MemoryRepo) UpsertHide(_ context.Context, h *entity.Hide) error {
|
|
if h == nil || h.UserID <= 0 || h.PeerID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
cp := *h
|
|
m.hides[hideKey{UserID: h.UserID, PeerID: h.PeerID}] = &cp
|
|
return nil
|
|
}
|
|
|
|
func (m *MemoryRepo) DeleteHide(_ context.Context, userID, peerID int64) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
key := hideKey{UserID: userID, PeerID: peerID}
|
|
if _, ok := m.hides[key]; !ok {
|
|
return errs.ErrNotFound
|
|
}
|
|
delete(m.hides, key)
|
|
return nil
|
|
}
|