Skip to content

Commit fd53028

Browse files
authored
Merge pull request #294 from Bwko/Lint/user.go
Lint models/user.go
2 parents b9b22b4 + 9963d61 commit fd53028

File tree

1 file changed

+46
-18
lines changed

1 file changed

+46
-18
lines changed

models/user.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"errors"
1313
"fmt"
1414
"image"
15+
// Needed for jpeg support
1516
_ "image/jpeg"
1617
"image/png"
1718
"os"
@@ -34,20 +35,35 @@ import (
3435
"code.gitea.io/gitea/modules/setting"
3536
)
3637

38+
// UserType defines the user type
3739
type UserType int
3840

3941
const (
42+
// UserTypeIndividual defines an individual user
4043
UserTypeIndividual UserType = iota // Historic reason to make it starts at 0.
44+
45+
// UserTypeOrganization defines an organization
4146
UserTypeOrganization
4247
)
4348

4449
var (
45-
ErrUserNotKeyOwner = errors.New("User does not the owner of public key")
46-
ErrEmailNotExist = errors.New("E-mail does not exist")
47-
ErrEmailNotActivated = errors.New("E-mail address has not been activated")
48-
ErrUserNameIllegal = errors.New("User name contains illegal characters")
50+
// ErrUserNotKeyOwner user does not own this key error
51+
ErrUserNotKeyOwner = errors.New("User does not own this public key")
52+
53+
// ErrEmailNotExist e-mail does not exist error
54+
ErrEmailNotExist = errors.New("E-mail does not exist")
55+
56+
// ErrEmailNotActivated e-mail address has not been activated error
57+
ErrEmailNotActivated = errors.New("E-mail address has not been activated")
58+
59+
// ErrUserNameIllegal user name contains illegal characters error
60+
ErrUserNameIllegal = errors.New("User name contains illegal characters")
61+
62+
// ErrLoginSourceNotActived login source is not actived error
4963
ErrLoginSourceNotActived = errors.New("Login source is not actived")
50-
ErrUnsupportedLoginType = errors.New("Login source is unknown")
64+
65+
// ErrUnsupportedLoginType login source is unknown error
66+
ErrUnsupportedLoginType = errors.New("Login source is unknown")
5167
)
5268

5369
// User represents the object of individual and member of organization.
@@ -112,28 +128,32 @@ type User struct {
112128
DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"`
113129
}
114130

131+
// BeforeInsert is invoked from XORM before inserting an object of this type.
115132
func (u *User) BeforeInsert() {
116133
u.CreatedUnix = time.Now().Unix()
117134
u.UpdatedUnix = u.CreatedUnix
118135
}
119136

137+
// BeforeUpdate is invoked from XORM before updating this object.
120138
func (u *User) BeforeUpdate() {
121139
if u.MaxRepoCreation < -1 {
122140
u.MaxRepoCreation = -1
123141
}
124142
u.UpdatedUnix = time.Now().Unix()
125143
}
126144

127-
// Set time to last login
145+
// SetLastLogin set time to last login
128146
func (u *User) SetLastLogin() {
129147
u.LastLoginUnix = time.Now().Unix()
130148
}
131149

150+
// UpdateDiffViewStyle updates the users diff view style
132151
func (u *User) UpdateDiffViewStyle(style string) error {
133152
u.DiffViewStyle = style
134153
return UpdateUser(u)
135154
}
136155

