MCPcopy Create free account
hub / github.com/gogs/gogs / DeleteCommentByID

Function DeleteCommentByID

internal/database/comment.go:505–551  ·  view source on GitHub ↗

DeleteCommentByID deletes the comment by given ID.

(doer *User, id int64)

Source from the content-addressed store, hash-verified

503
504// DeleteCommentByID deletes the comment by given ID.
505func DeleteCommentByID(doer *User, id int64) error {
506 comment, err := GetCommentByID(id)
507 if err != nil {
508 if IsErrCommentNotExist(err) {
509 return nil
510 }
511 return err
512 }
513
514 sess := x.NewSession()
515 defer sess.Close()
516 if err = sess.Begin(); err != nil {
517 return err
518 }
519
520 if _, err = sess.ID(comment.ID).Delete(new(Comment)); err != nil {
521 return err
522 }
523
524 if comment.Type == CommentTypeComment {
525 if _, err = sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil {
526 return err
527 }
528 }
529
530 if err = sess.Commit(); err != nil {
531 return errors.Newf("commit: %v", err)
532 }
533
534 _, err = DeleteAttachmentsByComment(comment.ID, true)
535 if err != nil {
536 log.Error("Failed to delete attachments by comment[%d]: %v", comment.ID, err)
537 }
538
539 if err = comment.Issue.LoadAttributes(); err != nil {
540 log.Error("Issue.LoadAttributes [issue_id: %d]: %v", comment.IssueID, err)
541 } else if err = PrepareWebhooks(comment.Issue.Repo, HookEventTypeIssueComment, &api.IssueCommentPayload{
542 Action: api.HOOK_ISSUE_COMMENT_DELETED,
543 Issue: comment.Issue.APIFormat(),
544 Comment: comment.APIFormat(),
545 Repository: comment.Issue.Repo.APIFormatLegacy(nil),
546 Sender: doer.APIFormat(),
547 }); err != nil {
548 log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
549 }
550 return nil
551}

Callers 2

DeleteCommentFunction · 0.92
DeleteIssueCommentFunction · 0.92

Calls 11

GetCommentByIDFunction · 0.85
IsErrCommentNotExistFunction · 0.85
PrepareWebhooksFunction · 0.85
DeleteMethod · 0.80
ExecMethod · 0.80
APIFormatLegacyMethod · 0.80
IDMethod · 0.65
ErrorMethod · 0.45
LoadAttributesMethod · 0.45
APIFormatMethod · 0.45

Tested by

no test coverage detected