2022-04-05 17:59:26 +00:00
|
|
|
import {Controller, view, Injectable, Collection, Inject, Routing, plaintext} from '@extollo/lib'
|
2022-04-05 15:47:15 +00:00
|
|
|
import {FeedPost} from '../../models/FeedPost.model'
|
2022-04-05 17:59:26 +00:00
|
|
|
import * as RSSFeed from 'feed'
|
2022-04-05 15:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Feed Controller
|
|
|
|
* ------------------------------------
|
|
|
|
* Backend for routes related to my post feed.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class Feed extends Controller {
|
2022-04-05 17:59:26 +00:00
|
|
|
@Inject()
|
|
|
|
protected readonly routing!: Routing
|
|
|
|
|
2022-04-05 15:47:15 +00:00
|
|
|
public async feed(feedPosts: Collection<FeedPost>) {
|
|
|
|
return view('feed', {
|
|
|
|
feedPosts: feedPosts.toArray(),
|
|
|
|
})
|
|
|
|
}
|
2022-04-05 17:59:26 +00:00
|
|
|
|
|
|
|
public async rss(feedPosts: Collection<FeedPost>) {
|
|
|
|
const feed = this.getFeed(feedPosts)
|
|
|
|
return plaintext(feed.rss2()).contentType('application/rss+xml; charset=UTF-8')
|
|
|
|
}
|
|
|
|
|
|
|
|
public async atom(feedPosts: Collection<FeedPost>) {
|
|
|
|
const feed = this.getFeed(feedPosts)
|
|
|
|
return plaintext(feed.atom1()).contentType('application/atom+xml; charset=UTF-8')
|
|
|
|
}
|
|
|
|
|
|
|
|
public async json(feedPosts: Collection<FeedPost>) {
|
|
|
|
const feed = this.getFeed(feedPosts)
|
|
|
|
return plaintext(feed.json1()).contentType('application/feed+json; charset=UTF-8')
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getFeed(feedPosts: Collection<FeedPost>): RSSFeed.Feed {
|
|
|
|
const feed = new RSSFeed.Feed({
|
|
|
|
title: 'Garrett Mills',
|
|
|
|
description: 'A sporadic collection of my thoughts, blog posts, and project updates',
|
|
|
|
id: `${this.routing.getAppUrl()}#about`,
|
|
|
|
link: this.routing.getNamedPath('feed').toRemote,
|
|
|
|
language: 'en',
|
|
|
|
image: this.routing.getAssetPath('favicon', 'apple-touch-icon.png').toRemote,
|
|
|
|
favicon: this.routing.getAssetPath('favicon', 'favicon.ico').toRemote,
|
|
|
|
copyright: `Copyright (c) ${(new Date).getFullYear()} Garrett Mills. See website for licensing details.`,
|
|
|
|
updated: feedPosts.whereMax('postedAt').first()?.postedAt,
|
|
|
|
generator: '',
|
|
|
|
feedLinks: {
|
|
|
|
json: this.routing.getNamedPath('feed.json').toRemote,
|
|
|
|
atom: this.routing.getNamedPath('feed.atom').toRemote,
|
|
|
|
rss: this.routing.getNamedPath('feed.rss').toRemote,
|
|
|
|
},
|
|
|
|
author: {
|
|
|
|
name: 'Garrett Mills',
|
|
|
|
email: 'shout@garrettmills.dev',
|
|
|
|
link: 'https://garrettmills.dev/#about',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
feed.addCategory('Technology')
|
|
|
|
feed.addCategory('Software Development')
|
|
|
|
|
|
|
|
feedPosts.each(post => {
|
|
|
|
feed.addItem({
|
|
|
|
title: post.tag, // FIXME better title generation
|
|
|
|
date: post.postedAt,
|
|
|
|
id: `${this.routing.getNamedPath('feed')}#${post.feedPostId}`,
|
|
|
|
link: `${this.routing.getNamedPath('feed')}#${post.feedPostId}`,
|
|
|
|
content: post.body,
|
|
|
|
author: [{
|
|
|
|
name: 'Garrett Mills',
|
|
|
|
email: 'shout@garrettmills.dev',
|
|
|
|
link: 'https://garrettmills.dev/#about',
|
|
|
|
}],
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
2022-04-05 15:47:15 +00:00
|
|
|
}
|