Final phase of the wiki upgrade (see WIKI_UPGRADE.md). Schema (additive): wiki_revisions table (per-save content snapshots). The FULLTEXT index on wiki_pages(title, body) shipped in Phase 1. Search: - MATCH ... AGAINST natural-language search over title + body, ordered by relevance - public: GET /public/wiki?q= (published only); admin: GET /admin/wiki?q= (all statuses) - public wiki index gains a search box; admin list gains a search field Revision history: - every create/update snapshots the page into wiki_revisions - admin endpoints: list revisions, get one, and restore (restore overwrites the page, rebuilds links, and appends a new revision — history stays append-only); logged as wiki.revision.restore - editor gains a History modal: revision list + word-level diff (jsdiff) of a chosen revision against the current page, with one-click restore Verified end-to-end: search matches body and title; two edits produce three revisions; diff renders added/removed words; restore reverts and records a new revision. No console errors. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
125 lines
3.5 KiB
JavaScript
125 lines
3.5 KiB
JavaScript
const posts = require('../../../model/posts/posts.model')
|
|
const wiki = require('../../../model/wiki/wiki.model')
|
|
const settings = require('../../../model/settings/settings.model')
|
|
const mailer = require('../../../utils/mailer')
|
|
|
|
const log = require('../../../utils/logger')('public')
|
|
|
|
async function getSettings(req, res) {
|
|
try {
|
|
return res.json(await settings.getPublic())
|
|
} catch (err) {
|
|
return res.status(500).json({ message: 'Internal Server Error' })
|
|
}
|
|
}
|
|
|
|
async function getStatus(req, res) {
|
|
try {
|
|
return res.json({
|
|
mode: (await settings.get('site_mode')) || 'live',
|
|
status_message: (await settings.get('status_message')) || '',
|
|
})
|
|
} catch (err) {
|
|
return res.status(500).json({ message: 'Internal Server Error' })
|
|
}
|
|
}
|
|
|
|
async function getPosts(req, res) {
|
|
const { category } = req.params
|
|
if (!posts.isValidUrlCategory(category)) {
|
|
return res.status(404).json({ message: 'Unknown category' })
|
|
}
|
|
try {
|
|
return res.json(await posts.listPublished(category))
|
|
} catch (err) {
|
|
return res.status(500).json({ message: 'Internal Server Error' })
|
|
}
|
|
}
|
|
|
|
async function getPost(req, res) {
|
|
const { category, idOrSlug } = req.params
|
|
if (!posts.isValidUrlCategory(category)) {
|
|
return res.status(404).json({ message: 'Unknown category' })
|
|
}
|
|
try {
|
|
const post = await posts.getPublished(category, idOrSlug)
|
|
if (!post) return res.status(404).json({ message: 'Not found' })
|
|
return res.json(post)
|
|
} catch (err) {
|
|
return res.status(500).json({ message: 'Internal Server Error' })
|
|
}
|
|
}
|
|
|
|
async function getWikiCategories(req, res) {
|
|
try {
|
|
return res.json(await wiki.listCategories())
|
|
} catch (err) {
|
|
return res.status(500).json({ message: 'Internal Server Error' })
|
|
}
|
|
}
|
|
|
|
async function getWikiTags(req, res) {
|
|
try {
|
|
return res.json(await wiki.listTags())
|
|
} catch (err) {
|
|
return res.status(500).json({ message: 'Internal Server Error' })
|
|
}
|
|
}
|
|
|
|
async function getWikiList(req, res) {
|
|
try {
|
|
// Full-text search takes precedence over category/tag filters.
|
|
const q = (req.query.q || '').trim()
|
|
if (q) return res.json(await wiki.search(q, { publishedOnly: true }))
|
|
|
|
const filters = {}
|
|
if (req.query.category) {
|
|
const category = await wiki.getCategoryBySlug(req.query.category)
|
|
if (!category) return res.json([]) // unknown category → no pages
|
|
filters.categoryId = category.id
|
|
}
|
|
if (req.query.tag) {
|
|
const tag = await wiki.getTagBySlug(req.query.tag)
|
|
if (!tag) return res.json([]) // unknown tag → no pages
|
|
filters.tagId = tag.id
|
|
}
|
|
return res.json(await wiki.listPublished(filters))
|
|
} catch (err) {
|
|
return res.status(500).json({ message: 'Internal Server Error' })
|
|
}
|
|
}
|
|
|
|
async function getWikiPage(req, res) {
|
|
try {
|
|
// Public sees published pages only; drafts 404 like any missing page.
|
|
const page = await wiki.getPublishedBySlug(req.params.slug)
|
|
if (!page) return res.status(404).json({ message: 'Not found' })
|
|
return res.json(page)
|
|
} catch (err) {
|
|
return res.status(500).json({ message: 'Internal Server Error' })
|
|
}
|
|
}
|
|
|
|
async function contact(req, res) {
|
|
const { name, email, message } = req.body
|
|
try {
|
|
const result = await mailer.sendContactMessage({ name, email, message })
|
|
return res.json(result)
|
|
} catch (err) {
|
|
log.error('contact send failed', err)
|
|
return res.status(502).json({ message: 'Could not send message right now.' })
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getSettings,
|
|
getStatus,
|
|
getPosts,
|
|
getPost,
|
|
getWikiCategories,
|
|
getWikiTags,
|
|
getWikiList,
|
|
getWikiPage,
|
|
contact,
|
|
}
|