refactor layout

* ui polishing
* frontend and backend fixes
This commit is contained in:
Philipinho
2024-05-31 21:51:44 +01:00
parent 046dd6d150
commit 06d854a7d2
95 changed files with 1548 additions and 821 deletions

View File

@ -1,8 +1,4 @@
import {
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import {
AbilityBuilder,
createMongoAbility,

View File

@ -1,7 +1,14 @@
import { IsString, IsUUID } from 'class-validator';
import {
IsBoolean,
IsNotEmpty,
IsOptional,
IsString,
IsUUID,
} from 'class-validator';
export class PageIdDto {
@IsString()
@IsNotEmpty()
pageId: string;
}
@ -14,3 +21,9 @@ export class PageHistoryIdDto {
@IsUUID()
historyId: string;
}
export class PageInfoDto extends PageIdDto {
@IsOptional()
@IsBoolean()
includeSpace: boolean;
}

View File

@ -0,0 +1,7 @@
import { IsOptional, IsString } from 'class-validator';
export class RecentPageDto {
@IsOptional()
@IsString()
spaceId: string;
}

View File

@ -12,7 +12,7 @@ import { PageService } from './services/page.service';
import { CreatePageDto } from './dto/create-page.dto';
import { UpdatePageDto } from './dto/update-page.dto';
import { MovePageDto } from './dto/move-page.dto';
import { PageHistoryIdDto, PageIdDto } from './dto/page.dto';
import { PageHistoryIdDto, PageIdDto, PageInfoDto } from './dto/page.dto';
import { PageHistoryService } from './services/page-history.service';
import { AuthUser } from '../../decorators/auth-user.decorator';
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
@ -26,6 +26,7 @@ import {
} from '../casl/interfaces/space-ability.type';
import SpaceAbilityFactory from '../casl/abilities/space-ability.factory';
import { PageRepo } from '@docmost/db/repos/page/page.repo';
import { RecentPageDto } from './dto/recent-page.dto';
@UseGuards(JwtAuthGuard)
@Controller('pages')
@ -39,8 +40,10 @@ export class PageController {
@HttpCode(HttpStatus.OK)
@Post('/info')
async getPage(@Body() pageIdDto: PageIdDto, @AuthUser() user: User) {
const page = await this.pageRepo.findById(pageIdDto.pageId);
async getPage(@Body() dto: PageInfoDto, @AuthUser() user: User) {
const page = await this.pageRepo.findById(dto.pageId, {
includeSpace: true,
});
if (!page) {
throw new NotFoundException('Page not found');
@ -117,24 +120,28 @@ export class PageController {
@HttpCode(HttpStatus.OK)
@Post('recent')
async getRecentSpacePages(
async getRecentPages(
@Body() recentPageDto: RecentPageDto,
@Body() pagination: PaginationOptions,
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
const ability = await this.spaceAbility.createForUser(
user,
workspace.defaultSpaceId,
);
if (recentPageDto.spaceId) {
const ability = await this.spaceAbility.createForUser(
user,
recentPageDto.spaceId,
);
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) {
throw new ForbiddenException();
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) {
throw new ForbiddenException();
}
return this.pageService.getRecentSpacePages(
recentPageDto.spaceId,
pagination,
);
}
return this.pageService.getRecentSpacePages(
workspace.defaultSpaceId,
pagination,
);
return this.pageService.getRecentPages(user.id, pagination);
}
// TODO: scope to workspaces

View File

@ -18,7 +18,7 @@ import { generateJitteredKeyBetween } from 'fractional-indexing-jittered';
import { MovePageDto } from '../dto/move-page.dto';
import { ExpressionBuilder } from 'kysely';
import { DB } from '@docmost/db/types/db';
import { genPageShortId } from '../../../helpers/nanoid.utils';
import { generateSlugId } from '../../../helpers';
@Injectable()
export class PageService {
@ -31,8 +31,13 @@ export class PageService {
pageId: string,
includeContent?: boolean,
includeYdoc?: boolean,
includeSpace?: boolean,
): Promise<Page> {
return this.pageRepo.findById(pageId, { includeContent, includeYdoc });
return this.pageRepo.findById(pageId, {
includeContent,
includeYdoc,
includeSpace,
});
}
async create(
@ -92,7 +97,7 @@ export class PageService {
}
const createdPage = await this.pageRepo.insertPage({
slugId: genPageShortId(),
slugId: generateSlugId(),
title: createPageDto.title,
position: pagePosition,
icon: createPageDto.icon,
@ -266,9 +271,14 @@ export class PageService {
spaceId: string,
pagination: PaginationOptions,
): Promise<PaginationResult<Page>> {
const pages = await this.pageRepo.getRecentPageUpdates(spaceId, pagination);
return await this.pageRepo.getRecentPagesInSpace(spaceId, pagination);
}
return pages;
async getRecentPages(
userId: string,
pagination: PaginationOptions,
): Promise<PaginationResult<Page>> {
return await this.pageRepo.getRecentPages(userId, pagination);
}
async forceDelete(pageId: string): Promise<void> {

View File

@ -1,9 +1,19 @@
import { IsBoolean, IsNumber, IsOptional, IsString } from 'class-validator';
import {
IsBoolean,
IsNotEmpty,
IsNumber,
IsOptional,
IsString,
} from 'class-validator';
export class SearchDTO {
@IsString()
query: string;
@IsNotEmpty()
@IsString()
spaceId: string;
@IsOptional()
@IsString()
creatorId?: string;

View File

@ -1,34 +1,51 @@
import {
Body,
Controller,
ForbiddenException,
HttpCode,
HttpStatus,
NotImplementedException,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { SearchService } from './search.service';
import { SearchDTO, SearchSuggestionDTO } from './dto/search.dto';
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { Workspace } from '@docmost/db/types/entity.types';
import { User, Workspace } from '@docmost/db/types/entity.types';
import SpaceAbilityFactory from '../casl/abilities/space-ability.factory';
import {
SpaceCaslAction,
SpaceCaslSubject,
} from '../casl/interfaces/space-ability.type';
import { AuthUser } from '../../decorators/auth-user.decorator';
@UseGuards(JwtAuthGuard)
@Controller('search')
export class SearchController {
constructor(private readonly searchService: SearchService) {}
constructor(
private readonly searchService: SearchService,
private readonly spaceAbility: SpaceAbilityFactory,
) {}
@HttpCode(HttpStatus.OK)
@Post()
async pageSearch(
@Body() searchDto: SearchDTO,
@AuthWorkspace() workspace: Workspace,
) {
return this.searchService.searchPage(
searchDto.query,
searchDto,
workspace.id,
);
async pageSearch(@Body() searchDto: SearchDTO, @AuthUser() user: User) {
if (searchDto.spaceId) {
const ability = await this.spaceAbility.createForUser(
user,
searchDto.spaceId,
);
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) {
throw new ForbiddenException();
}
return this.searchService.searchPage(searchDto.query, searchDto);
}
// TODO: search all spaces user is a member of if no spaceId provided
throw new NotImplementedException();
}
@Post('suggest')

View File

@ -4,17 +4,20 @@ import { SearchResponseDto } from './dto/search-response.dto';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB } from '@docmost/db/types/kysely.types';
import { sql } from 'kysely';
import { PageRepo } from '@docmost/db/repos/page/page.repo';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tsquery = require('pg-tsquery')();
@Injectable()
export class SearchService {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
constructor(
@InjectKysely() private readonly db: KyselyDB,
private pageRepo: PageRepo,
) {}
async searchPage(
query: string,
searchParams: SearchDTO,
workspaceId: string,
): Promise<SearchResponseDto[]> {
if (query.length < 1) {
return;
@ -28,6 +31,7 @@ export class SearchService {
'title',
'icon',
'parentPageId',
'slugId',
'creatorId',
'createdAt',
'updatedAt',
@ -36,7 +40,8 @@ export class SearchService {
'highlight',
),
])
.where('workspaceId', '=', workspaceId)
.select((eb) => this.pageRepo.withSpace(eb))
.where('spaceId', '=', searchParams.spaceId)
.where('tsv', '@@', sql<string>`to_tsquery(${searchQuery})`)
.$if(Boolean(searchParams.creatorId), (qb) =>
qb.where('creatorId', '=', searchParams.creatorId),

View File

@ -3,6 +3,6 @@ import { IsNotEmpty, IsString, IsUUID } from 'class-validator';
export class SpaceIdDto {
@IsString()
@IsNotEmpty()
@IsUUID()
//@IsUUID()
spaceId: string;
}

View File

@ -8,11 +8,12 @@ import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
import { AddSpaceMembersDto } from '../dto/add-space-members.dto';
import { InjectKysely } from 'nestjs-kysely';
import { SpaceMember, User } from '@docmost/db/types/entity.types';
import { Space, SpaceMember, User } from '@docmost/db/types/entity.types';
import { SpaceRepo } from '@docmost/db/repos/space/space.repo';
import { RemoveSpaceMemberDto } from '../dto/remove-space-member.dto';
import { UpdateSpaceMemberRoleDto } from '../dto/update-space-member-role.dto';
import { SpaceRole } from '../../../helpers/types/permission';
import { PaginationResult } from '@docmost/db/pagination/pagination';
@Injectable()
export class SpaceMemberService {
@ -49,11 +50,6 @@ export class SpaceMemberService {
workspaceId: string,
trx?: KyselyTransaction,
): Promise<void> {
//const existingSpaceUser = await manager.findOneBy(SpaceMember, {
// userId: userId,
// spaceId: spaceId,
// });
// validations?
await this.spaceMemberRepo.insertSpaceMember(
{
groupId: groupId,
@ -276,4 +272,11 @@ export class SpaceMemberService {
);
}
}
async getUserSpaces(
userId: string,
pagination: PaginationOptions,
): Promise<PaginationResult<Space>> {
return await this.spaceMemberRepo.getUserSpaces(userId, pagination);
}
}

View File

@ -5,6 +5,7 @@ import {
ForbiddenException,
HttpCode,
HttpStatus,
NotFoundException,
Post,
UseGuards,
} from '@nestjs/common';
@ -41,10 +42,8 @@ export class SpaceController {
@Body()
pagination: PaginationOptions,
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
// TODO: only show spaces user can see. e.g open and private with user being a member
return this.spaceService.getWorkspaceSpaces(workspace.id, pagination);
return this.spaceMemberService.getUserSpaces(user.id, pagination);
}
@HttpCode(HttpStatus.OK)
@ -54,15 +53,21 @@ export class SpaceController {
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
const ability = await this.spaceAbility.createForUser(
user,
const space = await this.spaceService.getSpaceInfo(
spaceIdDto.spaceId,
workspace.id,
);
if (!space) {
throw new NotFoundException('Space not found');
}
const ability = await this.spaceAbility.createForUser(user, space.id);
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Settings)) {
throw new ForbiddenException();
}
return this.spaceService.getSpaceInfo(spaceIdDto.spaceId, workspace.id);
return space;
}
@HttpCode(HttpStatus.OK)