Basic chat persistence for #26

This commit is contained in:
Gabe Kangas
2020-07-12 14:53:33 -07:00
parent a55878ff22
commit 8cedf05214
7 changed files with 124 additions and 7 deletions

View File

@@ -19,12 +19,14 @@ func Setup(listener models.ChatListener) {
errCh := make(chan error)
// Demo messages only. Remove me eventually!!!
messages = append(messages, models.ChatMessage{"", "Tom Nook", "I'll be there with Bells on! Ho ho!", "https://gamepedia.cursecdn.com/animalcrossingpocketcamp_gamepedia_en/thumb/4/4f/Timmy_Icon.png/120px-Timmy_Icon.png?version=87b38d7d6130411d113486c2db151385", "demo-message-1", "CHAT", time.Now()})
messages = append(messages, models.ChatMessage{"", "Redd", "Fool me once, shame on you. Fool me twice, stop foolin' me.", "https://vignette.wikia.nocookie.net/animalcrossing/images/3/3d/Redd2.gif/revision/latest?cb=20100710004252", "demo-message-2", "CHAT", time.Now()})
messages = append(messages, models.ChatMessage{"", "Kevin", "You just caught me before I was about to go work out weeweewee!", "https://vignette.wikia.nocookie.net/animalcrossing/images/2/20/NH-Kevin_poster.png/revision/latest/scale-to-width-down/100?cb=20200410185817", "demo-message-3", "CHAT", time.Now()})
messages = append(messages, models.ChatMessage{"", "Isabelle", " Isabelle is the mayor's highly capable secretary. She can be forgetful sometimes, but you can always count on her for information about the town. She wears her hair up in a bun that makes her look like a shih tzu. Mostly because she is one! She also has a twin brother named Digby.", "https://dodo.ac/np/images/thumb/7/7b/IsabelleTrophyWiiU.png/200px-IsabelleTrophyWiiU.png", "demo-message-4", "CHAT", time.Now()})
messages = append(messages, models.ChatMessage{"", "Judy", "myohmy, I'm dancing my dreams away.", "https://vignette.wikia.nocookie.net/animalcrossing/images/5/50/NH-Judy_poster.png/revision/latest/scale-to-width-down/100?cb=20200522063219", "demo-message-5", "CHAT", time.Now()})
messages = append(messages, models.ChatMessage{"", "Blathers", "Blathers is an owl with brown feathers. His face is white and he has a yellow beak. His arms are wing shaped and he has yellow talons. His eyes are very big with small black irises. He also has big pink cheek circles on his cheeks. His belly appears to be checkered in diamonds with light brown and white squares, similar to an argyle vest, which is traditionally associated with academia. His green bowtie further alludes to his academic nature.", "https://vignette.wikia.nocookie.net/animalcrossing/images/b/b3/NH-character-Blathers.png/revision/latest?cb=20200229053519", "demo-message-6", "CHAT", time.Now()})
messages = append(messages, models.ChatMessage{"", "Tom Nook", "I'll be there with Bells on! Ho ho!", "https://gamepedia.cursecdn.com/animalcrossingpocketcamp_gamepedia_en/thumb/4/4f/Timmy_Icon.png/120px-Timmy_Icon.png?version=87b38d7d6130411d113486c2db151385", "demo-message-1", "CHAT", true, time.Now()})
messages = append(messages, models.ChatMessage{"", "Redd", "Fool me once, shame on you. Fool me twice, stop foolin' me.", "https://vignette.wikia.nocookie.net/animalcrossing/images/3/3d/Redd2.gif/revision/latest?cb=20100710004252", "demo-message-2", "CHAT", true, time.Now()})
messages = append(messages, models.ChatMessage{"", "Kevin", "You just caught me before I was about to go work out weeweewee!", "https://vignette.wikia.nocookie.net/animalcrossing/images/2/20/NH-Kevin_poster.png/revision/latest/scale-to-width-down/100?cb=20200410185817", "demo-message-3", "CHAT", true, time.Now()})
messages = append(messages, models.ChatMessage{"", "Isabelle", " Isabelle is the mayor's highly capable secretary. She can be forgetful sometimes, but you can always count on her for information about the town. She wears her hair up in a bun that makes her look like a shih tzu. Mostly because she is one! She also has a twin brother named Digby.", "https://dodo.ac/np/images/thumb/7/7b/IsabelleTrophyWiiU.png/200px-IsabelleTrophyWiiU.png", "demo-message-4", "CHAT", true, time.Now()})
messages = append(messages, models.ChatMessage{"", "Judy", "myohmy, I'm dancing my dreams away.", "https://vignette.wikia.nocookie.net/animalcrossing/images/5/50/NH-Judy_poster.png/revision/latest/scale-to-width-down/100?cb=20200522063219", "demo-message-5", "CHAT", true, time.Now()})
messages = append(messages, models.ChatMessage{"", "Blathers", "Blathers is an owl with brown feathers. His face is white and he has a yellow beak. His arms are wing shaped and he has yellow talons. His eyes are very big with small black irises. He also has big pink cheek circles on his cheeks. His belly appears to be checkered in diamonds with light brown and white squares, similar to an argyle vest, which is traditionally associated with academia. His green bowtie further alludes to his academic nature.", "https://vignette.wikia.nocookie.net/animalcrossing/images/b/b3/NH-character-Blathers.png/revision/latest?cb=20200229053519", "demo-message-6", "CHAT", true, time.Now()})
messages = append(messages, getChatHistory()...)
_server = &server{
messages,

102
core/chat/persistence.go Normal file
View File

@@ -0,0 +1,102 @@
package chat
import (
"database/sql"
"os"
"time"
"github.com/gabek/owncast/models"
"github.com/gabek/owncast/utils"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
)
var _db *sql.DB
func init() {
file := "./chat.db"
// Create empty DB file if it doesn't exist.
if !utils.DoesFileExists(file) {
log.Traceln("Creating new chat history database...")
_, err := os.Create(file)
if err != nil {
log.Fatal(err.Error())
}
}
sqliteDatabase, _ := sql.Open("sqlite3", file)
_db = sqliteDatabase
createTable(sqliteDatabase)
}
func createTable(db *sql.DB) {
createTableSQL := `CREATE TABLE IF NOT EXISTS messages (
"id" string NOT NULL PRIMARY KEY,
"author" TEXT,
"body" TEXT,
"image" TEXT,
"messageType" TEXT,
"visible" INTEGER,
"timestamp" DATE
);`
stmt, err := _db.Prepare(createTableSQL)
if err != nil {
log.Fatal(err)
}
stmt.Exec()
}
func addMessage(message models.ChatMessage) {
tx, err := _db.Begin()
stmt, err := tx.Prepare("INSERT INTO messages(id, author, body, image, messageType, visible, timestamp) values(?, ?, ?, ?, ?, ?, ?)")
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec(message.ID, message.Author, message.Body, message.Image, message.MessageType, 1, message.Timestamp)
if err != nil {
log.Fatal(err)
}
tx.Commit()
defer stmt.Close()
}
func getChatHistory() []models.ChatMessage {
history := make([]models.ChatMessage, 0)
rows, err := _db.Query("SELECT * FROM messages WHERE visible = 1")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var id string
var author string
var body string
var image string
var messageType string
var visible int
var timestamp time.Time
err = rows.Scan(&id, &author, &body, &image, &messageType, &visible, &timestamp)
if err != nil {
log.Fatal(err)
}
message := models.ChatMessage{}
message.ID = id
message.Author = author
message.Body = body
message.Image = image
message.MessageType = messageType
message.Visible = visible == 1
message.Timestamp = timestamp
history = append(history, message)
}
return history
}

View File

@@ -116,7 +116,7 @@ func (s *server) Listen() {
s.Messages = append(s.Messages, msg)
s.listener.MessageSent(msg)
s.sendAll(msg)
addMessage(msg)
case ping := <-s.pingCh:
fmt.Println("PING?", ping)