issue_comment.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/xorm"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/markdown"
  13. )
  14. // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
  15. type CommentType int
  16. const (
  17. // Plain comment, can be associated with a commit (CommitID > 0) and a line (LineNum > 0)
  18. COMMENT_TYPE_COMMENT CommentType = iota
  19. COMMENT_TYPE_REOPEN
  20. COMMENT_TYPE_CLOSE
  21. // References.
  22. COMMENT_TYPE_ISSUE_REF
  23. // Reference from a commit (not part of a pull request)
  24. COMMENT_TYPE_COMMIT_REF
  25. // Reference from a comment
  26. COMMENT_TYPE_COMMENT_REF
  27. // Reference from a pull request
  28. COMMENT_TYPE_PULL_REF
  29. )
  30. type CommentTag int
  31. const (
  32. COMMENT_TAG_NONE CommentTag = iota
  33. COMMENT_TAG_POSTER
  34. COMMENT_TAG_WRITER
  35. COMMENT_TAG_OWNER
  36. )
  37. // Comment represents a comment in commit and issue page.
  38. type Comment struct {
  39. ID int64 `xorm:"pk autoincr"`
  40. Type CommentType
  41. PosterID int64
  42. Poster *User `xorm:"-"`
  43. IssueID int64 `xorm:"INDEX"`
  44. CommitID int64
  45. Line int64
  46. Content string `xorm:"TEXT"`
  47. RenderedContent string `xorm:"-"`
  48. Created time.Time `xorm:"-"`
  49. CreatedUnix int64
  50. // Reference issue in commit message
  51. CommitSHA string `xorm:"VARCHAR(40)"`
  52. Attachments []*Attachment `xorm:"-"`
  53. // For view issue page.
  54. ShowTag CommentTag `xorm:"-"`
  55. }
  56. func (c *Comment) BeforeInsert() {
  57. c.CreatedUnix = time.Now().Unix()
  58. }
  59. func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
  60. var err error
  61. switch colName {
  62. case "id":
  63. c.Attachments, err = GetAttachmentsByCommentID(c.ID)
  64. if err != nil {
  65. log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err)
  66. }
  67. case "poster_id":
  68. c.Poster, err = GetUserByID(c.PosterID)
  69. if err != nil {
  70. if IsErrUserNotExist(err) {
  71. c.PosterID = -1
  72. c.Poster = NewFakeUser()
  73. } else {
  74. log.Error(3, "GetUserByID[%d]: %v", c.ID, err)
  75. }
  76. }
  77. case "created_unix":
  78. c.Created = time.Unix(c.CreatedUnix, 0).Local()
  79. }
  80. }
  81. func (c *Comment) AfterDelete() {
  82. _, err := DeleteAttachmentsByComment(c.ID, true)
  83. if err != nil {
  84. log.Info("Could not delete files for comment %d on issue #%d: %s", c.ID, c.IssueID, err)
  85. }
  86. }
  87. // HashTag returns unique hash tag for comment.
  88. func (c *Comment) HashTag() string {
  89. return "issuecomment-" + com.ToStr(c.ID)
  90. }
  91. // EventTag returns unique event hash tag for comment.
  92. func (c *Comment) EventTag() string {
  93. return "event-" + com.ToStr(c.ID)
  94. }
  95. // MailParticipants sends new comment emails to repository watchers
  96. // and mentioned people.
  97. func (cmt *Comment) MailParticipants(opType ActionType, issue *Issue) (err error) {
  98. mentions := markdown.FindAllMentions(cmt.Content)
  99. if err = UpdateIssueMentions(cmt.IssueID, mentions); err != nil {
  100. return fmt.Errorf("UpdateIssueMentions [%d]: %v", cmt.IssueID, err)
  101. }
  102. switch opType {
  103. case ACTION_COMMENT_ISSUE:
  104. issue.Content = cmt.Content
  105. case ACTION_CLOSE_ISSUE:
  106. issue.Content = fmt.Sprintf("Closed #%d", issue.Index)
  107. case ACTION_REOPEN_ISSUE:
  108. issue.Content = fmt.Sprintf("Reopened #%d", issue.Index)
  109. }
  110. if err = mailIssueCommentToParticipants(issue, cmt.Poster, mentions); err != nil {
  111. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  112. }
  113. return nil
  114. }
  115. func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
  116. comment := &Comment{
  117. Type: opts.Type,
  118. PosterID: opts.Doer.ID,
  119. Poster: opts.Doer,
  120. IssueID: opts.Issue.ID,
  121. CommitID: opts.CommitID,
  122. CommitSHA: opts.CommitSHA,
  123. Line: opts.LineNum,
  124. Content: opts.Content,
  125. }
  126. if _, err = e.Insert(comment); err != nil {
  127. return nil, err
  128. }
  129. // Compose comment action, could be plain comment, close or reopen issue/pull request.
  130. // This object will be used to notify watchers in the end of function.
  131. act := &Action{
  132. ActUserID: opts.Doer.ID,
  133. ActUserName: opts.Doer.Name,
  134. ActEmail: opts.Doer.Email,
  135. Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
  136. RepoID: opts.Repo.ID,
  137. RepoUserName: opts.Repo.Owner.Name,
  138. RepoName: opts.Repo.Name,
  139. IsPrivate: opts.Repo.IsPrivate,
  140. }
  141. // Check comment type.
  142. switch opts.Type {
  143. case COMMENT_TYPE_COMMENT:
  144. act.OpType = ACTION_COMMENT_ISSUE
  145. if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
  146. return nil, err
  147. }
  148. // Check attachments
  149. attachments := make([]*Attachment, 0, len(opts.Attachments))
  150. for _, uuid := range opts.Attachments {
  151. attach, err := getAttachmentByUUID(e, uuid)
  152. if err != nil {
  153. if IsErrAttachmentNotExist(err) {
  154. continue
  155. }
  156. return nil, fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  157. }
  158. attachments = append(attachments, attach)
  159. }
  160. for i := range attachments {
  161. attachments[i].IssueID = opts.Issue.ID
  162. attachments[i].CommentID = comment.ID
  163. // No assign value could be 0, so ignore AllCols().
  164. if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
  165. return nil, fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  166. }
  167. }
  168. case COMMENT_TYPE_REOPEN:
  169. act.OpType = ACTION_REOPEN_ISSUE
  170. if opts.Issue.IsPull {
  171. act.OpType = ACTION_REOPEN_PULL_REQUEST
  172. }
  173. if opts.Issue.IsPull {
  174. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
  175. } else {
  176. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
  177. }
  178. if err != nil {
  179. return nil, err
  180. }
  181. case COMMENT_TYPE_CLOSE:
  182. act.OpType = ACTION_CLOSE_ISSUE
  183. if opts.Issue.IsPull {
  184. act.OpType = ACTION_CLOSE_PULL_REQUEST
  185. }
  186. if opts.Issue.IsPull {
  187. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
  188. } else {
  189. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
  190. }
  191. if err != nil {
  192. return nil, err
  193. }
  194. }
  195. // Notify watchers for whatever action comes in, ignore if no action type.
  196. if act.OpType > 0 {
  197. if err = notifyWatchers(e, act); err != nil {
  198. log.Error(4, "notifyWatchers: %v", err)
  199. }
  200. comment.MailParticipants(act.OpType, opts.Issue)
  201. }
  202. return comment, nil
  203. }
  204. func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
  205. cmtType := COMMENT_TYPE_CLOSE
  206. if !issue.IsClosed {
  207. cmtType = COMMENT_TYPE_REOPEN
  208. }
  209. return createComment(e, &CreateCommentOptions{
  210. Type: cmtType,
  211. Doer: doer,
  212. Repo: repo,
  213. Issue: issue,
  214. })
  215. }
  216. type CreateCommentOptions struct {
  217. Type CommentType
  218. Doer *User
  219. Repo *Repository
  220. Issue *Issue
  221. CommitID int64
  222. CommitSHA string
  223. LineNum int64
  224. Content string
  225. Attachments []string // UUIDs of attachments
  226. }
  227. // CreateComment creates comment of issue or commit.
  228. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
  229. sess := x.NewSession()
  230. defer sessionRelease(sess)
  231. if err = sess.Begin(); err != nil {
  232. return nil, err
  233. }
  234. comment, err = createComment(sess, opts)
  235. if err != nil {
  236. return nil, err
  237. }
  238. return comment, sess.Commit()
  239. }
  240. // CreateIssueComment creates a plain issue comment.
  241. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
  242. return CreateComment(&CreateCommentOptions{
  243. Type: COMMENT_TYPE_COMMENT,
  244. Doer: doer,
  245. Repo: repo,
  246. Issue: issue,
  247. Content: content,
  248. Attachments: attachments,
  249. })
  250. }
  251. // CreateRefComment creates a commit reference comment to issue.
  252. func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
  253. if len(commitSHA) == 0 {
  254. return fmt.Errorf("cannot create reference with empty commit SHA")
  255. }
  256. // Check if same reference from same commit has already existed.
  257. has, err := x.Get(&Comment{
  258. Type: COMMENT_TYPE_COMMIT_REF,
  259. IssueID: issue.ID,
  260. CommitSHA: commitSHA,
  261. })
  262. if err != nil {
  263. return fmt.Errorf("check reference comment: %v", err)
  264. } else if has {
  265. return nil
  266. }
  267. _, err = CreateComment(&CreateCommentOptions{
  268. Type: COMMENT_TYPE_COMMIT_REF,
  269. Doer: doer,
  270. Repo: repo,
  271. Issue: issue,
  272. CommitSHA: commitSHA,
  273. Content: content,
  274. })
  275. return err
  276. }
  277. // GetCommentByID returns the comment by given ID.
  278. func GetCommentByID(id int64) (*Comment, error) {
  279. c := new(Comment)
  280. has, err := x.Id(id).Get(c)
  281. if err != nil {
  282. return nil, err
  283. } else if !has {
  284. return nil, ErrCommentNotExist{id}
  285. }
  286. return c, nil
  287. }
  288. // GetCommentsByIssueID returns all comments of issue by given ID.
  289. func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
  290. comments := make([]*Comment, 0, 10)
  291. return comments, x.Where("issue_id=?", issueID).Asc("created_unix").Find(&comments)
  292. }
  293. // UpdateComment updates information of comment.
  294. func UpdateComment(c *Comment) error {
  295. _, err := x.Id(c.ID).AllCols().Update(c)
  296. return err
  297. }
  298. // DeleteCommentByID deletes a comment by given ID.
  299. func DeleteCommentByID(id int64) error {
  300. comment, err := GetCommentByID(id)
  301. if err != nil {
  302. return err
  303. }
  304. sess := x.NewSession()
  305. defer sessionRelease(sess)
  306. if err = sess.Begin(); err != nil {
  307. return err
  308. }
  309. if _, err = sess.Id(comment.ID).Delete(new(Comment)); err != nil {
  310. return err
  311. }
  312. if comment.Type == COMMENT_TYPE_COMMENT {
  313. if _, err = sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil {
  314. return err
  315. }
  316. }
  317. return sess.Commit()
  318. }