Skip to content

Commit

Permalink
feat: rename / move page + code styling fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NGPixel committed Dec 23, 2024
1 parent 7425a74 commit d9523fe
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 116 deletions.
24 changes: 12 additions & 12 deletions server/graph/resolvers/page.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default {

// -> Add Highlighting if enabled
if (WIKI.config.search.termHighlighting && hasQuery) {
searchCols.push(WIKI.db.knex.raw(`ts_headline(?, "searchContent", query, 'MaxWords=5, MinWords=3, MaxFragments=5') AS highlight`, [dictName]))
searchCols.push(WIKI.db.knex.raw('ts_headline(?, "searchContent", query, \'MaxWords=5, MinWords=3, MaxFragments=5\') AS highlight', [dictName]))
}

const results = await WIKI.db.knex
Expand Down Expand Up @@ -408,7 +408,7 @@ export default {
* CHECK FOR EDITING CONFLICT
*/
async checkConflicts (obj, args, context, info) {
let page = await WIKI.db.pages.query().select('path', 'locale', 'updatedAt').findById(args.id)
const page = await WIKI.db.pages.query().select('path', 'locale', 'updatedAt').findById(args.id)
if (page) {
if (WIKI.auth.checkAccess(context.req.user, ['write:pages', 'manage:pages'], {
path: page.path,
Expand All @@ -426,7 +426,7 @@ export default {
* FETCH LATEST VERSION FOR CONFLICT COMPARISON
*/
async checkConflictsLatest (obj, args, context, info) {
let page = await WIKI.db.pages.getPageFromDb(args.id)
const page = await WIKI.db.pages.getPageFromDb(args.id)
if (page) {
if (WIKI.auth.checkAccess(context.req.user, ['write:pages', 'manage:pages'], {
path: page.path,
Expand All @@ -449,7 +449,7 @@ export default {
/**
* CREATE PAGE
*/
async createPage(obj, args, context) {
async createPage (obj, args, context) {
try {
const page = await WIKI.db.pages.createPage({
...args,
Expand All @@ -466,7 +466,7 @@ export default {
/**
* UPDATE PAGE
*/
async updatePage(obj, args, context) {
async updatePage (obj, args, context) {
try {
const page = await WIKI.db.pages.updatePage({
...args,
Expand All @@ -483,7 +483,7 @@ export default {
/**
* CONVERT PAGE
*/
async convertPage(obj, args, context) {
async convertPage (obj, args, context) {
try {
await WIKI.db.pages.convertPage({
...args,
Expand All @@ -497,9 +497,9 @@ export default {
}
},
/**
* RENAME PAGE
* MOVE PAGE
*/
async renamePage(obj, args, context) {
async movePage (obj, args, context) {
try {
await WIKI.db.pages.movePage({
...args,
Expand All @@ -515,7 +515,7 @@ export default {
/**
* DELETE PAGE
*/
async deletePage(obj, args, context) {
async deletePage (obj, args, context) {
try {
await WIKI.db.pages.deletePage({
...args,
Expand Down Expand Up @@ -571,7 +571,7 @@ export default {
/**
* FLUSH PAGE CACHE
*/
async flushCache(obj, args, context) {
async flushCache (obj, args, context) {
try {
await WIKI.db.pages.flushCache()
WIKI.events.outbound.emit('flushCache')
Expand All @@ -585,7 +585,7 @@ export default {
/**
* MIGRATE ALL PAGES FROM SOURCE LOCALE TO TARGET LOCALE
*/
async migrateToLocale(obj, args, context) {
async migrateToLocale (obj, args, context) {
try {
const count = await WIKI.db.pages.migrateToLocale(args)
return {
Expand All @@ -599,7 +599,7 @@ export default {
/**
* REBUILD TREE
*/
async rebuildPageTree(obj, args, context) {
async rebuildPageTree (obj, args, context) {
try {
await WIKI.db.pages.rebuildTree()
return {
Expand Down
7 changes: 4 additions & 3 deletions server/graph/schemas/page.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ extend type Mutation {
editor: String!
): DefaultResponse

renamePage(
id: Int!
destinationPath: String!
movePage(
id: UUID!
destinationLocale: String!
destinationPath: String!
title: String
): DefaultResponse

deletePage(
Expand Down
117 changes: 66 additions & 51 deletions server/models/pages.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Model } from 'objection'
import { find, get, has, initial, isEmpty, isString, last, pick } from 'lodash-es'
import { cloneDeep, find, get, has, initial, isEmpty, isString, last, pick } from 'lodash-es'
import { Type as JSBinType } from 'js-binary'
import { getDictNameFromLocale } from '../helpers/common.mjs'
import { generateHash, getFileExtension, injectPageMetadata } from '../helpers/page.mjs'
Expand Down Expand Up @@ -888,15 +888,10 @@ export class Page extends Model {
* @returns {Promise} Promise with no value
*/
static async movePage (opts) {
let page
if (has(opts, 'id')) {
page = await WIKI.db.pages.query().findById(opts.id)
} else {
page = await WIKI.db.pages.query().findOne({
path: opts.path,
locale: opts.locale
})
if (!has(opts, 'id')) {
throw new Error('Missing page ID')
}
const page = await WIKI.db.pages.query().findById(opts.id)
if (!page) {
throw new WIKI.Error.PageNotFound()
}
Expand Down Expand Up @@ -947,63 +942,83 @@ export class Page extends Model {
versionDate: page.updatedAt
})

const destinationHash = generateHash({ path: opts.destinationPath, locale: opts.destinationLocale })
// -> Update page object
const updatedPage = cloneDeep(page)
updatedPage.path = opts.destinationPath
updatedPage.locale = opts.destinationLocale
updatedPage.title = opts.title ?? page.title
updatedPage.hash = generateHash({ path: opts.destinationPath, locale: opts.destinationLocale })
updatedPage.authorId = opts.user.id

// -> Move page
const destinationTitle = (page.title === page.path ? opts.destinationPath : page.title)
await WIKI.db.pages.query().patch({
path: opts.destinationPath,
locale: opts.destinationLocale,
title: destinationTitle,
hash: destinationHash
path: updatedPage.path,
locale: updatedPage.locale,
title: updatedPage.title,
hash: updatedPage.hash,
authorId: updatedPage.authorId
}).findById(page.id)
await WIKI.db.pages.deletePageFromCache(page.hash)
WIKI.events.outbound.emit('deletePageFromCache', page.hash)

// -> Rebuild page tree
await WIKI.db.pages.rebuildTree()
// -> Replace tree node
const pathParts = updatedPage.path.split('/')
await WIKI.db.knex('tree').where('id', page.id).del()
await WIKI.db.tree.addPage({
id: page.id,
parentPath: initial(pathParts).join('/'),
fileName: last(pathParts),
locale: updatedPage.locale,
title: updatedPage.title,
tags: updatedPage.tags,
meta: {
authorId: updatedPage.authorId,
contentType: updatedPage.contentType,
creatorId: updatedPage.creatorId,
description: updatedPage.description,
isBrowsable: updatedPage.isBrowsable,
ownerId: updatedPage.ownerId,
publishState: updatedPage.publishState,
publishEndDate: updatedPage.publishEndDate,
publishStartDate: updatedPage.publishStartDate
},
siteId: updatedPage.siteId
})

// -> Rename in Search Index
const pageContents = await WIKI.db.pages.query().findById(page.id).select('render')
page.safeContent = WIKI.db.pages.cleanHTML(pageContents.render)
await WIKI.data.searchEngine.renamed({
...page,
destinationPath: opts.destinationPath,
destinationLocale: opts.destinationLocale,
destinationHash
})
WIKI.db.pages.updatePageSearchVector({ id: page.id })

// -> Rename in Storage
if (!opts.skipStorage) {
await WIKI.db.storage.pageEvent({
event: 'renamed',
page: {
...page,
destinationPath: opts.destinationPath,
destinationLocale: opts.destinationLocale,
destinationHash,
moveAuthorId: opts.user.id,
moveAuthorName: opts.user.name,
moveAuthorEmail: opts.user.email
}
})
// await WIKI.db.storage.pageEvent({
// event: 'renamed',
// page: {
// ...page,
// destinationPath: updatedPage.path,
// destinationLocale: updatedPage.locale,
// destinationHash: updatedPage.hash,
// moveAuthorId: opts.user.id,
// moveAuthorName: opts.user.name,
// moveAuthorEmail: opts.user.email
// }
// })
}

// -> Reconnect Links : Changing old links to the new path
await WIKI.db.pages.reconnectLinks({
sourceLocale: page.locale,
sourcePath: page.path,
locale: opts.destinationLocale,
path: opts.destinationPath,
mode: 'move'
})
// // -> Reconnect Links : Changing old links to the new path
// await WIKI.db.pages.reconnectLinks({
// sourceLocale: page.locale,
// sourcePath: page.path,
// locale: opts.destinationLocale,
// path: opts.destinationPath,
// mode: 'move'
// })

// -> Reconnect Links : Validate invalid links to the new path
await WIKI.db.pages.reconnectLinks({
locale: opts.destinationLocale,
path: opts.destinationPath,
mode: 'create'
})
// // -> Reconnect Links : Validate invalid links to the new path
// await WIKI.db.pages.reconnectLinks({
// locale: opts.destinationLocale,
// path: opts.destinationPath,
// mode: 'create'
// })
}

/**
Expand Down
Loading

0 comments on commit d9523fe

Please sign in to comment.