You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.2 KiB

import {Controller, view, Inject, Injectable, collect, plaintext} from '@extollo/lib'
import {Home} from './Home.controller'
import {Blog as BlogService, BlogPost} from '../../services/Blog.service'
import {FeedPost} from '../../models/FeedPost.model'
/**
* Blog Controller
* ------------------------------------
* Put some description here.
*/
@Injectable()
export class Blog extends Controller {
@Inject()
protected readonly blog!: BlogService
public async index() {
const home = <Home> this.make(Home)
const posts = await this.blog.getAllPosts()
return view('blog:index', {
...home.getThemeCSS(),
...this.getBlogData(),
posts: posts.take(10).toArray(),
})
}
public async post() {
const home = <Home> this.make(Home)
const slug = this.request.safe('slug').string()
const post = await this.blog.getPost(slug)
if ( !post ) {
return view('blog:404', {
...home.getThemeCSS(),
...this.getBlogData(),
slug,
})
}
return view('blog:post', {
...home.getThemeCSS(),
...this.getBlogData(),
post,
renderedPost: await this.blog.renderPost(post.slug),
})
}
public async archive() {
const home = <Home> this.make(Home)
const postsByYear = await this.blog.getAllPosts()
.then(ps => ps.groupBy(p => p.date.getFullYear()))
return view('blog:archive', {
...home.getThemeCSS(),
...this.getBlogData(),
postsByYear,
postYears: collect(Object.keys(postsByYear)).sortDesc().toArray(),
})
}
public async tags() {
const home = <Home> this.make(Home)
const counts = {} as Record<string, number>
await this.blog.getAllPosts()
.then(ps => ps.each(post => {
post.tags.some(tag => {
if ( !counts[tag] ) counts[tag] = 1
else counts[tag] += 1
})
}))
return view('blog:tags', {
...home.getThemeCSS(),
...this.getBlogData(),
counts,
tags: collect(Object.keys(counts)).sort().toArray(),
})
}
public async tag() {
const home = <Home> this.make(Home)
const tag = this.request.safe('tag').string()
const posts = await this.blog.getAllPosts()
.then(ps => ps.filter(p => p.tags.includes(tag)).sortByDesc('date'))
console.log('tag posts', posts)
return view('blog:tag', {
...home.getThemeCSS(),
...this.getBlogData(),
posts: posts.toArray(),
tag,
})
}
public async feeds() {
const home = <Home> this.make(Home)
const subsByCategory = await this.blog.getSubs()
.then(ss => ss.groupBy('category'))
const categories = collect(Object.keys(subsByCategory)).sort().toArray()
return view('blog:feeds', {
...home.getThemeCSS(),
...this.getBlogData(),
subsByCategory,
categories,
})
}
public async rss() {
const feed = await this.blog.getFeed()
return plaintext(feed.rss2()).contentType('application/rss+xml; charset=UTF-8')
}
public async atom() {
const feed = await this.blog.getFeed()
return plaintext(feed.atom1()).contentType('application/atom+xml; charset=UTF-8')
}
public async json() {
const feed = await this.blog.getFeed()
return plaintext(feed.json1()).contentType('application/feed+json; charset=UTF-8')
}
public getBlogData(): any {
return {
blogUrl: (post: BlogPost): string => this.blog.getUrl(post),
blogDate: (date: Date): string => {
const year = date.getFullYear()
let month = String(date.getMonth() + 1)
if ( month.length < 2 ) month = `0${month}`
let day = String(date.getDate())
if ( day.length < 2 ) day = `0${day}`
return `${year}-${month}-${day}`
},
}
}
}