diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2017-10-02 00:52:35 +0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2017-10-01 19:52:35 +0300 |
commit | a8717e5e3ace4dd226547faae9c3a44616bbf6f0 (patch) | |
tree | 524dc024dec64131406486556951a53655ad8510 /models | |
parent | 1ad902d5298202d5be14fd5a9c8ed6ce781a23c8 (diff) | |
download | gitea-a8717e5e3ace4dd226547faae9c3a44616bbf6f0.tar.gz gitea-a8717e5e3ace4dd226547faae9c3a44616bbf6f0.zip |
Use AfterLoad instead of AfterSet on Structs (#2628)
* use AfterLoad instead of AfterSet on Structs
* fix the comments on AfterLoad
* fix the comments on action AfterLoad
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 10 | ||||
-rw-r--r-- | models/admin.go | 10 | ||||
-rw-r--r-- | models/attachment.go | 14 | ||||
-rw-r--r-- | models/gpg_key.go | 20 | ||||
-rw-r--r-- | models/issue.go | 15 | ||||
-rw-r--r-- | models/issue_comment.go | 37 | ||||
-rw-r--r-- | models/issue_milestone.go | 30 | ||||
-rw-r--r-- | models/issue_stopwatch.go | 12 | ||||
-rw-r--r-- | models/issue_tracked_time.go | 18 | ||||
-rw-r--r-- | models/lfs.go | 11 | ||||
-rw-r--r-- | models/login_source.go | 12 | ||||
-rw-r--r-- | models/pull.go | 15 | ||||
-rw-r--r-- | models/release.go | 10 | ||||
-rw-r--r-- | models/repo.go | 31 | ||||
-rw-r--r-- | models/repo_mirror.go | 20 | ||||
-rw-r--r-- | models/repo_unit.go | 9 | ||||
-rw-r--r-- | models/ssh_key.go | 36 | ||||
-rw-r--r-- | models/status.go | 12 | ||||
-rw-r--r-- | models/token.go | 19 | ||||
-rw-r--r-- | models/twofactor.go | 15 | ||||
-rw-r--r-- | models/user.go | 15 | ||||
-rw-r--r-- | models/webhook.go | 55 |
22 files changed, 153 insertions, 273 deletions
diff --git a/models/action.go b/models/action.go index 27cfdc865c..81174d112f 100644 --- a/models/action.go +++ b/models/action.go @@ -16,7 +16,6 @@ import ( "github.com/Unknwon/com" "github.com/go-xorm/builder" - "github.com/go-xorm/xorm" "code.gitea.io/git" api "code.gitea.io/sdk/gitea" @@ -91,12 +90,9 @@ type Action struct { CreatedUnix int64 `xorm:"INDEX created"` } -// AfterSet updates the webhook object upon setting a column. -func (a *Action) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - a.Created = time.Unix(a.CreatedUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (a *Action) AfterLoad() { + a.Created = time.Unix(a.CreatedUnix, 0).Local() } // GetOpType gets the ActionType of this action. diff --git a/models/admin.go b/models/admin.go index eb78f6e865..dc4acfd939 100644 --- a/models/admin.go +++ b/models/admin.go @@ -12,7 +12,6 @@ import ( "code.gitea.io/gitea/modules/util" "github.com/Unknwon/com" - "github.com/go-xorm/xorm" ) //NoticeType describes the notice type @@ -32,12 +31,9 @@ type Notice struct { CreatedUnix int64 `xorm:"INDEX created"` } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (n *Notice) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - n.Created = time.Unix(n.CreatedUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (n *Notice) AfterLoad() { + n.Created = time.Unix(n.CreatedUnix, 0).Local() } // TrStr returns a translation format string. diff --git a/models/attachment.go b/models/attachment.go index 2731ca4b7a..9c91613e10 100644 --- a/models/attachment.go +++ b/models/attachment.go @@ -12,7 +12,6 @@ import ( "path" "time" - "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" "code.gitea.io/gitea/modules/setting" @@ -31,13 +30,10 @@ type Attachment struct { CreatedUnix int64 `xorm:"created"` } -// AfterSet is invoked from XORM after setting the value of a field of +// AfterLoad is invoked from XORM after setting the value of a field of // this object. -func (a *Attachment) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - a.Created = time.Unix(a.CreatedUnix, 0).Local() - } +func (a *Attachment) AfterLoad() { + a.Created = time.Unix(a.CreatedUnix, 0).Local() } // IncreaseDownloadCount is update download count + 1 @@ -133,6 +129,10 @@ func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) { // GetAttachmentsByCommentID returns all attachments if comment by given ID. func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) { + return getAttachmentsByCommentID(x, commentID) +} + +func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) { attachments := make([]*Attachment, 0, 10) return attachments, x.Where("comment_id=?", commentID).Find(&attachments) } diff --git a/models/gpg_key.go b/models/gpg_key.go index 85a44338ea..6d6f049936 100644 --- a/models/gpg_key.go +++ b/models/gpg_key.go @@ -52,17 +52,15 @@ func (key *GPGKey) BeforeInsert() { key.CreatedUnix = key.Created.Unix() } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (key *GPGKey) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "key_id": - x.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey) - case "added_unix": - key.Added = time.Unix(key.AddedUnix, 0).Local() - case "expired_unix": - key.Expired = time.Unix(key.ExpiredUnix, 0).Local() - case "created_unix": - key.Created = time.Unix(key.CreatedUnix, 0).Local() +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (key *GPGKey) AfterLoad(session *xorm.Session) { + key.Added = time.Unix(key.AddedUnix, 0).Local() + key.Expired = time.Unix(key.ExpiredUnix, 0).Local() + key.Created = time.Unix(key.CreatedUnix, 0).Local() + + err := session.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey) + if err != nil { + log.Error(3, "Find Sub GPGkeys[%d]: %v", key.KeyID, err) } } diff --git a/models/issue.go b/models/issue.go index 0c68275d41..4f28bb7a61 100644 --- a/models/issue.go +++ b/models/issue.go @@ -67,17 +67,12 @@ func (issue *Issue) BeforeUpdate() { issue.DeadlineUnix = issue.Deadline.Unix() } -// AfterSet is invoked from XORM after setting the value of a field of +// AfterLoad is invoked from XORM after setting the value of a field of // this object. -func (issue *Issue) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "deadline_unix": - issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local() - case "created_unix": - issue.Created = time.Unix(issue.CreatedUnix, 0).Local() - case "updated_unix": - issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local() - } +func (issue *Issue) AfterLoad() { + issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local() + issue.Created = time.Unix(issue.CreatedUnix, 0).Local() + issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local() } func (issue *Issue) loadRepo(e Engine) (err error) { diff --git a/models/issue_comment.go b/models/issue_comment.go index 084a2a81b1..25d6535752 100644 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -112,30 +112,25 @@ type Comment struct { ShowTag CommentTag `xorm:"-"` } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (c *Comment) AfterSet(colName string, _ xorm.Cell) { +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (c *Comment) AfterLoad(session *xorm.Session) { + c.Created = time.Unix(c.CreatedUnix, 0).Local() + c.Updated = time.Unix(c.UpdatedUnix, 0).Local() + var err error - switch colName { - case "id": - c.Attachments, err = GetAttachmentsByCommentID(c.ID) - if err != nil { - log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err) - } + c.Attachments, err = getAttachmentsByCommentID(session, c.ID) + if err != nil { + log.Error(3, "getAttachmentsByCommentID[%d]: %v", c.ID, err) + } - case "poster_id": - c.Poster, err = GetUserByID(c.PosterID) - if err != nil { - if IsErrUserNotExist(err) { - c.PosterID = -1 - c.Poster = NewGhostUser() - } else { - log.Error(3, "GetUserByID[%d]: %v", c.ID, err) - } + c.Poster, err = getUserByID(session, c.PosterID) + if err != nil { + if IsErrUserNotExist(err) { + c.PosterID = -1 + c.Poster = NewGhostUser() + } else { + log.Error(3, "getUserByID[%d]: %v", c.ID, err) } - case "created_unix": - c.Created = time.Unix(c.CreatedUnix, 0).Local() - case "updated_unix": - c.Updated = time.Unix(c.UpdatedUnix, 0).Local() } } diff --git a/models/issue_milestone.go b/models/issue_milestone.go index 0001da90ef..5dbf4e111f 100644 --- a/models/issue_milestone.go +++ b/models/issue_milestone.go @@ -51,27 +51,21 @@ func (m *Milestone) BeforeUpdate() { m.ClosedDateUnix = m.ClosedDate.Unix() } -// AfterSet is invoked from XORM after setting the value of a field of +// AfterLoad is invoked from XORM after setting the value of a field of // this object. -func (m *Milestone) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "num_closed_issues": - m.NumOpenIssues = m.NumIssues - m.NumClosedIssues - - case "deadline_unix": - m.Deadline = time.Unix(m.DeadlineUnix, 0).Local() - if m.Deadline.Year() == 9999 { - return - } - - m.DeadlineString = m.Deadline.Format("2006-01-02") - if time.Now().Local().After(m.Deadline) { - m.IsOverDue = true - } +func (m *Milestone) AfterLoad() { + m.NumOpenIssues = m.NumIssues - m.NumClosedIssues + m.Deadline = time.Unix(m.DeadlineUnix, 0).Local() + if m.Deadline.Year() == 9999 { + return + } - case "closed_date_unix": - m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local() + m.DeadlineString = m.Deadline.Format("2006-01-02") + if time.Now().Local().After(m.Deadline) { + m.IsOverDue = true } + + m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local() } // State returns string representation of milestone status. diff --git a/models/issue_stopwatch.go b/models/issue_stopwatch.go index 3b5b4d57f3..b136c511f4 100644 --- a/models/issue_stopwatch.go +++ b/models/issue_stopwatch.go @@ -7,8 +7,6 @@ package models import ( "fmt" "time" - - "github.com/go-xorm/xorm" ) // Stopwatch represents a stopwatch for time tracking. @@ -26,13 +24,9 @@ func (s *Stopwatch) BeforeInsert() { s.CreatedUnix = time.Now().Unix() } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (s *Stopwatch) AfterSet(colName string, _ xorm.Cell) { - switch colName { - - case "created_unix": - s.Created = time.Unix(s.CreatedUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (s *Stopwatch) AfterLoad() { + s.Created = time.Unix(s.CreatedUnix, 0).Local() } func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) { diff --git a/models/issue_tracked_time.go b/models/issue_tracked_time.go index 33914dbb1f..b1c82b6207 100644 --- a/models/issue_tracked_time.go +++ b/models/issue_tracked_time.go @@ -8,7 +8,6 @@ import ( "time" "github.com/go-xorm/builder" - "github.com/go-xorm/xorm" ) // TrackedTime represents a time that was spent for a specific issue. @@ -17,22 +16,13 @@ type TrackedTime struct { IssueID int64 `xorm:"INDEX" json:"issue_id"` UserID int64 `xorm:"INDEX" json:"user_id"` Created time.Time `xorm:"-" json:"created"` - CreatedUnix int64 `json:"-"` + CreatedUnix int64 `xorm:"created" json:"-"` Time int64 `json:"time"` } -// BeforeInsert will be invoked by XORM before inserting a record -// representing this object. -func (t *TrackedTime) BeforeInsert() { - t.CreatedUnix = time.Now().Unix() -} - -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (t *TrackedTime) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - t.Created = time.Unix(t.CreatedUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (t *TrackedTime) AfterLoad() { + t.Created = time.Unix(t.CreatedUnix, 0).Local() } // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored. diff --git a/models/lfs.go b/models/lfs.go index be99306d75..842e48e409 100644 --- a/models/lfs.go +++ b/models/lfs.go @@ -3,8 +3,6 @@ package models import ( "errors" "time" - - "github.com/go-xorm/xorm" ) // LFSMetaObject stores metadata for LFS tracked files. @@ -109,10 +107,7 @@ func RemoveLFSMetaObjectByOid(oid string) error { return sess.Commit() } -// AfterSet stores the LFSMetaObject creation time in the database as local time. -func (m *LFSMetaObject) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - m.Created = time.Unix(m.CreatedUnix, 0).Local() - } +// AfterLoad stores the LFSMetaObject creation time in the database as local time. +func (m *LFSMetaObject) AfterLoad() { + m.Created = time.Unix(m.CreatedUnix, 0).Local() } diff --git a/models/login_source.go b/models/login_source.go index c43afd58b7..caf1a1dd4f 100644 --- a/models/login_source.go +++ b/models/login_source.go @@ -183,14 +183,10 @@ func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) { } } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (source *LoginSource) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - source.Created = time.Unix(source.CreatedUnix, 0).Local() - case "updated_unix": - source.Updated = time.Unix(source.UpdatedUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (source *LoginSource) AfterLoad() { + source.Created = time.Unix(source.CreatedUnix, 0).Local() + source.Updated = time.Unix(source.UpdatedUnix, 0).Local() } // TypeName return name of this login source type. diff --git a/models/pull.go b/models/pull.go index 31e1af7e8c..984a1b3038 100644 --- a/models/pull.go +++ b/models/pull.go @@ -80,17 +80,14 @@ func (pr *PullRequest) BeforeUpdate() { pr.MergedUnix = pr.Merged.Unix() } -// AfterSet is invoked from XORM after setting the value of a field of this object. +// AfterLoad is invoked from XORM after setting the values of all fields of this object. // Note: don't try to get Issue because will end up recursive querying. -func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "merged_unix": - if !pr.HasMerged { - return - } - - pr.Merged = time.Unix(pr.MergedUnix, 0).Local() +func (pr *PullRequest) AfterLoad() { + if !pr.HasMerged { + return } + + pr.Merged = time.Unix(pr.MergedUnix, 0).Local() } // Note: don't try to get Issue because will end up recursive querying. diff --git a/models/release.go b/models/release.go index 42054131a3..0909b7b3be 100644 --- a/models/release.go +++ b/models/release.go @@ -15,7 +15,6 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/sdk/gitea" "github.com/go-xorm/builder" - "github.com/go-xorm/xorm" ) // Release represents a release of repository. @@ -50,12 +49,9 @@ func (r *Release) BeforeInsert() { } } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (r *Release) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - r.Created = time.Unix(r.CreatedUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (r *Release) AfterLoad() { + r.Created = time.Unix(r.CreatedUnix, 0).Local() } func (r *Release) loadAttributes(e Engine) error { diff --git a/models/repo.go b/models/repo.go index 22a3a83229..6b457863c8 100644 --- a/models/repo.go +++ b/models/repo.go @@ -216,25 +216,18 @@ type Repository struct { UpdatedUnix int64 `xorm:"INDEX updated"` } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (repo *Repository) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "default_branch": - // FIXME: use models migration to solve all at once. - if len(repo.DefaultBranch) == 0 { - repo.DefaultBranch = "master" - } - case "num_closed_issues": - repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues - case "num_closed_pulls": - repo.NumOpenPulls = repo.NumPulls - repo.NumClosedPulls - case "num_closed_milestones": - repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones - case "created_unix": - repo.Created = time.Unix(repo.CreatedUnix, 0).Local() - case "updated_unix": - repo.Updated = time.Unix(repo.UpdatedUnix, 0) - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (repo *Repository) AfterLoad() { + // FIXME: use models migration to solve all at once. + if len(repo.DefaultBranch) == 0 { + repo.DefaultBranch = "master" + } + + repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues + repo.NumOpenPulls = repo.NumPulls - repo.NumClosedPulls + repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones + repo.Created = time.Unix(repo.CreatedUnix, 0).Local() + repo.Updated = time.Unix(repo.UpdatedUnix, 0) } // MustOwner always returns a valid *User object to avoid diff --git a/models/repo_mirror.go b/models/repo_mirror.go index 60d4f9f05a..0b6e130aed 100644 --- a/models/repo_mirror.go +++ b/models/repo_mirror.go @@ -55,24 +55,20 @@ func (m *Mirror) BeforeUpdate() { } } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (m *Mirror) AfterSet(colName string, _ xorm.Cell) { +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (m *Mirror) AfterLoad(session *xorm.Session) { if m == nil { return } var err error - switch colName { - case "repo_id": - m.Repo, err = GetRepositoryByID(m.RepoID) - if err != nil { - log.Error(3, "GetRepositoryByID[%d]: %v", m.ID, err) - } - case "updated_unix": - m.Updated = time.Unix(m.UpdatedUnix, 0).Local() - case "next_update_unix": - m.NextUpdate = time.Unix(m.NextUpdateUnix, 0).Local() + m.Repo, err = getRepositoryByID(session, m.RepoID) + if err != nil { + log.Error(3, "getRepositoryByID[%d]: %v", m.ID, err) } + + m.Updated = time.Unix(m.UpdatedUnix, 0).Local() + m.NextUpdate = time.Unix(m.NextUpdateUnix, 0).Local() } // ScheduleNextUpdate calculates and sets next update time. diff --git a/models/repo_unit.go b/models/repo_unit.go index 1553fa771d..cb647fd406 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -106,12 +106,9 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) { } } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (r *RepoUnit) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - r.Created = time.Unix(r.CreatedUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (r *RepoUnit) AfterLoad() { + r.Created = time.Unix(r.CreatedUnix, 0).Local() } // Unit returns Unit diff --git a/models/ssh_key.go b/models/ssh_key.go index c8f65ef0d1..a83d8fb60f 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -56,22 +56,18 @@ type PublicKey struct { Created time.Time `xorm:"-"` CreatedUnix int64 `xorm:"created"` - Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet. + Updated time.Time `xorm:"-"` UpdatedUnix int64 `xorm:"updated"` HasRecentActivity bool `xorm:"-"` HasUsed bool `xorm:"-"` } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (key *PublicKey) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - key.Created = time.Unix(key.CreatedUnix, 0).Local() - case "updated_unix": - key.Updated = time.Unix(key.UpdatedUnix, 0).Local() - key.HasUsed = key.Updated.After(key.Created) - key.HasRecentActivity = key.Updated.Add(7 * 24 * time.Hour).After(time.Now()) - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (key *PublicKey) AfterLoad() { + key.Created = time.Unix(key.CreatedUnix, 0).Local() + key.Updated = time.Unix(key.UpdatedUnix, 0).Local() + key.HasUsed = key.Updated.After(key.Created) + key.HasRecentActivity = key.Updated.Add(7 * 24 * time.Hour).After(time.Now()) } // OmitEmail returns content of public key without email address. @@ -612,22 +608,18 @@ type DeployKey struct { Created time.Time `xorm:"-"` CreatedUnix int64 `xorm:"created"` - Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet. + Updated time.Time `xorm:"-"` UpdatedUnix int64 `xorm:"updated"` HasRecentActivity bool `xorm:"-"` HasUsed bool `xorm:"-"` } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (key *DeployKey) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - key.Created = time.Unix(key.CreatedUnix, 0).Local() - case "updated_unix": - key.Updated = time.Unix(key.UpdatedUnix, 0).Local() - key.HasUsed = key.Updated.After(key.Created) - key.HasRecentActivity = key.Updated.Add(7 * 24 * time.Hour).After(time.Now()) - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (key *DeployKey) AfterLoad() { + key.Created = time.Unix(key.CreatedUnix, 0).Local() + key.Updated = time.Unix(key.UpdatedUnix, 0).Local() + key.HasUsed = key.Updated.After(key.Created) + key.HasRecentActivity = key.Updated.Add(7 * 24 * time.Hour).After(time.Now()) } // GetContent gets associated public key content. diff --git a/models/status.go b/models/status.go index d0c4a08744..e2e8adb77b 100644 --- a/models/status.go +++ b/models/status.go @@ -71,15 +71,11 @@ type CommitStatus struct { UpdatedUnix int64 `xorm:"INDEX updated"` } -// AfterSet is invoked from XORM after setting the value of a field of +// AfterLoad is invoked from XORM after setting the value of a field of // this object. -func (status *CommitStatus) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - status.Created = time.Unix(status.CreatedUnix, 0).Local() - case "updated_unix": - status.Updated = time.Unix(status.UpdatedUnix, 0).Local() - } +func (status *CommitStatus) AfterLoad() { + status.Created = time.Unix(status.CreatedUnix, 0).Local() + status.Updated = time.Unix(status.UpdatedUnix, 0).Local() } func (status *CommitStatus) loadRepo(e Engine) (err error) { diff --git a/models/token.go b/models/token.go index d4b86eb3ae..5053214b6e 100644 --- a/models/token.go +++ b/models/token.go @@ -7,7 +7,6 @@ package models import ( "time" - "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" "code.gitea.io/gitea/modules/base" @@ -22,22 +21,18 @@ type AccessToken struct { Created time.Time `xorm:"-"` CreatedUnix int64 `xorm:"INDEX created"` - Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet. + Updated time.Time `xorm:"-"` UpdatedUnix int64 `xorm:"INDEX updated"` HasRecentActivity bool `xorm:"-"` HasUsed bool `xorm:"-"` } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (t *AccessToken) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - t.Created = time.Unix(t.CreatedUnix, 0).Local() - case "updated_unix": - t.Updated = time.Unix(t.UpdatedUnix, 0).Local() - t.HasUsed = t.Updated.After(t.Created) - t.HasRecentActivity = t.Updated.Add(7 * 24 * time.Hour).After(time.Now()) - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (t *AccessToken) AfterLoad() { + t.Created = time.Unix(t.CreatedUnix, 0).Local() + t.Updated = time.Unix(t.UpdatedUnix, 0).Local() + t.HasUsed = t.Updated.After(t.Created) + t.HasRecentActivity = t.Updated.Add(7 * 24 * time.Hour).After(time.Now()) } // NewAccessToken creates new access token. diff --git a/models/twofactor.go b/models/twofactor.go index c983b56a5c..75b28b4c1b 100644 --- a/models/twofactor.go +++ b/models/twofactor.go @@ -11,7 +11,6 @@ import ( "time" "github.com/Unknwon/com" - "github.com/go-xorm/xorm" "github.com/pquerna/otp/totp" "code.gitea.io/gitea/modules/base" @@ -27,18 +26,14 @@ type TwoFactor struct { Created time.Time `xorm:"-"` CreatedUnix int64 `xorm:"INDEX created"` - Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet. + Updated time.Time `xorm:"-"` UpdatedUnix int64 `xorm:"INDEX updated"` } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (t *TwoFactor) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - t.Created = time.Unix(t.CreatedUnix, 0).Local() - case "updated_unix": - t.Updated = time.Unix(t.UpdatedUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (t *TwoFactor) AfterLoad() { + t.Created = time.Unix(t.CreatedUnix, 0).Local() + t.Updated = time.Unix(t.UpdatedUnix, 0).Local() } // GenerateScratchToken recreates the scratch token the user is using. diff --git a/models/user.go b/models/user.go index dbc15ae68b..bda80bfdf3 100644 --- a/models/user.go +++ b/models/user.go @@ -153,16 +153,11 @@ func (u *User) UpdateDiffViewStyle(style string) error { return UpdateUserCols(u, "diff_view_style") } -// AfterSet is invoked from XORM after setting the value of a field of this object. -func (u *User) AfterSet(colName string, _ xorm.Cell) { - switch colName { - case "created_unix": - u.Created = time.Unix(u.CreatedUnix, 0).Local() - case "updated_unix": - u.Updated = time.Unix(u.UpdatedUnix, 0).Local() - case "last_login_unix": - u.LastLogin = time.Unix(u.LastLoginUnix, 0).Local() - } +// AfterLoad is invoked from XORM after setting the values of all fields of this object. +func (u *User) AfterLoad() { + u.Created = time.Unix(u.CreatedUnix, 0).Local() + u.Updated = time.Unix(u.UpdatedUnix, 0).Local() + u.LastLogin = time.Unix(u.LastLoginUnix, 0).Local() } // getEmail returns an noreply email, if the user has set to keep his diff --git a/models/webhook.go b/models/webhook.go index bc7926c233..b6d1dff43b 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -19,7 +19,6 @@ import ( "code.gitea.io/gitea/modules/sync" api "code.gitea.io/sdk/gitea" - "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" ) @@ -112,20 +111,15 @@ type Webhook struct { UpdatedUnix int64 `xorm:"INDEX updated"` } -// AfterSet updates the webhook object upon setting a column -func (w *Webhook) AfterSet(colName string, _ xorm.Cell) { - var err error - switch colName { - case "events": - w.HookEvent = &HookEvent{} - if err = json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { - log.Error(3, "Unmarshal[%d]: %v", w.ID, err) - } - case "created_unix": - w.Created = time.Unix(w.CreatedUnix, 0).Local() - case "updated_unix": - w.Updated = time.Unix(w.UpdatedUnix, 0).Local() +// AfterLoad updates the webhook object upon setting a column +func (w *Webhook) AfterLoad() { + w.HookEvent = &HookEvent{} + if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { + log.Error(3, "Unmarshal[%d]: %v", w.ID, err) } + + w.Created = time.Unix(w.CreatedUnix, 0).Local() + w.Updated = time.Unix(w.UpdatedUnix, 0).Local() } // GetSlackHook returns slack metadata @@ -432,32 +426,17 @@ func (t *HookTask) BeforeUpdate() { } } -// AfterSet updates the webhook object upon setting a column -func (t *HookTask) AfterSet(colName string, _ xorm.Cell) { - var err error - switch colName { - case "delivered": - t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST") - - case "request_content": - if len(t.RequestContent) == 0 { - return - } - - t.RequestInfo = &HookRequest{} - if err = json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { - log.Error(3, "Unmarshal[%d]: %v", t.ID, err) - } +// AfterLoad updates the webhook object upon setting a column +func (t *HookTask) AfterLoad() { + t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST") - case "response_content": - if len(t.ResponseContent) == 0 { - return - } + if len(t.RequestContent) == 0 { + return + } - t.ResponseInfo = &HookResponse{} - if err = json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil { - log.Error(3, "Unmarshal [%d]: %v", t.ID, err) - } + t.RequestInfo = &HookRequest{} + if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { + log.Error(3, "Unmarshal[%d]: %v", t.ID, err) } } |