Recycle realm post when realm was deleted

This commit is contained in:
LittleSheep 2025-03-10 21:58:31 +08:00
parent cc47ff4583
commit 10bf4fdf77
2 changed files with 49 additions and 3 deletions

View File

@ -4,9 +4,12 @@ import (
"context"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log"
)
func (v *App) BroadcastEvent(ctx context.Context, in *proto.EventInfo) (*proto.EventResponse, error) {
@ -33,6 +36,22 @@ func (v *App) BroadcastEvent(ctx context.Context, in *proto.EventInfo) (*proto.E
}
}
tx.Commit()
case "realm":
var data struct {
ID int `json:"id"`
}
if err := jsoniter.Unmarshal(in.GetData(), &data); err != nil {
break
}
var posts []models.Post
if err := database.C.Where("realm_id = ?", data.ID).
Select("Body").Select("ID").
Find(&posts).Error; err != nil {
break
}
if err := services.DeletePostInBatch(posts); err != nil {
log.Error().Err(err).Msg("An error occurred when deleting post...")
}
}
}

View File

@ -719,9 +719,7 @@ func DeletePost(item models.Post) error {
pc := pproto.NewAttachmentServiceClient(conn)
_, err = pc.DeleteAttachment(context.Background(), &pproto.DeleteAttachmentRequest{
Rid: lo.Map(val, func(item string, _ int) string {
return item
}),
Rid: lo.Uniq(val),
UserId: lo.ToPtr(uint64(*item.Publisher.AccountID)),
})
if err != nil {
@ -732,6 +730,35 @@ func DeletePost(item models.Post) error {
return nil
}
func DeletePostInBatch(items []models.Post) error {
if err := database.C.Delete(&items).Error; err != nil {
return err
}
var attachments []string
for _, item := range items {
if val, ok := item.Body["attachments"].([]string); ok && len(val) > 0 {
attachments = append(attachments, val...)
}
}
conn, err := gap.Nx.GetClientGrpcConn("uc")
if err != nil {
return nil
}
pc := pproto.NewAttachmentServiceClient(conn)
_, err = pc.DeleteAttachment(context.Background(), &pproto.DeleteAttachmentRequest{
Rid: lo.Uniq(attachments),
// FIXME Some issues here, if the user linked others uploaded attachment, it will be deleted
})
if err != nil {
log.Error().Err(err).Msg("An error occurred when deleting post attachment...")
}
return nil
}
func ReactPost(user authm.Account, reaction models.Reaction) (bool, models.Reaction, error) {
var op models.Post
if err := database.C.