47 lines
868 B
Go
47 lines
868 B
Go
|
|
package posts
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"lmika.dev/lmika/weiro/models"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
deleteDebounce = 2 * time.Second
|
||
|
|
)
|
||
|
|
|
||
|
|
func (s *Service) DeletePost(ctx context.Context, pid int64, hardDelete bool) error {
|
||
|
|
site, ok := models.GetSite(ctx)
|
||
|
|
if !ok {
|
||
|
|
return models.SiteRequiredError
|
||
|
|
}
|
||
|
|
|
||
|
|
post, err := s.db.SelectPost(ctx, pid)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
} else if post.SiteID != site.ID {
|
||
|
|
return models.NotFoundError
|
||
|
|
}
|
||
|
|
|
||
|
|
if hardDelete && post.DeletedAt.Unix() > 0 {
|
||
|
|
delta := time.Now().Sub(post.DeletedAt)
|
||
|
|
if delta < deleteDebounce {
|
||
|
|
return models.DeleteDebounceError
|
||
|
|
}
|
||
|
|
|
||
|
|
return s.db.HardDeletePost(ctx, post.ID)
|
||
|
|
}
|
||
|
|
|
||
|
|
return s.db.SoftDeletePost(ctx, post.ID)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Service) RestorePost(ctx context.Context, pid int64) error {
|
||
|
|
post, err := s.db.SelectPost(ctx, pid)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
return s.db.RestorePost(ctx, post.ID)
|
||
|
|
}
|