Compare commits

..
Author SHA1 Message Date
Salihu efc113de3e convert only bare links to embeds 2026-08-24 23:12:59 +01:00
Salihu cd9c166927 Merge pull request #2417 from docmost/fix/checkbox-filtering
fix(ee): checkbox filtering
2026-08-23 21:21:02 +01:00
Salihu b814bd0f12 fix: checkbox filtering 2026-08-21 19:29:10 +01:00
8 changed files with 49 additions and 164 deletions
@@ -7,7 +7,6 @@ import {
import { CreatePageDto, ContentFormat } from '../dto/create-page.dto';
import { ContentOperation, UpdatePageDto } from '../dto/update-page.dto';
import { PageRepo } from '@docmost/db/repos/page/page.repo';
import { MAX_PAGE_TREE_DEPTH } from '@docmost/db/repos/page/constants';
import { PagePermissionRepo } from '@docmost/db/repos/page/page-permission.repo';
import { InsertablePage, Page, User } from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
@@ -112,14 +111,6 @@ export class PageService {
throw new NotFoundException('Parent page not found');
}
const ancestorIds = await this.pageRepo.getAncestorPageIds(
parentPage.id,
trx,
);
if (ancestorIds.length >= MAX_PAGE_TREE_DEPTH) {
throw new BadRequestException('Page nesting is too deep');
}
parentPageId = parentPage.id;
}
@@ -848,18 +839,6 @@ export class PageService {
) {
throw new NotFoundException('Parent page not found');
}
const ancestorIds = await this.pageRepo.getAncestorPageIds(
parentPage.id,
);
if (ancestorIds.includes(movedPage.id)) {
throw new BadRequestException(
'Cannot move a page under its own descendant',
);
}
if (ancestorIds.length >= MAX_PAGE_TREE_DEPTH) {
throw new BadRequestException('Page nesting is too deep');
}
parentPageId = parentPage.id;
}
}
@@ -889,7 +868,6 @@ export class PageService {
'spaceId',
'deletedAt',
])
.select(sql<number>`0`.as('depth'))
.where('id', '=', childPageId)
.where('deletedAt', 'is', null)
.unionAll((exp) =>
@@ -906,24 +884,12 @@ export class PageService {
'p.spaceId',
'p.deletedAt',
])
.select(sql<number>`pa.depth + 1`.as('depth'))
.innerJoin('page_ancestors as pa', 'pa.parentPageId', 'p.id')
.where('p.deletedAt', 'is', null)
.where('pa.depth', '<', MAX_PAGE_TREE_DEPTH),
.where('p.deletedAt', 'is', null),
),
)
.selectFrom('page_ancestors')
.select([
'id',
'slugId',
'title',
'icon',
'isBase',
'position',
'parentPageId',
'spaceId',
'deletedAt',
])
.selectAll('page_ancestors')
.select((eb) =>
eb
.exists(
@@ -1043,18 +1009,17 @@ export class PageService {
.withRecursive('page_descendants', (db) =>
db
.selectFrom('pages')
.select(['id', sql<number>`0`.as('depth')])
.select(['id'])
.where('id', '=', pageId)
.unionAll((exp) =>
exp
.selectFrom('pages as p')
.select(['p.id', sql<number>`pd.depth + 1`.as('depth')])
.innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId')
.where('pd.depth', '<', MAX_PAGE_TREE_DEPTH),
.select(['p.id'])
.innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId'),
),
)
.selectFrom('page_descendants')
.select(['id'])
.selectAll()
.execute();
const pageIds = descendants.map((d) => d.id);
@@ -2,8 +2,6 @@ import { Injectable, Logger } from '@nestjs/common';
import { Interval } from '@nestjs/schedule';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB } from '@docmost/db/types/kysely.types';
import { MAX_PAGE_TREE_DEPTH } from '@docmost/db/repos/page/constants';
import { sql } from 'kysely';
import { InjectQueue } from '@nestjs/bullmq';
import { Queue } from 'bullmq';
import { QueueJob, QueueName } from '../../../integrations/queue/constants';
@@ -78,18 +76,17 @@ export class TrashCleanupService {
.withRecursive('page_descendants', (db) =>
db
.selectFrom('pages')
.select(['id', sql<number>`0`.as('depth')])
.select(['id'])
.where('id', '=', pageId)
.unionAll((exp) =>
exp
.selectFrom('pages as p')
.select(['p.id', sql<number>`pd.depth + 1`.as('depth')])
.innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId')
.where('pd.depth', '<', MAX_PAGE_TREE_DEPTH),
.select(['p.id'])
.innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId'),
),
)
.selectFrom('page_descendants')
.select(['id'])
.selectAll()
.execute();
const pageIds = descendants.map((d) => d.id);
+2 -6
View File
@@ -20,7 +20,6 @@ import {
import { Node } from '@tiptap/pm/model';
import { ShareRepo } from '@docmost/db/repos/share/share.repo';
import { PagePermissionRepo } from '@docmost/db/repos/page/page-permission.repo';
import { MAX_PAGE_TREE_DEPTH } from '@docmost/db/repos/page/constants';
import { updateAttachmentAttr } from './share.util';
import { Page } from '@docmost/db/types/entity.types';
import { validate as isValidUUID } from 'uuid';
@@ -248,7 +247,6 @@ export class ShareService {
.else(false)
.end()
.as('found'),
sql<number>`0`.as('depth'),
])
.where(isValidUUID(childPageId) ? 'id' : 'slugId', '=', childPageId)
.unionAll((exp) =>
@@ -268,16 +266,14 @@ export class ShareService {
.else(false)
.end()
.as('found'),
sql<number>`pa.depth + 1`.as('depth'),
])
.innerJoin('page_ancestors as pa', 'pa.parentPageId', 'p.id')
// Continue recursing only when the target ancestor hasn't been found on that branch.
.where('pa.found', '=', false)
.where('pa.depth', '<', MAX_PAGE_TREE_DEPTH),
.where('pa.found', '=', false),
),
)
.selectFrom('page_ancestors')
.select(['id', 'slugId', 'title', 'parentPageId', 'spaceId'])
.selectAll()
.where('found', '=', true)
.limit(1)
.executeTakeFirst();
@@ -1 +0,0 @@
export const MAX_PAGE_TREE_DEPTH = 100;
@@ -24,7 +24,6 @@ import {
CacheKey,
PERMISSION_CACHE_TTL_MS,
} from '../../../common/helpers/cache-keys';
import { MAX_PAGE_TREE_DEPTH } from './constants';
export { PagePermissionMember } from './types/page-permission.types';
@@ -351,8 +350,7 @@ export class PagePermissionRepo {
'pages.id as ancestorId',
'pages.parentPageId',
sql<number>`ancestors.depth + 1`.as('depth'),
])
.where('ancestors.depth', '<', MAX_PAGE_TREE_DEPTH),
]),
),
)
.selectFrom('ancestors')
@@ -407,7 +405,6 @@ export class PagePermissionRepo {
SELECT p.id, p.parent_page_id, a.depth + 1
FROM pages p
JOIN ancestors a ON a.parent_page_id = p.id
WHERE a.depth < ${MAX_PAGE_TREE_DEPTH}
)
SELECT
bool_and(pp.id IS NOT NULL) AS "canAccess",
@@ -474,8 +471,7 @@ export class PagePermissionRepo {
'pages.id as ancestorId',
'pages.parentPageId',
sql<number>`ancestors.depth + 1`.as('depth'),
])
.where('ancestors.depth', '<', MAX_PAGE_TREE_DEPTH),
]),
),
)
.selectFrom('pages')
@@ -680,7 +676,6 @@ export class PagePermissionRepo {
'pages.id as pageId',
'pages.id as ancestorId',
'pages.parentPageId',
sql<number>`0`.as('depth'),
])
.where(sql<SqlBool>`pages.id = ANY(${pageIds}::uuid[])`)
.unionAll((eb) =>
@@ -695,9 +690,7 @@ export class PagePermissionRepo {
'allAncestors.pageId',
'pages.id as ancestorId',
'pages.parentPageId',
sql<number>`all_ancestors.depth + 1`.as('depth'),
])
.where('allAncestors.depth', '<', MAX_PAGE_TREE_DEPTH),
]),
),
)
.selectFrom('pages')
@@ -767,8 +760,7 @@ export class PagePermissionRepo {
'pages.id as ancestorId',
'pages.parentPageId',
sql<number>`all_ancestors.depth + 1`.as('depth'),
])
.where('allAncestors.depth', '<', MAX_PAGE_TREE_DEPTH),
]),
),
)
.selectFrom('pages')
@@ -873,22 +865,13 @@ export class PagePermissionRepo {
.withRecursive('ancestors', (qb) =>
qb
.selectFrom('pages')
.select([
'pages.id as ancestorId',
'pages.parentPageId',
sql<number>`0`.as('depth'),
])
.select(['pages.id as ancestorId', 'pages.parentPageId'])
.where('pages.id', '=', pageId)
.unionAll((eb) =>
eb
.selectFrom('pages')
.innerJoin('ancestors', 'ancestors.parentPageId', 'pages.id')
.select([
'pages.id as ancestorId',
'pages.parentPageId',
sql<number>`ancestors.depth + 1`.as('depth'),
])
.where('ancestors.depth', '<', MAX_PAGE_TREE_DEPTH),
.select(['pages.id as ancestorId', 'pages.parentPageId']),
),
)
.selectFrom('ancestors')
@@ -938,7 +921,6 @@ export class PagePermissionRepo {
'child.id as childId',
'child.id as ancestorId',
'child.parentPageId as ancestorParentId',
sql<number>`0`.as('depth'),
])
.where('child.parentPageId', 'in', parentIds)
.where('child.deletedAt', 'is', null)
@@ -954,9 +936,7 @@ export class PagePermissionRepo {
'childAncestors.childId',
'pages.id as ancestorId',
'pages.parentPageId as ancestorParentId',
sql<number>`child_ancestors.depth + 1`.as('depth'),
])
.where('childAncestors.depth', '<', MAX_PAGE_TREE_DEPTH),
]),
),
)
.selectFrom('pages as child')
@@ -1008,11 +988,7 @@ export class PagePermissionRepo {
.withRecursive('descendants', (qb) =>
qb
.selectFrom('pages')
.select([
'pages.id as descendantId',
'pages.parentPageId',
sql<number>`0`.as('depth'),
])
.select(['pages.id as descendantId', 'pages.parentPageId'])
.where('pages.id', '=', rootPageId)
.unionAll((eb) =>
eb
@@ -1022,13 +998,8 @@ export class PagePermissionRepo {
'descendants.descendantId',
'pages.parentPageId',
)
.select([
'pages.id as descendantId',
'pages.parentPageId',
sql<number>`descendants.depth + 1`.as('depth'),
])
.where('pages.deletedAt', 'is', null)
.where('descendants.depth', '<', MAX_PAGE_TREE_DEPTH),
.select(['pages.id as descendantId', 'pages.parentPageId'])
.where('pages.deletedAt', 'is', null),
),
)
.withRecursive('descendantAncestors', (qb) =>
@@ -1039,7 +1010,6 @@ export class PagePermissionRepo {
'descendants.descendantId',
'pages.id as ancestorId',
'pages.parentPageId as ancestorParentId',
sql<number>`0`.as('depth'),
])
.unionAll((eb) =>
eb
@@ -1053,9 +1023,7 @@ export class PagePermissionRepo {
'descendantAncestors.descendantId',
'pages.id as ancestorId',
'pages.parentPageId as ancestorParentId',
sql<number>`descendant_ancestors.depth + 1`.as('depth'),
])
.where('descendantAncestors.depth', '<', MAX_PAGE_TREE_DEPTH),
]),
),
)
.selectFrom('descendantAncestors')
@@ -1084,14 +1052,13 @@ export class PagePermissionRepo {
const results = await sql<{ userId: string }>`
WITH RECURSIVE ancestors AS (
SELECT id AS ancestor_id, parent_page_id, 0 AS depth
SELECT id AS ancestor_id, parent_page_id
FROM pages
WHERE id = ${pageId}::uuid
UNION ALL
SELECT p.id, p.parent_page_id, a.depth + 1
SELECT p.id, p.parent_page_id
FROM pages p
JOIN ancestors a ON a.parent_page_id = p.id
WHERE a.depth < ${MAX_PAGE_TREE_DEPTH}
)
SELECT cu.user_id AS "userId"
FROM unnest(${userIds}::uuid[]) AS cu(user_id)
@@ -16,7 +16,6 @@ import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { EventName } from '../../../common/events/event.contants';
import { MAX_PAGE_TREE_DEPTH } from './constants';
@Injectable()
export class PageRepo {
@@ -204,20 +203,19 @@ export class PageRepo {
.withRecursive('page_descendants', (db) =>
db
.selectFrom('pages')
.select(['id', sql<number>`0`.as('depth')])
.select(['id'])
.where('id', '=', pageId)
.where('deletedAt', 'is', null)
.unionAll((exp) =>
exp
.selectFrom('pages as p')
.select(['p.id', sql<number>`pd.depth + 1`.as('depth')])
.select(['p.id'])
.innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId')
.where('p.deletedAt', 'is', null)
.where('pd.depth', '<', MAX_PAGE_TREE_DEPTH),
.where('p.deletedAt', 'is', null),
),
)
.selectFrom('page_descendants')
.select(['id'])
.selectAll()
.execute();
const pageIds = descendants.map((d) => d.id);
@@ -274,18 +272,17 @@ export class PageRepo {
.withRecursive('page_descendants', (db) =>
db
.selectFrom('pages')
.select(['id', sql<number>`0`.as('depth')])
.select(['id'])
.where('id', '=', pageId)
.unionAll((exp) =>
exp
.selectFrom('pages as p')
.select(['p.id', sql<number>`pd.depth + 1`.as('depth')])
.innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId')
.where('pd.depth', '<', MAX_PAGE_TREE_DEPTH),
.select(['p.id'])
.innerJoin('page_descendants as pd', 'pd.id', 'p.parentPageId'),
),
)
.selectFrom('page_descendants')
.select(['id'])
.selectAll()
.execute();
const pageIds = pages.map((p) => p.id);
@@ -490,35 +487,6 @@ export class PageRepo {
.as('hasChildren');
}
async getAncestorPageIds(
pageId: string,
trx?: KyselyTransaction,
): Promise<string[]> {
const ancestors = await dbOrTx(this.db, trx)
.withRecursive('page_ancestors', (db) =>
db
.selectFrom('pages')
.select(['id', 'parentPageId', sql<number>`0`.as('depth')])
.where('id', '=', pageId)
.unionAll((exp) =>
exp
.selectFrom('pages as p')
.select([
'p.id',
'p.parentPageId',
sql<number>`pa.depth + 1`.as('depth'),
])
.innerJoin('page_ancestors as pa', 'pa.parentPageId', 'p.id')
.where('pa.depth', '<', MAX_PAGE_TREE_DEPTH),
),
)
.selectFrom('page_ancestors')
.select(['id'])
.execute();
return ancestors.map((ancestor) => ancestor.id);
}
async getPageAndDescendants(
parentPageId: string,
opts: { includeContent: boolean },
@@ -539,7 +507,6 @@ export class PageRepo {
'createdAt',
'updatedAt',
])
.select(sql<number>`0`.as('depth'))
.$if(opts?.includeContent, (qb) => qb.select('content'))
.where('id', '=', parentPageId)
.where('deletedAt', 'is', null)
@@ -558,27 +525,13 @@ export class PageRepo {
'p.createdAt',
'p.updatedAt',
])
.select(sql<number>`ph.depth + 1`.as('depth'))
.$if(opts?.includeContent, (qb) => qb.select('p.content'))
.innerJoin('page_hierarchy as ph', 'p.parentPageId', 'ph.id')
.where('p.deletedAt', 'is', null)
.where('ph.depth', '<', MAX_PAGE_TREE_DEPTH),
.where('p.deletedAt', 'is', null),
),
)
.selectFrom('page_hierarchy')
.select([
'id',
'slugId',
'title',
'icon',
'position',
'parentPageId',
'spaceId',
'workspaceId',
'createdAt',
'updatedAt',
])
.$if(opts?.includeContent, (qb) => qb.select('content'))
.selectAll()
.execute();
}
@@ -610,7 +563,6 @@ export class PageRepo {
'pages.workspaceId',
sql<boolean>`page_access.id IS NOT NULL`.as('isRestricted'),
])
.select(sql<number>`0`.as('depth'))
.$if(opts?.includeContent, (qb) => qb.select('pages.content'))
.where('pages.id', '=', parentPageId)
.where('pages.deletedAt', 'is', null)
@@ -630,12 +582,10 @@ export class PageRepo {
'p.workspaceId',
sql<boolean>`page_access.id IS NOT NULL`.as('isRestricted'),
])
.select(sql<number>`ph.depth + 1`.as('depth'))
.$if(opts?.includeContent, (qb) => qb.select('p.content'))
.where('p.deletedAt', 'is', null)
// Only recurse into children of non-restricted pages
.where('ph.isRestricted', '=', false)
.where('ph.depth', '<', MAX_PAGE_TREE_DEPTH),
.where('ph.isRestricted', '=', false),
),
)
.selectFrom('page_hierarchy')
@@ -97,6 +97,15 @@ export function xwikiFormatter($: CheerioAPI, $root: Cheerio<any>) {
}
}
function isBareLink($el: Cheerio<any>): boolean {
const href = $el.attr("href")?.trim();
const text = $el.text().trim();
if(!text || !href) return false
return text === href;
}
export function defaultHtmlFormatter($: CheerioAPI, $root: Cheerio<any>) {
normalizeTableColumnWidths($, $root);
@@ -104,7 +113,9 @@ export function defaultHtmlFormatter($: CheerioAPI, $root: Cheerio<any>) {
const $el = $(el);
const url = $el.attr('href')!;
const { provider } = getEmbedUrlAndProvider(url);
if (provider === 'iframe') return;
if (provider === 'iframe' || !isBareLink($el)) {
return;
}
const embed = `<div data-type=\"embed\" data-src=\"${url}\" data-provider=\"${provider}\" data-align=\"center\" data-width=\"640\" data-height=\"480\"></div>`;
$el.replaceWith(embed);