From 1cb2535de3341dd338f3e0651f96dade4c2ed5d6 Mon Sep 17 00:00:00 2001 From: Philip Okugbe <16838612+Philipinho@users.noreply.github.com> Date: Sat, 2 Aug 2025 00:14:00 +0100 Subject: [PATCH] fix trash in search (#1439) - delete share if page is trashed --- .../src/core/comment/comment.controller.ts | 7 ++++-- apps/server/src/core/search/search.service.ts | 2 ++ apps/server/src/core/share/share.service.ts | 10 ++++---- .../src/database/repos/page/page.repo.ts | 24 ++++++++++++------- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/apps/server/src/core/comment/comment.controller.ts b/apps/server/src/core/comment/comment.controller.ts index ea9447b6..5ced1656 100644 --- a/apps/server/src/core/comment/comment.controller.ts +++ b/apps/server/src/core/comment/comment.controller.ts @@ -43,7 +43,7 @@ export class CommentController { @AuthWorkspace() workspace: Workspace, ) { const page = await this.pageRepo.findById(createCommentDto.pageId); - if (!page) { + if (!page || page.deletedAt) { throw new NotFoundException('Page not found'); } @@ -90,7 +90,10 @@ export class CommentController { throw new NotFoundException('Comment not found'); } - const ability = await this.spaceAbility.createForUser(user, comment.spaceId); + const ability = await this.spaceAbility.createForUser( + user, + comment.spaceId, + ); if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) { throw new ForbiddenException(); } diff --git a/apps/server/src/core/search/search.service.ts b/apps/server/src/core/search/search.service.ts index db135c22..60432ce8 100644 --- a/apps/server/src/core/search/search.service.ts +++ b/apps/server/src/core/search/search.service.ts @@ -59,6 +59,7 @@ export class SearchService { .$if(Boolean(searchParams.creatorId), (qb) => qb.where('creatorId', '=', searchParams.creatorId), ) + .where('deletedAt', 'is', null) .orderBy('rank', 'desc') .limit(searchParams.limit | 20) .offset(searchParams.offset || 0); @@ -191,6 +192,7 @@ export class SearchService { sql`LOWER(f_unaccent(${`%${query}%`}))`, ), ) + .where('deletedAt', 'is', null) .where('workspaceId', '=', workspaceId) .limit(limit); diff --git a/apps/server/src/core/share/share.service.ts b/apps/server/src/core/share/share.service.ts index d71b6acd..15cdbab2 100644 --- a/apps/server/src/core/share/share.service.ts +++ b/apps/server/src/core/share/share.service.ts @@ -108,12 +108,12 @@ export class ShareService { includeCreator: true, }); - page.content = await this.updatePublicAttachments(page); - - if (!page) { + if (!page || page.deletedAt) { throw new NotFoundException('Shared page not found'); } + page.content = await this.updatePublicAttachments(page); + return { page, share }; } @@ -132,6 +132,7 @@ export class ShareService { sql`0`.as('level'), ]) .where(isValidUUID(pageId) ? 'id' : 'slugId', '=', pageId) + .where('deletedAt', 'is', null) .unionAll((union) => union .selectFrom('pages as p') @@ -144,7 +145,8 @@ export class ShareService { // Increase the level by 1 for each ancestor. sql`ph.level + 1`.as('level'), ]) - .innerJoin('page_hierarchy as ph', 'ph.parentPageId', 'p.id'), + .innerJoin('page_hierarchy as ph', 'ph.parentPageId', 'p.id') + .where('p.deletedAt', 'is', null), ), ) .selectFrom('page_hierarchy') diff --git a/apps/server/src/database/repos/page/page.repo.ts b/apps/server/src/database/repos/page/page.repo.ts index 14b40dce..c814240a 100644 --- a/apps/server/src/database/repos/page/page.repo.ts +++ b/apps/server/src/database/repos/page/page.repo.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { InjectKysely } from 'nestjs-kysely'; import { KyselyDB, KyselyTransaction } from '../../types/kysely.types'; -import { dbOrTx } from '../../utils'; +import { dbOrTx, executeTx } from '../../utils'; import { InsertablePage, Page, @@ -183,14 +183,20 @@ export class PageRepo { const pageIds = descendants.map((d) => d.id); - await this.db - .updateTable('pages') - .set({ - deletedById: deletedById, - deletedAt: currentDate, - }) - .where('id', 'in', pageIds) - .execute(); + if (pageIds.length > 0) { + await executeTx(this.db, async (trx) => { + await trx + .updateTable('pages') + .set({ + deletedById: deletedById, + deletedAt: currentDate, + }) + .where('id', 'in', pageIds) + .execute(); + + await trx.deleteFrom('shares').where('pageId', 'in', pageIds).execute(); + }); + } } async restorePage(pageId: string): Promise {