2017-09-12 06:48:13 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-01-21 14:51:52 +00:00
|
|
|
"strings"
|
2017-09-12 06:48:13 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IssueStopwatch creates or stops a stopwatch for the given issue.
|
|
|
|
func IssueStopwatch(c *context.Context) {
|
2017-10-16 07:55:43 +00:00
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-02-05 11:38:11 +00:00
|
|
|
|
|
|
|
var showSuccessMessage bool
|
|
|
|
|
|
|
|
if !models.StopwatchExists(c.User.ID, issue.ID) {
|
|
|
|
showSuccessMessage = true
|
|
|
|
}
|
|
|
|
|
2017-10-16 07:55:43 +00:00
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.User) {
|
2018-01-10 21:34:17 +00:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 06:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := models.CreateOrStopIssueStopwatch(c.User, issue); err != nil {
|
2018-01-10 21:34:17 +00:00
|
|
|
c.ServerError("CreateOrStopIssueStopwatch", err)
|
2017-09-12 06:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-05 11:38:11 +00:00
|
|
|
if showSuccessMessage {
|
|
|
|
c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))
|
|
|
|
}
|
|
|
|
|
2017-09-12 06:48:13 +00:00
|
|
|
url := issue.HTMLURL()
|
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelStopwatch cancel the stopwatch
|
|
|
|
func CancelStopwatch(c *context.Context) {
|
2017-10-16 07:55:43 +00:00
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.User) {
|
2018-01-10 21:34:17 +00:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 06:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := models.CancelStopwatch(c.User, issue); err != nil {
|
2018-01-10 21:34:17 +00:00
|
|
|
c.ServerError("CancelStopwatch", err)
|
2017-09-12 06:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
url := issue.HTMLURL()
|
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
2021-01-21 14:51:52 +00:00
|
|
|
|
|
|
|
// GetActiveStopwatch is the middleware that sets .ActiveStopwatch on context
|
|
|
|
func GetActiveStopwatch(c *context.Context) {
|
|
|
|
if strings.HasPrefix(c.Req.URL.Path, "/api") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.IsSigned {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, sw, err := models.HasUserStopwatch(c.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
c.ServerError("HasUserStopwatch", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sw == nil || sw.ID == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
issue, err := models.GetIssueByID(sw.IssueID)
|
|
|
|
if err != nil || issue == nil {
|
|
|
|
c.ServerError("GetIssueByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = issue.LoadRepo(); err != nil {
|
|
|
|
c.ServerError("LoadRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["ActiveStopwatch"] = StopwatchTmplInfo{
|
2021-11-16 18:18:25 +00:00
|
|
|
issue.Link(),
|
2021-01-21 14:51:52 +00:00
|
|
|
issue.Repo.FullName(),
|
|
|
|
issue.Index,
|
|
|
|
sw.Seconds() + 1, // ensure time is never zero in ui
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopwatchTmplInfo is a view on a stopwatch specifically for template rendering
|
|
|
|
type StopwatchTmplInfo struct {
|
2021-11-16 18:18:25 +00:00
|
|
|
IssueLink string
|
2021-01-21 14:51:52 +00:00
|
|
|
RepoSlug string
|
|
|
|
IssueIndex int64
|
|
|
|
Seconds int64
|
|
|
|
}
|