tree.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import { Model } from 'objection'
  2. import { differenceWith, dropRight, last, nth } from 'lodash-es'
  3. import {
  4. decodeFolderPath,
  5. decodeTreePath,
  6. encodeFolderPath,
  7. encodeTreePath,
  8. generateHash
  9. } from '../helpers/common.mjs'
  10. import { Site } from './sites.mjs'
  11. const rePathName = /^[a-z0-9-]+$/
  12. const reTitle = /^[^<>"]+$/
  13. /**
  14. * Tree model
  15. */
  16. export class Tree extends Model {
  17. static get tableName () { return 'tree' }
  18. static get jsonSchema () {
  19. return {
  20. type: 'object',
  21. required: ['fileName'],
  22. properties: {
  23. id: { type: 'string' },
  24. folderPath: { type: 'string' },
  25. fileName: { type: 'string' },
  26. type: { type: 'string' },
  27. title: { type: 'string' },
  28. createdAt: { type: 'string' },
  29. updatedAt: { type: 'string' }
  30. }
  31. }
  32. }
  33. static get jsonAttributes () {
  34. return ['meta']
  35. }
  36. static get relationMappings () {
  37. return {
  38. site: {
  39. relation: Model.BelongsToOneRelation,
  40. modelClass: Site,
  41. join: {
  42. from: 'tree.siteId',
  43. to: 'sites.id'
  44. }
  45. }
  46. }
  47. }
  48. $beforeUpdate () {
  49. this.updatedAt = new Date().toISOString()
  50. }
  51. $beforeInsert () {
  52. this.createdAt = new Date().toISOString()
  53. this.updatedAt = new Date().toISOString()
  54. }
  55. /**
  56. * Get a Folder
  57. *
  58. * @param {Object} args - Fetch Properties
  59. * @param {string} [args.id] - UUID of the folder
  60. * @param {string} [args.path] - Path of the folder
  61. * @param {string} [args.locale] - Locale code of the folder (when using path)
  62. * @param {string} [args.siteId] - UUID of the site in which the folder is (when using path)
  63. * @param {boolean} [args.createIfMissing] - Create the folder and its ancestor if it's missing (when using path)
  64. */
  65. static async getFolder ({ id, path, locale, siteId, createIfMissing = false }) {
  66. // Get by ID
  67. if (id) {
  68. const parent = await WIKI.db.knex('tree').where('id', id).first()
  69. if (!parent) {
  70. throw new Error('ERR_INVALID_FOLDER')
  71. }
  72. return parent
  73. } else {
  74. // Get by path
  75. const parentPath = encodeTreePath(path)
  76. const parentPathParts = parentPath.split('.')
  77. const parentFilter = {
  78. folderPath: encodeFolderPath(dropRight(parentPathParts).join('.')),
  79. fileName: last(parentPathParts)
  80. }
  81. const parent = await WIKI.db.knex('tree').where({
  82. ...parentFilter,
  83. type: 'folder',
  84. locale,
  85. siteId
  86. }).first()
  87. if (parent) {
  88. return parent
  89. } else if (createIfMissing) {
  90. return WIKI.db.tree.createFolder({
  91. parentPath: parentFilter.folderPath,
  92. pathName: parentFilter.fileName,
  93. title: parentFilter.fileName,
  94. locale,
  95. siteId
  96. })
  97. } else {
  98. throw new Error('ERR_INVALID_FOLDER')
  99. }
  100. }
  101. }
  102. /**
  103. * Add Page Entry
  104. *
  105. * @param {Object} args - New Page Properties
  106. * @param {string} [args.parentId] - UUID of the parent folder
  107. * @param {string} [args.parentPath] - Path of the parent folder
  108. * @param {string} args.pathName - Path name of the page to add
  109. * @param {string} args.title - Title of the page to add
  110. * @param {string} args.locale - Locale code of the page to add
  111. * @param {string} args.siteId - UUID of the site in which the page will be added
  112. * @param {string[]} [args.tags] - Tags of the assets
  113. * @param {Object} [args.meta] - Extra metadata
  114. */
  115. static async addPage ({ id, parentId, parentPath, fileName, title, locale, siteId, tags = [], meta = {} }) {
  116. const folder = (parentId || parentPath)
  117. ? await WIKI.db.tree.getFolder({
  118. id: parentId,
  119. path: parentPath,
  120. locale,
  121. siteId,
  122. createIfMissing: true
  123. })
  124. : {
  125. folderPath: '',
  126. fileName: ''
  127. }
  128. const folderPath = folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName
  129. const fullPath = folderPath ? `${folderPath}/${fileName}` : fileName
  130. WIKI.logger.debug(`Adding page ${fullPath} to tree...`)
  131. const pageEntry = await WIKI.db.knex('tree').insert({
  132. id,
  133. folderPath: encodeFolderPath(folderPath),
  134. fileName,
  135. type: 'page',
  136. title,
  137. hash: generateHash(fullPath),
  138. locale,
  139. siteId,
  140. tags,
  141. meta,
  142. navigationId: siteId
  143. }).returning('*')
  144. return pageEntry[0]
  145. }
  146. /**
  147. * Add Asset Entry
  148. *
  149. * @param {Object} args - New Asset Properties
  150. * @param {string} [args.parentId] - UUID of the parent folder
  151. * @param {string} [args.parentPath] - Path of the parent folder
  152. * @param {string} args.pathName - Path name of the asset to add
  153. * @param {string} args.title - Title of the asset to add
  154. * @param {string} args.locale - Locale code of the asset to add
  155. * @param {string} args.siteId - UUID of the site in which the asset will be added
  156. * @param {string[]} [args.tags] - Tags of the assets
  157. * @param {Object} [args.meta] - Extra metadata
  158. */
  159. static async addAsset ({ id, parentId, parentPath, fileName, title, locale, siteId, tags = [], meta = {} }) {
  160. const folder = (parentId || parentPath)
  161. ? await WIKI.db.tree.getFolder({
  162. id: parentId,
  163. path: parentPath,
  164. locale,
  165. siteId,
  166. createIfMissing: true
  167. })
  168. : {
  169. folderPath: '',
  170. fileName: ''
  171. }
  172. const folderPath = folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName
  173. const fullPath = folderPath ? `${folderPath}/${fileName}` : fileName
  174. WIKI.logger.debug(`Adding asset ${fullPath} to tree...`)
  175. const assetEntry = await WIKI.db.knex('tree').insert({
  176. id,
  177. folderPath: encodeFolderPath(folderPath),
  178. fileName,
  179. type: 'asset',
  180. title,
  181. hash: generateHash(fullPath),
  182. locale,
  183. siteId,
  184. tags,
  185. meta
  186. }).returning('*')
  187. return assetEntry[0]
  188. }
  189. /**
  190. * Create New Folder
  191. *
  192. * @param {Object} args - New Folder Properties
  193. * @param {string} [args.parentId] - UUID of the parent folder
  194. * @param {string} [args.parentPath] - Path of the parent folder
  195. * @param {string} args.pathName - Path name of the folder to create
  196. * @param {string} args.title - Title of the folder to create
  197. * @param {string} args.locale - Locale code of the folder to create
  198. * @param {string} args.siteId - UUID of the site in which the folder will be created
  199. */
  200. static async createFolder ({ parentId, parentPath, pathName, title, locale, siteId }) {
  201. // Validate path name
  202. if (!rePathName.test(pathName)) {
  203. throw new Error('ERR_INVALID_PATH')
  204. }
  205. // Validate title
  206. if (!reTitle.test(title)) {
  207. throw new Error('ERR_FOLDER_TITLE_INVALID')
  208. }
  209. parentPath = encodeTreePath(parentPath)
  210. WIKI.logger.debug(`Creating new folder ${pathName}...`)
  211. const parentPathParts = parentPath.split('.')
  212. const parentFilter = {
  213. folderPath: encodeFolderPath(dropRight(parentPathParts).join('.')),
  214. fileName: last(parentPathParts)
  215. }
  216. // Get parent path
  217. let parent = null
  218. if (parentId) {
  219. parent = await WIKI.db.knex('tree').where('id', parentId).first()
  220. if (!parent) {
  221. throw new Error('ERR_FOLDER_PARENT_INVALID')
  222. }
  223. parentPath = parent.folderPath ? `${decodeFolderPath(parent.folderPath)}.${parent.fileName}` : parent.fileName
  224. } else if (parentPath) {
  225. parent = await WIKI.db.knex('tree').where(parentFilter).first()
  226. } else {
  227. parentPath = ''
  228. }
  229. // Check for collision
  230. const existingFolder = await WIKI.db.knex('tree').select('id').where({
  231. siteId,
  232. locale,
  233. folderPath: encodeFolderPath(parentPath),
  234. fileName: pathName,
  235. type: 'folder'
  236. }).first()
  237. if (existingFolder) {
  238. throw new Error('ERR_FOLDER_DUPLICATE')
  239. }
  240. // Ensure all ancestors exist
  241. if (parentPath) {
  242. const expectedAncestors = []
  243. const existingAncestors = await WIKI.db.knex('tree').select('folderPath', 'fileName').where(builder => {
  244. const parentPathParts = parentPath.split('.')
  245. for (let i = 1; i <= parentPathParts.length; i++) {
  246. const ancestor = {
  247. folderPath: encodeFolderPath(dropRight(parentPathParts, i).join('.')),
  248. fileName: nth(parentPathParts, i * -1)
  249. }
  250. expectedAncestors.push(ancestor)
  251. builder.orWhere({
  252. ...ancestor,
  253. type: 'folder'
  254. })
  255. }
  256. })
  257. for (const ancestor of differenceWith(expectedAncestors, existingAncestors, (expAnc, exsAnc) => expAnc.folderPath === exsAnc.folderPath && expAnc.fileName === exsAnc.fileName)) {
  258. WIKI.logger.debug(`Creating missing parent folder ${ancestor.fileName} at path /${ancestor.folderPath}...`)
  259. const newAncestorFullPath = ancestor.folderPath ? `${decodeTreePath(ancestor.folderPath)}/${ancestor.fileName}` : ancestor.fileName
  260. const newAncestor = await WIKI.db.knex('tree').insert({
  261. ...ancestor,
  262. type: 'folder',
  263. title: ancestor.fileName,
  264. hash: generateHash(newAncestorFullPath),
  265. locale,
  266. siteId,
  267. meta: {
  268. children: 1
  269. }
  270. }).returning('*')
  271. // Parent didn't exist until now, assign it
  272. if (!parent && ancestor.folderPath === parentFilter.folderPath && ancestor.fileName === parentFilter.fileName) {
  273. parent = newAncestor[0]
  274. }
  275. }
  276. }
  277. // Create folder
  278. const fullPath = parentPath ? `${decodeTreePath(parentPath)}/${pathName}` : pathName
  279. const folder = await WIKI.db.knex('tree').insert({
  280. folderPath: encodeFolderPath(parentPath),
  281. fileName: pathName,
  282. type: 'folder',
  283. title,
  284. hash: generateHash(fullPath),
  285. locale,
  286. siteId,
  287. meta: {
  288. children: 0
  289. }
  290. }).returning('*')
  291. // Update parent ancestor count
  292. if (parent) {
  293. await WIKI.db.knex('tree').where('id', parent.id).update({
  294. meta: {
  295. ...(parent.meta ?? {}),
  296. children: (parent.meta?.children || 0) + 1
  297. }
  298. })
  299. }
  300. WIKI.logger.debug(`Created folder ${folder[0].id} successfully.`)
  301. return folder[0]
  302. }
  303. /**
  304. * Rename a folder
  305. *
  306. * @param {Object} args - Rename Folder Properties
  307. * @param {string} args.folderId - UUID of the folder to rename
  308. * @param {string} args.pathName - New path name of the folder
  309. * @param {string} args.title - New title of the folder
  310. */
  311. static async renameFolder ({ folderId, pathName, title }) {
  312. // Get folder
  313. const folder = await WIKI.db.knex('tree').where('id', folderId).first()
  314. if (!folder) {
  315. throw new Error('ERR_INVALID_FOLDER')
  316. }
  317. // Validate path name
  318. if (!rePathName.test(pathName)) {
  319. throw new Error('ERR_INVALID_PATH')
  320. }
  321. // Validate title
  322. if (!reTitle.test(title)) {
  323. throw new Error('ERR_FOLDER_TITLE_INVALID')
  324. }
  325. WIKI.logger.debug(`Renaming folder ${folder.id} path to ${pathName}...`)
  326. if (pathName !== folder.fileName) {
  327. // Check for collision
  328. const existingFolder = await WIKI.db.knex('tree')
  329. .whereNot('id', folder.id)
  330. .andWhere({
  331. siteId: folder.siteId,
  332. folderPath: folder.folderPath,
  333. fileName: pathName,
  334. type: 'folder'
  335. }).first()
  336. if (existingFolder) {
  337. throw new Error('ERR_FOLDER_DUPLICATE')
  338. }
  339. // Build new paths
  340. const oldFolderPath = encodeFolderPath(folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName)
  341. const newFolderPath = encodeFolderPath(folder.folderPath ? `${folder.folderPath}.${pathName}` : pathName)
  342. // Update children nodes
  343. WIKI.logger.debug(`Updating parent path of children nodes from ${oldFolderPath} to ${newFolderPath} ...`)
  344. await WIKI.db.knex('tree').where('siteId', folder.siteId).andWhere('folderPath', oldFolderPath).update({
  345. folderPath: newFolderPath
  346. })
  347. await WIKI.db.knex('tree').where('siteId', folder.siteId).andWhere('folderPath', '<@', oldFolderPath).update({
  348. folderPath: WIKI.db.knex.raw(`'${newFolderPath}' || subpath(tree."folderPath", nlevel('${newFolderPath}'))`)
  349. })
  350. // Rename the folder itself
  351. const fullPath = folder.folderPath ? `${decodeFolderPath(folder.folderPath)}/${pathName}` : pathName
  352. await WIKI.db.knex('tree').where('id', folder.id).update({
  353. fileName: pathName,
  354. title,
  355. hash: generateHash(fullPath)
  356. })
  357. } else {
  358. // Update the folder title only
  359. await WIKI.db.knex('tree').where('id', folder.id).update({
  360. title
  361. })
  362. }
  363. WIKI.logger.debug(`Renamed folder ${folder.id} successfully.`)
  364. }
  365. /**
  366. * Delete a folder
  367. *
  368. * @param {String} folderId Folder ID
  369. */
  370. static async deleteFolder (folderId) {
  371. // Get folder
  372. const folder = await WIKI.db.knex('tree').where('id', folderId).first()
  373. if (!folder) {
  374. throw new Error('ERR_INVALID_FOLDER')
  375. }
  376. const folderPath = folder.folderPath ? `${folder.folderPath}.${folder.fileName}` : folder.fileName
  377. WIKI.logger.debug(`Deleting folder ${folder.id} at path ${folderPath}...`)
  378. // Delete all children
  379. const deletedNodes = await WIKI.db.knex('tree').where('folderPath', '<@', encodeFolderPath(folderPath)).del().returning(['id', 'type'])
  380. // Delete folders
  381. const deletedFolders = deletedNodes.filter(n => n.type === 'folder').map(n => n.id)
  382. if (deletedFolders.length > 0) {
  383. WIKI.logger.debug(`Deleted ${deletedFolders.length} children folders.`)
  384. }
  385. // Delete pages
  386. const deletedPages = deletedNodes.filter(n => n.type === 'page').map(n => n.id)
  387. if (deletedPages.length > 0) {
  388. WIKI.logger.debug(`Deleting ${deletedPages.length} children pages...`)
  389. // TODO: Delete page
  390. }
  391. // Delete assets
  392. const deletedAssets = deletedNodes.filter(n => n.type === 'asset').map(n => n.id)
  393. if (deletedAssets.length > 0) {
  394. WIKI.logger.debug(`Deleting ${deletedPages.length} children assets...`)
  395. // TODO: Delete asset
  396. }
  397. // Delete the folder itself
  398. await WIKI.db.knex('tree').where('id', folder.id).del()
  399. // Update parent children count
  400. if (folder.folderPath) {
  401. const parentPathParts = folder.folderPath.split('.')
  402. const parent = await WIKI.db.knex('tree').where({
  403. folderPath: encodeFolderPath(dropRight(parentPathParts).join('.')),
  404. fileName: last(parentPathParts)
  405. }).first()
  406. await WIKI.db.knex('tree').where('id', parent.id).update({
  407. meta: {
  408. ...(parent.meta ?? {}),
  409. children: (parent.meta?.children || 1) - 1
  410. }
  411. })
  412. }
  413. WIKI.logger.debug(`Deleted folder ${folder.id} successfully.`)
  414. }
  415. }