mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-19 02:56:23 +08: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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user