mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-23 10:13:57 +02:00
fix(issues): use unified user display name pattern for more places (#39852)
* Fix #39761 --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
1 parent
f8dd2dd4cc
commit
3701fcec84
7 files changed
+31
-27
No files matched your search
@@ -350,7 +350,7 @@ func (list ReactionList) GetFirstUsers() string {
|
||||
if buffer.Len() > 0 {
|
||||
buffer.WriteString(", ")
|
||||
}
|
||||
buffer.WriteString(reaction.User.Name)
|
||||
buffer.WriteString(reaction.User.DisplayNameWithFullName())
|
||||
if rem--; rem == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -465,6 +465,7 @@ func (u *User) EmailTo() string {
|
||||
}
|
||||
|
||||
// TODO: DefaultShowFullName causes messy logic, there are already too many methods to display a user's "display name", need to refactor them
|
||||
// Search this TODO to see more details
|
||||
// * user.Name / user.FullName: 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
|
||||
@@ -491,6 +492,14 @@ func (u *User) GetDisplayName() string {
|
||||
return u.Name
|
||||
}
|
||||
|
||||
// DisplayNameWithFullName returns the username, full name is included if it is not empty
|
||||
func (u *User) DisplayNameWithFullName() string {
|
||||
if fullName := strings.TrimSpace(u.FullName); fullName != "" {
|
||||
return fmt.Sprintf("%s (%s)", u.Name, fullName)
|
||||
}
|
||||
return u.Name
|
||||
}
|
||||
|
||||
// ShortName ellipses username to length (still used by many templates), it calls GetDisplayName and respects DEFAULT_SHOW_FULL_NAME
|
||||
func (u *User) ShortName(length int) string {
|
||||
return util.EllipsisDisplayString(u.GetDisplayName(), length)
|
||||
|
||||
@@ -295,18 +295,13 @@ func TestNewGitSig(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDisplayName(t *testing.T) {
|
||||
users := make([]*user_model.User, 0, 20)
|
||||
err := db.GetEngine(t.Context()).Find(&users)
|
||||
assert.NoError(t, err)
|
||||
u := &user_model.User{Name: "name"}
|
||||
assert.Equal(t, "name", u.DisplayName())
|
||||
assert.Equal(t, "name", u.DisplayNameWithFullName())
|
||||
|
||||
for _, user := range users {
|
||||
displayName := user.DisplayName()
|
||||
assert.Equal(t, strings.TrimSpace(displayName), displayName)
|
||||
if len(strings.TrimSpace(user.FullName)) == 0 {
|
||||
assert.Equal(t, user.Name, displayName)
|
||||
}
|
||||
assert.NotEmpty(t, strings.TrimSpace(displayName))
|
||||
}
|
||||
u = &user_model.User{Name: "name", FullName: "Full Name"}
|
||||
assert.Equal(t, "Full Name", u.DisplayName())
|
||||
assert.Equal(t, "name (Full Name)", u.DisplayNameWithFullName())
|
||||
}
|
||||
|
||||
func TestCreateUserEmailAlreadyUsed(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user