Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_comment.go 18KB

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