[WIP] Add watch button on issue

This commit is contained in:
Andrey Nering 2017-03-27 21:15:12 -03:00
parent 9c267a071a
commit 0904e5b9b4
6 changed files with 116 additions and 0 deletions

View File

@ -491,6 +491,7 @@ func runWeb(ctx *cli.Context) error {
m.Group("/:index", func() { m.Group("/:index", func() {
m.Post("/title", repo.UpdateIssueTitle) m.Post("/title", repo.UpdateIssueTitle)
m.Post("/content", repo.UpdateIssueContent) m.Post("/content", repo.UpdateIssueContent)
m.Post("/watch", repo.IssueWatch)
m.Combo("/comments").Post(bindIgnErr(auth.CreateCommentForm{}), repo.NewComment) m.Combo("/comments").Post(bindIgnErr(auth.CreateCommentForm{}), repo.NewComment)
}) })

View File

@ -18,3 +18,54 @@ type IssueWatch struct {
func (iw *IssueWatch) BeforeInsert() { func (iw *IssueWatch) BeforeInsert() {
iw.CreatedUnix = time.Now().Unix() 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
}

View File

@ -652,6 +652,9 @@ issues.label.filter_sort.reverse_alphabetically = Reverse alphabetically
issues.num_participants = %d Participants issues.num_participants = %d Participants
issues.attachment.open_tab = `Click to see "%s" in a new tab` issues.attachment.open_tab = `Click to see "%s" in a new tab`
issues.attachment.download = `Click to download "%s"` 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.new = New Pull Request
pulls.compare_changes = Compare Changes pulls.compare_changes = Compare Changes

View File

@ -465,6 +465,20 @@ func ViewIssue(ctx *context.Context) {
} }
ctx.Data["Title"] = fmt.Sprintf("#%d - %s", issue.Index, issue.Title) 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. // Make sure type and URL matches.
if ctx.Params(":type") == "issues" && issue.IsPull { if ctx.Params(":type") == "issues" && issue.IsPull {
ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index)) ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index))

View File

@ -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)
}

View File

@ -98,5 +98,24 @@
{{end}} {{end}}
</div> </div>
</div> </div>
<div class="ui divider"></div>
<div class="ui watching">
<span class="text"><strong>{{.i18n.Tr "repo.issues.watch"}}</strong></span>
<div>
<form method="POST" action="{{$.RepoLink}}/issues/{{.Issue.Index}}/watch">
<input type="hidden" name="watch" value="{{if $.IssueWatch.IsWatching}}0{{else}}1{{end}}" />
{{$.CsrfTokenHtml}}
<button class="fluid ui button">
{{if $.IssueWatch.IsWatching}}
{{.i18n.Tr "repo.issues.unwatch_issue"}}
{{else}}
{{.i18n.Tr "repo.issues.watch_issue"}}
{{end}}
</button>
</form>
</div>
</div>
</div> </div>
</div> </div>