You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_comment.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/Unknwon/com"
  10. "github.com/go-xorm/builder"
  11. "github.com/go-xorm/xorm"
  12. api "code.gitea.io/sdk/gitea"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/markup"
  15. )
  16. // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
  17. type CommentType int
  18. // define unknown comment type
  19. const (
  20. CommentTypeUnknown CommentType = -1
  21. )
  22. // Enumerate all the comment types
  23. const (
  24. // Plain comment, can be associated with a commit (CommitID > 0) and a line (LineNum > 0)
  25. CommentTypeComment CommentType = iota
  26. CommentTypeReopen
  27. CommentTypeClose
  28. // References.
  29. CommentTypeIssueRef
  30. // Reference from a commit (not part of a pull request)
  31. CommentTypeCommitRef
  32. // Reference from a comment
  33. CommentTypeCommentRef
  34. // Reference from a pull request
  35. CommentTypePullRef
  36. // Labels changed
  37. CommentTypeLabel
  38. // Milestone changed
  39. CommentTypeMilestone
  40. // Assignees changed
  41. CommentTypeAssignees
  42. // Change Title
  43. CommentTypeChangeTitle
  44. // Delete Branch
  45. CommentTypeDeleteBranch
  46. // Start a stopwatch for time tracking
  47. CommentTypeStartTracking
  48. // Stop a stopwatch for time tracking
  49. CommentTypeStopTracking
  50. // Add time manual for time tracking
  51. CommentTypeAddTimeManual
  52. // Cancel a stopwatch for time tracking
  53. CommentTypeCancelTracking
  54. )
  55. // CommentTag defines comment tag type
  56. type CommentTag int
  57. // Enumerate all the comment tag types
  58. const (
  59. CommentTagNone CommentTag = iota
  60. CommentTagPoster
  61. CommentTagWriter
  62. CommentTagOwner
  63. )
  64. // Comment represents a comment in commit and issue page.
  65. type Comment struct {
  66. ID int64 `xorm:"pk autoincr"`
  67. Type CommentType
  68. PosterID int64 `xorm:"INDEX"`
  69. Poster *User `xorm:"-"`
  70. IssueID int64 `xorm:"INDEX"`
  71. LabelID int64
  72. Label *Label `xorm:"-"`
  73. OldMilestoneID int64
  74. MilestoneID int64
  75. OldMilestone *Milestone `xorm:"-"`
  76. Milestone *Milestone `xorm:"-"`
  77. OldAssigneeID int64
  78. AssigneeID int64
  79. Assignee *User `xorm:"-"`
  80. OldAssignee *User `xorm:"-"`
  81. OldTitle string
  82. NewTitle string
  83. CommitID int64
  84. Line int64
  85. Content string `xorm:"TEXT"`
  86. RenderedContent string `xorm:"-"`
  87. Created time.Time `xorm:"-"`
  88. CreatedUnix int64 `xorm:"INDEX created"`
  89. Updated time.Time `xorm:"-"`
  90. UpdatedUnix int64 `xorm:"INDEX updated"`
  91. // Reference issue in commit message
  92. CommitSHA string `xorm:"VARCHAR(40)"`
  93. Attachments []*Attachment `xorm:"-"`
  94. // For view issue page.
  95. ShowTag CommentTag `xorm:"-"`
  96. }
  97. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  98. func (c *Comment) AfterLoad(session *xorm.Session) {
  99. c.Created = time.Unix(c.CreatedUnix, 0).Local()
  100. c.Updated = time.Unix(c.UpdatedUnix, 0).Local()
  101. var err error
  102. c.Attachments, err = getAttachmentsByCommentID(session, c.ID)
  103. if err != nil {
  104. log.Error(3, "getAttachmentsByCommentID[%d]: %v", c.ID, err)
  105. }
  106. c.Poster, err = getUserByID(session, c.PosterID)
  107. if err != nil {
  108. if IsErrUserNotExist(err) {
  109. c.PosterID = -1
  110. c.Poster = NewGhostUser()
  111. } else {
  112. log.Error(3, "getUserByID[%d]: %v", c.ID, err)
  113. }
  114. }
  115. }
  116. // AfterDelete is invoked from XORM after the object is deleted.
  117. func (c *Comment) AfterDelete() {
  118. _, err := DeleteAttachmentsByComment(c.ID, true)
  119. if err != nil {
  120. log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err)
  121. }
  122. }
  123. // HTMLURL formats a URL-string to the issue-comment
  124. func (c *Comment) HTMLURL() string {
  125. issue, err := GetIssueByID(c.IssueID)
  126. if err != nil { // Silently dropping errors :unamused:
  127. log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
  128. return ""
  129. }
  130. return fmt.Sprintf("%s#%s", issue.HTMLURL(), c.HashTag())
  131. }
  132. // IssueURL formats a URL-string to the issue
  133. func (c *Comment) IssueURL() string {
  134. issue, err := GetIssueByID(c.IssueID)
  135. if err != nil { // Silently dropping errors :unamused:
  136. log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
  137. return ""
  138. }
  139. if issue.IsPull {
  140. return ""
  141. }
  142. return issue.HTMLURL()
  143. }
  144. // PRURL formats a URL-string to the pull-request
  145. func (c *Comment) PRURL() string {
  146. issue, err := GetIssueByID(c.IssueID)
  147. if err != nil { // Silently dropping errors :unamused:
  148. log.Error(4, "GetIssueByID(%d): %v", c.IssueID, err)
  149. return ""
  150. }
  151. if !issue.IsPull {
  152. return ""
  153. }
  154. return issue.HTMLURL()
  155. }
  156. // APIFormat converts a Comment to the api.Comment format
  157. func (c *Comment) APIFormat() *api.Comment {
  158. return &api.Comment{
  159. ID: c.ID,
  160. Poster: c.Poster.APIFormat(),
  161. HTMLURL: c.HTMLURL(),
  162. IssueURL: c.IssueURL(),
  163. PRURL: c.PRURL(),
  164. Body: c.Content,
  165. Created: c.Created,
  166. Updated: c.Updated,
  167. }
  168. }
  169. // HashTag returns unique hash tag for comment.
  170. func (c *Comment) HashTag() string {
  171. return "issuecomment-" + com.ToStr(c.ID)
  172. }
  173. // EventTag returns unique event hash tag for comment.
  174. func (c *Comment) EventTag() string {
  175. return "event-" + com.ToStr(c.ID)
  176. }
  177. // LoadLabel if comment.Type is CommentTypeLabel, then load Label
  178. func (c *Comment) LoadLabel() error {
  179. var label Label
  180. has, err := x.ID(c.LabelID).Get(&label)
  181. if err != nil {
  182. return err
  183. } else if has {
  184. c.Label = &label
  185. } else {
  186. // Ignore Label is deleted, but not clear this table
  187. log.Warn("Commit %d cannot load label %d", c.ID, c.LabelID)
  188. }
  189. return nil
  190. }
  191. // LoadMilestone if comment.Type is CommentTypeMilestone, then load milestone
  192. func (c *Comment) LoadMilestone() error {
  193. if c.OldMilestoneID > 0 {
  194. var oldMilestone Milestone
  195. has, err := x.ID(c.OldMilestoneID).Get(&oldMilestone)
  196. if err != nil {
  197. return err
  198. } else if has {
  199. c.OldMilestone = &oldMilestone
  200. }
  201. }
  202. if c.MilestoneID > 0 {
  203. var milestone Milestone
  204. has, err := x.ID(c.MilestoneID).Get(&milestone)
  205. if err != nil {
  206. return err
  207. } else if has {
  208. c.Milestone = &milestone
  209. }
  210. }
  211. return nil
  212. }
  213. // LoadAssignees if comment.Type is CommentTypeAssignees, then load assignees
  214. func (c *Comment) LoadAssignees() error {
  215. var err error
  216. if c.OldAssigneeID > 0 {
  217. c.OldAssignee, err = getUserByID(x, c.OldAssigneeID)
  218. if err != nil {
  219. return err
  220. }
  221. }
  222. if c.AssigneeID > 0 {
  223. c.Assignee, err = getUserByID(x, c.AssigneeID)
  224. if err != nil {
  225. return err
  226. }
  227. }
  228. return nil
  229. }
  230. // MailParticipants sends new comment emails to repository watchers
  231. // and mentioned people.
  232. func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
  233. mentions := markup.FindAllMentions(c.Content)
  234. if err = UpdateIssueMentions(e, c.IssueID, mentions); err != nil {
  235. return fmt.Errorf("UpdateIssueMentions [%d]: %v", c.IssueID, err)
  236. }
  237. switch opType {
  238. case ActionCommentIssue:
  239. issue.Content = c.Content
  240. case ActionCloseIssue:
  241. issue.Content = fmt.Sprintf("Closed #%d", issue.Index)
  242. case ActionReopenIssue:
  243. issue.Content = fmt.Sprintf("Reopened #%d", issue.Index)
  244. }
  245. if err = mailIssueCommentToParticipants(e, issue, c.Poster, c, mentions); err != nil {
  246. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  247. }
  248. return nil
  249. }
  250. func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
  251. var LabelID int64
  252. if opts.Label != nil {
  253. LabelID = opts.Label.ID
  254. }
  255. comment := &Comment{
  256. Type: opts.Type,
  257. PosterID: opts.Doer.ID,
  258. Poster: opts.Doer,
  259. IssueID: opts.Issue.ID,
  260. LabelID: LabelID,
  261. OldMilestoneID: opts.OldMilestoneID,
  262. MilestoneID: opts.MilestoneID,
  263. OldAssigneeID: opts.OldAssigneeID,
  264. AssigneeID: opts.AssigneeID,
  265. CommitID: opts.CommitID,
  266. CommitSHA: opts.CommitSHA,
  267. Line: opts.LineNum,
  268. Content: opts.Content,
  269. OldTitle: opts.OldTitle,
  270. NewTitle: opts.NewTitle,
  271. }
  272. if _, err = e.Insert(comment); err != nil {
  273. return nil, err
  274. }
  275. if err = opts.Repo.getOwner(e); err != nil {
  276. return nil, err
  277. }
  278. // Compose comment action, could be plain comment, close or reopen issue/pull request.
  279. // This object will be used to notify watchers in the end of function.
  280. act := &Action{
  281. ActUserID: opts.Doer.ID,
  282. ActUser: opts.Doer,
  283. Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
  284. RepoID: opts.Repo.ID,
  285. Repo: opts.Repo,
  286. Comment: comment,
  287. CommentID: comment.ID,
  288. IsPrivate: opts.Repo.IsPrivate,
  289. }
  290. // Check comment type.
  291. switch opts.Type {
  292. case CommentTypeComment:
  293. act.OpType = ActionCommentIssue
  294. if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
  295. return nil, err
  296. }
  297. // Check attachments
  298. attachments := make([]*Attachment, 0, len(opts.Attachments))
  299. for _, uuid := range opts.Attachments {
  300. attach, err := getAttachmentByUUID(e, uuid)
  301. if err != nil {
  302. if IsErrAttachmentNotExist(err) {
  303. continue
  304. }
  305. return nil, fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  306. }
  307. attachments = append(attachments, attach)
  308. }
  309. for i := range attachments {
  310. attachments[i].IssueID = opts.Issue.ID
  311. attachments[i].CommentID = comment.ID
  312. // No assign value could be 0, so ignore AllCols().
  313. if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
  314. return nil, fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  315. }
  316. }
  317. case CommentTypeReopen:
  318. act.OpType = ActionReopenIssue
  319. if opts.Issue.IsPull {
  320. act.OpType = ActionReopenPullRequest
  321. }
  322. if opts.Issue.IsPull {
  323. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
  324. } else {
  325. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
  326. }
  327. if err != nil {
  328. return nil, err
  329. }
  330. case CommentTypeClose:
  331. act.OpType = ActionCloseIssue
  332. if opts.Issue.IsPull {
  333. act.OpType = ActionClosePullRequest
  334. }
  335. if opts.Issue.IsPull {
  336. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
  337. } else {
  338. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
  339. }
  340. if err != nil {
  341. return nil, err
  342. }
  343. }
  344. // update the issue's updated_unix column
  345. if err = updateIssueCols(e, opts.Issue); err != nil {
  346. return nil, err
  347. }
  348. // Notify watchers for whatever action comes in, ignore if no action type.
  349. if act.OpType > 0 {
  350. if err = notifyWatchers(e, act); err != nil {
  351. log.Error(4, "notifyWatchers: %v", err)
  352. }
  353. if err = comment.MailParticipants(e, act.OpType, opts.Issue); err != nil {
  354. log.Error(4, "MailParticipants: %v", err)
  355. }
  356. }
  357. return comment, nil
  358. }
  359. func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
  360. cmtType := CommentTypeClose
  361. if !issue.IsClosed {
  362. cmtType = CommentTypeReopen
  363. }
  364. return createComment(e, &CreateCommentOptions{
  365. Type: cmtType,
  366. Doer: doer,
  367. Repo: repo,
  368. Issue: issue,
  369. })
  370. }
  371. func createLabelComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, label *Label, add bool) (*Comment, error) {
  372. var content string
  373. if add {
  374. content = "1"
  375. }
  376. return createComment(e, &CreateCommentOptions{
  377. Type: CommentTypeLabel,
  378. Doer: doer,
  379. Repo: repo,
  380. Issue: issue,
  381. Label: label,
  382. Content: content,
  383. })
  384. }
  385. func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldMilestoneID, milestoneID int64) (*Comment, error) {
  386. return createComment(e, &CreateCommentOptions{
  387. Type: CommentTypeMilestone,
  388. Doer: doer,
  389. Repo: repo,
  390. Issue: issue,
  391. OldMilestoneID: oldMilestoneID,
  392. MilestoneID: milestoneID,
  393. })
  394. }
  395. func createAssigneeComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldAssigneeID, assigneeID int64) (*Comment, error) {
  396. return createComment(e, &CreateCommentOptions{
  397. Type: CommentTypeAssignees,
  398. Doer: doer,
  399. Repo: repo,
  400. Issue: issue,
  401. OldAssigneeID: oldAssigneeID,
  402. AssigneeID: assigneeID,
  403. })
  404. }
  405. func createChangeTitleComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldTitle, newTitle string) (*Comment, error) {
  406. return createComment(e, &CreateCommentOptions{
  407. Type: CommentTypeChangeTitle,
  408. Doer: doer,
  409. Repo: repo,
  410. Issue: issue,
  411. OldTitle: oldTitle,
  412. NewTitle: newTitle,
  413. })
  414. }
  415. func createDeleteBranchComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, branchName string) (*Comment, error) {
  416. return createComment(e, &CreateCommentOptions{
  417. Type: CommentTypeDeleteBranch,
  418. Doer: doer,
  419. Repo: repo,
  420. Issue: issue,
  421. CommitSHA: branchName,
  422. })
  423. }
  424. // CreateCommentOptions defines options for creating comment
  425. type CreateCommentOptions struct {
  426. Type CommentType
  427. Doer *User
  428. Repo *Repository
  429. Issue *Issue
  430. Label *Label
  431. OldMilestoneID int64
  432. MilestoneID int64
  433. OldAssigneeID int64
  434. AssigneeID int64
  435. OldTitle string
  436. NewTitle string
  437. CommitID int64
  438. CommitSHA string
  439. LineNum int64
  440. Content string
  441. Attachments []string // UUIDs of attachments
  442. }
  443. // CreateComment creates comment of issue or commit.
  444. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
  445. sess := x.NewSession()
  446. defer sess.Close()
  447. if err = sess.Begin(); err != nil {
  448. return nil, err
  449. }
  450. comment, err = createComment(sess, opts)
  451. if err != nil {
  452. return nil, err
  453. }
  454. if err = sess.Commit(); err != nil {
  455. return nil, err
  456. }
  457. if opts.Type == CommentTypeComment {
  458. UpdateIssueIndexer(opts.Issue.ID)
  459. }
  460. return comment, nil
  461. }
  462. // CreateIssueComment creates a plain issue comment.
  463. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
  464. return CreateComment(&CreateCommentOptions{
  465. Type: CommentTypeComment,
  466. Doer: doer,
  467. Repo: repo,
  468. Issue: issue,
  469. Content: content,
  470. Attachments: attachments,
  471. })
  472. }
  473. // CreateRefComment creates a commit reference comment to issue.
  474. func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
  475. if len(commitSHA) == 0 {
  476. return fmt.Errorf("cannot create reference with empty commit SHA")
  477. }
  478. // Check if same reference from same commit has already existed.
  479. has, err := x.Get(&Comment{
  480. Type: CommentTypeCommitRef,
  481. IssueID: issue.ID,
  482. CommitSHA: commitSHA,
  483. })
  484. if err != nil {
  485. return fmt.Errorf("check reference comment: %v", err)
  486. } else if has {
  487. return nil
  488. }
  489. _, err = CreateComment(&CreateCommentOptions{
  490. Type: CommentTypeCommitRef,
  491. Doer: doer,
  492. Repo: repo,
  493. Issue: issue,
  494. CommitSHA: commitSHA,
  495. Content: content,
  496. })
  497. return err
  498. }
  499. // GetCommentByID returns the comment by given ID.
  500. func GetCommentByID(id int64) (*Comment, error) {
  501. c := new(Comment)
  502. has, err := x.ID(id).Get(c)
  503. if err != nil {
  504. return nil, err
  505. } else if !has {
  506. return nil, ErrCommentNotExist{id, 0}
  507. }
  508. return c, nil
  509. }
  510. // FindCommentsOptions describes the conditions to Find comments
  511. type FindCommentsOptions struct {
  512. RepoID int64
  513. IssueID int64
  514. Since int64
  515. Type CommentType
  516. }
  517. func (opts *FindCommentsOptions) toConds() builder.Cond {
  518. var cond = builder.NewCond()
  519. if opts.RepoID > 0 {
  520. cond = cond.And(builder.Eq{"issue.repo_id": opts.RepoID})
  521. }
  522. if opts.IssueID > 0 {
  523. cond = cond.And(builder.Eq{"comment.issue_id": opts.IssueID})
  524. }
  525. if opts.Since > 0 {
  526. cond = cond.And(builder.Gte{"comment.updated_unix": opts.Since})
  527. }
  528. if opts.Type != CommentTypeUnknown {
  529. cond = cond.And(builder.Eq{"comment.type": opts.Type})
  530. }
  531. return cond
  532. }
  533. func findComments(e Engine, opts FindCommentsOptions) ([]*Comment, error) {
  534. comments := make([]*Comment, 0, 10)
  535. sess := e.Where(opts.toConds())
  536. if opts.RepoID > 0 {
  537. sess.Join("INNER", "issue", "issue.id = comment.issue_id")
  538. }
  539. return comments, sess.
  540. Asc("comment.created_unix").
  541. Asc("comment.id").
  542. Find(&comments)
  543. }
  544. // FindComments returns all comments according options
  545. func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
  546. return findComments(x, opts)
  547. }
  548. // GetCommentsByIssueID returns all comments of an issue.
  549. func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
  550. return findComments(x, FindCommentsOptions{
  551. IssueID: issueID,
  552. Type: CommentTypeUnknown,
  553. })
  554. }
  555. // GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
  556. func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
  557. return findComments(x, FindCommentsOptions{
  558. IssueID: issueID,
  559. Type: CommentTypeUnknown,
  560. Since: since,
  561. })
  562. }
  563. // GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
  564. func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
  565. return findComments(x, FindCommentsOptions{
  566. RepoID: repoID,
  567. Type: CommentTypeUnknown,
  568. Since: since,
  569. })
  570. }
  571. // UpdateComment updates information of comment.
  572. func UpdateComment(c *Comment) error {
  573. if _, err := x.ID(c.ID).AllCols().Update(c); err != nil {
  574. return err
  575. } else if c.Type == CommentTypeComment {
  576. UpdateIssueIndexer(c.IssueID)
  577. }
  578. return nil
  579. }
  580. // DeleteComment deletes the comment
  581. func DeleteComment(comment *Comment) error {
  582. sess := x.NewSession()
  583. defer sess.Close()
  584. if err := sess.Begin(); err != nil {
  585. return err
  586. }
  587. if _, err := sess.Delete(&Comment{
  588. ID: comment.ID,
  589. }); err != nil {
  590. return err
  591. }
  592. if comment.Type == CommentTypeComment {
  593. if _, err := sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil {
  594. return err
  595. }
  596. }
  597. if _, err := sess.Where("comment_id = ?", comment.ID).Cols("is_deleted").Update(&Action{IsDeleted: true}); err != nil {
  598. return err
  599. }
  600. if err := sess.Commit(); err != nil {
  601. return err
  602. } else if comment.Type == CommentTypeComment {
  603. UpdateIssueIndexer(comment.IssueID)
  604. }
  605. return nil
  606. }