2017-09-12 12:18:13 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2017-09-12 12:18:13 +05:30
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-01-21 20:21:52 +05:30
|
|
|
"strings"
|
2017-09-12 12:18:13 +05:30
|
|
|
|
2022-04-26 02:15:22 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2017-09-12 12:18:13 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-04-26 02:15:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/eventsource"
|
2017-09-12 12:18:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// IssueStopwatch creates or stops a stopwatch for the given issue.
|
|
|
|
func IssueStopwatch(c *context.Context) {
|
2017-10-16 13:25:43 +05:30
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2019-02-05 17:08:11 +05:30
|
|
|
|
|
|
|
var showSuccessMessage bool
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if !issues_model.StopwatchExists(c.Doer.ID, issue.ID) {
|
2019-02-05 17:08:11 +05:30
|
|
|
showSuccessMessage = true
|
|
|
|
}
|
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.Doer) {
|
2018-01-11 03:04:17 +05:30
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 12:18:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if err := issues_model.CreateOrStopIssueStopwatch(c.Doer, issue); err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
c.ServerError("CreateOrStopIssueStopwatch", err)
|
2017-09-12 12:18:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:08:11 +05:30
|
|
|
if showSuccessMessage {
|
|
|
|
c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))
|
|
|
|
}
|
|
|
|
|
2023-02-11 12:04:11 +05:30
|
|
|
url := issue.Link()
|
2017-09-12 12:18:13 +05:30
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelStopwatch cancel the stopwatch
|
|
|
|
func CancelStopwatch(c *context.Context) {
|
2017-10-16 13:25:43 +05:30
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.Doer) {
|
2018-01-11 03:04:17 +05:30
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 12:18:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if err := issues_model.CancelStopwatch(c.Doer, issue); err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
c.ServerError("CancelStopwatch", err)
|
2017-09-12 12:18:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
stopwatches, err := issues_model.GetUserStopwatches(c.Doer.ID, db.ListOptions{})
|
2022-04-26 02:15:22 +05:30
|
|
|
if err != nil {
|
|
|
|
c.ServerError("GetUserStopwatches", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(stopwatches) == 0 {
|
|
|
|
eventsource.GetManager().SendMessage(c.Doer.ID, &eventsource.Event{
|
|
|
|
Name: "stopwatches",
|
|
|
|
Data: "{}",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-11 12:04:11 +05:30
|
|
|
url := issue.Link()
|
2017-09-12 12:18:13 +05:30
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
2021-01-21 20:21:52 +05:30
|
|
|
|
|
|
|
// GetActiveStopwatch is the middleware that sets .ActiveStopwatch on context
|
2022-04-08 14:41:15 +05:30
|
|
|
func GetActiveStopwatch(ctx *context.Context) {
|
|
|
|
if strings.HasPrefix(ctx.Req.URL.Path, "/api") {
|
2021-01-21 20:21:52 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 14:41:15 +05:30
|
|
|
if !ctx.IsSigned {
|
2021-01-21 20:21:52 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-28 00:16:00 +05:30
|
|
|
_, sw, issue, err := issues_model.HasUserStopwatch(ctx, ctx.Doer.ID)
|
2021-01-21 20:21:52 +05:30
|
|
|
if err != nil {
|
2022-04-08 14:41:15 +05:30
|
|
|
ctx.ServerError("HasUserStopwatch", err)
|
2021-01-21 20:21:52 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sw == nil || sw.ID == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 14:41:15 +05:30
|
|
|
ctx.Data["ActiveStopwatch"] = StopwatchTmplInfo{
|
2021-11-16 23:48:25 +05:30
|
|
|
issue.Link(),
|
2021-01-21 20:21:52 +05:30
|
|
|
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 23:48:25 +05:30
|
|
|
IssueLink string
|
2021-01-21 20:21:52 +05:30
|
|
|
RepoSlug string
|
|
|
|
IssueIndex int64
|
|
|
|
Seconds int64
|
|
|
|
}
|