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

issue_user.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2017 The Gitea 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. )
  8. // IssueUser represents an issue-user relation.
  9. type IssueUser struct {
  10. ID int64 `xorm:"pk autoincr"`
  11. UID int64 `xorm:"INDEX"` // User ID.
  12. IssueID int64
  13. IsRead bool
  14. IsAssigned bool
  15. IsMentioned bool
  16. }
  17. func newIssueUsers(e Engine, repo *Repository, issue *Issue) error {
  18. assignees, err := repo.getAssignees(e)
  19. if err != nil {
  20. return fmt.Errorf("getAssignees: %v", err)
  21. }
  22. // Poster can be anyone, append later if not one of assignees.
  23. isPosterAssignee := false
  24. // Leave a seat for poster itself to append later, but if poster is one of assignee
  25. // and just waste 1 unit is cheaper than re-allocate memory once.
  26. issueUsers := make([]*IssueUser, 0, len(assignees)+1)
  27. for _, assignee := range assignees {
  28. issueUsers = append(issueUsers, &IssueUser{
  29. IssueID: issue.ID,
  30. UID: assignee.ID,
  31. IsAssigned: assignee.ID == issue.AssigneeID,
  32. })
  33. isPosterAssignee = isPosterAssignee || assignee.ID == issue.PosterID
  34. }
  35. if !isPosterAssignee {
  36. issueUsers = append(issueUsers, &IssueUser{
  37. IssueID: issue.ID,
  38. UID: issue.PosterID,
  39. })
  40. }
  41. if _, err = e.Insert(issueUsers); err != nil {
  42. return err
  43. }
  44. return nil
  45. }
  46. func updateIssueUserByAssignee(e Engine, issue *Issue) (err error) {
  47. if _, err = e.Exec("UPDATE `issue_user` SET is_assigned = ? WHERE issue_id = ?", false, issue.ID); err != nil {
  48. return err
  49. }
  50. // Assignee ID equals to 0 means clear assignee.
  51. if issue.AssigneeID > 0 {
  52. if _, err = e.Exec("UPDATE `issue_user` SET is_assigned = ? WHERE uid = ? AND issue_id = ?", true, issue.AssigneeID, issue.ID); err != nil {
  53. return err
  54. }
  55. }
  56. return updateIssue(e, issue)
  57. }
  58. // UpdateIssueUserByAssignee updates issue-user relation for assignee.
  59. func UpdateIssueUserByAssignee(issue *Issue) (err error) {
  60. sess := x.NewSession()
  61. defer sess.Close()
  62. if err = sess.Begin(); err != nil {
  63. return err
  64. }
  65. if err = updateIssueUserByAssignee(sess, issue); err != nil {
  66. return err
  67. }
  68. return sess.Commit()
  69. }
  70. // UpdateIssueUserByRead updates issue-user relation for reading.
  71. func UpdateIssueUserByRead(uid, issueID int64) error {
  72. _, err := x.Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
  73. return err
  74. }
  75. // UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
  76. func UpdateIssueUsersByMentions(e Engine, issueID int64, uids []int64) error {
  77. for _, uid := range uids {
  78. iu := &IssueUser{
  79. UID: uid,
  80. IssueID: issueID,
  81. }
  82. has, err := e.Get(iu)
  83. if err != nil {
  84. return err
  85. }
  86. iu.IsMentioned = true
  87. if has {
  88. _, err = e.ID(iu.ID).Cols("is_mentioned").Update(iu)
  89. } else {
  90. _, err = e.Insert(iu)
  91. }
  92. if err != nil {
  93. return err
  94. }
  95. }
  96. return nil
  97. }