Compare commits

...

2 Commits

Author SHA1 Message Date
78e554577e 🐛 Prevent user from creating boost on same destination 2024-12-29 12:24:56 +08:00
a58f44d50e 🐛 Only apply active boost 2024-12-29 12:23:41 +08:00
2 changed files with 17 additions and 3 deletions

View File

@ -44,6 +44,16 @@ func ListBoostByAttachment(attachmentId uint) ([]models.AttachmentBoost, error)
return boosts, nil
}
func ListBoostByAttachmentWithStatus(attachmentId uint, status int) ([]models.AttachmentBoost, error) {
var boosts []models.AttachmentBoost
if err := database.C.
Where("attachment_id = ? AND status = ?", attachmentId, status).
Find(&boosts).Error; err != nil {
return boosts, err
}
return boosts, nil
}
func GetBoostByID(id uint) (models.AttachmentBoost, error) {
var boost models.AttachmentBoost
if err := database.C.
@ -56,7 +66,12 @@ func GetBoostByID(id uint) (models.AttachmentBoost, error) {
}
func CreateBoost(user *sec.UserInfo, source models.Attachment, destination int) (models.AttachmentBoost, error) {
boost := models.AttachmentBoost{
var boost models.AttachmentBoost
if err := database.C.Where("attachment_id = ? AND destination = ?", source.ID, destination).First(&boost); err == nil {
return boost, fmt.Errorf("boost already exists")
}
boost = models.AttachmentBoost{
Status: models.BoostStatusPending,
Destination: destination,
AttachmentID: source.ID,

View File

@ -52,13 +52,12 @@ func OpenAttachmentByRID(rid string, region ...string) (url string, mimetype str
Preload("Pool").
Preload("Thumbnail").
Preload("Compressed").
Preload("Boosts").
First(&attachment).Error; err != nil {
return
}
var boosts []models.AttachmentBoost
boosts, err = ListBoostByAttachment(attachment.ID)
boosts, err = ListBoostByAttachmentWithStatus(attachment.ID, models.BoostStatusActive)
if err != nil {
return
}