✨ Collapse post according to flag, view ratio
This commit is contained in:
parent
269e79ca58
commit
3a4948d590
@ -52,6 +52,7 @@ type Post struct {
|
||||
PinnedAt *time.Time `json:"pinned_at"`
|
||||
LockedAt *time.Time `json:"locked_at"`
|
||||
|
||||
IsCollapsed bool `json:"is_collapsed"`
|
||||
IsDraft bool `json:"is_draft"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
PublishedUntil *time.Time `json:"published_until"`
|
||||
|
@ -19,5 +19,21 @@ func NewFlag(post models.Post, account uint) (models.PostFlag, error) {
|
||||
if err := database.C.Save(&flag).Error; err != nil {
|
||||
return flag, err
|
||||
}
|
||||
if err := FlagCalculateCollapseStatus(post); err != nil {
|
||||
return flag, err
|
||||
}
|
||||
return flag, nil
|
||||
}
|
||||
|
||||
func FlagCalculateCollapseStatus(post models.Post) error {
|
||||
collapseLimit := 0.5
|
||||
|
||||
var flagCount int64
|
||||
if err := database.C.Model(&models.PostFlag{}).Where("post_id = ?", post.ID).Count(&flagCount).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if float64(flagCount)/float64(post.TotalViews) >= collapseLimit {
|
||||
return database.C.Model(&post).Update("is_collapsed", true).Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user