Skip to content

Commit 7ca8cbb

Browse files
committed
move Check-Logic where it belongs
1 parent 6cf71ee commit 7ca8cbb

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

models/issue_watch.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool
6464
return
6565
}
6666

67+
// CheckIssueWatch check if an user is watching an issue
68+
// it takes participants and repo watch into account
69+
func CheckIssueWatch(user *User, issue *Issue) (bool, error) {
70+
iw, exist, err := getIssueWatch(x, user.ID, issue.ID)
71+
if err != nil {
72+
return false, err
73+
}
74+
if exist {
75+
return iw.IsWatching, nil
76+
}
77+
w, err := getWatch(x, user.ID, issue.RepoID)
78+
if err != nil {
79+
return false, err
80+
}
81+
return isWatchMode(w.Mode) || IsUserParticipantsOfIssue(user, issue), nil
82+
}
83+
6784
// GetIssueWatchersIDs returns IDs of subscribers or explicit unsubscribers to a given issue id
6885
// but avoids joining with `user` for performance reasons
6986
// User permissions must be verified elsewhere if required

routers/repo/issue.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -737,21 +737,11 @@ func ViewIssue(ctx *context.Context) {
737737

738738
ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title)
739739

740-
var iw *models.IssueWatch
741-
var exists bool
742-
if ctx.User != nil {
743-
iw, exists, err = models.GetIssueWatch(ctx.User.ID, issue.ID)
744-
if err != nil {
745-
ctx.ServerError("GetIssueWatch", err)
746-
return
747-
}
748-
if !exists {
749-
iw = &models.IssueWatch{
750-
UserID: ctx.User.ID,
751-
IssueID: issue.ID,
752-
IsWatching: models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) || models.IsUserParticipantsOfIssue(ctx.User, issue),
753-
}
754-
}
740+
iw := models.IssueWatch{UserID: ctx.User.ID, IssueID: issue.ID}
741+
iw.IsWatching, err = models.CheckIssueWatch(ctx.User, issue)
742+
if err != nil {
743+
ctx.InternalServerError(err)
744+
return
755745
}
756746
ctx.Data["IssueWatch"] = iw
757747

0 commit comments

Comments
 (0)