@@ -24,6 +24,14 @@ const (
RepoWatchModeAuto // 3
)
// NotifyType specifies what kind of watch the user has on a repository
type NotifyType int8
const (
RejectAllNotification NotifyType = 0
ReceiveAllNotification NotifyType = 9
)
var ActionChan = make(chan *Action, 200)
var ActionChan4Task = make(chan Action, 200)
@@ -34,6 +42,7 @@ type Watch struct {
RepoID int64 `xorm:"UNIQUE(watch)"`
Mode RepoWatchMode `xorm:"SMALLINT NOT NULL DEFAULT 1"`
CreatedUnix int64 `xorm:"created"`
NotifyType NotifyType `xorm:"SMALLINT NOT NULL DEFAULT 0"`
}
// getWatch gets what kind of subscription a user has on a given repository; returns dummy record if none found
@@ -60,8 +69,20 @@ func IsWatching(userID, repoID int64) bool {
return err == nil && isWatchMode(watch.Mode)
}
// GetWatchNotifyType
func GetWatchNotifyType(userID, repoID int64) NotifyType {
watch, err := getWatch(x, userID, repoID)
if err != nil {
return RejectAllNotification
}
return watch.NotifyType
}
func watchRepoMode(e Engine, watch Watch, mode RepoWatchMode) (err error) {
if watch.Mode == mode {
if _, err := e.ID(watch.ID).Cols("notify_type").Update(watch); err != nil {
return err
}
return nil
}
if mode == RepoWatchModeAuto && (watch.Mode == RepoWatchModeDont || isWatchMode(watch.Mode)) {
@@ -109,7 +130,7 @@ func WatchRepoMode(userID, repoID int64, mode RepoWatchMode) (err error) {
return watchRepoMode(x, watch, mode)
}
func watchRepo(e Engine, userID, repoID int64, doWatch bool) (err error) {
func watchRepo(e Engine, userID, repoID int64, doWatch bool, notifyTypes ...NotifyType ) (err error) {
var watch Watch
if watch, err = getWatch(e, userID, repoID); err != nil {
return err
@@ -119,14 +140,19 @@ func watchRepo(e Engine, userID, repoID int64, doWatch bool) (err error) {
} else if !doWatch {
err = watchRepoMode(e, watch, RepoWatchModeNone)
} else {
notifyType := RejectAllNotification
if len(notifyTypes) > 0 {
notifyType = notifyTypes[0]
}
watch.NotifyType = notifyType
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 WatchRepo(userID, repoID int64, watch bool, notifyType ...NotifyType ) (err error) {
return watchRepo(x, userID, repoID, watch, notifyType... )
}
func getWatchers(e Engine, repoID int64) ([]*Watch, error) {
@@ -156,6 +182,7 @@ func getRepoWatchersIDs(e Engine, repoID int64) ([]int64, error) {
return ids, e.Table("watch").
Where("watch.repo_id=?", repoID).
And("watch.mode<>?", RepoWatchModeDont).
And("watch.notify_type > ?", RejectAllNotification).
Select("user_id").
Find(&ids)
}