Have got soft and hard deleting
This commit is contained in:
parent
aef3bb6a1e
commit
3ea5823ca0
27 changed files with 588 additions and 55 deletions
46
services/posts/delete.go
Normal file
46
services/posts/delete.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
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)
|
||||
}
|
||||
|
|
@ -6,13 +6,13 @@ import (
|
|||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
func (s *Service) ListPosts(ctx context.Context) ([]*models.Post, error) {
|
||||
func (s *Service) ListPosts(ctx context.Context, showDeleted bool) ([]*models.Post, error) {
|
||||
site, ok := models.GetSite(ctx)
|
||||
if !ok {
|
||||
return nil, models.SiteRequiredError
|
||||
}
|
||||
|
||||
posts, err := s.db.SelectPostsOfSite(ctx, site.ID)
|
||||
posts, err := s.db.SelectPostsOfSite(ctx, site.ID, showDeleted)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ func (p *Publisher) Publish(ctx context.Context, site models.Site) error {
|
|||
}
|
||||
|
||||
// Fetch all content of site
|
||||
posts, err := p.db.SelectPostsOfSite(ctx, site.ID)
|
||||
posts, err := p.db.SelectPostsOfSite(ctx, site.ID, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue