2017-09-12 08:48:13 +02: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 15:51:52 +01:00
|
|
|
"strings"
|
2017-09-12 08:48:13 +02:00
|
|
|
|
2022-04-25 22:45:22 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 11:37:59 +02:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2017-09-12 08:48:13 +02:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-04-25 22:45:22 +02:00
|
|
|
"code.gitea.io/gitea/modules/eventsource"
|
2017-09-12 08:48:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// IssueStopwatch creates or stops a stopwatch for the given issue.
|
|
|
|
func IssueStopwatch(c *context.Context) {
|
2017-10-16 09:55:43 +02:00
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-02-05 12:38:11 +01:00
|
|
|
|
|
|
|
var showSuccessMessage bool
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
if !issues_model.StopwatchExists(c.Doer.ID, issue.ID) {
|
2019-02-05 12:38:11 +01:00
|
|
|
showSuccessMessage = true
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:03:22 +01:00
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.Doer) {
|
2018-01-10 22:34:17 +01:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 08:48:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
if err := issues_model.CreateOrStopIssueStopwatch(c.Doer, issue); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
c.ServerError("CreateOrStopIssueStopwatch", err)
|
2017-09-12 08:48:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-05 12:38:11 +01:00
|
|
|
if showSuccessMessage {
|
|
|
|
c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:48:13 +02:00
|
|
|
url := issue.HTMLURL()
|
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelStopwatch cancel the stopwatch
|
|
|
|
func CancelStopwatch(c *context.Context) {
|
2017-10-16 09:55:43 +02:00
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 08:03:22 +01:00
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.Doer) {
|
2018-01-10 22:34:17 +01:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 08:48:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
if err := issues_model.CancelStopwatch(c.Doer, issue); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
c.ServerError("CancelStopwatch", err)
|
2017-09-12 08:48:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
stopwatches, err := issues_model.GetUserStopwatches(c.Doer.ID, db.ListOptions{})
|
2022-04-25 22:45:22 +02:00
|
|
|
if err != nil {
|
|
|
|
c.ServerError("GetUserStopwatches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(stopwatches) == 0 {
|
|
|
|
eventsource.GetManager().SendMessage(c.Doer.ID, &eventsource.Event{
|
|
|
|
Name: "stopwatches",
|
|
|
|
Data: "{}",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:48:13 +02:00
|
|
|
url := issue.HTMLURL()
|
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
2021-01-21 15:51:52 +01:00
|
|
|
|
|
|
|
// GetActiveStopwatch is the middleware that sets .ActiveStopwatch on context
|
2022-04-08 11:11:15 +02:00
|
|
|
func GetActiveStopwatch(ctx *context.Context) {
|
|
|
|
if strings.HasPrefix(ctx.Req.URL.Path, "/api") {
|
2021-01-21 15:51:52 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:11:15 +02:00
|
|
|
if !ctx.IsSigned {
|
2021-01-21 15:51:52 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
_, sw, err := issues_model.HasUserStopwatch(ctx, ctx.Doer.ID)
|
2021-01-21 15:51:52 +01:00
|
|
|
if err != nil {
|
2022-04-08 11:11:15 +02:00
|
|
|
ctx.ServerError("HasUserStopwatch", err)
|
2021-01-21 15:51:52 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sw == nil || sw.ID == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 11:37:59 +02:00
|
|
|
issue, err := issues_model.GetIssueByID(ctx, sw.IssueID)
|
2021-01-21 15:51:52 +01:00
|
|
|
if err != nil || issue == nil {
|
2022-06-17 23:47:15 +02:00
|
|
|
if !issues_model.IsErrIssueNotExist(err) {
|
|
|
|
ctx.ServerError("GetIssueByID", err)
|
|
|
|
}
|
2021-01-21 15:51:52 +01:00
|
|
|
return
|
|
|
|
}
|
2022-04-08 11:11:15 +02:00
|
|
|
if err = issue.LoadRepo(ctx); err != nil {
|
|
|
|
ctx.ServerError("LoadRepo", err)
|
2021-01-21 15:51:52 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 11:11:15 +02:00
|
|
|
ctx.Data["ActiveStopwatch"] = StopwatchTmplInfo{
|
2021-11-16 19:18:25 +01:00
|
|
|
issue.Link(),
|
2021-01-21 15:51:52 +01: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 19:18:25 +01:00
|
|
|
IssueLink string
|
2021-01-21 15:51:52 +01:00
|
|
|
RepoSlug string
|
|
|
|
IssueIndex int64
|
|
|
|
Seconds int64
|
|
|
|
}
|