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.

issue_stopwatch.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "net/http"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. )
  11. // IssueStopwatch creates or stops a stopwatch for the given issue.
  12. func IssueStopwatch(c *context.Context) {
  13. issue := GetActionIssue(c)
  14. if c.Written() {
  15. return
  16. }
  17. var showSuccessMessage bool
  18. if !models.StopwatchExists(c.User.ID, issue.ID) {
  19. showSuccessMessage = true
  20. }
  21. if !c.Repo.CanUseTimetracker(issue, c.User) {
  22. c.NotFound("CanUseTimetracker", nil)
  23. return
  24. }
  25. if err := models.CreateOrStopIssueStopwatch(c.User, issue); err != nil {
  26. c.ServerError("CreateOrStopIssueStopwatch", err)
  27. return
  28. }
  29. if showSuccessMessage {
  30. c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))
  31. }
  32. url := issue.HTMLURL()
  33. c.Redirect(url, http.StatusSeeOther)
  34. }
  35. // CancelStopwatch cancel the stopwatch
  36. func CancelStopwatch(c *context.Context) {
  37. issue := GetActionIssue(c)
  38. if c.Written() {
  39. return
  40. }
  41. if !c.Repo.CanUseTimetracker(issue, c.User) {
  42. c.NotFound("CanUseTimetracker", nil)
  43. return
  44. }
  45. if err := models.CancelStopwatch(c.User, issue); err != nil {
  46. c.ServerError("CancelStopwatch", err)
  47. return
  48. }
  49. url := issue.HTMLURL()
  50. c.Redirect(url, http.StatusSeeOther)
  51. }
  52. // GetActiveStopwatch is the middleware that sets .ActiveStopwatch on context
  53. func GetActiveStopwatch(c *context.Context) {
  54. if strings.HasPrefix(c.Req.URL.Path, "/api") {
  55. return
  56. }
  57. if !c.IsSigned {
  58. return
  59. }
  60. _, sw, err := models.HasUserStopwatch(c.User.ID)
  61. if err != nil {
  62. c.ServerError("HasUserStopwatch", err)
  63. return
  64. }
  65. if sw == nil || sw.ID == 0 {
  66. return
  67. }
  68. issue, err := models.GetIssueByID(sw.IssueID)
  69. if err != nil || issue == nil {
  70. c.ServerError("GetIssueByID", err)
  71. return
  72. }
  73. if err = issue.LoadRepo(); err != nil {
  74. c.ServerError("LoadRepo", err)
  75. return
  76. }
  77. c.Data["ActiveStopwatch"] = StopwatchTmplInfo{
  78. issue.Repo.FullName(),
  79. issue.Index,
  80. sw.Seconds() + 1, // ensure time is never zero in ui
  81. }
  82. }
  83. // StopwatchTmplInfo is a view on a stopwatch specifically for template rendering
  84. type StopwatchTmplInfo struct {
  85. RepoSlug string
  86. IssueIndex int64
  87. Seconds int64
  88. }