updated tests, moved code closer to the caller.
authorPavel Zaitsev <pavel@arslogi.ca>
Mon, 27 Jul 2020 23:11:03 +0000 (19:11 -0400)
committerPavel Zaitsev <pavel@arslogi.ca>
Wed, 29 Jul 2020 22:23:34 +0000 (18:23 -0400)
* 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
chat/room.go
chat/room_test.go
host.go

index 0fc8cc18ec6a22d647af5369c016737fc5e27253..3f96e281bdeb74cfe5e3daa26c2128d1ec2e9503 100644 (file)
@@ -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)
+       }
+
 }
index 12f8ce89d6d79342a172dd2e16bc98f4e8449d15..5b345089ccea50445272fc7a3fc49f372125c9ce 100644 (file)
@@ -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))
index 110c033ec1973241150948c0e19ee096fa5210bb..c316e15d8df5f60f0c7b796fda941146d319d857 100644 (file)
@@ -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 808d9fc5bbae8f15d20bfb84ad226f47661a61b4..7be6aed1f88233b6b9f484e942f8373eec9c93dd 100644 (file)
--- 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
                        }