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

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