1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package issues
import (
"context"
"fmt"
"time"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
)
// ErrIssueStopwatchNotExist represents an error that stopwatch is not exist
type ErrIssueStopwatchNotExist struct {
UserID int64
IssueID int64
}
func (err ErrIssueStopwatchNotExist) Error() string {
return fmt.Sprintf("issue stopwatch doesn't exist[uid: %d, issue_id: %d", err.UserID, err.IssueID)
}
func (err ErrIssueStopwatchNotExist) Unwrap() error {
return util.ErrNotExist
}
// ErrIssueStopwatchAlreadyExist represents an error that stopwatch is already exist
type ErrIssueStopwatchAlreadyExist struct {
UserID int64
IssueID int64
}
func (err ErrIssueStopwatchAlreadyExist) Error() string {
return fmt.Sprintf("issue stopwatch already exists[uid: %d, issue_id: %d", err.UserID, err.IssueID)
}
func (err ErrIssueStopwatchAlreadyExist) Unwrap() error {
return util.ErrAlreadyExist
}
// Stopwatch represents a stopwatch for time tracking.
type Stopwatch struct {
ID int64 `xorm:"pk autoincr"`
IssueID int64 `xorm:"INDEX"`
UserID int64 `xorm:"INDEX"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
}
func init() {
db.RegisterModel(new(Stopwatch))
}
// Seconds returns the amount of time passed since creation, based on local server time
func (s Stopwatch) Seconds() int64 {
return int64(timeutil.TimeStampNow() - s.CreatedUnix)
}
// Duration returns a human-readable duration string based on local server time
func (s Stopwatch) Duration() string {
return util.SecToTime(s.Seconds())
}
func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
sw = new(Stopwatch)
exists, err = db.GetEngine(ctx).
Where("user_id = ?", userID).
And("issue_id = ?", issueID).
Get(sw)
return sw, exists, err
}
// UserIDCount is a simple coalition of UserID and Count
type UserStopwatch struct {
UserID int64
StopWatches []*Stopwatch
}
// GetUIDsAndNotificationCounts between the two provided times
func GetUIDsAndStopwatch() ([]*UserStopwatch, error) {
sws := []*Stopwatch{}
if err := db.GetEngine(db.DefaultContext).Where("issue_id != 0").Find(&sws); err != nil {
return nil, err
}
if len(sws) == 0 {
return []*UserStopwatch{}, nil
}
lastUserID := int64(-1)
res := []*UserStopwatch{}
for _, sw := range sws {
if lastUserID == sw.UserID {
lastUserStopwatch := res[len(res)-1]
lastUserStopwatch.StopWatches = append(lastUserStopwatch.StopWatches, sw)
} else {
res = append(res, &UserStopwatch{
UserID: sw.UserID,
StopWatches: []*Stopwatch{sw},
})
}
}
return res, nil
}
// GetUserStopwatches return list of all stopwatches of a user
func GetUserStopwatches(userID int64, listOptions db.ListOptions) ([]*Stopwatch, error) {
sws := make([]*Stopwatch, 0, 8)
sess := db.GetEngine(db.DefaultContext).Where("stopwatch.user_id = ?", userID)
if listOptions.Page != 0 {
sess = db.SetSessionPagination(sess, &listOptions)
}
err := sess.Find(&sws)
if err != nil {
return nil, err
}
return sws, nil
}
// CountUserStopwatches return count of all stopwatches of a user
func CountUserStopwatches(userID int64) (int64, error) {
return db.GetEngine(db.DefaultContext).Where("user_id = ?", userID).Count(&Stopwatch{})
}
// StopwatchExists returns true if the stopwatch exists
func StopwatchExists(userID, issueID int64) bool {
_, exists, _ := getStopwatch(db.DefaultContext, userID, issueID)
return exists
}
// HasUserStopwatch returns true if the user has a stopwatch
func HasUserStopwatch(ctx context.Context, userID int64) (exists bool, sw *Stopwatch, err error) {
sw = new(Stopwatch)
exists, err = db.GetEngine(ctx).
Where("user_id = ?", userID).
Get(sw)
return exists, sw, err
}
// FinishIssueStopwatchIfPossible if stopwatch exist then finish it otherwise ignore
func FinishIssueStopwatchIfPossible(ctx context.Context, user *user_model.User, issue *Issue) error {
_, exists, err := getStopwatch(ctx, user.ID, issue.ID)
if err != nil {
return err
}
if !exists {
return nil
}
return FinishIssueStopwatch(ctx, user, issue)
}
// CreateOrStopIssueStopwatch create an issue stopwatch if it's not exist, otherwise finish it
func CreateOrStopIssueStopwatch(user *user_model.User, issue *Issue) error {
_, exists, err := getStopwatch(db.DefaultContext, user.ID, issue.ID)
if err != nil {
return err
}
if exists {
return FinishIssueStopwatch(db.DefaultContext, user, issue)
}
return CreateIssueStopwatch(db.DefaultContext, user, issue)
}
// FinishIssueStopwatch if stopwatch exist then finish it otherwise return an error
func FinishIssueStopwatch(ctx context.Context, user *user_model.User, issue *Issue) error {
sw, exists, err := getStopwatch(ctx, user.ID, issue.ID)
if err != nil {
return err
}
if !exists {
return ErrIssueStopwatchNotExist{
UserID: user.ID,
IssueID: issue.ID,
}
}
// Create tracked time out of the time difference between start date and actual date
timediff := time.Now().Unix() - int64(sw.CreatedUnix)
// Create TrackedTime
tt := &TrackedTime{
Created: time.Now(),
IssueID: issue.ID,
UserID: user.ID,
Time: timediff,
}
if err := db.Insert(ctx, tt); err != nil {
return err
}
if err := issue.LoadRepo(ctx); err != nil {
return err
}
if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Repo: issue.Repo,
Content: util.SecToTime(timediff),
Type: CommentTypeStopTracking,
TimeID: tt.ID,
}); err != nil {
return err
}
_, err = db.DeleteByBean(ctx, sw)
return err
}
// CreateIssueStopwatch creates a stopwatch if not exist, otherwise return an error
func CreateIssueStopwatch(ctx context.Context, user *user_model.User, issue *Issue) error {
if err := issue.LoadRepo(ctx); err != nil {
return err
}
// if another stopwatch is running: stop it
exists, sw, err := HasUserStopwatch(ctx, user.ID)
if err != nil {
return err
}
if exists {
issue, err := GetIssueByID(ctx, sw.IssueID)
if err != nil {
return err
}
if err := FinishIssueStopwatch(ctx, user, issue); err != nil {
return err
}
}
// Create stopwatch
sw = &Stopwatch{
UserID: user.ID,
IssueID: issue.ID,
}
if err := db.Insert(ctx, sw); err != nil {
return err
}
if err := issue.LoadRepo(ctx); err != nil {
return err
}
if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Repo: issue.Repo,
Type: CommentTypeStartTracking,
}); err != nil {
return err
}
return nil
}
// CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
func CancelStopwatch(user *user_model.User, issue *Issue) error {
ctx, committer, err := db.TxContext(db.DefaultContext)
if err != nil {
return err
}
defer committer.Close()
if err := cancelStopwatch(ctx, user, issue); err != nil {
return err
}
return committer.Commit()
}
func cancelStopwatch(ctx context.Context, user *user_model.User, issue *Issue) error {
e := db.GetEngine(ctx)
sw, exists, err := getStopwatch(ctx, user.ID, issue.ID)
if err != nil {
return err
}
if exists {
if _, err := e.Delete(sw); err != nil {
return err
}
if err := issue.LoadRepo(ctx); err != nil {
return err
}
if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
Doer: user,
Issue: issue,
Repo: issue.Repo,
Type: CommentTypeCancelTracking,
}); err != nil {
return err
}
}
return nil
}
|