93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package model
|
|
|
|
import (
|
|
"context"
|
|
|
|
"spark-server/internal/module/moment/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
)
|
|
|
|
func (m *MemoryRepo) ListAllByAuthor(_ context.Context, authorID int64) ([]*entity.Post, error) {
|
|
if authorID <= 0 {
|
|
return nil, errs.ErrInvalidArgument
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
out := make([]*entity.Post, 0)
|
|
for _, row := range m.posts {
|
|
if row.UserID == authorID {
|
|
out = append(out, clonePost(row))
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *MemoryRepo) PurgeUser(_ context.Context, userID int64) error {
|
|
if userID <= 0 {
|
|
return errs.ErrInvalidArgument
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
owned := map[int64]bool{}
|
|
for id, post := range m.posts {
|
|
if post.UserID == userID {
|
|
owned[id] = true
|
|
}
|
|
}
|
|
for _, like := range m.likes {
|
|
if like.UserID != userID || owned[like.PostID] {
|
|
continue
|
|
}
|
|
if post, ok := m.posts[like.PostID]; ok && post.LikeCount > 0 {
|
|
post.LikeCount--
|
|
}
|
|
}
|
|
for _, c := range m.comments {
|
|
if c.UserID != userID || owned[c.PostID] || c.Status == entity.StatusDeleted {
|
|
continue
|
|
}
|
|
if post, ok := m.posts[c.PostID]; ok && post.CommentCount > 0 {
|
|
post.CommentCount--
|
|
}
|
|
}
|
|
for key, like := range m.likes {
|
|
if like.UserID == userID || owned[like.PostID] {
|
|
delete(m.likes, key)
|
|
}
|
|
}
|
|
for id, c := range m.comments {
|
|
if c.UserID == userID || owned[c.PostID] {
|
|
delete(m.comments, id)
|
|
}
|
|
}
|
|
for id, n := range m.notices {
|
|
if n.ToUserID == userID || n.FromUserID == userID || owned[n.PostID] {
|
|
delete(m.notices, id)
|
|
}
|
|
}
|
|
for key, h := range m.hides {
|
|
if h.UserID == userID || h.PeerID == userID {
|
|
delete(m.hides, key)
|
|
}
|
|
}
|
|
for postID, peers := range m.audience {
|
|
if owned[postID] {
|
|
delete(m.audience, postID)
|
|
continue
|
|
}
|
|
kept := peers[:0]
|
|
for _, peer := range peers {
|
|
if peer != userID {
|
|
kept = append(kept, peer)
|
|
}
|
|
}
|
|
m.audience[postID] = kept
|
|
}
|
|
delete(m.settings, userID)
|
|
for id := range owned {
|
|
delete(m.posts, id)
|
|
delete(m.audience, id)
|
|
}
|
|
return nil
|
|
}
|