This repository has been archived on 2023-02-01. You can view files and clone it, but cannot push or open issues or pull requests.
gitea/models/issue_user.go

120 lines
2.9 KiB
Go
Raw Permalink Normal View History

2017-02-09 05:44:18 +00:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"fmt"
2018-05-09 16:29:04 +00:00
"github.com/go-xorm/xorm"
)
// 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
}
func newIssueUsers(e Engine, repo *Repository, issue *Issue) error {
assignees, err := repo.getAssignees(e)
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{
2018-05-09 16:29:04 +00:00
IssueID: issue.ID,
UID: assignee.ID,
})
isPosterAssignee = isPosterAssignee || assignee.ID == issue.PosterID
}
if !isPosterAssignee {
issueUsers = append(issueUsers, &IssueUser{
IssueID: issue.ID,
UID: issue.PosterID,
})
}
if _, err = e.Insert(issueUsers); err != nil {
return err
}
return nil
}
2018-05-09 16:29:04 +00:00
func updateIssueAssignee(e *xorm.Session, issue *Issue, assigneeID int64) (removed bool, err error) {
// Check if the user exists
assignee, err := getUserByID(e, assigneeID)
2018-05-09 16:29:04 +00:00
if err != nil {
return false, err
}
2018-05-09 16:29:04 +00:00
// Check if the submitted user is already assigne, if yes delete him otherwise add him
var i int
for i = 0; i < len(issue.Assignees); i++ {
if issue.Assignees[i].ID == assigneeID {
2018-05-09 16:29:04 +00:00
break
}
}
2018-05-09 16:29:04 +00:00
assigneeIn := IssueAssignees{AssigneeID: assigneeID, IssueID: issue.ID}
toBeDeleted := i < len(issue.Assignees)
2018-05-09 16:29:04 +00:00
if toBeDeleted {
issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i:]...)
2018-05-09 16:29:04 +00:00
_, err = e.Delete(assigneeIn)
if err != nil {
return toBeDeleted, err
}
} else {
issue.Assignees = append(issue.Assignees, assignee)
2018-05-09 16:29:04 +00:00
_, err = e.Insert(assigneeIn)
if err != nil {
return toBeDeleted, err
}
}
2018-05-09 16:29:04 +00:00
return toBeDeleted, nil
}
// UpdateIssueUserByRead updates issue-user relation for reading.
func UpdateIssueUserByRead(uid, issueID int64) error {
_, err := x.Exec("UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?", true, uid, issueID)
return err
}
// UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
func UpdateIssueUsersByMentions(ctx DBContext, issueID int64, uids []int64) error {
for _, uid := range uids {
iu := &IssueUser{
UID: uid,
IssueID: issueID,
}
has, err := ctx.e.Get(iu)
if err != nil {
return err
}
iu.IsMentioned = true
if has {
_, err = ctx.e.ID(iu.ID).Cols("is_mentioned").Update(iu)
} else {
_, err = ctx.e.Insert(iu)
}
if err != nil {
return err
}
}
return nil
}