48 lines
1003 B
Go
48 lines
1003 B
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.eeqj.de/sneak/feta/toot"
|
|
|
|
"github.com/google/uuid"
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
|
)
|
|
|
|
func (m *Manager) TootExists(t *toot.Toot) bool {
|
|
var try StoredToot
|
|
if m.db.Where("Hash = ?", t.GetHash()).First(&try).RecordNotFound() {
|
|
return false
|
|
} else {
|
|
return true
|
|
}
|
|
}
|
|
|
|
func (m *Manager) StoreToot(t *toot.Toot) error {
|
|
|
|
nt := new(StoredToot)
|
|
nt.UUID = uuid.New()
|
|
nt.ServerCreated = t.Parsed.CreatedAt
|
|
nt.Original = t.Original
|
|
// FIXME normalize this, check for @ and append hostname if none
|
|
nt.Acct = fmt.Sprintf("%s@%s", t.Parsed.Account.Acct, strings.ToLower(t.FromHost))
|
|
nt.URL = t.Parsed.URL
|
|
nt.Content = t.Parsed.Content
|
|
nt.Hostname = strings.ToLower(t.FromHost)
|
|
nt.Hash = t.GetHash()
|
|
r := m.db.Create(&nt)
|
|
//panic(fmt.Sprintf("%+v", t))
|
|
return r.Error
|
|
}
|
|
|
|
func (m *Manager) StoreToots(tc []*toot.Toot) error {
|
|
for _, item := range tc {
|
|
err := m.StoreToot(item)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|