Converted RSS feed to be DB based

This commit is contained in:
2024-03-09 10:12:46 +01:00
parent fa5f189cda
commit 88e0d5086c
5 changed files with 88 additions and 136 deletions

View File

@ -258,13 +258,13 @@ func (db *DB) GetArticle(id int64) (*Article, error) {
return article, nil
}
func (db *DB) GetUnpublishedArticles() ([]*Article, error) {
func (db *DB) GetCertainArticles(published bool) ([]*Article, error) {
query := `
SELECT id, title, created, description, content, author_id
FROM articles
WHERE published = ?
`
rows, err := db.Query(query, false)
rows, err := db.Query(query, published)
if err != nil {
return nil, fmt.Errorf("error querying articles: %v", err)
}

View File

@ -1,104 +1,49 @@
package data
import (
"encoding/gob"
"fmt"
"os"
"sync"
"time"
"git.streifling.com/jason/rss"
)
type Channel struct {
addCh chan *rss.Item
setCh chan rss.Channel
getCh chan rss.Channel
channel rss.Channel
wg sync.WaitGroup
}
func initChannel() *Channel {
return &Channel{
addCh: make(chan *rss.Item),
setCh: make(chan rss.Channel),
getCh: make(chan rss.Channel),
channel: rss.Channel{
Items: make([]*rss.Item, 0),
},
}
}
func (c *Channel) start() {
c.wg.Done()
for {
select {
case item := <-c.addCh:
c.channel.Items = append(c.channel.Items, item)
case c.getCh <- c.channel:
case c.channel = <-c.setCh:
}
}
}
func NewChannel(title, link, desc string) *Channel {
channel := initChannel()
channel.channel = rss.Channel{
func GetChannel(db *DB, title, link, desc string) (*rss.Channel, error) {
channel := &rss.Channel{
Title: title,
Link: link,
Description: desc,
Items: make([]*rss.Item, 0),
}
channel.wg.Add(1)
go channel.start()
channel.wg.Wait()
return channel
}
func (c *Channel) Get() rss.Channel {
return <-c.getCh
}
func (f *Channel) Set(channel rss.Channel) {
f.setCh <- channel
}
func LoadChannel(filename string) (*Channel, error) {
file, err := os.Open(filename)
articles, err := db.GetCertainArticles(true)
if err != nil {
return nil, fmt.Errorf("error opening file %v: %v", filename, err)
}
defer file.Close()
tmpChannel := new(rss.Channel)
if err = gob.NewDecoder(file).Decode(tmpChannel); err != nil {
return nil, fmt.Errorf("error decoding channel from file %v: %v", filename, err)
return nil, fmt.Errorf("error fetching published articles: %v", err)
}
channel := initChannel()
channel.wg.Add(1)
go channel.start()
channel.wg.Wait()
channel.Set(*tmpChannel)
for _, article := range articles {
tags, err := db.GetArticleTags(article.ID)
if err != nil {
return nil, fmt.Errorf("error fetching tags for article %v: %v", article.Title, err)
}
tagNames := make([]string, 0)
for _, tag := range tags {
tagNames = append(tagNames, tag.Name)
}
user, err := db.GetUser(article.AuthorID)
if err != nil {
return nil, fmt.Errorf("error finding user %v: %v", article.AuthorID, err)
}
channel.Items = append(channel.Items, &rss.Item{
Title: article.Title,
Author: user.FirstName + user.LastName,
PubDate: article.Created.Format(time.RFC1123Z),
Description: article.Desc,
Content: &rss.Content{Value: article.Content},
Categories: tagNames,
})
}
return channel, nil
}
func (c *Channel) Save(filename string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating file %v: %v", filename, err)
}
defer file.Close()
channel := c.Get()
if err = gob.NewEncoder(file).Encode(channel); err != nil {
return fmt.Errorf("error encoding file %v: %v", filename, err)
}
return nil
}
func (c *Channel) Add(i *rss.Item) {
c.addCh <- i
}