gitea源码

issue_stopwatch.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "code.gitea.io/gitea/models/db"
  6. issues_model "code.gitea.io/gitea/models/issues"
  7. "code.gitea.io/gitea/modules/eventsource"
  8. "code.gitea.io/gitea/services/context"
  9. )
  10. // IssueStartStopwatch creates a stopwatch for the given issue.
  11. func IssueStartStopwatch(c *context.Context) {
  12. issue := GetActionIssue(c)
  13. if c.Written() {
  14. return
  15. }
  16. if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
  17. c.NotFound(nil)
  18. return
  19. }
  20. if ok, err := issues_model.CreateIssueStopwatch(c, c.Doer, issue); err != nil {
  21. c.ServerError("CreateIssueStopwatch", err)
  22. return
  23. } else if !ok {
  24. c.Flash.Warning(c.Tr("repo.issues.stopwatch_already_created"))
  25. } else {
  26. c.Flash.Success(c.Tr("repo.issues.tracker_auto_close"))
  27. }
  28. c.JSONRedirect("")
  29. }
  30. // IssueStopStopwatch stops a stopwatch for the given issue.
  31. func IssueStopStopwatch(c *context.Context) {
  32. issue := GetActionIssue(c)
  33. if c.Written() {
  34. return
  35. }
  36. if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
  37. c.NotFound(nil)
  38. return
  39. }
  40. if ok, err := issues_model.FinishIssueStopwatch(c, c.Doer, issue); err != nil {
  41. c.ServerError("FinishIssueStopwatch", err)
  42. return
  43. } else if !ok {
  44. c.Flash.Warning(c.Tr("repo.issues.stopwatch_already_stopped"))
  45. }
  46. c.JSONRedirect("")
  47. }
  48. // CancelStopwatch cancel the stopwatch
  49. func CancelStopwatch(c *context.Context) {
  50. issue := GetActionIssue(c)
  51. if c.Written() {
  52. return
  53. }
  54. if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
  55. c.NotFound(nil)
  56. return
  57. }
  58. if _, err := issues_model.CancelStopwatch(c, c.Doer, issue); err != nil {
  59. c.ServerError("CancelStopwatch", err)
  60. return
  61. }
  62. stopwatches, err := issues_model.GetUserStopwatches(c, c.Doer.ID, db.ListOptions{})
  63. if err != nil {
  64. c.ServerError("GetUserStopwatches", err)
  65. return
  66. }
  67. if len(stopwatches) == 0 {
  68. eventsource.GetManager().SendMessage(c.Doer.ID, &eventsource.Event{
  69. Name: "stopwatches",
  70. Data: "{}",
  71. })
  72. }
  73. c.JSONRedirect("")
  74. }