2020-10-17 13:30:30 +00:00
|
|
|
package feeds
|
|
|
|
|
|
|
|
import (
|
2020-11-08 19:51:05 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-17 13:30:30 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
2020-10-17 18:50:02 +00:00
|
|
|
"gorm.io/gorm/logger"
|
2020-10-17 13:30:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (fs *FeedStore) getDB() *gorm.DB {
|
2020-10-17 18:50:02 +00:00
|
|
|
if fs.db == nil {
|
2020-10-17 19:55:05 +00:00
|
|
|
db, err := gorm.Open(sqlite.Open(viper.GetString("DB_PATH")), &gorm.Config{
|
2020-10-17 18:50:02 +00:00
|
|
|
Logger: logger.Default.LogMode(logger.Error),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to connect database")
|
|
|
|
}
|
|
|
|
db.AutoMigrate(&Feed{})
|
|
|
|
db.AutoMigrate(&Item{})
|
|
|
|
|
|
|
|
fs.db = db
|
2020-10-17 13:30:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 18:50:02 +00:00
|
|
|
return fs.db
|
2020-10-17 13:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeedStore) GetFeed(id string) *Feed {
|
|
|
|
feed := &Feed{}
|
|
|
|
fs.getDB().Preload("Items").Where("id = ?", id).First(feed)
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
2021-02-21 10:29:07 +00:00
|
|
|
func (fs *FeedStore) DeleteFeed(id string) {
|
|
|
|
fs.getDB().Delete(Feed{ID: id})
|
|
|
|
fs.getDB().Unscoped().Delete(Item{}, "feed_id = ?", id)
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:30:30 +00:00
|
|
|
func (fs *FeedStore) GetItem(id string) *Item {
|
|
|
|
item := &Item{}
|
|
|
|
fs.getDB().Where("id = ?", id).First(item)
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeedStore) GetFeeds() *[]Feed {
|
|
|
|
feeds := &[]Feed{}
|
2020-10-17 18:02:52 +00:00
|
|
|
fs.getDB().Order("title asc").Find(feeds)
|
2020-10-17 13:30:30 +00:00
|
|
|
return feeds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeedStore) GetUnread() *[]ItemWithFeed {
|
|
|
|
items := &[]ItemWithFeed{}
|
|
|
|
fs.getDB().Table("items").
|
|
|
|
Where("read = ?", false).
|
|
|
|
Select("items.*, feeds.title as feed_title, feeds.homepage_url as feed_homepage_url").
|
|
|
|
Order("items.created desc, items.title").
|
|
|
|
Joins("left join feeds on feeds.id = items.feed_id").
|
|
|
|
Find(items)
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2020-11-08 20:19:41 +00:00
|
|
|
func (fs *FeedStore) GetSaved() *[]ItemWithFeed {
|
|
|
|
items := &[]ItemWithFeed{}
|
|
|
|
fs.getDB().Table("items").
|
|
|
|
Where("save = ?", true).
|
|
|
|
Select("items.*, feeds.title as feed_title, feeds.homepage_url as feed_homepage_url").
|
|
|
|
Order("items.created desc, items.title").
|
|
|
|
Joins("left join feeds on feeds.id = items.feed_id").
|
|
|
|
Find(items)
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:51:05 +00:00
|
|
|
func (fs *FeedStore) DeleteOldReadItems() {
|
|
|
|
t := time.Now()
|
|
|
|
threshold := t.Add(-time.Hour * 24 * 7)
|
|
|
|
fs.getDB().Table("items").
|
2020-11-08 19:56:31 +00:00
|
|
|
Where("save = ? and read = ? and created < ?", false, true, threshold).
|
2020-11-08 19:51:05 +00:00
|
|
|
Delete(Item{})
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:38:55 +00:00
|
|
|
func (fs *FeedStore) GetAll() *[]ItemWithFeed {
|
|
|
|
items := &[]ItemWithFeed{}
|
|
|
|
fs.getDB().Table("items").
|
|
|
|
Select("items.*, feeds.title as feed_title, feeds.homepage_url as feed_homepage_url").
|
|
|
|
Order("items.created desc, items.title").
|
|
|
|
Joins("left join feeds on feeds.id = items.feed_id").
|
|
|
|
Find(items)
|
|
|
|
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:30:30 +00:00
|
|
|
func (fs *FeedStore) SaveFeed(feed Feed) {
|
|
|
|
fs.getDB().Omit("Items").Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "id"}},
|
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"title", "description", "homepage_url", "image_url", "last_updated"}),
|
|
|
|
}).Create(feed)
|
|
|
|
|
|
|
|
for _, item := range feed.Items {
|
|
|
|
fs.getDB().Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "id"}},
|
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"title", "description", "content", "url", "image_url", "last_updated"}),
|
|
|
|
}).Create(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FeedStore) MarkAsRead(itemID string) {
|
|
|
|
item := &Item{}
|
|
|
|
fs.getDB().Where("id = ?", itemID).First(item)
|
|
|
|
|
|
|
|
item.Read = true
|
|
|
|
|
|
|
|
fs.getDB().Save(*item)
|
|
|
|
}
|
2020-11-08 20:19:41 +00:00
|
|
|
|
|
|
|
func (fs *FeedStore) ToggleSaved(itemID string) {
|
|
|
|
item := &Item{}
|
|
|
|
fs.getDB().Where("id = ?", itemID).First(item)
|
|
|
|
|
|
|
|
item.Save = !item.Save
|
|
|
|
|
|
|
|
fs.getDB().Save(*item)
|
|
|
|
}
|