From 7cb17e016b2a1785c5c7cde017417e85e280328a Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Mon, 10 Oct 2016 22:24:11 +0200
Subject: [PATCH] Slack support multiple channels

---
 bot/bot.go                | 16 +++++++++-------
 bot/collection.go         |  1 -
 site/content.go           |  6 ------
 transport/irc/events.go   |  5 ++---
 transport/slack/events.go | 15 ++++++---------
 transport/slack/slack.go  | 17 +++++++++++++----
 6 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/bot/bot.go b/bot/bot.go
index f09e026..d2353df 100644
--- a/bot/bot.go
+++ b/bot/bot.go
@@ -14,20 +14,23 @@ import (
 type Bot interface {
 	Collection
 
-	ParseLine(author string, source string, line string, contents chan *site.Content) error
+	ParseLine(author string, line string, contents chan *site.Content) error
 }
 
 type PlayBot struct {
 	Collection
+
+	Source string
 }
 
 func NewPlayBot(source string, db Db, readers []site.Reader) *PlayBot {
 	return &PlayBot{
 		Collection: &PlayBotCollection{
-			Source:  source,
 			Db:      db,
 			Readers: readers,
 		},
+
+		Source: source,
 	}
 }
 
@@ -48,7 +51,7 @@ func (*PlayBot) ExtractTags(line string) []string {
 	return tags
 }
 
-func (pb *PlayBot) MakePost(author string, source string, url string, tags []string) *Post {
+func (pb *PlayBot) MakePost(author string, url string, tags []string) *Post {
 	content, err := pb.GetByUrl(url)
 	if err != nil {
 		log.Print(err)
@@ -59,24 +62,23 @@ func (pb *PlayBot) MakePost(author string, source string, url string, tags []str
 	post := &Post{
 		Date:    time.Now(),
 		Author:  author,
-		Source:  source,
+		Source:  pb.Source,
 		Content: content,
 	}
 
 	return post
 }
 
-func (pb *PlayBot) ParseLine(author string, source string, line string, contents chan *site.Content) error {
+func (pb *PlayBot) ParseLine(author string, line string, contents chan *site.Content) error {
 	tags := pb.ExtractTags(line)
 	urls := xurls.Strict.FindAllString(line, -1)
 	var wg sync.WaitGroup
 
 	for _, url := range urls {
-		log.Print(url)
 		wg.Add(1)
 		go func(url string) {
 			defer wg.Done()
-			post := pb.MakePost(author, source, url, tags)
+			post := pb.MakePost(author, url, tags)
 			pb.InsertPost(post)
 			if post != nil {
 				contents <- post.Content
diff --git a/bot/collection.go b/bot/collection.go
index c1adf6c..dc6f1c9 100644
--- a/bot/collection.go
+++ b/bot/collection.go
@@ -11,7 +11,6 @@ type Collection interface {
 }
 
 type PlayBotCollection struct {
-	Source  string
 	Db      Db
 	Readers []site.Reader
 }
diff --git a/site/content.go b/site/content.go
index a3d20fc..3b3829e 100644
--- a/site/content.go
+++ b/site/content.go
@@ -1,9 +1,5 @@
 package site
 
-import (
-	"log"
-)
-
 type Content struct {
 	Author       string `gorm:"column:sender"`
 	Duration     int
@@ -23,9 +19,7 @@ func (Content) TableName() string {
 }
 
 func (c *Content) AddTags(tags []string) {
-	log.Print(tags)
 	for _, tag := range tags {
 		c.Tags = append(c.Tags, Tag{Tag: tag})
-		log.Print(c.Tags)
 	}
 }
diff --git a/transport/irc/events.go b/transport/irc/events.go
index 4a4a84e..7a3b39b 100644
--- a/transport/irc/events.go
+++ b/transport/irc/events.go
@@ -11,7 +11,7 @@ func (t *IrcTransport) connected(conn *irc.Conn, line *irc.Line) {
 		t.Logger.Printf("Join %s", channel)
 		conn.Join(channel)
 
-		bot := t.botFactory.GetBot("irc.iiens.net")
+		bot := t.botFactory.GetBot(t.getSourceName(channel))
 		t.bots[channel] = bot
 	}
 }
@@ -26,8 +26,7 @@ func (t *IrcTransport) privmsg(conn *irc.Conn, line *irc.Line) {
 	bot := t.bots[channel]
 	contents := make(chan *site.Content)
 
-	source := t.getSourceName(channel)
-	go bot.ParseLine(line.Nick, source, msg, contents)
+	go bot.ParseLine(line.Nick, msg, contents)
 
 	for content := range contents {
 		t.printNewContent(conn, content, channel)
diff --git a/transport/slack/events.go b/transport/slack/events.go
index 2f22132..950b8cd 100644
--- a/transport/slack/events.go
+++ b/transport/slack/events.go
@@ -8,20 +8,17 @@ import (
 
 func (t *SlackTransport) connected(ev *slack.ConnectedEvent) {
 	t.Logger.Print("Connected")
-
-	for _, channel := range t.channels {
-		bot := t.botFactory.GetBot(t.Name)
-		t.bots[channel] = bot
-	}
-
-	t.bot = t.botFactory.GetBot(t.Name)
 }
 
 func (t *SlackTransport) message(ev *slack.MessageEvent) {
+	bot, ok := t.bots[ev.Channel]
+	if !ok {
+		return
+	}
+
 	contents := make(chan *site.Content)
 
-	source := t.getSourceName(ev.Channel)
-	go t.bot.ParseLine(ev.User, source, ev.Text, contents)
+	go bot.ParseLine(ev.User, ev.Text, contents)
 
 	for content := range contents {
 		t.printNewContent(content, ev.Channel)
diff --git a/transport/slack/slack.go b/transport/slack/slack.go
index ddf2bf6..a42da6b 100644
--- a/transport/slack/slack.go
+++ b/transport/slack/slack.go
@@ -17,7 +17,6 @@ type SlackTransport struct {
 	api        *slack.Client
 	bot        bot.Bot
 	bots       map[string]bot.Bot
-	channels   []string
 	botFactory bot.Factory
 	rtm        *slack.RTM
 	quit       chan bool
@@ -44,9 +43,19 @@ func New(name string, config map[string]interface{}, factory bot.Factory, quit c
 		},
 	}
 
-	channels, ok := config["chans"]
-	for _, channel := range channels.([]interface{}) {
-		t.channels = append(t.channels, channel.(string))
+	channels, err := t.api.GetChannels(true)
+	if err != nil {
+		return nil, err
+	}
+
+	for _, channel := range channels {
+		if !channel.IsMember {
+			continue
+		}
+
+		t.Logger.Printf("Create bot for %s", channel.Name)
+		bot := t.botFactory.GetBot(t.getSourceName(channel.Name))
+		t.bots[channel.ID] = bot
 	}
 
 	return t, nil
-- 
GitLab