From a3a934fff29a4ed4b0595a1d4c87d0c43156b812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Fri, 22 Jan 2016 04:45:41 +0100 Subject: [PATCH] Prevent duplicate users in ChannelStore --- storage/channel.go | 7 +++++++ storage/channel_test.go | 2 ++ 2 files changed, 9 insertions(+) diff --git a/storage/channel.go b/storage/channel.go index 3aa23e33..e358b916 100644 --- a/storage/channel.go +++ b/storage/channel.go @@ -51,6 +51,13 @@ func (c *ChannelStore) AddUser(user, server, channel string) { } if users, ok := c.users[server][channel]; ok { + for _, u := range users { + if u == user { + c.userLock.Unlock() + return + } + } + c.users[server][channel] = append(users, user) } else { c.users[server][channel] = []string{user} diff --git a/storage/channel_test.go b/storage/channel_test.go index add3107d..4002ff92 100644 --- a/storage/channel_test.go +++ b/storage/channel_test.go @@ -16,6 +16,8 @@ func TestGetSetUsers(t *testing.T) { func TestAddRemoveUser(t *testing.T) { channelStore := NewChannelStore() channelStore.AddUser("user", "srv", "#chan") + channelStore.AddUser("user", "srv", "#chan") + assert.Len(t, channelStore.GetUsers("srv", "#chan"), 1) channelStore.AddUser("user2", "srv", "#chan") assert.Equal(t, []string{"user", "user2"}, channelStore.GetUsers("srv", "#chan")) channelStore.RemoveUser("user", "srv", "#chan")