mirror of
https://gitcode.com/gitea/gitea.git
synced 2025-07-05 20:28:37 +08:00
.github
assets
build
cmd
contrib
custom
docker
docs
integrations
models
fixtures
migrations
access.go
access_test.go
action.go
action_list.go
action_test.go
admin.go
admin_test.go
attachment.go
attachment_test.go
avatar.go
branches.go
branches_test.go
commit_status.go
commit_status_test.go
consistency.go
context.go
convert.go
error.go
error_oauth2.go
external_login_user.go
fixture_generation.go
fixture_test.go
gpg_key.go
gpg_key_test.go
helper.go
helper_directory.go
helper_environment.go
issue.go
issue_assignees.go
issue_assignees_test.go
issue_comment.go
issue_comment_list.go
issue_comment_test.go
issue_dependency.go
issue_dependency_test.go
issue_label.go
issue_label_test.go
issue_list.go
issue_list_test.go
issue_lock.go
issue_milestone.go
issue_milestone_test.go
issue_reaction.go
issue_reaction_test.go
issue_stopwatch.go
issue_stopwatch_test.go
issue_test.go
issue_tracked_time.go
issue_tracked_time_test.go
issue_user.go
issue_user_test.go
issue_watch.go
issue_watch_test.go
issue_xref.go
issue_xref_test.go
lfs.go
lfs_lock.go
list_options.go
log.go
login_source.go
main_test.go
migrate.go
models.go
models_test.go
notification.go
notification_test.go
oauth2.go
oauth2_application.go
oauth2_application_test.go
org.go
org_team.go
org_team_test.go
org_test.go
project.go
project_board.go
project_issue.go
project_test.go
pull.go
pull_list.go
pull_sign.go
pull_test.go
release.go
repo.go
repo_activity.go
repo_avatar.go
repo_branch.go
repo_collaboration.go
repo_collaboration_test.go
repo_generate.go
repo_generate_test.go
repo_indexer.go
repo_issue.go
repo_language_stats.go
repo_list.go
repo_list_test.go
repo_mirror.go
repo_permission.go
repo_permission_test.go
repo_redirect.go
repo_redirect_test.go
repo_sign.go
repo_test.go
repo_unit.go
repo_watch.go
repo_watch_test.go
review.go
review_test.go
ssh_key.go
ssh_key_test.go
star.go
star_test.go
task.go
test_fixtures.go
token.go
token_test.go
topic.go
topic_test.go
twofactor.go
u2f.go
u2f_test.go
unit.go
unit_tests.go
update.go
upload.go
user.go
user_avatar.go
user_follow.go
user_follow_test.go
user_heatmap.go
user_heatmap_test.go
user_mail.go
user_mail_test.go
user_openid.go
user_openid_test.go
user_test.go
userlist.go
userlist_test.go
webhook.go
webhook_test.go
wiki.go
wiki_test.go
modules
options
public
routers
services
snap
templates
tools
vendor
web_src
.air.conf
.changelog.yml
.drone.yml
.editorconfig
.eslintrc
.gitattributes
.gitignore
.golangci.yml
.ignore
.lgtm
.npmrc
.revive.toml
.stylelintrc
BSDmakefile
CHANGELOG.md
CONTRIBUTING.md
DCO
Dockerfile
Dockerfile.rootless
LICENSE
MAINTAINERS
Makefile
README.md
README_ZH.md
SECURITY.md
build.go
go.mod
go.sum
main.go
package-lock.json
package.json
semantic.json
webpack.config.js

