mirror of
https://github.com/docmost/docmost.git
synced 2026-05-12 09:54:03 +08:00
WIP
This commit is contained in:
@@ -25,6 +25,7 @@ import { MigrationService } from '@docmost/db/services/migration.service';
|
||||
import { UserTokenRepo } from './repos/user-token/user-token.repo';
|
||||
import { BacklinkRepo } from '@docmost/db/repos/backlink/backlink.repo';
|
||||
import { ShareRepo } from '@docmost/db/repos/share/share.repo';
|
||||
import { PagePermissionRepo } from '@docmost/db/repos/page/page-permission-repo.service';
|
||||
|
||||
// https://github.com/brianc/node-postgres/issues/811
|
||||
types.setTypeParser(types.builtins.INT8, (val) => Number(val));
|
||||
@@ -75,7 +76,8 @@ types.setTypeParser(types.builtins.INT8, (val) => Number(val));
|
||||
AttachmentRepo,
|
||||
UserTokenRepo,
|
||||
BacklinkRepo,
|
||||
ShareRepo
|
||||
ShareRepo,
|
||||
PagePermissionRepo,
|
||||
],
|
||||
exports: [
|
||||
WorkspaceRepo,
|
||||
@@ -90,7 +92,8 @@ types.setTypeParser(types.builtins.INT8, (val) => Number(val));
|
||||
AttachmentRepo,
|
||||
UserTokenRepo,
|
||||
BacklinkRepo,
|
||||
ShareRepo
|
||||
ShareRepo,
|
||||
PagePermissionRepo,
|
||||
],
|
||||
})
|
||||
export class DatabaseModule
|
||||
|
||||
@@ -3,7 +3,7 @@ import { type Kysely, sql } from 'kysely';
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.alterTable('pages')
|
||||
.addColumn('contributor_ids', sql`uuid[]`, (col) => col.defaultTo("{}"))
|
||||
.addColumn('contributor_ids', sql`uuid[]`, (col) => col.defaultTo('{}'))
|
||||
.execute();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { Kysely, sql } from 'kysely';
|
||||
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.createTable('page_permissions')
|
||||
.addColumn('id', 'uuid', (col) =>
|
||||
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
|
||||
)
|
||||
.addColumn('user_id', 'uuid', (col) =>
|
||||
col.references('users.id').onDelete('cascade'),
|
||||
)
|
||||
.addColumn('group_id', 'uuid', (col) =>
|
||||
col.references('groups.id').onDelete('cascade'),
|
||||
)
|
||||
.addColumn('page_id', 'uuid', (col) =>
|
||||
col.notNull().references('pages.id').onDelete('cascade'),
|
||||
)
|
||||
.addColumn('role', 'varchar', (col) => col.notNull())
|
||||
.addColumn('cascade', 'boolean', (col) => col.defaultTo(true).notNull()) // children can inherit
|
||||
.addColumn('added_by_id', 'uuid', (col) =>
|
||||
col.references('users.id').onDelete('set null'),
|
||||
)
|
||||
.addColumn('created_at', 'timestamptz', (col) =>
|
||||
col.notNull().defaultTo(sql`now()`),
|
||||
)
|
||||
.addColumn('updated_at', 'timestamptz', (col) =>
|
||||
col.notNull().defaultTo(sql`now()`),
|
||||
)
|
||||
.addColumn('deleted_at', 'timestamptz')
|
||||
.addUniqueConstraint('unique_page_user', ['page_id', 'user_id'])
|
||||
.addUniqueConstraint('unique_page_group', ['page_id', 'group_id'])
|
||||
.addCheckConstraint(
|
||||
'allow_either_user_id_or_group_id_check',
|
||||
sql`(user_id IS NOT NULL AND group_id IS NULL) OR (user_id IS NULL AND group_id IS NOT NULL)`,
|
||||
)
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.alterTable('pages')
|
||||
.addColumn('is_restricted', 'boolean', (col) =>
|
||||
col.defaultTo(false).notNull(),
|
||||
)
|
||||
.addColumn('restricted_by_id', 'uuid', (col) =>
|
||||
col.references('users.id').onDelete('set null'),
|
||||
)
|
||||
.execute();
|
||||
|
||||
// Add indexes for performance
|
||||
await db.schema
|
||||
.createIndex('idx_page_permissions_page_id')
|
||||
.on('page_permissions')
|
||||
.column('page_id')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createIndex('idx_page_permissions_user_id')
|
||||
.on('page_permissions')
|
||||
.column('user_id')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createIndex('idx_page_permissions_group_id')
|
||||
.on('page_permissions')
|
||||
.column('group_id')
|
||||
.execute();
|
||||
|
||||
// Create user_shared_pages table for tracking orphaned page access
|
||||
await db.schema
|
||||
.createTable('user_shared_pages')
|
||||
.addColumn('user_id', 'uuid', (col) =>
|
||||
col.notNull().references('users.id').onDelete('cascade'),
|
||||
)
|
||||
.addColumn('page_id', 'uuid', (col) =>
|
||||
col.notNull().references('pages.id').onDelete('cascade'),
|
||||
)
|
||||
.addColumn('shared_at', 'timestamptz', (col) =>
|
||||
col.notNull().defaultTo(sql`now()`),
|
||||
)
|
||||
.addPrimaryKeyConstraint('user_shared_pages_pkey', ['user_id', 'page_id'])
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createIndex('idx_user_shared_pages_user_id')
|
||||
.on('user_shared_pages')
|
||||
.column('user_id')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createIndex('idx_user_shared_pages_shared_at')
|
||||
.on('user_shared_pages')
|
||||
.column('shared_at')
|
||||
.execute();
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
await db.schema.alterTable('pages').dropColumn('is_restricted').execute();
|
||||
await db.schema.alterTable('pages').dropColumn('restricted_by_id').execute();
|
||||
|
||||
await db.schema.dropTable('user_shared_pages').execute();
|
||||
|
||||
await db.schema.dropTable('page_permissions').execute();
|
||||
}
|
||||
@@ -22,5 +22,5 @@ export class PaginationOptions {
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
query: string;
|
||||
query?: string;
|
||||
}
|
||||
|
||||
@@ -105,7 +105,10 @@ export class CommentRepo {
|
||||
return Number(result?.count) > 0;
|
||||
}
|
||||
|
||||
async hasChildrenFromOtherUsers(commentId: string, userId: string): Promise<boolean> {
|
||||
async hasChildrenFromOtherUsers(
|
||||
commentId: string,
|
||||
userId: string,
|
||||
): Promise<boolean> {
|
||||
const result = await this.db
|
||||
.selectFrom('comments')
|
||||
.select((eb) => eb.fn.count('id').as('count'))
|
||||
|
||||
@@ -57,7 +57,11 @@ export class GroupUserRepo {
|
||||
|
||||
if (pagination.query) {
|
||||
query = query.where((eb) =>
|
||||
eb(sql`f_unaccent(users.name)`, 'ilike', sql`f_unaccent(${'%' + pagination.query + '%'})`),
|
||||
eb(
|
||||
sql`f_unaccent(users.name)`,
|
||||
'ilike',
|
||||
sql`f_unaccent(${'%' + pagination.query + '%'})`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -114,7 +114,11 @@ export class GroupRepo {
|
||||
|
||||
if (pagination.query) {
|
||||
query = query.where((eb) =>
|
||||
eb(sql`f_unaccent(name)`, 'ilike', sql`f_unaccent(${'%' + pagination.query + '%'})`).or(
|
||||
eb(
|
||||
sql`f_unaccent(name)`,
|
||||
'ilike',
|
||||
sql`f_unaccent(${'%' + pagination.query + '%'})`,
|
||||
).or(
|
||||
sql`f_unaccent(description)`,
|
||||
'ilike',
|
||||
sql`f_unaccent(${'%' + pagination.query + '%'})`,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -22,24 +22,6 @@ export class PageRepo {
|
||||
private spaceMemberRepo: SpaceMemberRepo,
|
||||
) {}
|
||||
|
||||
withHasChildren(eb: ExpressionBuilder<DB, 'pages'>) {
|
||||
return eb
|
||||
.selectFrom('pages as child')
|
||||
.select((eb) =>
|
||||
eb
|
||||
.case()
|
||||
.when(eb.fn.countAll(), '>', 0)
|
||||
.then(true)
|
||||
.else(false)
|
||||
.end()
|
||||
.as('count'),
|
||||
)
|
||||
.whereRef('child.parentPageId', '=', 'pages.id')
|
||||
.where('child.deletedAt', 'is', null)
|
||||
.limit(1)
|
||||
.as('hasChildren');
|
||||
}
|
||||
|
||||
private baseFields: Array<keyof Page> = [
|
||||
'id',
|
||||
'slugId',
|
||||
@@ -379,6 +361,24 @@ export class PageRepo {
|
||||
).as('contributors');
|
||||
}
|
||||
|
||||
withHasChildren(eb: ExpressionBuilder<DB, 'pages'>) {
|
||||
return eb
|
||||
.selectFrom('pages as child')
|
||||
.select((eb) =>
|
||||
eb
|
||||
.case()
|
||||
.when(eb.fn.countAll(), '>', 0)
|
||||
.then(true)
|
||||
.else(false)
|
||||
.end()
|
||||
.as('count'),
|
||||
)
|
||||
.whereRef('child.parentPageId', '=', 'pages.id')
|
||||
.where('child.deletedAt', 'is', null)
|
||||
.limit(1)
|
||||
.as('hasChildren');
|
||||
}
|
||||
|
||||
async getPageAndDescendants(
|
||||
parentPageId: string,
|
||||
opts: { includeContent: boolean },
|
||||
@@ -420,4 +420,46 @@ export class PageRepo {
|
||||
.selectAll()
|
||||
.execute();
|
||||
}
|
||||
|
||||
async update(
|
||||
pageId: string,
|
||||
updatablePage: UpdatablePage,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
const db = dbOrTx(this.db, trx);
|
||||
await db
|
||||
.updateTable('pages')
|
||||
.set({ ...updatablePage, updatedAt: new Date() })
|
||||
.where('id', '=', pageId)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async getAllDescendants(
|
||||
pageId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<string[]> {
|
||||
const db = dbOrTx(this.db, trx);
|
||||
|
||||
// Recursive CTE to get all descendants
|
||||
const descendants = await db
|
||||
.withRecursive('page_tree', (qb) =>
|
||||
qb
|
||||
.selectFrom('pages')
|
||||
.select(['id', 'parentPageId'])
|
||||
.where('parentPageId', '=', pageId)
|
||||
.where('deletedAt', 'is', null)
|
||||
.unionAll((eb) =>
|
||||
eb
|
||||
.selectFrom('pages as p')
|
||||
.innerJoin('page_tree as pt', 'p.parentPageId', 'pt.id')
|
||||
.select(['p.id', 'p.parentPageId'])
|
||||
.where('p.deletedAt', 'is', null),
|
||||
),
|
||||
)
|
||||
.selectFrom('page_tree')
|
||||
.select('id')
|
||||
.execute();
|
||||
|
||||
return descendants.map((d) => d.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { KyselyDB } from '../../types/kysely.types';
|
||||
import { Page } from '../../types/entity.types';
|
||||
import { PageMemberRole } from './page-permission-repo.service';
|
||||
|
||||
@Injectable()
|
||||
export class SharedPagesRepo {
|
||||
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
||||
|
||||
async addSharedPage(userId: string, pageId: string): Promise<void> {
|
||||
await this.db
|
||||
.insertInto('userSharedPages')
|
||||
.values({
|
||||
userId,
|
||||
pageId,
|
||||
sharedAt: new Date(),
|
||||
})
|
||||
.onConflict((oc) => oc.columns(['userId', 'pageId']).doNothing())
|
||||
.execute();
|
||||
}
|
||||
|
||||
async removeSharedPage(userId: string, pageId: string): Promise<void> {
|
||||
await this.db
|
||||
.deleteFrom('userSharedPages')
|
||||
.where('userId', '=', userId)
|
||||
.where('pageId', '=', pageId)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async getUserSharedPages(userId: string): Promise<Page[]> {
|
||||
return await this.db
|
||||
.selectFrom('userSharedPages as usp')
|
||||
.innerJoin('pages as p', 'p.id', 'usp.pageId')
|
||||
.innerJoin('pagePermissions as pm', (join) =>
|
||||
join
|
||||
.onRef('pm.pageId', '=', 'p.id')
|
||||
.on('pm.userId', '=', userId)
|
||||
.on('pm.role', '!=', PageMemberRole.NONE),
|
||||
)
|
||||
.selectAll('p')
|
||||
.where('usp.userId', '=', userId)
|
||||
.where('p.deletedAt', 'is', null)
|
||||
.orderBy('usp.sharedAt', 'desc')
|
||||
.execute();
|
||||
}
|
||||
|
||||
async isPageSharedWithUser(userId: string, pageId: string): Promise<boolean> {
|
||||
const result = await this.db
|
||||
.selectFrom('userSharedPages')
|
||||
.select('userId')
|
||||
.where('userId', '=', userId)
|
||||
.where('pageId', '=', pageId)
|
||||
.executeTakeFirst();
|
||||
|
||||
return !!result;
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,11 @@ export class SpaceRepo {
|
||||
|
||||
if (pagination.query) {
|
||||
query = query.where((eb) =>
|
||||
eb(sql`f_unaccent(name)`, 'ilike', sql`f_unaccent(${'%' + pagination.query + '%'})`).or(
|
||||
eb(
|
||||
sql`f_unaccent(name)`,
|
||||
'ilike',
|
||||
sql`f_unaccent(${'%' + pagination.query + '%'})`,
|
||||
).or(
|
||||
sql`f_unaccent(description)`,
|
||||
'ilike',
|
||||
sql`f_unaccent(${'%' + pagination.query + '%'})`,
|
||||
|
||||
+42
-8
@@ -3,15 +3,18 @@
|
||||
* Please do not edit it manually.
|
||||
*/
|
||||
|
||||
import type { ColumnType } from "kysely";
|
||||
import type { ColumnType } from 'kysely';
|
||||
|
||||
export type AuthProviderType = "google" | "oidc" | "saml";
|
||||
export type Generated<T> =
|
||||
T extends ColumnType<infer S, infer I, infer U>
|
||||
? ColumnType<S, I | undefined, U>
|
||||
: ColumnType<T, T | undefined, T>;
|
||||
|
||||
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
|
||||
? ColumnType<S, I | undefined, U>
|
||||
: ColumnType<T, T | undefined, T>;
|
||||
|
||||
export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>;
|
||||
export type Int8 = ColumnType<
|
||||
string,
|
||||
bigint | number | string,
|
||||
bigint | number | string
|
||||
>;
|
||||
|
||||
export type Json = JsonValue;
|
||||
|
||||
@@ -62,13 +65,21 @@ export interface AuthProviders {
|
||||
deletedAt: Timestamp | null;
|
||||
id: Generated<string>;
|
||||
isEnabled: Generated<boolean>;
|
||||
ldapBaseDn: string | null;
|
||||
ldapBindDn: string | null;
|
||||
ldapBindPassword: string | null;
|
||||
ldapTlsCaCert: string | null;
|
||||
ldapTlsEnabled: Generated<boolean | null>;
|
||||
ldapUrl: string | null;
|
||||
ldapUserAttributes: Json | null;
|
||||
ldapUserSearchFilter: string | null;
|
||||
name: string;
|
||||
oidcClientId: string | null;
|
||||
oidcClientSecret: string | null;
|
||||
oidcIssuer: string | null;
|
||||
samlCertificate: string | null;
|
||||
samlUrl: string | null;
|
||||
type: AuthProviderType;
|
||||
type: string;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
workspaceId: string;
|
||||
}
|
||||
@@ -186,6 +197,19 @@ export interface PageHistory {
|
||||
workspaceId: string;
|
||||
}
|
||||
|
||||
export interface PagePermissions {
|
||||
addedById: string | null;
|
||||
cascade: Generated<boolean>;
|
||||
createdAt: Generated<Timestamp>;
|
||||
deletedAt: Timestamp | null;
|
||||
groupId: string | null;
|
||||
id: Generated<string>;
|
||||
pageId: string;
|
||||
role: string;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
userId: string | null;
|
||||
}
|
||||
|
||||
export interface Pages {
|
||||
content: Json | null;
|
||||
contributorIds: Generated<string[] | null>;
|
||||
@@ -197,9 +221,11 @@ export interface Pages {
|
||||
icon: string | null;
|
||||
id: Generated<string>;
|
||||
isLocked: Generated<boolean>;
|
||||
isRestricted: Generated<boolean>;
|
||||
lastUpdatedById: string | null;
|
||||
parentPageId: string | null;
|
||||
position: string | null;
|
||||
restrictedById: string | null;
|
||||
slugId: string;
|
||||
spaceId: string;
|
||||
textContent: string | null;
|
||||
@@ -284,6 +310,12 @@ export interface Users {
|
||||
workspaceId: string | null;
|
||||
}
|
||||
|
||||
export interface UserSharedPages {
|
||||
pageId: string;
|
||||
sharedAt: Generated<Timestamp>;
|
||||
userId: string;
|
||||
}
|
||||
|
||||
export interface UserTokens {
|
||||
createdAt: Generated<Timestamp>;
|
||||
expiresAt: Timestamp | null;
|
||||
@@ -342,12 +374,14 @@ export interface DB {
|
||||
groups: Groups;
|
||||
groupUsers: GroupUsers;
|
||||
pageHistory: PageHistory;
|
||||
pagePermissions: PagePermissions;
|
||||
pages: Pages;
|
||||
shares: Shares;
|
||||
spaceMembers: SpaceMembers;
|
||||
spaces: Spaces;
|
||||
userMfa: UserMfa;
|
||||
users: Users;
|
||||
userSharedPages: UserSharedPages;
|
||||
userTokens: UserTokens;
|
||||
workspaceInvitations: WorkspaceInvitations;
|
||||
workspaces: Workspaces;
|
||||
|
||||
@@ -4,8 +4,10 @@ import {
|
||||
Comments,
|
||||
Groups,
|
||||
Pages,
|
||||
PagePermissions,
|
||||
Spaces,
|
||||
Users,
|
||||
UserSharedPages,
|
||||
Workspaces,
|
||||
PageHistory as History,
|
||||
GroupUsers,
|
||||
@@ -48,6 +50,15 @@ export type SpaceMember = Selectable<SpaceMembers>;
|
||||
export type InsertableSpaceMember = Insertable<SpaceMembers>;
|
||||
export type UpdatableSpaceMember = Updateable<Omit<SpaceMembers, 'id'>>;
|
||||
|
||||
// PageMember
|
||||
export type PagePermission = Selectable<PagePermissions>;
|
||||
export type InsertablePagePermission = Insertable<PagePermissions>;
|
||||
export type UpdatablePagePermission = Updateable<Omit<PagePermissions, 'id'>>;
|
||||
|
||||
// UserSharedPage
|
||||
export type UserSharedPage = Selectable<UserSharedPages>;
|
||||
export type InsertableUserSharedPage = Insertable<UserSharedPages>;
|
||||
|
||||
// Group
|
||||
export type ExtendedGroup = Groups & { memberCount: number };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user