From a922c3ff6a65d6d0550f36d866a301a6737ca8a2 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 19 Mar 2014 23:20:55 -0400 Subject: [PATCH] Watch backend --- models/models.go | 3 ++- models/repo.go | 19 ++++++++++++++----- routers/repo/single.go | 22 ++++++++++++++++++++++ routers/user/setting.go | 2 -- templates/repo/commits.tmpl | 2 +- templates/user/profile.tmpl | 4 ++-- web.go | 5 +++-- 7 files changed, 44 insertions(+), 13 deletions(-) diff --git a/models/models.go b/models/models.go index d6a6200eb..2e0bb759d 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 fce7d7f53..187862fe2 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 3d0447edd..141a6cdae 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 719e0c191..053f327f0 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 8b4e41f74..2e67a1200 100644 --- a/templates/repo/commits.tmpl +++ b/templates/repo/commits.tmpl @@ -26,7 +26,7 @@ {{$r := List .Commits}} {{range $r}} - {{.Committer.Name}} + {{.Committer.Name}} {{SubStr .Id.String 0 7}} {{.Message}} {{TimeSince .Committer.When}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index 8dca29ffe..2d76d9bf0 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -3,8 +3,8 @@
- - user-avatar + + user-avatar {{.Owner.Name}}
diff --git a/web.go b/web.go index 2a9df17c8..f793518a1 100644 --- a/web.go +++ b/web.go @@ -73,8 +73,8 @@ func runWeb(*cli.Context) { m.Use(middleware.InitContext()) - ignSignIn := middleware.SignInRequire(false) - reqSignIn, reqSignOut := middleware.SignInRequire(true), middleware.SignOutRequire() + reqSignIn, ignSignIn := middleware.SignInRequire(true), middleware.SignInRequire(false) + reqSignOut := middleware.SignOutRequire() // Routers. m.Get("/", ignSignIn, routers.Home) m.Get("/issues", reqSignIn, user.Issues) @@ -106,6 +106,7 @@ func runWeb(*cli.Context) { m.Get("/:username/:reponame/issues", ignSignIn, middleware.RepoAssignment(true), repo.Issues) m.Get("/:username/:reponame/pulls", ignSignIn, middleware.RepoAssignment(true), repo.Pulls) m.Get("/:username/:reponame/branches", ignSignIn, middleware.RepoAssignment(true), repo.Branches) + m.Get("/:username/:reponame/action/:action", reqSignIn, middleware.RepoAssignment(true), repo.Action) m.Get("/:username/:reponame/tree/:branchname/**", ignSignIn, middleware.RepoAssignment(true), repo.Single) m.Get("/:username/:reponame/tree/:branchname",