2017-02-09 08:44:18 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2017-02-04 15:37:26 +03:00
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"fmt"
2021-09-19 14:49:59 +03:00
"code.gitea.io/gitea/models/db"
2017-02-04 15:37:26 +03: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 14:49:59 +03:00
func init ( ) {
db . RegisterModel ( new ( IssueUser ) )
}
func newIssueUsers ( e db . Engine , repo * Repository , issue * Issue ) error {
2017-02-04 15:37:26 +03:00
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 19:29:04 +03:00
IssueID : issue . ID ,
UID : assignee . ID ,
2017-02-04 15:37:26 +03:00
} )
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
}
// UpdateIssueUserByRead updates issue-user relation for reading.
func UpdateIssueUserByRead ( uid , issueID int64 ) error {
2021-09-19 14:49:59 +03:00
_ , err := db . DefaultContext ( ) . Engine ( ) . Exec ( "UPDATE `issue_user` SET is_read=? WHERE uid=? AND issue_id=?" , true , uid , issueID )
2017-02-04 15:37:26 +03:00
return err
}
// UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
2021-09-19 14:49:59 +03:00
func UpdateIssueUsersByMentions ( ctx * db . Context , issueID int64 , uids [ ] int64 ) error {
2017-02-04 15:37:26 +03:00
for _ , uid := range uids {
iu := & IssueUser {
UID : uid ,
IssueID : issueID ,
}
2021-09-19 14:49:59 +03:00
has , err := ctx . Engine ( ) . Get ( iu )
2017-02-04 15:37:26 +03:00
if err != nil {
return err
}
iu . IsMentioned = true
if has {
2021-09-19 14:49:59 +03:00
_ , err = ctx . Engine ( ) . ID ( iu . ID ) . Cols ( "is_mentioned" ) . Update ( iu )
2017-02-04 15:37:26 +03:00
} else {
2021-09-19 14:49:59 +03:00
_ , err = ctx . Engine ( ) . Insert ( iu )
2017-02-04 15:37:26 +03:00
}
if err != nil {
return err
}
}
return nil
}