From 59ea3c0413723521e7504fa5b1be2c3780cbb2ea Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 19 Mar 2014 23:39:00 -0400 Subject: [PATCH] Finish watch backend --- models/action.go | 31 +++++++++++++++++++++---------- models/repo.go | 8 ++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/models/action.go b/models/action.go index d388bca9..b3be0935 100644 --- a/models/action.go +++ b/models/action.go @@ -55,16 +55,27 @@ func CommitRepoAction(userId int64, userName string, if err != nil { return err } - _, err = orm.InsertOne(&Action{ - UserId: userId, - ActUserId: userId, - ActUserName: userName, - OpType: OP_COMMIT_REPO, - Content: string(bs), - RepoId: repoId, - RepoName: repoName, - }) - return err + + // Add feeds for user self and all watchers. + watches, err := GetWatches(repoId) + if err != nil { + return err + } + watches = append(watches, Watch{UserId: userId}) + + for i := range watches { + _, err = orm.InsertOne(&Action{ + UserId: watches[i].UserId, + ActUserId: userId, + ActUserName: userName, + OpType: OP_COMMIT_REPO, + Content: string(bs), + RepoId: repoId, + RepoName: repoName, + }) + return err + } + return nil } // NewRepoAction records action for create repository. diff --git a/models/repo.go b/models/repo.go index 187862fe..fa6d9702 100644 --- a/models/repo.go +++ b/models/repo.go @@ -50,6 +50,7 @@ type Watch struct { UserId int64 `xorm:"UNIQUE(watch)"` } +// Watch or unwatch repository. func WatchRepo(userId, repoId int64, watch bool) (err error) { if watch { _, err = orm.Insert(&Watch{RepoId: repoId, UserId: userId}) @@ -59,6 +60,13 @@ func WatchRepo(userId, repoId int64, watch bool) (err error) { return err } +// GetWatches returns all watches of given repository. +func GetWatches(repoId int64) ([]Watch, error) { + watches := make([]Watch, 0, 10) + err := orm.Find(&watches, &Watch{RepoId: repoId}) + return watches, err +} + var ( gitInitLocker = sync.Mutex{} LanguageIgns, Licenses []string