2024-02-24 14:49:29 +01:00
|
|
|
package data
|
2024-02-18 12:41:49 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-09 10:12:46 +01:00
|
|
|
"time"
|
2024-02-18 12:41:49 +01:00
|
|
|
|
2024-03-05 17:13:59 +01:00
|
|
|
"git.streifling.com/jason/rss"
|
2024-02-18 12:41:49 +01:00
|
|
|
)
|
|
|
|
|
2024-03-09 10:12:46 +01:00
|
|
|
func GetChannel(db *DB, title, link, desc string) (*rss.Channel, error) {
|
|
|
|
channel := &rss.Channel{
|
2024-03-02 00:28:42 +01:00
|
|
|
Title: title,
|
2024-03-05 17:13:59 +01:00
|
|
|
Link: link,
|
2024-03-02 00:28:42 +01:00
|
|
|
Description: desc,
|
2024-03-09 10:12:46 +01:00
|
|
|
Items: make([]*rss.Item, 0),
|
2024-02-18 12:41:49 +01:00
|
|
|
}
|
2024-03-02 00:28:42 +01:00
|
|
|
|
2024-03-09 10:12:46 +01:00
|
|
|
articles, err := db.GetCertainArticles(true)
|
2024-02-18 14:01:06 +01:00
|
|
|
if err != nil {
|
2024-03-09 10:12:46 +01:00
|
|
|
return nil, fmt.Errorf("error fetching published articles: %v", err)
|
2024-02-18 14:31:28 +01:00
|
|
|
}
|
2024-02-18 14:01:06 +01:00
|
|
|
|
2024-03-09 10:12:46 +01:00
|
|
|
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)
|
|
|
|
}
|
2024-02-18 14:01:06 +01:00
|
|
|
|
2024-03-09 10:12:46 +01:00
|
|
|
user, err := db.GetUser(article.AuthorID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error finding user %v: %v", article.AuthorID, err)
|
|
|
|
}
|
2024-02-18 12:41:49 +01:00
|
|
|
|
2024-03-09 10:12:46 +01:00
|
|
|
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,
|
|
|
|
})
|
2024-02-18 12:41:49 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 10:12:46 +01:00
|
|
|
return channel, nil
|
2024-03-01 21:01:38 +01:00
|
|
|
}
|