2022-08-21 21:54:24 +00:00
|
|
|
import path from 'path'
|
2022-08-22 17:17:39 +00:00
|
|
|
import striptags from 'striptags'
|
2022-08-18 09:59:48 +00:00
|
|
|
import Parent from '../base/serve.mjs'
|
2022-08-21 21:54:24 +00:00
|
|
|
import fs from 'fs/promises'
|
2022-08-22 17:17:39 +00:00
|
|
|
import config from '../base/config.mjs'
|
2022-08-18 09:59:48 +00:00
|
|
|
|
|
|
|
export default class ServeHandler extends Parent {
|
2022-08-21 21:54:24 +00:00
|
|
|
traverseTree(set, tree) {
|
|
|
|
for (let branch of tree) {
|
|
|
|
if (branch.children && branch.children.length) {
|
|
|
|
set.add(branch.id)
|
|
|
|
this.traverseTree(set, branch.children)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 17:17:39 +00:00
|
|
|
getDescriptionFromBlocks(blocks) {
|
|
|
|
return encodeURI(blocks
|
|
|
|
.filter(x => x.type === 'header'
|
|
|
|
|| x.type === 'paragraph'
|
|
|
|
|| x.type === 'quote'
|
|
|
|
|| x.type === 'list'
|
|
|
|
|| x.type === 'code'
|
|
|
|
|| x.type === 'htmlraw')
|
|
|
|
.map(x => {
|
|
|
|
if (x.type === 'htmlraw') {
|
|
|
|
return striptags(x.data.html)
|
|
|
|
}
|
|
|
|
if (x.type === 'quote') {
|
|
|
|
return `"${x.data.text}"` + (x.data.caption ? ` - ${x.data.caption}` : '')
|
|
|
|
}
|
|
|
|
if (x.type === 'list') {
|
|
|
|
return x.data.items.join('. ')
|
|
|
|
}
|
|
|
|
return x.data.code ? `"${x.data.code}"` : x.data.text
|
|
|
|
})
|
|
|
|
.map(x => striptags(x))
|
|
|
|
.map(x => {
|
|
|
|
console.log(x)
|
|
|
|
return x
|
|
|
|
})
|
|
|
|
.filter(x => x)
|
|
|
|
.join('. ')
|
|
|
|
.replace('/\.+/g', '.')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-18 09:59:48 +00:00
|
|
|
async serveIndex(ctx) {
|
2022-08-22 17:17:39 +00:00
|
|
|
if (config.get('NODE_ENV') === 'development') {
|
|
|
|
let indexFile = await fs.readFile(path.join(this.root, 'index.html'))
|
2022-10-17 07:52:55 +00:00
|
|
|
this.loadTemplate(indexFile)
|
2022-08-22 17:17:39 +00:00
|
|
|
}
|
2022-08-21 21:54:24 +00:00
|
|
|
|
2022-08-18 09:59:48 +00:00
|
|
|
let payload = {
|
2022-10-17 07:52:55 +00:00
|
|
|
headerDescription: 'A small fansubbing and scanlation group translating and encoding our favourite shows from Japan.',
|
2022-08-18 09:59:48 +00:00
|
|
|
headerImage: this.frontend + '/assets/img/heart.png',
|
|
|
|
headerTitle: 'NFP Moe - Anime/Manga translation group',
|
|
|
|
headerUrl: this.frontend + ctx.url,
|
|
|
|
payloadData: null,
|
|
|
|
payloadTree: null,
|
|
|
|
version: this.version,
|
|
|
|
nonce: ctx.state.nonce,
|
2022-08-21 21:54:24 +00:00
|
|
|
type: 'page',
|
|
|
|
banner: false,
|
|
|
|
media: false,
|
2022-08-22 17:17:39 +00:00
|
|
|
in_debug: config.get('NODE_ENV') === 'development' && false,
|
2022-08-18 09:59:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-08-21 21:54:24 +00:00
|
|
|
let tree = await this.pageRoutes.getPageTree(ctx, true)
|
|
|
|
let setOfBranches = new Set()
|
|
|
|
this.traverseTree(setOfBranches, tree.tree)
|
|
|
|
|
|
|
|
payload.payloadTree = JSON.stringify(tree)
|
2022-08-18 09:59:48 +00:00
|
|
|
if (ctx.url === '/' || (ctx.url.startsWith('/page/') && ctx.url.lastIndexOf('/') === 5)) {
|
|
|
|
ctx.params.path = null
|
|
|
|
if (ctx.url.lastIndexOf('/') === 5) {
|
|
|
|
ctx.params.path = ctx.url.slice(ctx.url.lastIndexOf('/') + 1)
|
|
|
|
}
|
2022-08-21 21:54:24 +00:00
|
|
|
let data = await this.pageRoutes.getPage(ctx, true)
|
|
|
|
if (!data.page) {
|
|
|
|
payload.type = 'frontpage'
|
2022-08-22 17:17:39 +00:00
|
|
|
} else if (setOfBranches.has(data.page.id)) {
|
2022-08-21 21:54:24 +00:00
|
|
|
payload.type = 'page_with_children'
|
|
|
|
}
|
2022-08-22 17:17:39 +00:00
|
|
|
|
|
|
|
if (data.page) {
|
|
|
|
payload.headerTitle = data.page.name + ' - NFP Moe'
|
2022-10-17 07:52:55 +00:00
|
|
|
payload.headerDescription = 'Page ' + data.page.name + ' over at NFP Moe. ' + payload.headerDescription
|
|
|
|
if (data.page.content.blocks?.length) {
|
2022-08-22 17:17:39 +00:00
|
|
|
payload.headerDescription = this.getDescriptionFromBlocks(data.page.content.blocks) || payload.headerDescription
|
|
|
|
}
|
|
|
|
if (data.page.media_alt_prefix) {
|
|
|
|
payload.headerImage = data.page.media_alt_prefix + '_small.jpg'
|
|
|
|
}
|
|
|
|
}
|
2022-08-21 21:54:24 +00:00
|
|
|
payload.media = data.page?.media_avif_preview || false
|
|
|
|
payload.banner = data.featured?.banner_avif_preview || data.page?.banner_avif_preview || false
|
|
|
|
payload.payloadData = JSON.stringify(data)
|
|
|
|
} else if (ctx.url.startsWith('/article/') && ctx.url.lastIndexOf('/') === 8) {
|
|
|
|
ctx.params.path = ctx.url.slice(ctx.url.lastIndexOf('/') + 1)
|
|
|
|
let data = await this.articleRoutes.getArticle(ctx, true)
|
2022-08-22 17:17:39 +00:00
|
|
|
|
|
|
|
if (data.article) {
|
|
|
|
payload.headerTitle = data.article.name + ' - NFP Moe'
|
|
|
|
if (data.article.content.blocks.length) {
|
|
|
|
payload.headerDescription = this.getDescriptionFromBlocks(data.article.content.blocks) || payload.headerDescription
|
|
|
|
}
|
|
|
|
if (data.article.media_alt_prefix) {
|
|
|
|
payload.headerImage = data.article.media_alt_prefix + '_small.jpg'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-21 21:54:24 +00:00
|
|
|
payload.media = data.article?.media_avif_preview || false
|
|
|
|
payload.payloadData = JSON.stringify(data)
|
|
|
|
payload.type = 'article'
|
2022-10-17 07:52:55 +00:00
|
|
|
} else if (ctx.url !== '/login' && !ctx.url.startsWith('/admin/')) {
|
|
|
|
ctx.status = 404
|
2022-08-18 09:59:48 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
ctx.log.error(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = this.template(payload)
|
|
|
|
ctx.type = 'text/html; charset=utf-8'
|
|
|
|
}
|
|
|
|
}
|