From 8f97a2d0b166e96b03b27ef92f3beaffd6c72d89 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 16 Jul 2025 19:45:30 -0700 Subject: [PATCH] improvements --- models/repo/attachment.go | 13 +++++++++++++ services/attachment/attachment.go | 11 +---------- services/issue/comments.go | 3 +-- services/user/delete.go | 8 +------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/models/repo/attachment.go b/models/repo/attachment.go index cb70bcc52f1..d0690a1319a 100644 --- a/models/repo/attachment.go +++ b/models/repo/attachment.go @@ -185,6 +185,19 @@ func UpdateAttachment(ctx context.Context, atta *Attachment) error { return err } +func DeleteAttachments(ctx context.Context, attachments []*Attachment) (int64, error) { + if len(attachments) == 0 { + return 0, nil + } + + ids := make([]int64, 0, len(attachments)) + for _, a := range attachments { + ids = append(ids, a.ID) + } + + return db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(attachments[0]) +} + // DeleteAttachmentsByRelease deletes all attachments associated with the given release. func DeleteAttachmentsByRelease(ctx context.Context, releaseID int64) error { _, err := db.GetEngine(ctx).Where("release_id = ?", releaseID).Delete(&Attachment{}) diff --git a/services/attachment/attachment.go b/services/attachment/attachment.go index f55ec1ddb6e..d430819357c 100644 --- a/services/attachment/attachment.go +++ b/services/attachment/attachment.go @@ -71,16 +71,7 @@ func DeleteAttachment(ctx context.Context, a *repo_model.Attachment) error { // DeleteAttachments deletes the given attachments and optionally the associated files. func DeleteAttachments(ctx context.Context, attachments []*repo_model.Attachment) (int, error) { - if len(attachments) == 0 { - return 0, nil - } - - ids := make([]int64, 0, len(attachments)) - for _, a := range attachments { - ids = append(ids, a.ID) - } - - cnt, err := db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(attachments[0]) + cnt, err := repo_model.DeleteAttachments(ctx, attachments) if err != nil { return 0, err } diff --git a/services/issue/comments.go b/services/issue/comments.go index 4a6b987ea16..c415c0f91a7 100644 --- a/services/issue/comments.go +++ b/services/issue/comments.go @@ -152,8 +152,7 @@ func deleteComment(ctx context.Context, comment *issues_model.Comment, removeAtt if removeAttachments { // delete comment attachments - _, err := db.GetEngine(ctx).Where("comment_id = ?", comment.ID).NoAutoCondition().Delete(&repo_model.Attachment{}) - if err != nil { + if _, err := repo_model.DeleteAttachments(ctx, comment.Attachments); err != nil { return nil, err } diff --git a/services/user/delete.go b/services/user/delete.go index 89941243fc7..63874f392c3 100644 --- a/services/user/delete.go +++ b/services/user/delete.go @@ -131,13 +131,7 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (cleanup ut return nil, err } - ids := make([]int64, 0, len(comment.Attachments)) - for _, a := range comment.Attachments { - ids = append(ids, a.ID) - } - - _, err := db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(&repo_model.Attachment{}) - if err != nil { + if _, err := repo_model.DeleteAttachments(ctx, comment.Attachments); err != nil { return nil, err }