Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,6 @@ func (u *User) IsTokenAccessAllowed() bool {
return u.Type == UserTypeIndividual || u.Type == UserTypeBot
}

// DisplayName returns full name if it's not empty,
// returns username otherwise.
func (u *User) DisplayName() string {
trimmed := strings.TrimSpace(u.FullName)
if len(trimmed) > 0 {
return trimmed
}
return u.Name
}

// EmailTo returns a string suitable to be put into a e-mail `To:` header.
func (u *User) EmailTo() string {
sanitizedDisplayName := globalVars().emailToReplacer.Replace(u.DisplayName())
Expand All @@ -444,6 +434,21 @@ func (u *User) EmailTo() string {
return fmt.Sprintf("%s <%s>", mime.QEncoding.Encode("utf-8", add.Name), add.Address)
}

// TODO: there are 4-5 methods to display a user's "name", need to refactor them
// * user.Name / user.FullName (DefaultShowFullName): directly used in templates
// * user.DisplayName(): always show FullName if it's not empty, otherwise show Name
// * user.GetDisplayName(): show FullName if it's not empty and DefaultShowFullName is set, otherwise show Name

// DisplayName returns full name if it's not empty,
// returns username otherwise.
func (u *User) DisplayName() string {
trimmed := strings.TrimSpace(u.FullName)
if len(trimmed) > 0 {
return trimmed
}
return u.Name
}

// GetDisplayName returns full name if it's not empty and DEFAULT_SHOW_FULL_NAME is set,
// returns username otherwise.
func (u *User) GetDisplayName() string {
Expand All @@ -456,17 +461,6 @@ func (u *User) GetDisplayName() string {
return u.Name
}

// GetCompleteName returns the full name and username in the form of
// "Full Name (username)" if full name is not empty, otherwise it returns
// "username".
func (u *User) GetCompleteName() string {
trimmedFullName := strings.TrimSpace(u.FullName)
if len(trimmedFullName) > 0 {
return fmt.Sprintf("%s (%s)", trimmedFullName, u.Name)
}
return u.Name
}

func gitSafeName(name string) string {
return strings.TrimSpace(strings.NewReplacer("\n", "", "<", "", ">", "").Replace(name))
}
Expand Down
6 changes: 5 additions & 1 deletion modules/setting/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var UI = struct {
ReactionMaxUserNum int
MaxDisplayFileSize int64
ShowUserEmail bool
DefaultShowFullName bool
DefaultTheme string
Themes []string
FileIconTheme string
Expand All @@ -43,6 +42,11 @@ var UI = struct {

AmbiguousUnicodeDetection bool

// TODO: DefaultShowFullName is introduced by https://github.com/go-gitea/gitea/pull/6710
// But that PR didn't do it right. For most cases, either "username" or "username (Full Name)" should be used.
// Only in very few cases (e.g.: unimportant lists, narrow layout), "username" or "Full Name" can be used.
DefaultShowFullName bool

Notification struct {
MinTimeout time.Duration
TimeoutStep time.Duration
Expand Down
13 changes: 9 additions & 4 deletions services/mailer/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,23 @@ func (b64embedder *mailAttachmentBase64Embedder) AttachmentSrcToBase64DataURI(ct

func fromDisplayName(u *user_model.User) string {
if setting.MailService.FromDisplayNameFormatTemplate != nil {
var ctx bytes.Buffer
err := setting.MailService.FromDisplayNameFormatTemplate.Execute(&ctx, map[string]any{
var buf bytes.Buffer
err := setting.MailService.FromDisplayNameFormatTemplate.Execute(&buf, map[string]any{
"DisplayName": u.DisplayName(),
"AppName": setting.AppName,
"Domain": setting.Domain,
})
if err == nil {
return mime.QEncoding.Encode("utf-8", ctx.String())
return mime.QEncoding.Encode("utf-8", buf.String())
}
log.Error("fromDisplayName: %w", err)
}
return u.GetCompleteName()
def := u.Name
if fullName := strings.TrimSpace(u.FullName); fullName != "" {
// use "Full Name (username)" for email's sender name if Full Name is not empty
def = fullName + " (" + u.Name + ")"
Comment thread
wxiaoguang marked this conversation as resolved.
}
return def
}

func generateMetadataHeaders(repo *repo_model.Repository) map[string]string {
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/search_name.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<span class="gt-ellipsis">{{.Name}}{{if DefaultShowFullName}}<span class="search-fullname"> {{.FullName}}</span>{{end}}</span>
<span class="gt-ellipsis">{{.Name}}{{if .FullName}}<span class="search-fullname"> ({{.FullName}})</span>{{end}}</span>
4 changes: 2 additions & 2 deletions web_src/js/features/repo-issue-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ function initDropdownUserRemoteSearch(el: Element) {
// the content is provided by backend IssuePosters handler
processedResults.length = 0;
for (const item of resp.results) {
let nameHtml = html`<img class="ui avatar tw-align-middle" src="${item.avatar_link}" aria-hidden="true" alt width="20" height="20"><span class="gt-ellipsis">${item.username}</span>`;
if (item.full_name) nameHtml += html`<span class="search-fullname tw-ml-2">${item.full_name}</span>`;
let nameHtml = html`<img class="ui avatar tw-align-middle" src="${item.avatar_link}" aria-hidden="true" alt width="20" height="20"><span class="gt-ellipsis">${item.username}`;
nameHtml += item.full_name ? html`<span class="search-fullname"> (${item.full_name})</span></span>` : '</span>';
if (selectedUsername.toLowerCase() === item.username.toLowerCase()) selectedUsername = item.username;
processedResults.push({value: item.username, name: nameHtml});
}
Expand Down
Loading