🐛 Fix realm did not update attachment meta

This commit is contained in:
LittleSheep 2025-03-11 00:04:10 +08:00
parent 651a738646
commit 35e5eadb05
2 changed files with 56 additions and 2 deletions

View File

@ -8,6 +8,8 @@ import (
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"git.solsynth.dev/hypernet/paperclip/pkg/filekit"
pproto "git.solsynth.dev/hypernet/paperclip/pkg/proto"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"git.solsynth.dev/hypernet/passport/pkg/internal/gap"
@ -75,6 +77,20 @@ func NewRealm(realm models.Realm, user models.Account) (models.Realm, error) {
{AccountID: user.ID, PowerLevel: 100},
}
var attachments []string
if realm.Avatar != nil && len(*realm.Avatar) > 0 {
attachments = append(attachments, *realm.Avatar)
}
if realm.Banner != nil && len(*realm.Banner) > 0 {
attachments = append(attachments, *realm.Banner)
}
if len(attachments) > 0 {
filekit.CountAttachmentUsage(gap.Nx, &pproto.UpdateUsageRequest{
Rid: attachments,
Delta: 1,
})
}
err := database.C.Save(&realm).Error
return realm, err
}
@ -176,8 +192,31 @@ func RemoveRealmMember(user models.Account, affected models.RealmMember, target
return nil
}
func EditRealm(realm models.Realm) (models.Realm, error) {
func EditRealm(realm, og models.Realm) (models.Realm, error) {
err := database.C.Save(&realm).Error
if err == nil {
var minusAttachments, plusAttachments []string
if realm.Avatar != og.Avatar && realm.Avatar != nil {
minusAttachments = append(minusAttachments, *og.Avatar)
plusAttachments = append(plusAttachments, *realm.Avatar)
}
if realm.Banner != og.Banner && realm.Banner != nil {
minusAttachments = append(minusAttachments, *og.Banner)
plusAttachments = append(plusAttachments, *realm.Banner)
}
if len(minusAttachments) > 0 {
filekit.CountAttachmentUsage(gap.Nx, &pproto.UpdateUsageRequest{
Rid: minusAttachments,
Delta: -1,
})
}
if len(plusAttachments) > 0 {
filekit.CountAttachmentUsage(gap.Nx, &pproto.UpdateUsageRequest{
Rid: plusAttachments,
Delta: 1,
})
}
}
return realm, err
}
@ -194,6 +233,20 @@ func DeleteRealm(realm models.Realm) error {
if err := tx.Commit().Error; err != nil {
return err
} else {
var attachments []string
if realm.Avatar != nil && len(*realm.Avatar) > 0 {
attachments = append(attachments, *realm.Avatar)
}
if realm.Banner != nil && len(*realm.Banner) > 0 {
attachments = append(attachments, *realm.Banner)
}
if len(attachments) > 0 {
filekit.CountAttachmentUsage(gap.Nx, &pproto.UpdateUsageRequest{
Rid: attachments,
Delta: -1,
})
}
conn := gap.Nx.GetNexusGrpcConn()
_, _ = proto.NewDirectoryServiceClient(conn).BroadcastEvent(context.Background(), &proto.EventInfo{
Event: "deletion",

View File

@ -124,6 +124,7 @@ func editRealm(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
og := realm
realm.Alias = data.Alias
realm.Name = data.Name
realm.Description = data.Description
@ -133,7 +134,7 @@ func editRealm(c *fiber.Ctx) error {
realm.IsPublic = data.IsPublic
realm.IsCommunity = data.IsCommunity
realm, err := services.EditRealm(realm)
realm, err := services.EditRealm(realm, og)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {