Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the way user names are displayed in text export #132

Merged
merged 3 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 37 additions & 6 deletions internal/structures/user_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,42 @@ func NewUserIndex(us []slack.User) UserIndex {
return usermap
}

// Username tries to resolve the username by ID. If the user index is not
// initialised, it will return the ID, otherwise, if the user is not found in
// cache, it will assume that the user is external, and return the ID with
// "external" prefix.
// Username tries to resolve the username by ID. If it fails, it returns the
// user ID. If the user is not found in index, is assumes that it is an external
// user and returns ID with "external" prefix.
func (idx UserIndex) Username(id string) string {
return idx.userattr(id, func(user *slack.User) string {
return user.Name
})
}

// DisplayName tries to resolve the display name by ID. if the index is empty, it
// returns the user ID. If the user is not found in index, is assumes that it is
// an external user and returns ID with "external" prefix. If it does find the
// user and display name is unavailble, it returns the Real Name.
func (idx UserIndex) DisplayName(id string) string {
return idx.userattr(id, func(user *slack.User) string {
return nvl(user.Profile.DisplayName, user.RealName)
})
}

func nvl(s string, ss ...string) string {
if s != "" {
return s
}
for _, alt := range ss {
if alt != "" {
return alt
}
}
return "" // you got no luck at all, don't you.
}

// userattr finds the user by ID and calls a function fn with that user. If the
// user index is not initialised, it will return the ID, otherwise, if the user
// is not found in index, it will assume that the user is external, and return
// the ID with "external" prefix.
func (idx UserIndex) userattr(id string, fn func(user *slack.User) string) string {
if idx == nil {
// no user cache, use the IDs.
return id
Expand All @@ -33,7 +64,7 @@ func (idx UserIndex) Username(id string) string {
if !ok {
return "<external>:" + id
}
return user.Name
return fn(user)
}

// Sender returns username for the message
Expand All @@ -46,7 +77,7 @@ func (idx UserIndex) Sender(msg *slack.Message) string {
}

if userid != "" {
return idx.Username(userid)
return idx.DisplayName(userid)
}

return ""
Expand Down
40 changes: 40 additions & 0 deletions internal/structures/user_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,43 @@ func TestUserIndex_IsDeleted(t *testing.T) {
}

}

func Test_nvl(t *testing.T) {
type args struct {
s string
ss []string
}
tests := []struct {
name string
args args
want string
}{
{
"returns the fist arg",
args{"a", []string{"b", "c", "d"}},
"a",
},
{
"returns the fist arg",
args{"", []string{"b", "c", "d"}},
"b",
},
{
"returns the fist arg",
args{"", []string{"", "", "d"}},
"d",
},
{
"returns empty if everything is empty",
args{"", []string{"", "", ""}},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := nvl(tt.args.s, tt.args.ss...); got != tt.want {
t.Errorf("nvl() = %v, want %v", got, tt.want)
}
})
}
}