diff --git a/irc_channel.go b/irc_channel.go new file mode 100644 index 0000000..3bca3be --- /dev/null +++ b/irc_channel.go @@ -0,0 +1,39 @@ +package main + +// Channel represents an IRC channel. It maps to Slack's groups and channels. +// Private messages are handled differently. +type Channel struct { + Members []string + Topic string + // Slack groups are different from channels. Here I try to uniform them for + // IRC, but I still need to know which is which to use the right API calls. + IsGroup bool +} + +// MembersDiff compares the members of this channel with another members list +// and return a slice of members who joined and a slice of members who left. +func (c Channel) MembersDiff(otherMembers []string) ([]string, []string) { + var membersMap = map[string]bool{} + for _, m := range c.Members { + membersMap[m] = true + } + var otherMembersMap = map[string]bool{} + for _, m := range otherMembers { + otherMembersMap[m] = true + } + + added := make([]string, 0) + for _, m := range otherMembers { + if _, ok := membersMap[m]; !ok { + added = append(added, m) + } + } + + removed := make([]string, 0) + for _, m := range c.Members { + if _, ok := otherMembersMap[m]; !ok { + removed = append(removed, m) + } + } + return added, removed +} diff --git a/irc_channel_test.go b/irc_channel_test.go new file mode 100644 index 0000000..67b3145 --- /dev/null +++ b/irc_channel_test.go @@ -0,0 +1,27 @@ +package main + +import ( + "testing" +) + +func TestMembersDiffEmpty(t *testing.T) { + c := Channel{Members: []string{}} + a, r := c.MembersDiff([]string{}) + if len(a) != 0 { + t.Fatalf("Added members: %v; want empty list", a) + } + if len(r) != 0 { + t.Fatalf("Removed members: %v; want empty list", r) + } +} + +func TestMembersDiffNonEmpty(t *testing.T) { + c := Channel{Members: []string{"removed1"}} + a, r := c.MembersDiff([]string{"added1"}) + if !(len(a) == 1 && a[0] == "added1") { + t.Fatalf("Added members: %v; want: %v", a, []string{"added1"}) + } + if !(len(r) == 1 && r[0] == "removed1") { + t.Fatalf("Removed members: %v; want: %v", a, []string{"removed1"}) + } +} diff --git a/irc_context.go b/irc_context.go index 6b0d740..f09d40a 100644 --- a/irc_context.go +++ b/irc_context.go @@ -8,44 +8,6 @@ import ( "github.com/nlopes/slack" ) -// Channel represents an IRC channel. It maps to Slack's groups and channels. -// Private messages are handled differently. -type Channel struct { - Members []string - Topic string - // Slack groups are different from channels. Here I try to uniform them for - // IRC, but I still need to know which is which to use the right API calls. - IsGroup bool -} - -// MembersDiff compares the members of this channel with another members list -// and return a slice of members who joined and a slice of members who left. -func (c Channel) MembersDiff(otherMembers []string) ([]string, []string) { - var membersMap = map[string]bool{} - for _, m := range c.Members { - membersMap[m] = true - } - var otherMembersMap = map[string]bool{} - for _, m := range otherMembers { - otherMembersMap[m] = true - } - - added := make([]string, 0) - for _, m := range otherMembers { - if _, ok := membersMap[m]; !ok { - added = append(added, m) - } - } - - removed := make([]string, 0) - for _, m := range c.Members { - if _, ok := otherMembersMap[m]; !ok { - removed = append(removed, m) - } - } - return added, removed -} - // IrcContext holds the client context information type IrcContext struct { Conn *net.TCPConn