mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 12:55:00 +00:00
refactor(server): use DocModel to access doc meta (#10593)
This commit is contained in:
@@ -3,8 +3,9 @@ import { Transactional } from '@nestjs-cls/transactional';
|
||||
import type { Update } from '@prisma/client';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
import { DocIsNotPublic } from '../base/error';
|
||||
import { BaseModel } from './base';
|
||||
import { Doc, publicUserSelect } from './common';
|
||||
import { Doc, DocRole, PublicDocMode, publicUserSelect } from './common';
|
||||
|
||||
export interface DocRecord extends Doc {}
|
||||
|
||||
@@ -342,6 +343,12 @@ export class DocModel extends BaseModel {
|
||||
})) as Prisma.WorkspaceDocGetPayload<{ select: Select }> | null;
|
||||
}
|
||||
|
||||
async setDefaultRole(workspaceId: string, docId: string, role: DocRole) {
|
||||
return await this.upsertMeta(workspaceId, docId, {
|
||||
defaultRole: role,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the workspace public doc metas.
|
||||
*/
|
||||
@@ -365,5 +372,51 @@ export class DocModel extends BaseModel {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the workspace has any public docs.
|
||||
*/
|
||||
async hasPublic(workspaceId: string) {
|
||||
const count = await this.getPublicsCount(workspaceId);
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish a doc as public.
|
||||
*/
|
||||
async publish(
|
||||
workspaceId: string,
|
||||
docId: string,
|
||||
mode: PublicDocMode = PublicDocMode.Page
|
||||
) {
|
||||
return await this.upsertMeta(workspaceId, docId, {
|
||||
public: true,
|
||||
mode,
|
||||
});
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
async unpublish(workspaceId: string, docId: string) {
|
||||
const docMeta = await this.getMeta(workspaceId, docId);
|
||||
if (!docMeta?.public) {
|
||||
throw new DocIsNotPublic();
|
||||
}
|
||||
|
||||
return await this.upsertMeta(workspaceId, docId, {
|
||||
public: false,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the doc is public.
|
||||
*/
|
||||
async isPublic(workspaceId: string, docId: string) {
|
||||
const docMeta = await this.getMeta(workspaceId, docId, {
|
||||
select: {
|
||||
public: true,
|
||||
},
|
||||
});
|
||||
return docMeta?.public ?? false;
|
||||
}
|
||||
// #endregion
|
||||
}
|
||||
|
||||
@@ -2,9 +2,8 @@ import { Injectable } from '@nestjs/common';
|
||||
import { Transactional } from '@nestjs-cls/transactional';
|
||||
import { type Workspace } from '@prisma/client';
|
||||
|
||||
import { DocIsNotPublic, EventBus } from '../base';
|
||||
import { EventBus } from '../base';
|
||||
import { BaseModel } from './base';
|
||||
import { DocRole, PublicDocMode } from './common';
|
||||
|
||||
declare global {
|
||||
interface Events {
|
||||
@@ -90,78 +89,4 @@ export class WorkspaceModel extends BaseModel {
|
||||
return workspace?.enableUrlPreview ?? false;
|
||||
}
|
||||
// #endregion
|
||||
|
||||
// #region doc
|
||||
async getDoc(workspaceId: string, docId: string) {
|
||||
return await this.db.workspaceDoc.findUnique({
|
||||
where: {
|
||||
workspaceId_docId: { workspaceId, docId },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async isPublicPage(workspaceId: string, docId: string) {
|
||||
const doc = await this.getDoc(workspaceId, docId);
|
||||
if (doc?.public) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const workspace = await this.get(workspaceId);
|
||||
return workspace?.public ?? false;
|
||||
}
|
||||
|
||||
async publishDoc(
|
||||
workspaceId: string,
|
||||
docId: string,
|
||||
mode: PublicDocMode = PublicDocMode.Page
|
||||
) {
|
||||
return await this.db.workspaceDoc.upsert({
|
||||
where: { workspaceId_docId: { workspaceId, docId } },
|
||||
update: { public: true, mode },
|
||||
create: { workspaceId, docId, public: true, mode },
|
||||
});
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
async revokePublicDoc(workspaceId: string, docId: string) {
|
||||
const doc = await this.getDoc(workspaceId, docId);
|
||||
|
||||
if (!doc?.public) {
|
||||
throw new DocIsNotPublic();
|
||||
}
|
||||
|
||||
return await this.db.workspaceDoc.update({
|
||||
where: { workspaceId_docId: { workspaceId, docId } },
|
||||
data: { public: false },
|
||||
});
|
||||
}
|
||||
|
||||
async hasPublicDoc(workspaceId: string) {
|
||||
const count = await this.db.workspaceDoc.count({
|
||||
where: {
|
||||
workspaceId,
|
||||
public: true,
|
||||
},
|
||||
});
|
||||
|
||||
return count > 0;
|
||||
}
|
||||
|
||||
async getPublicDocs(workspaceId: string) {
|
||||
return await this.db.workspaceDoc.findMany({
|
||||
where: {
|
||||
workspaceId,
|
||||
public: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async setDocDefaultRole(workspaceId: string, docId: string, role: DocRole) {
|
||||
await this.db.workspaceDoc.upsert({
|
||||
where: { workspaceId_docId: { workspaceId, docId } },
|
||||
update: { defaultRole: role },
|
||||
create: { workspaceId, docId, defaultRole: role },
|
||||
});
|
||||
}
|
||||
// #endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user