summaryrefslogtreecommitdiffstats
path: root/models/repo_watch.go
blob: cb864fb46dab0e86e53c8b88080f5beb682f9d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// 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"

	"code.gitea.io/gitea/modules/setting"
)

// RepoWatchMode specifies what kind of watch the user has on a repository
type RepoWatchMode int8

const (
	// RepoWatchModeNone don't watch
	RepoWatchModeNone RepoWatchMode = iota // 0
	// RepoWatchModeNormal watch repository (from other sources)
	RepoWatchModeNormal // 1
	// RepoWatchModeDont explicit don't auto-watch
	RepoWatchModeDont // 2
	// RepoWatchModeAuto watch repository (from AutoWatchOnChanges)
	RepoWatchModeAuto // 3
)

// Watch is connection request for receiving repository notification.
type Watch struct {
	ID     int64         `xorm:"pk autoincr"`
	UserID int64         `xorm:"UNIQUE(watch)"`
	RepoID int64         `xorm:"UNIQUE(watch)"`
	Mode   RepoWatchMode `xorm:"SMALLINT NOT NULL DEFAULT 1"`
}

// getWatch gets what kind of subscription a user has on a given repository; returns dummy record if none found
func getWatch(e Engine, userID, repoID int64) (Watch, error) {
	watch := Watch{UserID: userID, RepoID: repoID}
	has, err := e.Get(&watch)
	if err != nil {
		return watch, err
	}
	if !has {
		watch.Mode = RepoWatchModeNone
	}
	return watch, nil
}

// Decodes watchability of RepoWatchMode
func isWatchMode(mode RepoWatchMode) bool {
	return mode != RepoWatchModeNone && mode != RepoWatchModeDont
}

// IsWatching checks if user has watched given repository.
func IsWatching(userID, repoID int64) bool {
	watch, err := getWatch(x, userID, repoID)
	return err == nil && isWatchMode(watch.Mode)
}

func watchRepoMode(e Engine, watch Watch, mode RepoWatchMode) (err error) {
	if watch.Mode == mode {
		return nil
	}
	if mode == RepoWatchModeAuto && (watch.Mode == RepoWatchModeDont || isWatchMode(watch.Mode)) {
		// Don't auto watch if already watching or deliberately not watching
		return nil
	}

	hadrec := watch.Mode != RepoWatchModeNone
	needsrec := mode != RepoWatchModeNone
	repodiff := 0

	if isWatchMode(mode) && !isWatchMode(watch.Mode) {
		repodiff = 1
	} else if !isWatchMode(mode) && isWatchMode(watch.Mode) {
		repodiff = -1
	}

	watch.Mode = mode

	if !hadrec && needsrec {
		watch.Mode = mode
		if _, err = e.Insert(watch); err != nil {
			return err
		}
	} else if needsrec {
		watch.Mode = mode
		if _, err := e.ID(watch.ID).AllCols().Update(watch); err != nil {
			return err
		}
	} else if _, err = e.Delete(Watch{ID: watch.ID}); err != nil {
		return err
	}
	if repodiff != 0 {
		_, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + ? WHERE id = ?", repodiff, watch.RepoID)
	}
	return err
}

// WatchRepoMode watch repository in specific mode.
func WatchRepoMode(userID, repoID int64, mode RepoWatchMode) (err error) {
	var watch Watch
	if watch, err = getWatch(x, userID, repoID); err != nil {
		return err
	}
	return watchRepoMode(x, watch, mode)
}

func watchRepo(e Engine, userID, repoID int64, doWatch bool) (err error) {
	var watch Watch
	if watch, err = getWatch(e, userID, repoID); err != nil {
		return err
	}
	if !doWatch && watch.Mode == RepoWatchModeAuto {
		err = watchRepoMode(e, watch, RepoWatchModeDont)
	} else if !doWatch {
		err = watchRepoMode(e, watch, RepoWatchModeNone)
	} else {
		err = watchRepoMode(e, watch, RepoWatchModeNormal)
	}
	return err
}

