diff --git a/pkg/internal/grpc/services.go b/pkg/internal/grpc/services.go index 18cef7f..c1218cc 100644 --- a/pkg/internal/grpc/services.go +++ b/pkg/internal/grpc/services.go @@ -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...") + } } } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 80a119a..ee848c4 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -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.