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.

81 lines
2.0 KiB

  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. IsMentioned bool
  15. }
  16. func newIssueUsers(e Engine, repo *Repository, issue *Issue) error {
  17. assignees, err := repo.getAssignees(e)
  18. if err != nil {
  19. return fmt.Errorf("getAssignees: %v", err)
  20. }
  21. // Poster can be anyone, append later if not one of assignees.
  22. isPosterAssignee := false
  23. // Leave a seat for poster itself to append later, but if poster is one of assignee
  24. // and just waste 1 unit is cheaper than re-allocate memory once.
  25. issueUsers := make([]*IssueUser, 0, len(assignees)+1)
  26. for _, assignee := range assignees {
  27. issueUsers = append(issueUsers, &IssueUser{
  28. IssueID: issue.ID,
  29. UID: assignee.ID,
  30. })
  31. isPosterAssignee = isPosterAssignee || assignee.ID == issue.PosterID
  32. }
  33. if !isPosterAssignee {
  34. issueUsers = append(issueUsers, &IssueUser{
  35. IssueID: issue.ID,
  36. UID: issue.PosterID,
  37. })
  38. }
  39. if _, err = e.Insert(issueUsers); err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. // UpdateIssueUserByRead updates issue-user relation for reading.
  45. func UpdateIssueUserByRead(uid, issueID int64) error {
  46. _, err := x.Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
  47. return err
  48. }
  49. // UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
  50. func UpdateIssueUsersByMentions(ctx DBContext, issueID int64, uids []int64) error {
  51. for _, uid := range uids {
  52. iu := &IssueUser{
  53. UID: uid,
  54. IssueID: issueID,
  55. }
  56. has, err := ctx.e.Get(iu)
  57. if err != nil {
  58. return err
  59. }
  60. iu.IsMentioned = true
  61. if has {
  62. _, err = ctx.e.ID(iu.ID).Cols("is_mentioned").Update(iu)
  63. } else {
  64. _, err = ctx.e.Insert(iu)
  65. }
  66. if err != nil {
  67. return err
  68. }
  69. }
  70. return nil
  71. }