🐛 Fix publisher info did not marked used

This commit is contained in:
LittleSheep 2025-03-10 23:58:25 +08:00
parent f06bc2d382
commit 76c78e0c01
2 changed files with 49 additions and 2 deletions

View File

@ -191,6 +191,7 @@ func editPublisher(c *fiber.Ctx) error {
return err
}
og := publisher
publisher.Name = data.Name
publisher.Nick = data.Nick
publisher.Description = data.Description
@ -200,7 +201,7 @@ func editPublisher(c *fiber.Ctx) error {
publisher.AccountID = data.AccountID
}
if publisher, err = services.EditPublisher(user, publisher); err != nil {
if publisher, err = services.EditPublisher(user, publisher, og); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -2,8 +2,12 @@ package services
import (
"fmt"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/paperclip/pkg/filekit"
"git.solsynth.dev/hypernet/paperclip/pkg/proto"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
)
@ -33,13 +37,23 @@ func CreatePersonalPublisher(user authm.Account, name, nick, desc, avatar, banne
Banner: banner,
AccountID: &user.ID,
}
var attachments []string
if user.Avatar != nil && len(publisher.Avatar) == 0 {
attachments = append(attachments, *user.Avatar)
publisher.Avatar = *user.Avatar
}
if user.Banner != nil && len(publisher.Banner) == 0 {
attachments = append(attachments, *user.Banner)
publisher.Banner = *user.Banner
}
if len(attachments) > 0 {
filekit.CountAttachmentUsage(gap.Nx, &proto.UpdateUsageRequest{
Rid: attachments,
Delta: 1,
})
}
if err := database.C.Create(&publisher).Error; err != nil {
return publisher, err
}
@ -57,26 +71,58 @@ func CreateOrganizationPublisher(user authm.Account, realm authm.Realm, name, ni
RealmID: &realm.ID,
AccountID: &user.ID,
}
var attachments []string
if realm.Avatar != nil && len(publisher.Avatar) == 0 {
attachments = append(attachments, *realm.Avatar)
publisher.Avatar = *realm.Avatar
}
if realm.Banner != nil && len(publisher.Banner) == 0 {
attachments = append(attachments, *realm.Banner)
publisher.Banner = *realm.Banner
}
if len(attachments) > 0 {
filekit.CountAttachmentUsage(gap.Nx, &proto.UpdateUsageRequest{
Rid: attachments,
Delta: 1,
})
}
if err := database.C.Create(&publisher).Error; err != nil {
return publisher, err
}
return publisher, nil
}
func EditPublisher(user authm.Account, publisher models.Publisher) (models.Publisher, error) {
func EditPublisher(user authm.Account, publisher, og models.Publisher) (models.Publisher, error) {
if publisher.Type == models.PublisherTypePersonal {
if *publisher.AccountID != user.ID {
return publisher, fmt.Errorf("you cannot transfer personal publisher")
}
}
var minusAttachments, plusAttachments []string
if publisher.Avatar != og.Avatar {
minusAttachments = append(minusAttachments, og.Avatar)
plusAttachments = append(plusAttachments, publisher.Avatar)
}
if publisher.Banner != og.Banner {
minusAttachments = append(minusAttachments, og.Banner)
plusAttachments = append(plusAttachments, publisher.Banner)
}
if len(minusAttachments) > 0 {
filekit.CountAttachmentUsage(gap.Nx, &proto.UpdateUsageRequest{
Rid: minusAttachments,
Delta: -1,
})
}
if len(plusAttachments) > 0 {
filekit.CountAttachmentUsage(gap.Nx, &proto.UpdateUsageRequest{
Rid: plusAttachments,
Delta: 1,
})
}
err := database.C.Save(&publisher).Error
return publisher, err
}