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.

393 lines
11 KiB

6 years ago
6 years ago
  1. const Model = require('objection').Model
  2. const _ = require('lodash')
  3. const JSBinType = require('js-binary').Type
  4. const pageHelper = require('../helpers/page')
  5. const path = require('path')
  6. const fs = require('fs-extra')
  7. const yaml = require('js-yaml')
  8. /* global WIKI */
  9. const frontmatterRegex = {
  10. html: /^(<!-{2}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{2}>)?(?:\n|\r)*([\w\W]*)*/,
  11. legacy: /^(<!-- TITLE: ?([\w\W]+?) -{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) -{2}>)?(?:\n|\r)*([\w\W]*)*/i,
  12. markdown: /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?(?:\n|\r)*([\w\W]*)*/
  13. }
  14. /**
  15. * Pages model
  16. */
  17. module.exports = class Page extends Model {
  18. static get tableName() { return 'pages' }
  19. static get jsonSchema () {
  20. return {
  21. type: 'object',
  22. required: ['path', 'title'],
  23. properties: {
  24. id: {type: 'integer'},
  25. path: {type: 'string'},
  26. hash: {type: 'string'},
  27. title: {type: 'string'},
  28. description: {type: 'string'},
  29. isPublished: {type: 'boolean'},
  30. privateNS: {type: 'string'},
  31. publishStartDate: {type: 'string'},
  32. publishEndDate: {type: 'string'},
  33. content: {type: 'string'},
  34. contentType: {type: 'string'},
  35. createdAt: {type: 'string'},
  36. updatedAt: {type: 'string'}
  37. }
  38. }
  39. }
  40. static get relationMappings() {
  41. return {
  42. tags: {
  43. relation: Model.ManyToManyRelation,
  44. modelClass: require('./tags'),
  45. join: {
  46. from: 'pages.id',
  47. through: {
  48. from: 'pageTags.pageId',
  49. to: 'pageTags.tagId'
  50. },
  51. to: 'tags.id'
  52. }
  53. },
  54. author: {
  55. relation: Model.BelongsToOneRelation,
  56. modelClass: require('./users'),
  57. join: {
  58. from: 'pages.authorId',
  59. to: 'users.id'
  60. }
  61. },
  62. creator: {
  63. relation: Model.BelongsToOneRelation,
  64. modelClass: require('./users'),
  65. join: {
  66. from: 'pages.creatorId',
  67. to: 'users.id'
  68. }
  69. },
  70. editor: {
  71. relation: Model.BelongsToOneRelation,
  72. modelClass: require('./editors'),
  73. join: {
  74. from: 'pages.editorKey',
  75. to: 'editors.key'
  76. }
  77. },
  78. locale: {
  79. relation: Model.BelongsToOneRelation,
  80. modelClass: require('./locales'),
  81. join: {
  82. from: 'pages.localeCode',
  83. to: 'locales.code'
  84. }
  85. }
  86. }
  87. }
  88. $beforeUpdate() {
  89. this.updatedAt = new Date().toISOString()
  90. }
  91. $beforeInsert() {
  92. this.createdAt = new Date().toISOString()
  93. this.updatedAt = new Date().toISOString()
  94. }
  95. static get cacheSchema() {
  96. return new JSBinType({
  97. id: 'uint',
  98. authorId: 'uint',
  99. authorName: 'string',
  100. createdAt: 'string',
  101. creatorId: 'uint',
  102. creatorName: 'string',
  103. description: 'string',
  104. isPrivate: 'boolean',
  105. isPublished: 'boolean',
  106. publishEndDate: 'string',
  107. publishStartDate: 'string',
  108. render: 'string',
  109. title: 'string',
  110. toc: 'string',
  111. updatedAt: 'string'
  112. })
  113. }
  114. /**
  115. * Inject page metadata into contents
  116. */
  117. injectMetadata () {
  118. let meta = [
  119. ['title', this.title],
  120. ['description', this.description],
  121. ['published', this.isPublished.toString()],
  122. ['date', this.updatedAt],
  123. ['tags', '']
  124. ]
  125. switch (this.contentType) {
  126. case 'markdown':
  127. return '---\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n---\n\n' + this.content
  128. case 'html':
  129. return '<!--\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n-->\n\n' + this.content
  130. default:
  131. return this.content
  132. }
  133. }
  134. /**
  135. * Parse injected page metadata from raw content
  136. *
  137. * @param {String} raw Raw file contents
  138. * @param {String} contentType Content Type
  139. */
  140. static parseMetadata (raw, contentType) {
  141. let result
  142. switch (contentType) {
  143. case 'markdown':
  144. result = frontmatterRegex.markdown.exec(raw)
  145. if (result[2]) {
  146. return {
  147. ...yaml.safeLoad(result[2]),
  148. content: result[3]
  149. }
  150. } else {
  151. // Attempt legacy v1 format
  152. result = frontmatterRegex.legacy.exec(raw)
  153. if (result[2]) {
  154. return {
  155. title: result[2],
  156. description: result[4],
  157. content: result[5]
  158. }
  159. }
  160. }
  161. break
  162. case 'html':
  163. result = frontmatterRegex.html.exec(raw)
  164. if (result[2]) {
  165. return {
  166. ...yaml.safeLoad(result[2]),
  167. content: result[3]
  168. }
  169. }
  170. break
  171. }
  172. return {
  173. content: raw
  174. }
  175. }
  176. static async createPage(opts) {
  177. await WIKI.models.pages.query().insert({
  178. authorId: opts.authorId,
  179. content: opts.content,
  180. creatorId: opts.authorId,
  181. contentType: _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text'),
  182. description: opts.description,
  183. editorKey: opts.editor,
  184. hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' }),
  185. isPrivate: opts.isPrivate,
  186. isPublished: opts.isPublished,
  187. localeCode: opts.locale,
  188. path: opts.path,
  189. publishEndDate: opts.publishEndDate || '',
  190. publishStartDate: opts.publishStartDate || '',
  191. title: opts.title,
  192. toc: '[]'
  193. })
  194. const page = await WIKI.models.pages.getPageFromDb({
  195. path: opts.path,
  196. locale: opts.locale,
  197. userId: opts.authorId,
  198. isPrivate: opts.isPrivate
  199. })
  200. await WIKI.models.pages.renderPage(page)
  201. await WIKI.data.searchEngine.created(page)
  202. if (!opts.skipStorage) {
  203. await WIKI.models.storage.pageEvent({
  204. event: 'created',
  205. page
  206. })
  207. }
  208. return page
  209. }
  210. static async updatePage(opts) {
  211. const ogPage = await WIKI.models.pages.query().findById(opts.id)
  212. if (!ogPage) {
  213. throw new Error('Invalid Page Id')
  214. }
  215. await WIKI.models.pageHistory.addVersion({
  216. ...ogPage,
  217. isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
  218. action: 'updated'
  219. })
  220. await WIKI.models.pages.query().patch({
  221. authorId: opts.authorId,
  222. content: opts.content,
  223. description: opts.description,
  224. isPublished: opts.isPublished === true || opts.isPublished === 1,
  225. publishEndDate: opts.publishEndDate || '',
  226. publishStartDate: opts.publishStartDate || '',
  227. title: opts.title
  228. }).where('id', ogPage.id)
  229. const page = await WIKI.models.pages.getPageFromDb({
  230. path: ogPage.path,
  231. locale: ogPage.localeCode,
  232. userId: ogPage.authorId,
  233. isPrivate: ogPage.isPrivate
  234. })
  235. await WIKI.models.pages.renderPage(page)
  236. await WIKI.data.searchEngine.updated(page)
  237. if (!opts.skipStorage) {
  238. await WIKI.models.storage.pageEvent({
  239. event: 'updated',
  240. page
  241. })
  242. }
  243. return page
  244. }
  245. static async deletePage(opts) {
  246. let page
  247. if (_.has(opts, 'id')) {
  248. page = await WIKI.models.pages.query().findById(opts.id)
  249. } else {
  250. page = await await WIKI.models.pages.query().findOne({
  251. path: opts.path,
  252. localeCode: opts.locale
  253. })
  254. }
  255. if (!page) {
  256. throw new Error('Invalid Page Id')
  257. }
  258. await WIKI.models.pageHistory.addVersion({
  259. ...page,
  260. action: 'deleted'
  261. })
  262. await WIKI.models.pages.query().delete().where('id', page.id)
  263. await WIKI.models.pages.deletePageFromCache(page)
  264. await WIKI.data.searchEngine.deleted(page)
  265. if (!opts.skipStorage) {
  266. await WIKI.models.storage.pageEvent({
  267. event: 'deleted',
  268. page
  269. })
  270. }
  271. }
  272. static async renderPage(page) {
  273. const renderJob = await WIKI.scheduler.registerJob({
  274. name: 'render-page',
  275. immediate: true,
  276. worker: true
  277. }, page.id)
  278. return renderJob.finished
  279. }
  280. static async getPage(opts) {
  281. let page = await WIKI.models.pages.getPageFromCache(opts)
  282. if (!page) {
  283. page = await WIKI.models.pages.getPageFromDb(opts)
  284. if (page) {
  285. await WIKI.models.pages.savePageToCache(page)
  286. }
  287. }
  288. return page
  289. }
  290. static async getPageFromDb(opts) {
  291. const queryModeID = _.isNumber(opts)
  292. return WIKI.models.pages.query()
  293. .column([
  294. 'pages.*',
  295. {
  296. authorName: 'author.name',
  297. authorEmail: 'author.email',
  298. creatorName: 'creator.name',
  299. creatorEmail: 'creator.email'
  300. }
  301. ])
  302. .joinRelation('author')
  303. .joinRelation('creator')
  304. .where(queryModeID ? {
  305. 'pages.id': opts
  306. } : {
  307. 'pages.path': opts.path,
  308. 'pages.localeCode': opts.locale
  309. })
  310. .andWhere(builder => {
  311. if (queryModeID) return
  312. builder.where({
  313. 'pages.isPublished': true
  314. }).orWhere({
  315. 'pages.isPublished': false,
  316. 'pages.authorId': opts.userId
  317. })
  318. })
  319. .andWhere(builder => {
  320. if (queryModeID) return
  321. if (opts.isPrivate) {
  322. builder.where({ 'pages.isPrivate': true, 'pages.privateNS': opts.privateNS })
  323. } else {
  324. builder.where({ 'pages.isPrivate': false })
  325. }
  326. })
  327. .first()
  328. }
  329. static async savePageToCache(page) {
  330. const cachePath = path.join(process.cwd(), `data/cache/${page.hash}.bin`)
  331. await fs.outputFile(cachePath, WIKI.models.pages.cacheSchema.encode({
  332. id: page.id,
  333. authorId: page.authorId,
  334. authorName: page.authorName,
  335. createdAt: page.createdAt,
  336. creatorId: page.creatorId,
  337. creatorName: page.creatorName,
  338. description: page.description,
  339. isPrivate: page.isPrivate === 1 || page.isPrivate === true,
  340. isPublished: page.isPublished === 1 || page.isPublished === true,
  341. publishEndDate: page.publishEndDate,
  342. publishStartDate: page.publishStartDate,
  343. render: page.render,
  344. title: page.title,
  345. toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
  346. updatedAt: page.updatedAt
  347. }))
  348. }
  349. static async getPageFromCache(opts) {
  350. const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' })
  351. const cachePath = path.join(process.cwd(), `data/cache/${pageHash}.bin`)
  352. try {
  353. const pageBuffer = await fs.readFile(cachePath)
  354. let page = WIKI.models.pages.cacheSchema.decode(pageBuffer)
  355. return {
  356. ...page,
  357. path: opts.path,
  358. localeCode: opts.locale,
  359. isPrivate: opts.isPrivate
  360. }
  361. } catch (err) {
  362. if (err.code === 'ENOENT') {
  363. return false
  364. }
  365. WIKI.logger.error(err)
  366. throw err
  367. }
  368. }
  369. static async deletePageFromCache(page) {
  370. return fs.remove(path.join(process.cwd(), `data/cache/${page.hash}.bin`))
  371. }
  372. }