diff --git a/models/models.go b/models/models.go index d6a6200eb3..2e0bb759d4 100644 --- a/models/models.go +++ b/models/models.go @@ -88,7 +88,8 @@ func setEngine() { func init() { setEngine() - if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Access), new(Action)); err != nil { + if err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Access), + new(Action), new(Watch)); err != nil { fmt.Printf("sync database struct error: %v\n", err) os.Exit(2) } diff --git a/models/repo.go b/models/repo.go index fce7d7f530..187862fe27 100644 --- a/models/repo.go +++ b/models/repo.go @@ -43,11 +43,20 @@ type Repository struct { Updated time.Time `xorm:"updated"` } -type Star struct { - Id int64 - RepoId int64 - UserId int64 - Created time.Time `xorm:"created"` +// Watch is connection request for receiving repository notifycation. +type Watch struct { + Id int64 + RepoId int64 `xorm:"UNIQUE(watch)"` + UserId int64 `xorm:"UNIQUE(watch)"` +} + +func WatchRepo(userId, repoId int64, watch bool) (err error) { + if watch { + _, err = orm.Insert(&Watch{RepoId: repoId, UserId: userId}) + } else { + _, err = orm.Delete(&Watch{0, repoId, userId}) + } + return err } var ( diff --git a/routers/repo/single.go b/routers/repo/single.go index 3d0447eddd..141a6cdae2 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -208,3 +208,25 @@ func Pulls(ctx *middleware.Context) { ctx.Data["IsRepoToolbarPulls"] = true ctx.HTML(200, "repo/pulls", ctx.Data) } + +func Action(ctx *middleware.Context, params martini.Params) { + var err error + switch params["action"] { + case "watch": + err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true) + case "unwatch": + err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false) + } + + if err != nil { + log.Error("repo.Action(%s): %v", params["action"], err) + ctx.JSON(200, map[string]interface{}{ + "ok": false, + "err": err.Error(), + }) + return + } + ctx.JSON(200, map[string]interface{}{ + "ok": true, + }) +} diff --git a/routers/user/setting.go b/routers/user/setting.go index 719e0c1919..053f327f0f 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -93,7 +93,6 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" { id, err := strconv.ParseInt(ctx.Query("id"), 10, 64) if err != nil { - ctx.Data["ErrorMsg"] = err log.Error("ssh.DelPublicKey: %v", err) ctx.JSON(200, map[string]interface{}{ "ok": false, @@ -107,7 +106,6 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { } if err = models.DeletePublicKey(k); err != nil { - ctx.Data["ErrorMsg"] = err log.Error("ssh.DelPublicKey: %v", err) ctx.JSON(200, map[string]interface{}{ "ok": false, diff --git a/templates/repo/commits.tmpl b/templates/repo/commits.tmpl index 8b4e41f748..2e67a1200a 100644 --- a/templates/repo/commits.tmpl +++ b/templates/repo/commits.tmpl @@ -26,7 +26,7 @@ {{$r := List .Commits}} {{range $r}}