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_user.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issues
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. )
  10. // IssueUser represents an issue-user relation.
  11. type IssueUser struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. UID int64 `xorm:"INDEX"` // User ID.
  14. IssueID int64 `xorm:"INDEX"`
  15. IsRead bool
  16. IsMentioned bool
  17. }
  18. func init() {
  19. db.RegisterModel(new(IssueUser))
  20. }
  21. // NewIssueUsers inserts an issue related users
  22. func NewIssueUsers(ctx context.Context, repo *repo_model.Repository, issue *Issue) error {
  23. assignees, err := repo_model.GetRepoAssignees(ctx, repo)
  24. if err != nil {
  25. return fmt.Errorf("getAssignees: %w", err)
  26. }
  27. // Poster can be anyone, append later if not one of assignees.
  28. isPosterAssignee := false
  29. // Leave a seat for poster itself to append later, but if poster is one of assignee
  30. // and just waste 1 unit is cheaper than re-allocate memory once.
  31. issueUsers := make([]*IssueUser, 0, len(assignees)+1)
  32. for _, assignee := range assignees {
  33. issueUsers = append(issueUsers, &IssueUser{
  34. IssueID: issue.ID,
  35. UID: assignee.ID,
  36. })
  37. isPosterAssignee = isPosterAssignee || assignee.ID == issue.PosterID
  38. }
  39. if !isPosterAssignee {
  40. issueUsers = append(issueUsers, &IssueUser{
  41. IssueID: issue.ID,
  42. UID: issue.PosterID,
  43. })
  44. }
  45. return db.Insert(ctx, issueUsers)
  46. }
  47. // UpdateIssueUserByRead updates issue-user relation for reading.
  48. func UpdateIssueUserByRead(ctx context.Context, uid, issueID int64) error {
  49. _, err := db.GetEngine(ctx).Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
  50. return err
  51. }
  52. // UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
  53. func UpdateIssueUsersByMentions(ctx context.Context, issueID int64, uids []int64) error {
  54. for _, uid := range uids {
  55. iu := &IssueUser{
  56. UID: uid,
  57. IssueID: issueID,
  58. }
  59. has, err := db.GetEngine(ctx).Get(iu)
  60. if err != nil {
  61. return err
  62. }
  63. iu.IsMentioned = true
  64. if has {
  65. _, err = db.GetEngine(ctx).ID(iu.ID).Cols("is_mentioned").Update(iu)
  66. } else {
  67. _, err = db.GetEngine(ctx).Insert(iu)
  68. }
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. return nil
  74. }
  75. // GetIssueMentionIDs returns all mentioned user IDs of an issue.
  76. func GetIssueMentionIDs(ctx context.Context, issueID int64) ([]int64, error) {
  77. var ids []int64
  78. return ids, db.GetEngine(ctx).Table(IssueUser{}).
  79. Where("issue_id=?", issueID).
  80. And("is_mentioned=?", true).
  81. Select("uid").
  82. Find(&ids)
  83. }