51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package control
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.streifling.com/jason/rss"
|
|
"streifling.com/jason/cpolis/cmd/model"
|
|
)
|
|
|
|
func GetChannel(db *model.DB, title, link, description string) (*rss.Channel, error) {
|
|
channel := &rss.Channel{
|
|
Title: title,
|
|
Link: link,
|
|
Description: description,
|
|
Items: make([]*rss.Item, 0),
|
|
}
|
|
|
|
articles, err := db.GetCertainArticles(true, false)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error fetching published articles: %v", err)
|
|
}
|
|
|
|
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.Description,
|
|
Content: &rss.Content{Value: article.Content},
|
|
Categories: tagNames,
|
|
})
|
|
}
|
|
|
|
return channel, nil
|
|
}
|