diff options
Diffstat (limited to 'models/repo.go')
-rw-r--r-- | models/repo.go | 62 |
1 files changed, 54 insertions, 8 deletions
diff --git a/models/repo.go b/models/repo.go index c2398fe7da..bec3cb212f 100644 --- a/models/repo.go +++ b/models/repo.go @@ -137,6 +137,7 @@ type Repository struct { NumTags int `xorm:"-"` IsPrivate bool IsMirror bool + *Mirror `xorm:"-"` IsFork bool `xorm:"NOT NULL DEFAULT false"` IsBare bool IsGoget bool @@ -150,6 +151,11 @@ func (repo *Repository) GetOwner() (err error) { return err } +func (repo *Repository) GetMirror() (err error) { + repo.Mirror, err = GetMirror(repo.Id) + return err +} + // DescriptionHtml does special handles to description and return HTML string. func (repo *Repository) DescriptionHtml() template.HTML { sanitize := func(s string) string { @@ -953,21 +959,33 @@ type Watch struct { } // Watch or unwatch repository. -func WatchRepo(uid, rid int64, watch bool) (err error) { +func WatchRepo(uid, repoId int64, watch bool) (err error) { if watch { - if _, err = x.Insert(&Watch{RepoId: rid, UserId: uid}); err != nil { + if IsWatching(uid, repoId) { + return nil + } + if _, err = x.Insert(&Watch{RepoId: repoId, UserId: uid}); err != nil { return err } - _, err = x.Exec("UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?", rid) + _, err = x.Exec("UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?", repoId) } else { - if _, err = x.Delete(&Watch{0, uid, rid}); err != nil { + if !IsWatching(uid, repoId) { + return nil + } + if _, err = x.Delete(&Watch{0, uid, repoId}); err != nil { return err } - _, err = x.Exec("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?", rid) + _, err = x.Exec("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?", repoId) } return err } +// IsWatching checks if user has watched given repository. +func IsWatching(uid, rid int64) bool { + has, _ := x.Get(&Watch{0, uid, rid}) + return has +} + // GetWatchers returns all watchers of given repository. func GetWatchers(rid int64) ([]*Watch, error) { watches := make([]*Watch, 0, 10) @@ -1003,9 +1021,37 @@ func NotifyWatchers(act *Action) error { return nil } -// IsWatching checks if user has watched given repository. -func IsWatching(uid, rid int64) bool { - has, _ := x.Get(&Watch{0, uid, rid}) +type Star struct { + Id int64 + Uid int64 `xorm:"UNIQUE(s)"` + RepoId int64 `xorm:"UNIQUE(s)"` +} + +// Star or unstar repository. +func StarRepo(uid, repoId int64, star bool) (err error) { + if star { + if IsStaring(uid, repoId) { + return nil + } + if _, err = x.Insert(&Star{Uid: uid, RepoId: repoId}); err != nil { + return err + } + _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoId) + } else { + if !IsStaring(uid, repoId) { + return nil + } + if _, err = x.Delete(&Star{0, uid, repoId}); err != nil { + return err + } + _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoId) + } + return err +} + +// IsStaring checks if user has starred given repository. +func IsStaring(uid, repoId int64) bool { + has, _ := x.Get(&Star{0, uid, repoId}) return has } |