From 5885f7fbddd75f559c10c3b9bf74ba4fb50e981f Mon Sep 17 00:00:00 2001 From: Pavel Zaitsev Date: Mon, 27 Jul 2020 19:11:03 -0400 Subject: [PATCH] updated tests, moved code closer to the caller. * addded condition for zero time on lastMsg. * removed extra paramter in NamePrefix * moved code from NamePrefix to completeName * removed extra parameter in tests calling to NamePrefix --- chat/message/user.go | 8 +++++++- chat/room.go | 10 +--------- chat/room_test.go | 8 ++++---- host.go | 15 ++++++++++----- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/chat/message/user.go b/chat/message/user.go index 0fc8cc1..3f96e28 100644 --- a/chat/message/user.go +++ b/chat/message/user.go @@ -258,5 +258,11 @@ func (a RecentActiveUsers) Less(i, j int) bool { defer a[i].mu.Unlock() a[j].mu.Lock() defer a[j].mu.Unlock() - return a[i].lastMsg.After(a[j].lastMsg) + + if a[i].lastMsg.IsZero() { + return a[i].joined.Before(a[j].joined) + } else { + return a[i].lastMsg.After(a[j].lastMsg) + } + } diff --git a/chat/room.go b/chat/room.go index 12f8ce8..5b34508 100644 --- a/chat/room.go +++ b/chat/room.go @@ -226,7 +226,7 @@ func (r *Room) SetTopic(s string) { // NamesPrefix lists all members' names with a given prefix, used to query // for autocompletion purposes. Sorted by which user was last active. -func (r *Room) NamesPrefix(prefix string, current_user *message.User) []string { +func (r *Room) NamesPrefix(prefix string) []string { items := r.Members.ListPrefix(prefix) // Sort results by recently active @@ -235,14 +235,6 @@ func (r *Room) NamesPrefix(prefix string, current_user *message.User) []string { users = append(users, item.Value().(*Member).User) } sort.Sort(message.RecentActiveUsers(users)) - for i, user := range users { - if user.Name() == current_user.Name() { - // move it to the end. one user in the list? - save := users[0] - copy(users[i:], users[i+1:]) - users[len(users)-1] = save - } - } // Pull out names names := make([]string, 0, len(items)) diff --git a/chat/room_test.go b/chat/room_test.go index 110c033..c316e15 100644 --- a/chat/room_test.go +++ b/chat/room_test.go @@ -394,20 +394,20 @@ func TestRoomNamesPrefix(t *testing.T) { members[3].HandleMsg(message.NewMsg("hi")) // foo members[1].HandleMsg(message.NewMsg("hi")) // aab - if got, want := r.NamesPrefix("a", members[3].User), []string{"aab", "aaa", "aac"}; !reflect.DeepEqual(got, want) { + if got, want := r.NamesPrefix("a"), []string{"aab", "aaa", "aac"}; !reflect.DeepEqual(got, want) { t.Errorf("got: %q; want: %q", got, want) } members[2].HandleMsg(message.NewMsg("hi")) // aac - if got, want := r.NamesPrefix("a", members[3].User), []string{"aac", "aab", "aaa"}; !reflect.DeepEqual(got, want) { + if got, want := r.NamesPrefix("a"), []string{"aac", "aab", "aaa"}; !reflect.DeepEqual(got, want) { t.Errorf("got: %q; want: %q", got, want) } - if got, want := r.NamesPrefix("f", members[0].User), []string{"foo"}; !reflect.DeepEqual(got, want) { + if got, want := r.NamesPrefix("f"), []string{"foo"}; !reflect.DeepEqual(got, want) { t.Errorf("got: %q; want: %q", got, want) } - if got, want := r.NamesPrefix("bar", members[3].User), []string{}; !reflect.DeepEqual(got, want) { + if got, want := r.NamesPrefix("bar"), []string{}; !reflect.DeepEqual(got, want) { t.Errorf("got: %q; want: %q", got, want) } } diff --git a/host.go b/host.go index 808d9fc..7be6aed 100644 --- a/host.go +++ b/host.go @@ -243,14 +243,19 @@ func (h *Host) Serve() { h.listener.Serve() } -func (h *Host) completeName(partial string, current_user *message.User) string { - names := h.NamesPrefix(partial, current_user) +func (h *Host) completeName(partial string, skipName string) string { + names := h.NamesPrefix(partial) if len(names) == 0 { // Didn't find anything return "" + } else if name := names[0]; name != skipName { + // First name is not the skipName, great + return name + } else if len(names) > 1 { + // Next candidate + return names[1] } - - return names[len(names)-1] + return "" } func (h *Host) completeCommand(partial string) string { @@ -300,7 +305,7 @@ func (h *Host) AutoCompleteFunction(u *message.User) func(line string, pos int, } } else { // Name - completed = h.completeName(partial, u) + completed = h.completeName(partial, u.Name()) if completed == "" { return }