diff options
author | Ethan Koenig <etk39@cornell.edu> | 2017-02-17 03:02:11 -0500 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-02-17 16:02:11 +0800 |
commit | be48b32e639eacc88e075aae2a4b55b95745d685 (patch) | |
tree | 29249e2de453ffc1edc95e962a8b5bc11c2ff516 /models/repo.go | |
parent | 847527fd6dd8e53eea918e6e41da6ebc64be1388 (diff) | |
download | gitea-be48b32e639eacc88e075aae2a4b55b95745d685.tar.gz gitea-be48b32e639eacc88e075aae2a4b55b95745d685.zip |
Unit tests for repo watching (#963)
Diffstat (limited to 'models/repo.go')
-rw-r--r-- | models/repo.go | 103 |
1 files changed, 0 insertions, 103 deletions
diff --git a/models/repo.go b/models/repo.go index 13ff6dd4f7..490c8a3274 100644 --- a/models/repo.go +++ b/models/repo.go @@ -2256,109 +2256,6 @@ func (repos MirrorRepositoryList) LoadAttributes() error { return repos.loadAttributes(x) } -// __ __ __ .__ -// / \ / \_____ _/ |_ ____ | |__ -// \ \/\/ /\__ \\ __\/ ___\| | \ -// \ / / __ \| | \ \___| Y \ -// \__/\ / (____ /__| \___ >___| / -// \/ \/ \/ \/ - -// 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)"` -} - -func isWatching(e Engine, userID, repoID int64) bool { - has, _ := e.Get(&Watch{0, userID, repoID}) - return has -} - -// IsWatching checks if user has watched given repository. -func IsWatching(userID, repoID int64) bool { - return isWatching(x, userID, repoID) -} - -func watchRepo(e Engine, userID, repoID int64, watch bool) (err error) { - if watch { - if isWatching(e, userID, repoID) { - return nil - } - if _, err = e.Insert(&Watch{RepoID: repoID, UserID: userID}); err != nil { - return err - } - _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?", repoID) - } else { - if !isWatching(e, userID, repoID) { - return nil - } - if _, err = e.Delete(&Watch{0, userID, repoID}); err != nil { - return err - } - _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?", repoID) - } - 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.Find(&watches, &Watch{RepoID: repoID}) -} - -// 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") - 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) - } - - for i := range watches { - if act.ActUserID == watches[i].UserID { - continue - } - - act.ID = 0 - act.UserID = watches[i].UserID - 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) -} - // ___________ __ // \_ _____/__________| | __ // | __)/ _ \_ __ \ |/ / |