issue_comment.go 11 KB

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