package main import ( "errors" "fmt" "strings" "time" "github.com/br0xen/boltease" ) type model struct { dbOpen int bolt *boltease.DB dbFileName string dbChanged bool Users []User FeedSources []FeedSource Feeds []Feed 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()) } m.Site = NewSiteData(m) if err = m.Site.LoadFromDB(); err != nil { return nil, err } if err = m.LoadUsers(); err != nil { return nil, err } if err = m.LoadFeeds(); err != nil { return nil, err } return m, nil } func (m *model) initDB() error { var err error if err = m.openDB(); err != nil { return err } defer m.closeDB() if err = m.bolt.MkBucketPath([]string{"site"}); err != nil { return err } if err = m.bolt.MkBucketPath([]string{"users"}); err != nil { return err } if err = m.bolt.MkBucketPath([]string{"feeds"}); err != nil { return err } return nil } 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() } } func (m *model) saveChanges() { fmt.Println("Saving Site to DB") m.Site.SaveToDB() //fmt.Println("Saving Feeds to DB") //m.SaveAllFeeds(m.FeedSources) fmt.Println("Saving Users to DB") 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) } } // So we have allSubs which contains all subscribed feeds for all users for _, sub := range allSubs { fmt.Println("Updating Feed: " + sub) pts := strings.Split(sub, ";") if len(pts) != 2 { continue } c, err := m.GetFeed(pts[0], pts[1]) if err != nil { fmt.Println(sub, ":", err) continue } if err = c.Update(); err != nil { fmt.Println(sub, ":", err.Error()) continue } if err = m.SaveFeed(c); err != nil { fmt.Println(sub, ":", err.Error()) continue } } } type Source interface { downloadList() []Feed getRssItem(slug string) (string, error) getUrl(slug string, dt time.Time) (string, error) getDescription(slug string, dt time.Time) (string, error) }