diff options
author | Joe Chen <jc@unknwon.io> | 2022-10-30 23:04:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-30 23:04:24 +0800 |
commit | 1905b19ee7fcc5622ad3ad4d85cc8fc2e81a0b56 (patch) | |
tree | 11c6c7bd33837733167f5f3adc9c09dacfbdd19f /internal/db/users.go | |
parent | 131be6e074039e590488892d7a99ebdbe6eb4668 (diff) |
refactor(db): migrate methods off `user.go` (#7219)
Diffstat (limited to 'internal/db/users.go')
-rw-r--r-- | internal/db/users.go | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/internal/db/users.go b/internal/db/users.go index bdd6501b..af0db727 100644 --- a/internal/db/users.go +++ b/internal/db/users.go @@ -22,6 +22,7 @@ import ( "gogs.io/gogs/internal/cryptoutil" "gogs.io/gogs/internal/errutil" "gogs.io/gogs/internal/osutil" + "gogs.io/gogs/internal/strutil" "gogs.io/gogs/internal/tool" "gogs.io/gogs/internal/userutil" ) @@ -451,9 +452,7 @@ type User struct { LoginSource int64 `xorm:"NOT NULL DEFAULT 0" gorm:"not null;default:0"` LoginName string Type UserType - OwnedOrgs []*User `xorm:"-" gorm:"-" json:"-"` - Orgs []*User `xorm:"-" gorm:"-" json:"-"` - Repos []*Repository `xorm:"-" gorm:"-" json:"-"` + Orgs []*User `xorm:"-" gorm:"-" json:"-"` Location string Website string Rands string `xorm:"VARCHAR(10)" gorm:"type:VARCHAR(10)"` @@ -521,6 +520,11 @@ func (u *User) IsOrganization() bool { return u.Type == UserTypeOrganization } +// IsMailable returns true if the user is eligible to receive emails. +func (u *User) IsMailable() bool { + return u.IsActive +} + // APIFormat returns the API format of a user. func (u *User) APIFormat() *api.User { return &api.User{ @@ -562,6 +566,15 @@ func (u *User) CanImportLocal() bool { return conf.Repository.EnableLocalPathMigration && (u.IsAdmin || u.AllowImportLocal) } +// DisplayName returns the full name of the user if it's not empty, returns the +// username otherwise. +func (u *User) DisplayName() string { + if len(u.FullName) > 0 { + return u.FullName + } + return u.Name +} + // HomeURLPath returns the URL path to the user or organization home page. // // TODO(unknwon): This is also used in templates, which should be fixed by @@ -649,3 +662,20 @@ func (u *User) IsUserOrgOwner(orgId int64) bool { func (u *User) IsPublicMember(orgId int64) bool { return IsPublicMembership(orgId, u.ID) } + +// GetOrganizationCount returns the count of organization membership that the +// user has. +// +// TODO(unknwon): This is also used in templates, which should be fixed by +// having a dedicated type `template.User`. +func (u *User) GetOrganizationCount() (int64, error) { + return OrgUsers.CountByUser(context.TODO(), u.ID) +} + +// ShortName truncates and returns the username at most in given length. +// +// TODO(unknwon): This is also used in templates, which should be fixed by +// having a dedicated type `template.User`. +func (u *User) ShortName(length int) string { + return strutil.Ellipsis(u.Name, length) +} |