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.8KB

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