@@ -15,6 +15,8 @@ import (
1515 user_model "code.gitea.io/gitea/models/user"
1616 "code.gitea.io/gitea/modules/gitrepo"
1717 "code.gitea.io/gitea/modules/json"
18+ "code.gitea.io/gitea/modules/log"
19+ "code.gitea.io/gitea/modules/storage"
1820 "code.gitea.io/gitea/modules/timeutil"
1921 git_service "code.gitea.io/gitea/services/git"
2022 notify_service "code.gitea.io/gitea/services/notify"
@@ -131,17 +133,40 @@ func UpdateComment(ctx context.Context, c *issues_model.Comment, contentVersion
131133}
132134
133135// DeleteComment deletes the comment
134- func DeleteComment (ctx context.Context , doer * user_model.User , comment * issues_model.Comment ) error {
135- err := db .WithTx (ctx , func (ctx context.Context ) error {
136- return issues_model .DeleteComment (ctx , comment )
136+ func DeleteComment (ctx context.Context , doer * user_model.User , comment * issues_model.Comment ) (* issues_model.Comment , error ) {
137+ deletedReviewComment , err := db .WithTx2 (ctx , func (ctx context.Context ) (* issues_model.Comment , error ) {
138+ if err := comment .LoadAttachments (ctx ); err != nil {
139+ return nil , err
140+ }
141+
142+ deletedReviewComment , err := issues_model .DeleteComment (ctx , comment )
143+ if err != nil {
144+ return nil , err
145+ }
146+
147+ // delete comment attachments
148+ if _ , err := repo_model .DeleteAttachments (ctx , comment .Attachments , true ); err != nil {
149+ return nil , fmt .Errorf ("delete attachments: %w" , err )
150+ }
151+
152+ for _ , attachment := range comment .Attachments {
153+ if err := storage .Attachments .Delete (repo_model .AttachmentRelativePath (attachment .UUID )); err != nil {
154+ // Even delete files failed, but the attachments has been removed from database, so we
155+ // should not return error but only record the error on logs.
156+ // users have to delete this attachments manually or we should have a
157+ // synchronize between database attachment table and attachment storage
158+ log .Error ("delete attachment[uuid: %s] failed: %v" , attachment .UUID , err )
159+ }
160+ }
161+ return deletedReviewComment , nil
137162 })
138163 if err != nil {
139- return err
164+ return nil , err
140165 }
141166
142167 notify_service .DeleteComment (ctx , doer , comment )
143168
144- return nil
169+ return deletedReviewComment , nil
145170}
146171
147172// LoadCommentPushCommits Load push commits
0 commit comments