From 0904e5b9b47912e1396c6d20111e15ab39255185 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Mon, 27 Mar 2017 21:15:12 -0300 Subject: [PATCH] [WIP] Add watch button on issue --- cmd/web.go | 1 + models/issue_watch.go | 51 +++++++++++++++++++ options/locale/locale_en-US.ini | 3 ++ routers/repo/issue.go | 14 +++++ routers/repo/issue_watch.go | 28 ++++++++++ .../repo/issue/view_content/sidebar.tmpl | 19 +++++++ 6 files changed, 116 insertions(+) create mode 100644 routers/repo/issue_watch.go diff --git a/cmd/web.go b/cmd/web.go index 3a20191bc..7fcbfc359 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -491,6 +491,7 @@ func runWeb(ctx *cli.Context) error { m.Group("/:index", func() { m.Post("/title", repo.UpdateIssueTitle) m.Post("/content", repo.UpdateIssueContent) + m.Post("/watch", repo.IssueWatch) m.Combo("/comments").Post(bindIgnErr(auth.CreateCommentForm{}), repo.NewComment) }) diff --git a/models/issue_watch.go b/models/issue_watch.go index 96e080136..122017858 100644 --- a/models/issue_watch.go +++ b/models/issue_watch.go @@ -18,3 +18,54 @@ type IssueWatch struct { func (iw *IssueWatch) BeforeInsert() { iw.CreatedUnix = time.Now().Unix() } + +// CreateOrUpdateIssueWatch set watching for a user and issue +func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error { + s := x.NewSession() + defer s.Close() + if err := s.Begin(); err != nil { + return err + } + + iw, exists, err := getIssueWatch(s, userID, issueID) + if err != nil { + return err + } + + if !exists { + iw = &IssueWatch{ + UserID: userID, + IssueID: issueID, + IsWatching: isWatching, + } + + if _, err := s.Insert(iw); err != nil { + return err + } + } else { + iw.IsWatching = isWatching + + if _, err := s.Id(iw.ID).Update(iw); err != nil { + return err + } + } + + if err := s.Commit(); err != nil { + return err + } + return nil +} + +// GetIssueWatch returns an issue watch by user and issue +func GetIssueWatch(userID, issueID int64) (iw *IssueWatch, exists bool, err error) { + iw, exists, err = getIssueWatch(x, userID, issueID) + return +} +func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool, err error) { + iw = new(IssueWatch) + exists, err = e. + Where("user_id = ?", userID). + And("issue_id = ?", issueID). + Get(iw) + return +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 225107596..3950c7699 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -652,6 +652,9 @@ issues.label.filter_sort.reverse_alphabetically = Reverse alphabetically issues.num_participants = %d Participants issues.attachment.open_tab = `Click to see "%s" in a new tab` issues.attachment.download = `Click to download "%s"` +issues.watch = Watch +issues.watch_issue = Watch issue +issues.unwatch_issue = Unwatch issue pulls.new = New Pull Request pulls.compare_changes = Compare Changes diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 0a723d755..17865b976 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -465,6 +465,20 @@ func ViewIssue(ctx *context.Context) { } ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title) + iw, exists, err := models.GetIssueWatch(ctx.User.ID, issue.ID) + if err != nil { + ctx.Handle(500, "GetIssueWatch", err) + return + } + if !exists { + iw = &models.IssueWatch{ + UserID: ctx.User.ID, + IssueID: issue.ID, + IsWatching: false, + } + } + ctx.Data["IssueWatch"] = iw + // Make sure type and URL matches. if ctx.Params(":type") == "issues" && issue.IsPull { ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index)) diff --git a/routers/repo/issue_watch.go b/routers/repo/issue_watch.go new file mode 100644 index 000000000..6d44779f2 --- /dev/null +++ b/routers/repo/issue_watch.go @@ -0,0 +1,28 @@ +package repo + +import ( + "fmt" + "net/http" + "strconv" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" +) + +// IssueWatch sets issue watching +func IssueWatch(c *context.Context) { + watch, err := strconv.ParseBool(c.Req.PostForm.Get("watch")) + if err != nil { + c.Handle(http.StatusInternalServerError, "watch is not bool", err) + return + } + issueID := c.ParamsInt64("index") + + if err := models.CreateOrUpdateIssueWatch(c.User.ID, issueID, watch); err != nil { + c.Handle(http.StatusInternalServerError, "CreateOrUpdateIssueWatch", err) + return + } + + url := fmt.Sprintf("%s/issues/%d", c.Repo.RepoLink, issueID) + c.Redirect(url, http.StatusSeeOther) +} diff --git a/templates/repo/issue/view_content/sidebar.tmpl b/templates/repo/issue/view_content/sidebar.tmpl index ea46e5f94..9a4a6cb1a 100644 --- a/templates/repo/issue/view_content/sidebar.tmpl +++ b/templates/repo/issue/view_content/sidebar.tmpl @@ -98,5 +98,24 @@ {{end}} + +
+ +
+ {{.i18n.Tr "repo.issues.watch"}} +
+
+ + {{$.CsrfTokenHtml}} + +
+
+