ribbit/model.go

135 lines
2.5 KiB
Go
Raw Permalink Normal View History

2018-04-26 11:53:03 +00:00
package main
import (
"errors"
"fmt"
"strings"
"time"
"github.com/br0xen/boltease"
)
type model struct {
2020-09-28 18:03:57 +00:00
dbOpen int
2018-04-26 11:53:03 +00:00
bolt *boltease.DB
dbFileName string
2020-09-28 18:03:57 +00:00
dbChanged bool
2018-04-26 11:53:03 +00:00
2019-01-13 14:22:29 +00:00
Users []User
FeedSources []FeedSource
Feeds []Feed
2018-04-26 11:53:03 +00:00
Site *SiteData
}
func NewModel() (*model, error) {
var err error
m := new(model)
m.dbFileName = DbName
m.bolt, err = boltease.Create(m.dbFileName, 0600, nil)
if err != nil {
return nil, err
}
if err = m.initDB(); err != nil {
return nil, errors.New("Unable to initialzie DB: " + err.Error())
}
2019-01-10 16:39:42 +00:00
m.Site = NewSiteData(m)
if err = m.Site.LoadFromDB(); err != nil {
return nil, err
}
2018-04-26 11:53:03 +00:00
if err = m.LoadUsers(); err != nil {
return nil, err
}
2019-01-10 16:39:42 +00:00
if err = m.LoadFeeds(); err != nil {
2018-04-26 11:53:03 +00:00
return nil, err
}
return m, nil
}
func (m *model) initDB() error {
var err error
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2018-04-26 11:53:03 +00:00
return err
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2018-04-26 11:53:03 +00:00
if err = m.bolt.MkBucketPath([]string{"site"}); err != nil {
return err
}
if err = m.bolt.MkBucketPath([]string{"users"}); err != nil {
return err
}
2019-01-10 16:39:42 +00:00
if err = m.bolt.MkBucketPath([]string{"feeds"}); err != nil {
2018-04-26 11:53:03 +00:00
return err
}
return nil
}
2020-09-28 18:03:57 +00:00
func (m *model) openDB() error {
m.dbOpen = m.dbOpen + 1
if m.dbOpen > 1 {
return nil
}
var err error
if err = m.openDB(); err != nil {
return err
}
return nil
}
func (m *model) closeDB() {
m.dbOpen = m.dbOpen - 1
if m.dbOpen == 0 {
m.closeDB()
}
}
2018-04-26 11:53:03 +00:00
func (m *model) saveChanges() {
2020-09-28 18:03:57 +00:00
fmt.Println("Saving Site to DB")
2019-01-10 16:39:42 +00:00
m.Site.SaveToDB()
2020-09-28 18:03:57 +00:00
//fmt.Println("Saving Feeds to DB")
//m.SaveAllFeeds(m.FeedSources)
fmt.Println("Saving Users to DB")
2018-04-26 11:53:03 +00:00
m.SaveAllUsers(m.Users)
}
func (m *model) UpdateAllUserFeeds() {
var allSubs []string
for _, usr := range m.Users {
// Pull all user subs
for _, sub := range usr.SubSlugs {
allSubs = addStringIfUnique(sub, allSubs)
}
}
2019-01-10 16:39:42 +00:00
// So we have allSubs which contains all subscribed feeds for all users
2018-04-26 11:53:03 +00:00
for _, sub := range allSubs {
2019-01-10 16:39:42 +00:00
fmt.Println("Updating Feed: " + sub)
2018-04-26 11:53:03 +00:00
pts := strings.Split(sub, ";")
if len(pts) != 2 {
continue
}
2019-01-10 16:39:42 +00:00
c, err := m.GetFeed(pts[0], pts[1])
2018-04-26 11:53:03 +00:00
if err != nil {
fmt.Println(sub, ":", err)
continue
}
if err = c.Update(); err != nil {
fmt.Println(sub, ":", err.Error())
continue
}
2019-01-10 16:39:42 +00:00
if err = m.SaveFeed(c); err != nil {
2018-04-26 11:53:03 +00:00
fmt.Println(sub, ":", err.Error())
continue
}
}
}
type Source interface {
2019-01-10 16:39:42 +00:00
downloadList() []Feed
getRssItem(slug string) (string, error)
getUrl(slug string, dt time.Time) (string, error)
getDescription(slug string, dt time.Time) (string, error)
}