2017-02-09 11:14:18 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2017-02-04 18:07:26 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
package issues
|
2017-02-04 18:07:26 +05:30
|
|
|
|
|
|
|
import (
|
2021-09-23 21:15:36 +05:30
|
|
|
"context"
|
2017-02-04 18:07:26 +05:30
|
|
|
"fmt"
|
2021-09-19 17:19:59 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2017-02-04 18:07:26 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// 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 17:19:59 +05:30
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(IssueUser))
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
// NewIssueUsers inserts an issue related users
|
|
|
|
func NewIssueUsers(ctx context.Context, repo *repo_model.Repository, issue *Issue) error {
|
2022-06-06 13:31:49 +05:30
|
|
|
assignees, err := repo_model.GetRepoAssignees(ctx, repo)
|
2017-02-04 18:07:26 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("getAssignees: %w", err)
|
2017-02-04 18:07:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 21:59:04 +05:30
|
|
|
IssueID: issue.ID,
|
|
|
|
UID: assignee.ID,
|
2017-02-04 18:07:26 +05:30
|
|
|
})
|
|
|
|
isPosterAssignee = isPosterAssignee || assignee.ID == issue.PosterID
|
|
|
|
}
|
|
|
|
if !isPosterAssignee {
|
|
|
|
issueUsers = append(issueUsers, &IssueUser{
|
|
|
|
IssueID: issue.ID,
|
|
|
|
UID: issue.PosterID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
return db.Insert(ctx, issueUsers)
|
2017-02-04 18:07:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueUserByRead updates issue-user relation for reading.
|
2023-08-05 08:10:27 +05:30
|
|
|
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 18:07:26 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateIssueUsersByMentions updates issue-user pairs by mentioning.
|
2021-09-23 21:15:36 +05:30
|
|
|
func UpdateIssueUsersByMentions(ctx context.Context, issueID int64, uids []int64) error {
|
2017-02-04 18:07:26 +05:30
|
|
|
for _, uid := range uids {
|
|
|
|
iu := &IssueUser{
|
|
|
|
UID: uid,
|
|
|
|
IssueID: issueID,
|
|
|
|
}
|
2021-09-23 21:15:36 +05:30
|
|
|
has, err := db.GetEngine(ctx).Get(iu)
|
2017-02-04 18:07:26 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
iu.IsMentioned = true
|
|
|
|
if has {
|
2021-09-23 21:15:36 +05:30
|
|
|
_, err = db.GetEngine(ctx).ID(iu.ID).Cols("is_mentioned").Update(iu)
|
2017-02-04 18:07:26 +05:30
|
|
|
} else {
|
2021-09-23 21:15:36 +05:30
|
|
|
_, err = db.GetEngine(ctx).Insert(iu)
|
2017-02-04 18:07:26 +05:30
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
Refactor and enhance issue indexer to support both searching, filtering and paging (#26012)
Fix #24662.
Replace #24822 and #25708 (although it has been merged)
## Background
In the past, Gitea supported issue searching with a keyword and
conditions in a less efficient way. It worked by searching for issues
with the keyword and obtaining limited IDs (as it is heavy to get all)
on the indexer (bleve/elasticsearch/meilisearch), and then querying with
conditions on the database to find a subset of the found IDs. This is
why the results could be incomplete.
To solve this issue, we need to store all fields that could be used as
conditions in the indexer and support both keyword and additional
conditions when searching with the indexer.
## Major changes
- Redefine `IndexerData` to include all fields that could be used as
filter conditions.
- Refactor `Search(ctx context.Context, kw string, repoIDs []int64,
limit, start int, state string)` to `Search(ctx context.Context, options
*SearchOptions)`, so it supports more conditions now.
- Change the data type stored in `issueIndexerQueue`. Use
`IndexerMetadata` instead of `IndexerData` in case the data has been
updated while it is in the queue. This also reduces the storage size of
the queue.
- Enhance searching with Bleve/Elasticsearch/Meilisearch, make them
fully support `SearchOptions`. Also, update the data versions.
- Keep most logic of database indexer, but remove
`issues.SearchIssueIDsByKeyword` in `models` to avoid confusion where is
the entry point to search issues.
- Start a Meilisearch instance to test it in unit tests.
- Add unit tests with almost full coverage to test
Bleve/Elasticsearch/Meilisearch indexer.
---------
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
2023-07-31 11:58:53 +05:30
|
|
|
|
|
|
|
// GetIssueMentionIDs returns all mentioned user IDs of an issue.
|
|
|
|
func GetIssueMentionIDs(ctx context.Context, issueID int64) ([]int64, error) {
|
|
|
|
var ids []int64
|
|
|
|
return ids, db.GetEngine(ctx).Table(IssueUser{}).
|
|
|
|
Where("issue_id=?", issueID).
|
|
|
|
And("is_mentioned=?", true).
|
|
|
|
Select("uid").
|
|
|
|
Find(&ids)
|
|
|
|
}
|