You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.2 KiB
109 lines
2.2 KiB
7 years ago
|
// 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"
|
||
4 years ago
|
"strings"
|
||
7 years ago
|
|
||
|
"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) {
|
||
7 years ago
|
issue := GetActionIssue(c)
|
||
|
if c.Written() {
|
||
|
return
|
||
|
}
|
||
6 years ago
|
|
||
|
var showSuccessMessage bool
|
||
|
|
||
|
if !models.StopwatchExists(c.User.ID, issue.ID) {
|
||
|
showSuccessMessage = true
|
||
|
}
|
||
|
|
||
7 years ago
|
if !c.Repo.CanUseTimetracker(issue, c.User) {
|
||
7 years ago
|
c.NotFound("CanUseTimetracker", nil)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if err := models.CreateOrStopIssueStopwatch(c.User, issue); err != nil {
|
||
7 years ago
|
c.ServerError("CreateOrStopIssueStopwatch", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
6 years ago
|
if showSuccessMessage {
|
||
|
c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))
|
||
|
}
|
||
|
|
||
7 years ago
|
url := issue.HTMLURL()
|
||
|
c.Redirect(url, http.StatusSeeOther)
|
||
|
}
|
||
|
|
||
|
// CancelStopwatch cancel the stopwatch
|
||
|
func CancelStopwatch(c *context.Context) {
|
||
7 years ago
|
issue := GetActionIssue(c)
|
||
|
if c.Written() {
|
||
|
return
|
||
|
}
|
||
|
if !c.Repo.CanUseTimetracker(issue, c.User) {
|
||
7 years ago
|
c.NotFound("CanUseTimetracker", nil)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if err := models.CancelStopwatch(c.User, issue); err != nil {
|
||
7 years ago
|
c.ServerError("CancelStopwatch", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
url := issue.HTMLURL()
|
||
|
c.Redirect(url, http.StatusSeeOther)
|
||
|
}
|
||
4 years ago
|
|
||
|
// 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{
|
||
|
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 {
|
||
|
RepoSlug string
|
||
|
IssueIndex int64
|
||
|
Seconds int64
|
||
|
}
|