@ -9,6 +9,7 @@ import (
"time"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/util"
@ -132,12 +133,26 @@ func StopwatchExists(userID, issueID int64) bool {
}
}
// HasUserStopwatch returns true if the user has a stopwatch
// HasUserStopwatch returns true if the user has a stopwatch
func HasUserStopwatch ( ctx context . Context , userID int64 ) ( exists bool , sw * Stopwatch , err error ) {
func HasUserStopwatch ( ctx context . Context , userID int64 ) ( exists bool , sw * Stopwatch , issue * Issue , err error ) {
sw = new ( Stopwatch )
type stopwatchIssueRepo struct {
Stopwatch ` xorm:"extends" `
Issue ` xorm:"extends" `
repo . Repository ` xorm:"extends" `
}
swIR := new ( stopwatchIssueRepo )
exists , err = db . GetEngine ( ctx ) .
exists , err = db . GetEngine ( ctx ) .
Table ( "stopwatch" ) .
Where ( "user_id = ?" , userID ) .
Where ( "user_id = ?" , userID ) .
Get ( sw )
Join ( "INNER" , "issue" , "issue.id = stopwatch.issue_id" ) .
return exists , sw , err
Join ( "INNER" , "repository" , "repository.id = issue.repo_id" ) .
Get ( swIR )
if exists {
sw = & swIR . Stopwatch
issue = & swIR . Issue
issue . Repo = & swIR . Repository
}
return exists , sw , issue , err
}
}
// FinishIssueStopwatchIfPossible if stopwatch exist then finish it otherwise ignore
// FinishIssueStopwatchIfPossible if stopwatch exist then finish it otherwise ignore
@ -217,23 +232,18 @@ func CreateIssueStopwatch(ctx context.Context, user *user_model.User, issue *Iss
}
}
// if another stopwatch is running: stop it
// if another stopwatch is running: stop it
exists , sw , err := HasUserStopwatch ( ctx , user . ID )
exists , _, otherIssue , err := HasUserStopwatch ( ctx , user . ID )
if err != nil {
if err != nil {
return err
return err
}
}
if exists {
if exists {
issue , err := GetIssueByID ( ctx , sw . IssueID )
if err := FinishIssueStopwatch ( ctx , user , otherIssue ) ; err != nil {
if err != nil {
return err
}
if err := FinishIssueStopwatch ( ctx , user , issue ) ; err != nil {
return err
return err
}
}
}
}
// Create stopwatch
// Create stopwatch
sw = & Stopwatch {
sw : = & Stopwatch {
UserID : user . ID ,
UserID : user . ID ,
IssueID : issue . ID ,
IssueID : issue . ID ,
}
}