2017-02-09 06:44:18 +01:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-02-04 13:37:26 +01:00
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
package issues
|
2017-02-04 13:37:26 +01:00
|
|
|
|
|
|
|
import (
|
2021-09-23 17:45:36 +02:00
|
|
|
"context"
|
2017-02-04 13:37:26 +01:00
|
|
|
"fmt"
|
2021-09-19 13:49:59 +02:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 02:27:50 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2017-02-04 13:37:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// IssueUser represents an issue-user relation.
|
|
|
|
type IssueUser struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UID int64 `xorm:"INDEX"` // User ID.
|
|
|
|
IssueID int64
|
|
|
|
IsRead bool
|
|
|
|
IsMentioned bool
|
|
|
|
}
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(IssueUser))
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
// NewIssueUsers inserts an issue related users
|
|
|
|
func NewIssueUsers(ctx context.Context, repo *repo_model.Repository, issue *Issue) error {
|
2022-06-06 10:01:49 +02:00
|
|
|
assignees, err := repo_model.GetRepoAssignees(ctx, repo)
|
2017-02-04 13:37:26 +01:00
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("getAssignees: %w", err)
|
2017-02-04 13:37:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Poster can be anyone, append later if not one of assignees.
|
|
|
|
isPosterAssignee := false
|
|
|
|
|
|
|
|
// Leave a seat for poster itself to append later, but if poster is one of assignee
|
|
|
|
// and just waste 1 unit is cheaper than re-allocate memory once.
|
|
|
|
issueUsers := make([]*IssueUser, 0, len(assignees)+1)
|
|
|
|
for _, assignee := range assignees {
|
|
|
|
issueUsers = append(issueUsers, &IssueUser{
|
2018-05-09 18:29:04 +02:00
|
|
|
IssueID: issue.ID,
|
|
|
|
UID: assignee.ID,
|
2017-02-04 13:37:26 +01:00
|
|
|
})
|
|
|
|
isPosterAssignee = isPosterAssignee || assignee.ID == issue.PosterID
|
|
|
|
}
|
|
|
|
if !isPosterAssignee {
|
|
|
|
issueUsers = append(issueUsers, &IssueUser{
|
|
|
|
IssueID: issue.ID,
|
|
|
|
UID: issue.PosterID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-10 02:27:50 +01:00
|
|
|
return db.Insert(ctx, issueUsers)
|
2017-02-04 13:37:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueUserByRead updates issue-user relation for reading.
|
2023-08-05 09:35:08 +02:00
|
|
|
func UpdateIssueUserByRead(ctx context.Context, uid, issueID int64) error {
|
|
|
|
_, err := db.GetEngine(ctx).Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
|
2017-02-04 13:37:26 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
|
2021-09-23 17:45:36 +02:00
|
|
|
func UpdateIssueUsersByMentions(ctx context.Context, issueID int64, uids []int64) error {
|
2017-02-04 13:37:26 +01:00
|
|
|
for _, uid := range uids {
|
|
|
|
iu := &IssueUser{
|
|
|
|
UID: uid,
|
|
|
|
IssueID: issueID,
|
|
|
|
}
|
2021-09-23 17:45:36 +02:00
|
|
|
has, err := db.GetEngine(ctx).Get(iu)
|
2017-02-04 13:37:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iu.IsMentioned = true
|
|
|
|
if has {
|
2021-09-23 17:45:36 +02:00
|
|
|
_, err = db.GetEngine(ctx).ID(iu.ID).Cols("is_mentioned").Update(iu)
|
2017-02-04 13:37:26 +01:00
|
|
|
} else {
|
2021-09-23 17:45:36 +02:00
|
|
|
_, err = db.GetEngine(ctx).Insert(iu)
|
2017-02-04 13:37:26 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|