From fa176d83c32da9a8dc3fc1145e834e2b95f2b795 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@cervoi.se>
Date: Wed, 5 Oct 2016 23:58:36 +0200
Subject: [PATCH] Try to save Post

---
 bot/bot.go              | 33 +++++++++++++++++++++++++--------
 bot/collection.go       | 18 ++++++++----------
 bot/db.go               | 41 ++++++++++++++++++++++++++++++++++++++++-
 bot/factory.go          | 27 ++++++++++++---------------
 bot/post.go             |  3 +--
 bot/tag.go              |  6 ------
 main.go                 |  5 ++++-
 site/content.go         |  9 ++++++++-
 site/youtube.go         |  3 ---
 transport/irc/events.go |  7 ++++---
 transport/irc/irc.go    |  4 ++++
 transport/irc/print.go  | 11 +++++++----
 12 files changed, 113 insertions(+), 54 deletions(-)
 delete mode 100644 bot/tag.go

diff --git a/bot/bot.go b/bot/bot.go
index c378019..7712d57 100644
--- a/bot/bot.go
+++ b/bot/bot.go
@@ -4,6 +4,7 @@ import (
 	"log"
 	"regexp"
 	"sync"
+	"time"
 
 	"github.com/mvdan/xurls"
 
@@ -13,7 +14,7 @@ import (
 type Bot interface {
 	Collection
 
-	ParseLine(author string, line string, contents chan *site.Content) error
+	ParseLine(author string, source string, line string, contents chan *site.Content) error
 }
 
 type PlayBot struct {
@@ -47,7 +48,25 @@ func (*PlayBot) ExtractTags(line string) []string {
 	return tags
 }
 
-func (pb *PlayBot) ParseLine(author string, line string, contents chan *site.Content) error {
+func (pb *PlayBot) MakePost(author string, source string, url string, tags []string) *Post {
+	content, err := pb.GetByUrl(url)
+	if err != nil {
+		log.Print(err)
+		return nil
+	}
+
+	content.AddTags(tags)
+	post := &Post{
+		Date:    time.Now(),
+		Author:  author,
+		Source:  source,
+		Content: content,
+	}
+
+	return post
+}
+
+func (pb *PlayBot) ParseLine(author string, source string, line string, contents chan *site.Content) error {
 	tags := pb.ExtractTags(line)
 	urls := xurls.Strict.FindAllString(line, -1)
 	var wg sync.WaitGroup
@@ -56,12 +75,10 @@ func (pb *PlayBot) ParseLine(author string, line string, contents chan *site.Con
 		wg.Add(1)
 		go func(url string) {
 			defer wg.Done()
-
-			content, err := pb.Add(url, tags)
-			if err != nil {
-				log.Print(err)
-			} else {
-				contents <- content
+			post := pb.MakePost(author, source, url, tags)
+			pb.InsertPost(post)
+			if post != nil {
+				contents <- post.Content
 			}
 		}(url)
 	}
diff --git a/bot/collection.go b/bot/collection.go
index 49d22cc..eb40344 100644
--- a/bot/collection.go
+++ b/bot/collection.go
@@ -1,14 +1,13 @@
 package bot
 
 import (
-	"log"
-
 	"git.iiens.net/morignot2011/playbot/site"
 )
 
 type Collection interface {
-	Add(url string, tags []string) (*site.Content, error)
-	Get()
+	GetByUrl(url string) (*site.Content, error)
+
+	InsertPost(*Post) error
 }
 
 type PlayBotCollection struct {
@@ -17,11 +16,9 @@ type PlayBotCollection struct {
 	Readers []site.Reader
 }
 
-func (pb *PlayBotCollection) Add(url string, tags []string) (*site.Content, error) {
-	log.Printf("%s: Add() %s, %q", pb.Source, url, tags)
-
+func (pbc *PlayBotCollection) GetByUrl(url string) (*site.Content, error) {
 	var content *site.Content
-	for _, reader := range pb.Readers {
+	for _, reader := range pbc.Readers {
 		var err error
 		content, err = reader.Read(url)
 		if err != nil {
@@ -32,9 +29,10 @@ func (pb *PlayBotCollection) Add(url string, tags []string) (*site.Content, erro
 		}
 	}
 
-	pb.Db.Create(content)
 	return content, nil
 }
 
-func (*PlayBotCollection) Get() {
+func (pbc *PlayBotCollection) InsertPost(post *Post) error {
+	pbc.Db.Insert(post)
+	return nil
 }
diff --git a/bot/db.go b/bot/db.go
index 90b83eb..2e182f7 100644
--- a/bot/db.go
+++ b/bot/db.go
@@ -1,12 +1,51 @@
 package bot
 
+import (
+	"fmt"
+	"log"
+
+	"github.com/jinzhu/gorm"
+	_ "github.com/jinzhu/gorm/dialects/mysql"
+)
+
 type DbParams struct {
 	Host     string
 	Port     string
 	User     string
 	Password string
+	DbName   string
 }
 
 type Db interface {
-	Create(interface{}) Db
+	Insert(interface{}) error
+}
+
+type PlayBotDb struct {
+	db *gorm.DB
+}
+
+func NewPlayBotDb(dbParams DbParams) (Db, error) {
+	conn := fmt.Sprintf(
+		"%s:%s@tcp(%s:%s)/%s",
+		dbParams.User,
+		dbParams.Password,
+		dbParams.Host,
+		dbParams.Port,
+		dbParams.DbName,
+	)
+
+	log.Printf("About to connect")
+	db, err := gorm.Open("mysql", conn)
+	log.Printf("Connected")
+	if err != nil {
+		return nil, err
+	}
+
+	db.LogMode(true)
+	return &PlayBotDb{db}, nil
+}
+
+func (pdb *PlayBotDb) Insert(value interface{}) error {
+	pdb.db.Create(value)
+	return nil
 }
diff --git a/bot/factory.go b/bot/factory.go
index 9db1936..63cf01d 100644
--- a/bot/factory.go
+++ b/bot/factory.go
@@ -1,8 +1,6 @@
 package bot
 
 import (
-	"log"
-
 	"git.iiens.net/morignot2011/playbot/site"
 )
 
@@ -11,23 +9,22 @@ type Factory interface {
 }
 
 type playBotFactory struct {
+	db      Db
 	readers []site.Reader
 }
 
-func NewPlayBotFactory(dbParams DbParams, readers []site.Reader) *playBotFactory {
-	return &playBotFactory{readers}
-}
-
-func (f *playBotFactory) GetBot(source string) Bot {
-	// give DB as arg
+func NewPlayBotFactory(dbParams DbParams, readers []site.Reader) (*playBotFactory, error) {
+	db, err := NewPlayBotDb(dbParams)
+	if err != nil {
+		return nil, err
+	}
 
-	db := &DummyDb{}
-	return NewPlayBot(source, db, f.readers)
+	return &playBotFactory{
+		db:      db,
+		readers: readers,
+	}, nil
 }
 
-type DummyDb struct{}
-
-func (db *DummyDb) Create(value interface{}) Db {
-	log.Printf("Create(): %q", value)
-	return db
+func (f *playBotFactory) GetBot(source string) Bot {
+	return NewPlayBot(source, f.db, f.readers)
 }
diff --git a/bot/post.go b/bot/post.go
index 0a37648..f4d8d8c 100644
--- a/bot/post.go
+++ b/bot/post.go
@@ -11,8 +11,7 @@ type Post struct {
 	Date    time.Time
 	Author  string `gorm:"column:sender_irc"`
 	Source  string `gorm:"column:chan"`
-	Content site.Content
-	Tag     Tag
+	Content *site.Content
 }
 
 func (Post) TableName() string {
diff --git a/bot/tag.go b/bot/tag.go
deleted file mode 100644
index a648842..0000000
--- a/bot/tag.go
+++ /dev/null
@@ -1,6 +0,0 @@
-package bot
-
-type Tag struct {
-	Tag       string
-	ContentId int `gorm:"colmun:id"`
-}
diff --git a/main.go b/main.go
index d850bd5..8f27e4f 100644
--- a/main.go
+++ b/main.go
@@ -83,7 +83,10 @@ func main() {
 		readers = append(readers, reader)
 	}
 
-	factory := bot.NewPlayBotFactory(config.Db, readers)
+	factory, err := bot.NewPlayBotFactory(config.Db, readers)
+	if err != nil {
+		log.Fatal(err)
+	}
 
 	for name, config := range config.Transports {
 		c := startTransport(name, config, factory)
diff --git a/site/content.go b/site/content.go
index 96109b3..3b3829e 100644
--- a/site/content.go
+++ b/site/content.go
@@ -9,10 +9,17 @@ type Content struct {
 	IsCollection bool   `gorm:"column:playlist"`
 	Source       string `gorm:"column:type"`
 	SourceId     string `gorm:"column:external_id"`
+	Tags         []Tag
 	Title        string
 	Url          string
 }
 
-func (Content) Table() string {
+func (Content) TableName() string {
 	return "playbot"
 }
+
+func (c *Content) AddTags(tags []string) {
+	for _, tag := range tags {
+		c.Tags = append(c.Tags, Tag{Tag: tag})
+	}
+}
diff --git a/site/youtube.go b/site/youtube.go
index 4f96853..e16f952 100644
--- a/site/youtube.go
+++ b/site/youtube.go
@@ -3,7 +3,6 @@ package site
 import (
 	"errors"
 	"fmt"
-	"log"
 	"net/http"
 	"regexp"
 
@@ -25,10 +24,8 @@ func NewYoutube(key string) (Reader, error) {
 }
 
 func (yt Youtube) Read(url string) (*Content, error) {
-	log.Print(url)
 	re := regexp.MustCompile(`(?:^|[^!])https?://(?:www.youtube.com/watch\?[a-zA-Z0-9_=&-]*v=|youtu.be/)([a-zA-Z0-9_-]+)`)
 	match := re.FindStringSubmatch(url)
-	log.Print(match)
 	if len(match) == 0 {
 		return nil, nil
 	}
diff --git a/transport/irc/events.go b/transport/irc/events.go
index 86c1867..362f5d2 100644
--- a/transport/irc/events.go
+++ b/transport/irc/events.go
@@ -10,9 +10,9 @@ func (t *IrcTransport) connected(conn *irc.Conn, line *irc.Line) {
 	for _, channel := range t.channels {
 		t.Logger.Printf("Join %s", channel)
 		conn.Join(channel)
+
 		bot := t.botFactory.GetBot("irc.iiens.net")
 		t.bots[channel] = bot
-		bot.Add("lol.com", []string{})
 	}
 }
 
@@ -26,9 +26,10 @@ func (t *IrcTransport) privmsg(conn *irc.Conn, line *irc.Line) {
 	b := t.bots[channel]
 	contents := make(chan *site.Content)
 
-	go b.ParseLine(line.Nick, msg, contents)
+	source := t.getSourceName(channel)
+	go b.ParseLine(line.Nick, source, msg, contents)
 
 	for content := range contents {
-		t.printContent(conn, content, channel)
+		t.printNewContent(conn, content, channel)
 	}
 }
diff --git a/transport/irc/irc.go b/transport/irc/irc.go
index 08a1f15..5bede3e 100644
--- a/transport/irc/irc.go
+++ b/transport/irc/irc.go
@@ -104,3 +104,7 @@ func (t *IrcTransport) Run() error {
 
 	return nil
 }
+
+func (t *IrcTransport) getSourceName(channel string) string {
+	return fmt.Sprintf("%s@%s", channel, t.Name)
+}
diff --git a/transport/irc/print.go b/transport/irc/print.go
index ec899b0..8129102 100644
--- a/transport/irc/print.go
+++ b/transport/irc/print.go
@@ -2,7 +2,6 @@ package irc
 
 import (
 	"fmt"
-	"log"
 
 	irc "github.com/fluffle/goirc/client"
 
@@ -36,7 +35,7 @@ const (
 	LIGHT_GREY  string = "\x0315"
 )
 
-func (t *IrcTransport) printContent(conn *irc.Conn, content *site.Content, channel string) {
+func (t *IrcTransport) printNewContent(conn *irc.Conn, content *site.Content, channel string) {
 	msg := fmt.Sprintf("%s[%d] %s%s", YELLOW, content.Id, GREEN, content.Title)
 
 	if content.Author != "" {
@@ -61,11 +60,15 @@ func (t *IrcTransport) printContent(conn *irc.Conn, content *site.Content, chann
 		msg += fmt.Sprintf("%02d)%s", s, NORMAL)
 	}
 
-	msg += " => " + content.Url + ORANGE
+	msg += NORMAL
+
+	for _, tag := range content.Tags {
+		msg += " #" + tag.Tag
+	}
 
 	conn.Privmsg(channel, msg)
 }
 
 func (t *IrcTransport) printError(err error) {
-	log.Printf("Error: %s", err)
+	t.Logger.Printf("Error: %s", err)
 }
-- 
GitLab