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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 = NewGhostUser()
  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. Content: fmt.Sprintf("%d|%s", opts.Issue.Index, strings.Split(opts.Content, "\n")[0]),
  135. RepoID: opts.Repo.ID,
  136. RepoUserName: opts.Repo.Owner.Name,
  137. RepoName: opts.Repo.Name,
  138. IsPrivate: opts.Repo.IsPrivate,
  139. }
  140. // Check comment type.
  141. switch opts.Type {
  142. case COMMENT_TYPE_COMMENT:
  143. act.OpType = ACTION_COMMENT_ISSUE
  144. if _, err = e.Exec("UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
  145. return nil, err
  146. }
  147. // Check attachments
  148. attachments := make([]*Attachment, 0, len(opts.Attachments))
  149. for _, uuid := range opts.Attachments {
  150. attach, err := getAttachmentByUUID(e, uuid)
  151. if err != nil {
  152. if IsErrAttachmentNotExist(err) {
  153. continue
  154. }
  155. return nil, fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  156. }
  157. attachments = append(attachments, attach)
  158. }
  159. for i := range attachments {
  160. attachments[i].IssueID = opts.Issue.ID
  161. attachments[i].CommentID = comment.ID
  162. // No assign value could be 0, so ignore AllCols().
  163. if _, err = e.Id(attachments[i].ID).Update(attachments[i]); err != nil {
  164. return nil, fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  165. }
  166. }
  167. case COMMENT_TYPE_REOPEN:
  168. act.OpType = ACTION_REOPEN_ISSUE
  169. if opts.Issue.IsPull {
  170. act.OpType = ACTION_REOPEN_PULL_REQUEST
  171. }
  172. if opts.Issue.IsPull {
  173. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls-1 WHERE id=?", opts.Repo.ID)
  174. } else {
  175. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues-1 WHERE id=?", opts.Repo.ID)
  176. }
  177. if err != nil {
  178. return nil, err
  179. }
  180. case COMMENT_TYPE_CLOSE:
  181. act.OpType = ACTION_CLOSE_ISSUE
  182. if opts.Issue.IsPull {
  183. act.OpType = ACTION_CLOSE_PULL_REQUEST
  184. }
  185. if opts.Issue.IsPull {
  186. _, err = e.Exec("UPDATE `repository` SET num_closed_pulls=num_closed_pulls+1 WHERE id=?", opts.Repo.ID)
  187. } else {
  188. _, err = e.Exec("UPDATE `repository` SET num_closed_issues=num_closed_issues+1 WHERE id=?", opts.Repo.ID)
  189. }
  190. if err != nil {
  191. return nil, err
  192. }
  193. }
  194. // Notify watchers for whatever action comes in, ignore if no action type.
  195. if act.OpType > 0 {
  196. if err = notifyWatchers(e, act); err != nil {
  197. log.Error(4, "notifyWatchers: %v", err)
  198. }
  199. comment.MailParticipants(act.OpType, opts.Issue)
  200. }
  201. return comment, nil
  202. }
  203. func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
  204. cmtType := COMMENT_TYPE_CLOSE
  205. if !issue.IsClosed {
  206. cmtType = COMMENT_TYPE_REOPEN
  207. }
  208. return createComment(e, &CreateCommentOptions{
  209. Type: cmtType,
  210. Doer: doer,
  211. Repo: repo,
  212. Issue: issue,
  213. })
  214. }
  215. type CreateCommentOptions struct {
  216. Type CommentType
  217. Doer *User
  218. Repo *Repository
  219. Issue *Issue
  220. CommitID int64
  221. CommitSHA string
  222. LineNum int64
  223. Content string
  224. Attachments []string // UUIDs of attachments
  225. }
  226. // CreateComment creates comment of issue or commit.
  227. func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
  228. sess := x.NewSession()
  229. defer sessionRelease(sess)
  230. if err = sess.Begin(); err != nil {
  231. return nil, err
  232. }
  233. comment, err = createComment(sess, opts)
  234. if err != nil {
  235. return nil, err
  236. }
  237. return comment, sess.Commit()
  238. }
  239. // CreateIssueComment creates a plain issue comment.
  240. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
  241. return CreateComment(&CreateCommentOptions{
  242. Type: COMMENT_TYPE_COMMENT,
  243. Doer: doer,
  244. Repo: repo,
  245. Issue: issue,
  246. Content: content,
  247. Attachments: attachments,
  248. })
  249. }
  250. // CreateRefComment creates a commit reference comment to issue.
  251. func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
  252. if len(commitSHA) == 0 {
  253. return fmt.Errorf("cannot create reference with empty commit SHA")
  254. }
  255. // Check if same reference from same commit has already existed.
  256. has, err := x.Get(&Comment{
  257. Type: COMMENT_TYPE_COMMIT_REF,
  258. IssueID: issue.ID,
  259. CommitSHA: commitSHA,
  260. })
  261. if err != nil {
  262. return fmt.Errorf("check reference comment: %v", err)
  263. } else if has {
  264. return nil
  265. }
  266. _, err = CreateComment(&CreateCommentOptions{
  267. Type: COMMENT_TYPE_COMMIT_REF,
  268. Doer: doer,
  269. Repo: repo,
  270. Issue: issue,
  271. CommitSHA: commitSHA,
  272. Content: content,
  273. })
  274. return err
  275. }
  276. // GetCommentByID returns the comment by given ID.
  277. func GetCommentByID(id int64) (*Comment, error) {
  278. c := new(Comment)
  279. has, err := x.Id(id).Get(c)
  280. if err != nil {
  281. return nil, err
  282. } else if !has {
  283. return nil, ErrCommentNotExist{id}
  284. }
  285. return c, nil
  286. }
  287. // GetCommentsByIssueID returns all comments of issue by given ID.
  288. func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
  289. comments := make([]*Comment, 0, 10)
  290. return comments, x.Where("issue_id=?", issueID).Asc("created_unix").Find(&comments)
  291. }
  292. // UpdateComment updates information of comment.
  293. func UpdateComment(c *Comment) error {
  294. _, err := x.Id(c.ID).AllCols().Update(c)
  295. return err
  296. }
  297. // DeleteCommentByID deletes a comment by given ID.
  298. func DeleteCommentByID(id int64) error {
  299. comment, err := GetCommentByID(id)
  300. if err != nil {
  301. return err
  302. }
  303. sess := x.NewSession()
  304. defer sessionRelease(sess)
  305. if err = sess.Begin(); err != nil {
  306. return err
  307. }
  308. if _, err = sess.Id(comment.ID).Delete(new(Comment)); err != nil {
  309. return err
  310. }
  311. if comment.Type == COMMENT_TYPE_COMMENT {
  312. if _, err = sess.Exec("UPDATE `issue` SET num_comments = num_comments - 1 WHERE id = ?", comment.IssueID); err != nil {
  313. return err
  314. }
  315. }
  316. return sess.Commit()
  317. }