// WatchRepo watch or unwatch repository.
func WatchRepo(userID, repoID int64, watch bool) (err error) {
	return watchRepo(x, userID, repoID, watch)
}

func getWatchers(e Engine, repoID int64) ([]*Watch, error) {
	watches := make([]*Watch, 0, 10)
	return watches, e.Where("`watch`.repo_id=?", repoID).
		And("`watch`.mode<>?", RepoWatchModeDont).
		And("`user`.is_active=?", true).
		And("`user`.prohibit_login=?", false).
		Join("INNER", "`user`", "`user`.id = `watch`.user_id").
		Find(&watches)
}

// GetWatchers returns all watchers of given repository.
func GetWatchers(repoID int64) ([]*Watch, error) {
	return getWatchers(x, repoID)
}

// GetWatchers returns range of users watching given repository.
func (repo *Repository) GetWatchers(page int) ([]*User, error) {
	users := make([]*User, 0, ItemsPerPage)
	sess := x.Where("watch.repo_id=?", repo.ID).
		Join("LEFT", "watch", "`user`.id=`watch`.user_id").
		And("`watch`.mode<>?", RepoWatchModeDont)
	if page > 0 {
		sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
	}
	return users, sess.Find(&users)
}

func notifyWatchers(e Engine, act *Action) error {
	// Add feeds for user self and all watchers.
	watches, err := getWatchers(e, act.RepoID)
	if err != nil {
		return fmt.Errorf("get watchers: %v", err)
	}

	// Add feed for actioner.
	act.UserID = act.ActUserID
	if _, err = e.InsertOne(act); err != nil {
		return fmt.Errorf("insert new actioner: %v", err)
	}

	act.loadRepo()
	// check repo owner exist.
	if err := act.Repo.getOwner(e); err != nil {
		return fmt.Errorf("can't get repo owner: %v", err)
	}

	// Add feed for organization
	if act.Repo.Owner.IsOrganization() && act.ActUserID != act.Repo.Owner.ID {
		act.ID = 0
		act.UserID = act.Repo.Owner.ID
		if _, err = e.InsertOne(act); err != nil {
			return fmt.Errorf("insert new actioner: %v", err)
		}
	}

	for i := range watches {
		if act.ActUserID == watches[i].UserID {
			continue
		}

		act.ID = 0
		act.UserID = watches[i].UserID
		act.Repo.Units = nil

		switch act.OpType {
		case ActionCommitRepo, ActionPushTag, ActionDeleteTag, ActionDeleteBranch:
			if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeCode) {
				continue
			}
		case ActionCreateIssue, ActionCommentIssue, ActionCloseIssue, ActionReopenIssue:
			if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeIssues) {
				continue
			}
		case ActionCreatePullRequest, ActionMergePullRequest, ActionClosePullRequest, ActionReopenPullRequest:
			if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypePullRequests) {
				continue
			}
		}

		if _, err = e.InsertOne(act); err != nil {
			return fmt.Errorf("insert new action: %v", err)
		}
	}
	return nil
}

// NotifyWatchers creates batch of actions for every watcher.
func NotifyWatchers(act *Action) error {
	return notifyWatchers(x, act)
}

func watchIfAuto(e Engine, userID, repoID int64, isWrite bool) error {
	if !isWrite || !setting.Service.AutoWatchOnChanges {
		return nil
	}
	watch, err := getWatch(e, userID, repoID)
	if err != nil {
		return err
	}
	if watch.Mode != RepoWatchModeNone {
		return nil
	}
	return watchRepoMode(e, watch, RepoWatchModeAuto)
}

// WatchIfAuto subscribes to repo if AutoWatchOnChanges is set
func WatchIfAuto(userID int64, repoID int64, isWrite bool) error {
	return watchIfAuto(x, userID, repoID, isWrite)
}