close #10962 Adds `GET /api/v1/repos/{owner}/{repo}/issues/{index}/subscriptions/check` -> return a `WachInfo`
129 lines
3.9 KiB
Go
129 lines
3.9 KiB
Go
// 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 (
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
)
|
|
|
|
// IssueWatch is connection request for receiving issue notification.
|
|
type IssueWatch struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
UserID int64 `xorm:"UNIQUE(watch) NOT NULL"`
|
|
IssueID int64 `xorm:"UNIQUE(watch) NOT NULL"`
|
|
IsWatching bool `xorm:"NOT NULL"`
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"updated NOT NULL"`
|
|
}
|
|
|
|
// IssueWatchList contains IssueWatch
|
|
type IssueWatchList []*IssueWatch
|
|
|
|
// CreateOrUpdateIssueWatch set watching for a user and issue
|
|
func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error {
|
|
iw, exists, err := getIssueWatch(x, userID, issueID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !exists {
|
|
iw = &IssueWatch{
|
|
UserID: userID,
|
|
IssueID: issueID,
|
|
IsWatching: isWatching,
|
|
}
|
|
|
|
if _, err := x.Insert(iw); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
iw.IsWatching = isWatching
|
|
|
|
if _, err := x.ID(iw.ID).Cols("is_watching", "updated_unix").Update(iw); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetIssueWatch returns all IssueWatch objects from db by user and issue
|
|
// the current Web-UI need iw object for watchers AND explicit non-watchers
|
|
func GetIssueWatch(userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
|
|
return getIssueWatch(x, userID, issueID)
|
|
}
|
|
|
|
// Return watcher AND explicit non-watcher if entry in db exist
|
|
func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
|
|
iw = new(IssueWatch)
|
|
exists, err = e.
|
|
Where("user_id = ?", userID).
|
|
And("issue_id = ?", issueID).
|
|
Get(iw)
|
|
return
|
|
}
|
|
|
|
// CheckIssueWatch check if an user is watching an issue
|
|
// it takes participants and repo watch into account
|
|
func CheckIssueWatch(user *User, issue *Issue) (bool, error) {
|
|
iw, exist, err := getIssueWatch(x, user.ID, issue.ID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if exist {
|
|
return iw.IsWatching, nil
|
|
}
|
|
w, err := getWatch(x, user.ID, issue.RepoID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return isWatchMode(w.Mode) || IsUserParticipantsOfIssue(user, issue), nil
|
|
}
|
|
|
|
// GetIssueWatchersIDs returns IDs of subscribers or explicit unsubscribers to a given issue id
|
|
// but avoids joining with `user` for performance reasons
|
|
// User permissions must be verified elsewhere if required
|
|
func GetIssueWatchersIDs(issueID int64, watching bool) ([]int64, error) {
|
|
return getIssueWatchersIDs(x, issueID, watching)
|
|
}
|
|
|
|
func getIssueWatchersIDs(e Engine, issueID int64, watching bool) ([]int64, error) {
|
|
ids := make([]int64, 0, 64)
|
|
return ids, e.Table("issue_watch").
|
|
Where("issue_id=?", issueID).
|
|
And("is_watching = ?", watching).
|
|
Select("user_id").
|
|
Find(&ids)
|
|
}
|
|
|
|
// GetIssueWatchers returns watchers/unwatchers of a given issue
|
|
func GetIssueWatchers(issueID int64, listOptions ListOptions) (IssueWatchList, error) {
|
|
return getIssueWatchers(x, issueID, listOptions)
|
|
}
|
|
|
|
func getIssueWatchers(e Engine, issueID int64, listOptions ListOptions) (IssueWatchList, error) {
|
|
sess := e.
|
|
Where("`issue_watch`.issue_id = ?", issueID).
|
|
And("`issue_watch`.is_watching = ?", true).
|
|
And("`user`.is_active = ?", true).
|
|
And("`user`.prohibit_login = ?", false).
|
|
Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id")
|
|
|
|
if listOptions.Page != 0 {
|
|
sess = listOptions.setSessionPagination(sess)
|
|
watches := make([]*IssueWatch, 0, listOptions.PageSize)
|
|
return watches, sess.Find(&watches)
|
|
}
|
|
watches := make([]*IssueWatch, 0, 8)
|
|
return watches, sess.Find(&watches)
|
|
}
|
|
|
|
func removeIssueWatchersByRepoID(e Engine, userID int64, repoID int64) error {
|
|
_, err := e.
|
|
Join("INNER", "issue", "`issue`.id = `issue_watch`.issue_id AND `issue`.repo_id = ?", repoID).
|
|
Where("`issue_watch`.user_id = ?", userID).
|
|
Delete(new(IssueWatch))
|
|
return err
|
|
}
|