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.
88 lines
2.3 KiB
88 lines
2.3 KiB
8 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
8 years ago
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
2 years ago
|
package issues
|
||
8 years ago
|
|
||
|
import (
|
||
3 years ago
|
"context"
|
||
8 years ago
|
"fmt"
|
||
3 years ago
|
|
||
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
8 years ago
|
)
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(IssueUser))
|
||
|
}
|
||
|
|
||
2 years ago
|
// NewIssueUsers inserts an issue related users
|
||
|
func NewIssueUsers(ctx context.Context, repo *repo_model.Repository, issue *Issue) error {
|
||
2 years ago
|
assignees, err := repo_model.GetRepoAssignees(ctx, repo)
|
||
8 years ago
|
if err != nil {
|
||
|
return fmt.Errorf("getAssignees: %v", err)
|
||
|
}
|
||
|
|
||
|
// 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{
|
||
7 years ago
|
IssueID: issue.ID,
|
||
|
UID: assignee.ID,
|
||
8 years ago
|
})
|
||
|
isPosterAssignee = isPosterAssignee || assignee.ID == issue.PosterID
|
||
|
}
|
||
|
if !isPosterAssignee {
|
||
|
issueUsers = append(issueUsers, &IssueUser{
|
||
|
IssueID: issue.ID,
|
||
|
UID: issue.PosterID,
|
||
|
})
|
||
|
}
|
||
|
|
||
3 years ago
|
return db.Insert(ctx, issueUsers)
|
||
8 years ago
|
}
|
||
|
|
||
|
// UpdateIssueUserByRead updates issue-user relation for reading.
|
||
|
func UpdateIssueUserByRead(uid, issueID int64) error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
// UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
|
||
3 years ago
|
func UpdateIssueUsersByMentions(ctx context.Context, issueID int64, uids []int64) error {
|
||
8 years ago
|
for _, uid := range uids {
|
||
|
iu := &IssueUser{
|
||
|
UID: uid,
|
||
|
IssueID: issueID,
|
||
|
}
|
||
3 years ago
|
has, err := db.GetEngine(ctx).Get(iu)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
iu.IsMentioned = true
|
||
|
if has {
|
||
3 years ago
|
_, err = db.GetEngine(ctx).ID(iu.ID).Cols("is_mentioned").Update(iu)
|
||
8 years ago
|
} else {
|
||
3 years ago
|
_, err = db.GetEngine(ctx).Insert(iu)
|
||
8 years ago
|
}
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|