156+
// AfterSet is invoked from XORM after setting the value of a field of this object.
137157
func (u *User) AfterSet(colName string, _ xorm.Cell) {
138158
switch colName {
139159
case "full_name":
@@ -147,6 +167,7 @@ func (u *User) AfterSet(colName string, _ xorm.Cell) {
147167
}
148168
}
149169

170+
// APIFormat converts a User to api.User
150171
func (u *User) APIFormat() *api.User {
151172
return &api.User{
152173
ID: u.ID,
@@ -157,7 +178,7 @@ func (u *User) APIFormat() *api.User {
157178
}
158179
}
159180

160-
// returns true if user login type is LoginPlain.
181+
// IsLocal returns true if user login type is LoginPlain.
161182
func (u *User) IsLocal() bool {
162183
return u.LoginType <= LoginPlain
163184
}
@@ -168,13 +189,15 @@ func (u *User) HasForkedRepo(repoID int64) bool {
168189
return has
169190
}
170191

192+
// RepoCreationNum returns the number of repositories created by the user
171193
func (u *User) RepoCreationNum() int {
172194
if u.MaxRepoCreation <= -1 {
173195
return setting.Repository.MaxCreationLimit
174196
}
175197
return u.MaxRepoCreation
176198
}
177199

200+
// CanCreateRepo returns if user login can create a repository
178201
func (u *User) CanCreateRepo() bool {
179202
if u.MaxRepoCreation <= -1 {
180203
if setting.Repository.MaxCreationLimit <= -1 {
@@ -261,15 +284,15 @@ func (u *User) GenerateRandomAvatar() error {
261284
// which includes app sub-url as prefix. However, it is possible
262285
// to return full URL if user enables Gravatar-like service.
263286
func (u *User) RelAvatarLink() string {
264-
defaultImgUrl := setting.AppSubURL + "/img/avatar_default.png"
287+
defaultImgURL := setting.AppSubURL + "/img/avatar_default.png"
265288
if u.ID == -1 {
266-
return defaultImgUrl
289+
return defaultImgURL
267290
}
268291

269292
switch {
270293
case u.UseCustomAvatar:
271294
if !com.IsExist(u.CustomAvatarPath()) {
272-
return defaultImgUrl
295+
return defaultImgURL
273296
}
274297
return setting.AppSubURL + "/avatars/" + com.ToStr(u.ID)
275298
case setting.DisableGravatar, setting.OfflineMode:
@@ -293,7 +316,7 @@ func (u *User) AvatarLink() string {
293316
return link
294317
}
295318

296-
// User.GetFollwoers returns range of user's followers.
319+
// GetFollowers returns range of user's followers.
297320
func (u *User) GetFollowers(page int) ([]*User, error) {
298321
users := make([]*User, 0, ItemsPerPage)
299322
sess := x.
@@ -307,6 +330,7 @@ func (u *User) GetFollowers(page int) ([]*User, error) {
307330
return users, sess.Find(&users)
308331
}
309332

333+
// IsFollowing returns true if user is following followID.
310334
func (u *User) IsFollowing(followID int64) bool {
311335
return IsFollowing(u.ID, followID)
312336
}
@@ -418,13 +442,13 @@ func (u *User) IsOrganization() bool {
418442
}
419443

420444
// IsUserOrgOwner returns true if user is in the owner team of given organization.
421-
func (u *User) IsUserOrgOwner(orgId int64) bool {
422-
return IsOrganizationOwner(orgId, u.ID)
445+
func (u *User) IsUserOrgOwner(orgID int64) bool {
446+
return IsOrganizationOwner(orgID, u.ID)
423447
}
424448

425449
// IsPublicMember returns true if user public his/her membership in give organization.
426-
func (u *User) IsPublicMember(orgId int64) bool {
427-
return IsPublicMembership(orgId, u.ID)
450+
func (u *User) IsPublicMember(orgID int64) bool {
451+
return IsPublicMembership(orgID, u.ID)
428452
}
429453

430454
func (u *User) getOrganizationCount(e Engine) (int64, error) {
@@ -444,7 +468,7 @@ func (u *User) GetRepositories(page, pageSize int) (err error) {
444468
return err
445469
}
446470

447-
// GetRepositories returns mirror repositories that user owns, including private repositories.
471+
// GetMirrorRepositories returns mirror repositories that user owns, including private repositories.
448472
func (u *User) GetMirrorRepositories() ([]*Repository, error) {
449473
return GetUserMirrorRepositories(u.ID)
450474
}
@@ -481,6 +505,7 @@ func (u *User) DisplayName() string {
481505
return u.Name
482506
}
483507

508+
// ShortName ellipses username to length
484509
func (u *User) ShortName(length int) string {
485510
return base.EllipsisString(u.Name, length)
486511
}
@@ -542,6 +567,7 @@ func isUsableName(names, patterns []string, name string) error {
542567
return nil
543568
}
544569

570+
// IsUsableUsername returns an error when a username is reserved
545571
func IsUsableUsername(name string) error {
546572
return isUsableName(reservedUsernames, reservedUserPatterns, name)
547573
}
@@ -630,7 +656,7 @@ func getVerifyUser(code string) (user *User) {
630656
return nil
631657
}
632658

633-
// verify active code when active account
659+
// VerifyUserActiveCode verifies active code when active account
634660
func VerifyUserActiveCode(code string) (user *User) {
635661
minutes := setting.Service.ActiveCodeLives
636662

@@ -646,7 +672,7 @@ func VerifyUserActiveCode(code string) (user *User) {
646672
return nil
647673
}
648674

649-
// verify active code when active account
675+
// VerifyActiveEmailCode verifies active email code when active account
650676
func VerifyActiveEmailCode(code, email string) *EmailAddress {
651677
minutes := setting.Service.ActiveCodeLives
652678

@@ -1063,6 +1089,7 @@ func GetUserByEmail(email string) (*User, error) {
10631089
return nil, ErrUserNotExist{0, email, 0}
10641090
}
10651091

1092+
// SearchUserOptions contains the options for searching
10661093
type SearchUserOptions struct {
10671094
Keyword string
10681095
Type UserType
@@ -1123,6 +1150,7 @@ type Follow struct {
11231150
FollowID int64 `xorm:"UNIQUE(follow)"`
11241151
}
11251152

1153+
// IsFollowing returns true if user is following followID.
11261154
func IsFollowing(userID, followID int64) bool {
11271155
has, _ := x.Get(&Follow{UserID: userID, FollowID: followID})
11281156
return has

0 commit comments

Comments
 (0)