bling bling

This commit is contained in:
2024-11-25 19:46:23 +07:00
parent c063f1a10e
commit 84a09375da
6 changed files with 152 additions and 39 deletions

View File

@@ -92,13 +92,17 @@ export class DocumentSchema extends PothosSchema {
myDocuments: t.prismaField({
type: [this.document()],
args: this.builder.generator.findManyArgs('Document'),
resolve: async (query, _parent, _args, ctx: SchemaContext) => {
resolve: async (query, _parent, args, ctx: SchemaContext) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http?.me?.id) throw new Error('User not found')
return await this.prisma.document.findMany({
...query,
orderBy: args.orderBy ?? undefined,
where: {
ownerId: ctx.http?.me?.id,
OR: [
{ ownerId: ctx.http.me.id },
{ collaborators: { some: { userId: ctx.http.me.id } } },
],
},
})
},
@@ -215,7 +219,7 @@ export class DocumentSchema extends PothosSchema {
},
}),
updateDocument: t.field({
eventUpdateDocument: t.field({
type: this.documentDelta(),
args: {
data: t.arg({
@@ -237,6 +241,52 @@ export class DocumentSchema extends PothosSchema {
return args.data
},
}),
updateDocument: t.prismaField({
type: this.document(),
args: {
documentId: t.arg({ type: 'String', required: true }),
data: t.arg({
type: this.builder.generator.getUpdateInput('Document', [
'id',
'ownerId',
'createdAt',
'updatedAt',
'collaborationSessionId',
'collaborationSession',
'owner',
'fileUrl',
'previewImageUrl',
]),
required: true,
}),
},
resolve: async (query, _parent, args, ctx: SchemaContext) => {
if (ctx.isSubscription) throw new Error('Not allowed')
if (!ctx.http?.me?.id) throw new Error('User not found')
// check if user is owner or collaborator
const document = await this.prisma.document.findUnique({
where: { id: args.documentId },
include: {
collaborators: true,
},
})
if (!document) throw new Error('Document not found')
if (
document.ownerId !== ctx.http?.me?.id &&
!document.isPublic &&
!document.collaborators.some(
(c) => c.userId === ctx.http?.me?.id && c.writable,
)
)
throw new Error('User is not owner or collaborator of document')
return await this.prisma.document.update({
...query,
where: { id: args.documentId },
data: args.data,
})
},
}),
addCollaborator: t.prismaField({
type: this.documentCollaborator(),
args: {
@@ -275,16 +325,35 @@ export class DocumentSchema extends PothosSchema {
required: true,
}),
},
subscribe: (_, args, ctx: SchemaContext) => {
subscribe: async (_, args, ctx: SchemaContext) => {
if (!ctx.isSubscription) throw new Error('Not allowed')
const {
websocket: { pubSub },
} = ctx
const documentId = args.documentId
// check user permission
const document = await this.prisma.document.findUnique({
where: { id: documentId },
include: {
collaborators: true,
},
})
if (!document) throw new Error('Document not found')
if (!document.isPublic) {
if (
document.ownerId !== ctx.websocket?.me?.id &&
!document.collaborators.some(
(c) => c.userId === ctx.websocket?.me?.id && c.writable,
)
)
throw new Error('User is not owner or collaborator of document')
}
return pubSub.asyncIterator([
`${DocumentEvent.CHANGED}.${args.documentId}`,
`${DocumentEvent.CREATED}.${args.documentId}`,
`${DocumentEvent.DELETED}.${args.documentId}`,
`${DocumentEvent.SAVED}.${args.documentId}`,
`${DocumentEvent.CHANGED}.${documentId}`,
`${DocumentEvent.CREATED}.${documentId}`,
`${DocumentEvent.DELETED}.${documentId}`,
`${DocumentEvent.SAVED}.${documentId}`,
`${DocumentEvent.ACTIVE_DOCUMENT_ID_CHANGED}.${documentId}`,
]) as unknown as AsyncIterable<DocumentDelta>
},
resolve: async (payload: DocumentDelta, _args, ctx: SchemaContext) => {