diff --git a/bot/bot.go b/bot/bot.go index f09e0263203186b1e8058ef02fae2918663a4a13..d2353df5597bcdace447fd3ce32e4288e847b44e 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 c1adf6c1e296031c61de118cdd33427f1ec53cc7..dc6f1c9c981a7734334fea9579682bff90da2193 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 a3d20fc1b70eaac1653b0906896fadbac417b8d0..3b3829e7dfc270350957c8be57c87fabbb58ea9f 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 4a4a84eadae0d4e1e8f6c00e44b19db0905f8974..7a3b39b28edd179e61666c0e4f873c9c16fd7819 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 2f2213242670a46d8dd142d6904ed93678c74343..950b8cdfeef467a07c512442b7f5ace55c82482c 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 ddf2bf69f24cce126c8e3a6b19681dfed6798c04..a42da6b28b6f688da3f651525cc8bb02ce389ec7 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