Add watch button on issue

This commit is contained in:
Andrey Nering 2017-03-29 20:31:47 -03:00
parent a0d0de7233
commit b674460748
6 changed files with 111 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

@ -16,5 +16,45 @@ type IssueWatch struct {
// BeforeInsert is invoked from XORM before inserting an object of this type. // BeforeInsert is invoked from XORM before inserting an object of this type.
func (iw *IssueWatch) BeforeInsert() { func (iw *IssueWatch) BeforeInsert() {
iw.Created = time.Now()
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 {
iw, exists, err := getIssueWatch(x, userID, issueID)
if err != nil {
return err
}
if !exists {
iw = &IssueWatch{
UserID: userID,
IssueID: issueID,
IsWatching: isWatching,
}
if _, err := x.Insert(iw); err != nil {
return err
}
} else {
if _, err := x.Table(&IssueWatch{}).Id(iw.ID).Update(map[string]interface{}{"is_watching": isWatching}); 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: models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID),
}
}
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,34 @@
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
}
issueIndex := c.ParamsInt64("index")
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, issueIndex)
if err != nil {
c.Handle(http.StatusInternalServerError, "GetIssueByIndex", err)
return
}
if err := models.CreateOrUpdateIssueWatch(c.User.ID, issue.ID, watch); err != nil {
c.Handle(http.StatusInternalServerError, "CreateOrUpdateIssueWatch", err)
return
}
url := fmt.Sprintf("%s/issues/%d", c.Repo.RepoLink, issueIndex)
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>