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