mirror of https://github.com/requarks/wiki
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.
174 lines
4.3 KiB
174 lines
4.3 KiB
2 years ago
|
import { Model } from 'objection'
|
||
|
import { defaultsDeep, keyBy } from 'lodash-es'
|
||
3 years ago
|
|
||
|
/**
|
||
|
* Site model
|
||
|
*/
|
||
2 years ago
|
export class Site extends Model {
|
||
3 years ago
|
static get tableName () { return 'sites' }
|
||
|
|
||
|
static get jsonSchema () {
|
||
|
return {
|
||
|
type: 'object',
|
||
|
required: ['hostname'],
|
||
|
|
||
|
properties: {
|
||
|
id: { type: 'string' },
|
||
|
hostname: { type: 'string' },
|
||
|
isEnabled: { type: 'boolean', default: false }
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static get jsonAttributes () {
|
||
|
return ['config']
|
||
|
}
|
||
|
|
||
3 years ago
|
static async getSiteByHostname ({ hostname, forceReload = false }) {
|
||
|
if (forceReload) {
|
||
2 years ago
|
await WIKI.db.sites.reloadCache()
|
||
3 years ago
|
}
|
||
|
const siteId = WIKI.sitesMappings[hostname] || WIKI.sitesMappings['*']
|
||
|
if (siteId) {
|
||
|
return WIKI.sites[siteId]
|
||
|
}
|
||
|
return null
|
||
|
}
|
||
|
|
||
|
static async reloadCache () {
|
||
|
WIKI.logger.info('Reloading site configurations...')
|
||
2 years ago
|
const sites = await WIKI.db.sites.query().orderBy('id')
|
||
2 years ago
|
WIKI.sites = keyBy(sites, 'id')
|
||
3 years ago
|
WIKI.sitesMappings = {}
|
||
|
for (const site of sites) {
|
||
|
WIKI.sitesMappings[site.hostname] = site.id
|
||
|
}
|
||
|
WIKI.logger.info(`Loaded ${sites.length} site configurations [ OK ]`)
|
||
|
}
|
||
|
|
||
3 years ago
|
static async createSite (hostname, config) {
|
||
2 years ago
|
const newSite = await WIKI.db.sites.query().insertAndFetch({
|
||
3 years ago
|
hostname,
|
||
|
isEnabled: true,
|
||
2 years ago
|
config: defaultsDeep(config, {
|
||
3 years ago
|
title: 'My Wiki Site',
|
||
|
description: '',
|
||
|
company: '',
|
||
|
contentLicense: '',
|
||
3 years ago
|
footerExtra: '',
|
||
3 years ago
|
pageExtensions: ['md', 'html', 'txt'],
|
||
3 years ago
|
defaults: {
|
||
|
timezone: 'America/New_York',
|
||
|
dateFormat: 'YYYY-MM-DD',
|
||
|
timeFormat: '12h'
|
||
|
},
|
||
|
features: {
|
||
|
ratings: false,
|
||
|
ratingsMode: 'off',
|
||
|
comments: false,
|
||
|
contributions: false,
|
||
|
profile: true,
|
||
|
search: true
|
||
|
},
|
||
|
logoUrl: '',
|
||
|
logoText: true,
|
||
3 years ago
|
sitemap: true,
|
||
3 years ago
|
robots: {
|
||
|
index: true,
|
||
|
follow: true
|
||
|
},
|
||
|
locale: 'en',
|
||
|
localeNamespacing: false,
|
||
|
localeNamespaces: [],
|
||
2 years ago
|
assets: {
|
||
|
logo: false,
|
||
|
logoExt: 'svg',
|
||
2 years ago
|
favicon: false,
|
||
|
faviconExt: 'svg',
|
||
|
loginBg: false
|
||
2 years ago
|
},
|
||
3 years ago
|
theme: {
|
||
|
dark: false,
|
||
3 years ago
|
colorPrimary: '#1976D2',
|
||
|
colorSecondary: '#02C39A',
|
||
|
colorAccent: '#FF9800',
|
||
3 years ago
|
colorHeader: '#000000',
|
||
3 years ago
|
colorSidebar: '#1976D2',
|
||
3 years ago
|
injectCSS: '',
|
||
|
injectHead: '',
|
||
|
injectBody: '',
|
||
3 years ago
|
contentWidth: 'full',
|
||
3 years ago
|
sidebarPosition: 'left',
|
||
|
tocPosition: 'right',
|
||
|
showSharingMenu: true,
|
||
3 years ago
|
showPrintBtn: true,
|
||
|
baseFont: 'roboto',
|
||
|
contentFont: 'roboto'
|
||
2 years ago
|
},
|
||
2 years ago
|
editors: {
|
||
|
asciidoc: {
|
||
|
isActive: true,
|
||
|
config: {}
|
||
|
},
|
||
|
markdown: {
|
||
|
isActive: true,
|
||
|
config: {
|
||
|
allowHTML: true,
|
||
|
kroki: false,
|
||
|
krokiServerUrl: 'https://kroki.io',
|
||
|
latexEngine: 'katex',
|
||
|
lineBreaks: true,
|
||
|
linkify: true,
|
||
|
multimdTable: true,
|
||
|
plantuml: false,
|
||
|
plantumlServerUrl: 'https://www.plantuml.com/plantuml/',
|
||
|
quotes: 'english',
|
||
|
tabWidth: 2,
|
||
|
typographer: false,
|
||
|
underline: true
|
||
|
}
|
||
|
},
|
||
|
wysiwyg: {
|
||
|
isActive: true,
|
||
|
config: {}
|
||
|
}
|
||
|
},
|
||
2 years ago
|
uploads: {
|
||
|
conflictBehavior: 'overwrite',
|
||
|
normalizeFilename: true
|
||
3 years ago
|
}
|
||
|
})
|
||
|
})
|
||
|
|
||
3 years ago
|
WIKI.logger.debug(`Creating new DB storage for site ${newSite.id}`)
|
||
|
|
||
2 years ago
|
await WIKI.db.storage.query().insert({
|
||
3 years ago
|
module: 'db',
|
||
|
siteId: newSite.id,
|
||
|
isEnabled: true,
|
||
|
contentTypes: {
|
||
|
activeTypes: ['pages', 'images', 'documents', 'others', 'large'],
|
||
|
largeThreshold: '5MB'
|
||
|
},
|
||
|
assetDelivery: {
|
||
|
streaming: true,
|
||
|
directAccess: false
|
||
|
},
|
||
|
state: {
|
||
|
current: 'ok'
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return newSite
|
||
|
}
|
||
|
|
||
|
static async updateSite (id, patch) {
|
||
2 years ago
|
return WIKI.db.sites.query().findById(id).patch(patch)
|
||
3 years ago
|
}
|
||
|
|
||
|
static async deleteSite (id) {
|
||
2 years ago
|
await WIKI.db.storage.query().delete().where('siteId', id)
|
||
|
return WIKI.db.sites.query().deleteById(id)
|
||
3 years ago
|
}
|
||
|
}
|