88 "context"
99 "encoding/hex"
1010 "fmt"
11+ "html/template"
1112 "mime"
1213 "net/mail"
1314 "net/url"
@@ -28,6 +29,7 @@ import (
2829 "code.gitea.io/gitea/modules/base"
2930 "code.gitea.io/gitea/modules/container"
3031 "code.gitea.io/gitea/modules/git"
32+ "code.gitea.io/gitea/modules/htmlutil"
3133 "code.gitea.io/gitea/modules/httplib"
3234 "code.gitea.io/gitea/modules/log"
3335 "code.gitea.io/gitea/modules/optional"
@@ -417,16 +419,6 @@ func (u *User) IsTokenAccessAllowed() bool {
417419 return u .Type == UserTypeIndividual || u .Type == UserTypeBot
418420}
419421
420- // DisplayName returns full name if it's not empty,
421- // returns username otherwise.
422- func (u * User ) DisplayName () string {
423- trimmed := strings .TrimSpace (u .FullName )
424- if len (trimmed ) > 0 {
425- return trimmed
426- }
427- return u .Name
428- }
429-
430422// EmailTo returns a string suitable to be put into a e-mail `To:` header.
431423func (u * User ) EmailTo () string {
432424 sanitizedDisplayName := globalVars ().emailToReplacer .Replace (u .DisplayName ())
@@ -445,27 +437,45 @@ func (u *User) EmailTo() string {
445437 return fmt .Sprintf ("%s <%s>" , mime .QEncoding .Encode ("utf-8" , add .Name ), add .Address )
446438}
447439
448- // GetDisplayName returns full name if it's not empty and DEFAULT_SHOW_FULL_NAME is set,
449- // returns username otherwise.
440+ // TODO: DefaultShowFullName causes messy logic, there are already too many methods to display a user's "display name", need to refactor them
441+ // * user.Name / user.FullName: directly used in templates
442+ // * user.DisplayName(): always show FullName if it's not empty, otherwise show Name
443+ // * user.GetDisplayName(): show FullName if it's not empty and DefaultShowFullName is set, otherwise show Name
444+ // * user.ShortName(): used a lot in templates, but it should be removed and let frontend use "ellipsis" styles
445+ // * activity action.ShortActUserName/GetActDisplayName/GetActDisplayNameTitle, etc: duplicate and messy
446+
447+ // DisplayName returns full name if it's not empty, returns username otherwise.
448+ func (u * User ) DisplayName () string {
449+ fullName := strings .TrimSpace (u .FullName )
450+ if fullName != "" {
451+ return fullName
452+ }
453+ return u .Name
454+ }
455+
456+ // GetDisplayName returns full name if it's not empty and DEFAULT_SHOW_FULL_NAME is set, otherwise, username.
450457func (u * User ) GetDisplayName () string {
451458 if setting .UI .DefaultShowFullName {
452- trimmed := strings .TrimSpace (u .FullName )
453- if len ( trimmed ) > 0 {
454- return trimmed
459+ fullName := strings .TrimSpace (u .FullName )
460+ if fullName != "" {
461+ return fullName
455462 }
456463 }
457464 return u .Name
458465}
459466
460- // GetCompleteName returns the full name and username in the form of
461- // "Full Name (username)" if full name is not empty, otherwise it returns
462- // "username".
463- func (u * User ) GetCompleteName () string {
464- trimmedFullName := strings .TrimSpace (u .FullName )
465- if len (trimmedFullName ) > 0 {
466- return fmt .Sprintf ("%s (%s)" , trimmedFullName , u .Name )
467+ // ShortName ellipses username to length (still used by many templates), it calls GetDisplayName and respects DEFAULT_SHOW_FULL_NAME
468+ func (u * User ) ShortName (length int ) string {
469+ return util .EllipsisDisplayString (u .GetDisplayName (), length )
470+ }
471+
472+ func (u * User ) GetShortDisplayNameLinkHTML () template.HTML {
473+ fullName := strings .TrimSpace (u .FullName )
474+ displayName , displayTooltip := u .Name , fullName
475+ if setting .UI .DefaultShowFullName && fullName != "" {
476+ displayName , displayTooltip = fullName , u .Name
467477 }
468- return u . Name
478+ return htmlutil . HTMLFormat ( `<a class="muted" href="%s" data-tooltip-content="%s">%s</a>` , u . HomeLink (), displayTooltip , displayName )
469479}
470480
471481func gitSafeName (name string ) string {
@@ -488,14 +498,6 @@ func (u *User) GitName() string {
488498 return fmt .Sprintf ("user-%d" , u .ID )
489499}
490500
491- // ShortName ellipses username to length
492- func (u * User ) ShortName (length int ) string {
493- if setting .UI .DefaultShowFullName && len (u .FullName ) > 0 {
494- return util .EllipsisDisplayString (u .FullName , length )
495- }
496- return util .EllipsisDisplayString (u .Name , length )
497- }
498-
499501// IsMailable checks if a user is eligible to receive emails.
500502// System users like Ghost and Gitea Actions are excluded.
501503func (u * User ) IsMailable () bool {
0 commit comments