Refactor CollaborationSession and Document schemas to improve code clarity and functionality. Enhance error handling in collaboration session retrieval and update logic. Introduce new request sync events in Document schema and update Minio service for document page management. Adjust cron job to use current date for future schedule date checks. Update user schema for better error handling and improve service descriptions in order schema. Remove global decorators from Workshop modules for better module encapsulation.
This commit is contained in:
@@ -66,6 +66,12 @@ export class DocumentSchema extends PothosSchema {
|
||||
senderId: t.string({
|
||||
nullable: true,
|
||||
}),
|
||||
requestSync: t.boolean({
|
||||
nullable: true,
|
||||
}),
|
||||
totalPage: t.int({
|
||||
nullable: true,
|
||||
}),
|
||||
}),
|
||||
})
|
||||
}
|
||||
@@ -143,6 +149,28 @@ export class DocumentSchema extends PothosSchema {
|
||||
return document
|
||||
},
|
||||
}),
|
||||
eventDocumentClientRequestSync: t.field({
|
||||
type: this.documentDelta(),
|
||||
args: {
|
||||
documentId: t.arg({ type: 'String', required: true }),
|
||||
pageIndex: t.arg({ type: 'Int', required: true }),
|
||||
},
|
||||
resolve: async (_, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (!ctx.http?.me?.id) throw new Error('User not found')
|
||||
if (!args.documentId) throw new Error('Document id not found')
|
||||
if (!args.pageIndex) throw new Error('Page index not found')
|
||||
const delta = await this.minio.getDocumentPage(args.documentId, args.pageIndex)
|
||||
if (!delta) throw new Error('Delta not found')
|
||||
return {
|
||||
documentId: args.documentId,
|
||||
pageIndex: args.pageIndex,
|
||||
delta,
|
||||
senderId: 'server',
|
||||
eventType: DocumentEvent.CLIENT_REQUEST_SYNC,
|
||||
}
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
this.builder.mutationFields((t) => ({
|
||||
@@ -226,24 +254,32 @@ export class DocumentSchema extends PothosSchema {
|
||||
return args.data
|
||||
},
|
||||
}),
|
||||
eventDocumentRequestSync: t.field({
|
||||
|
||||
eventDocumentServerRequestSync: t.field({
|
||||
type: this.documentDelta(),
|
||||
args: {
|
||||
data: t.arg({
|
||||
type: this.documentDeltaInput(),
|
||||
required: true,
|
||||
}),
|
||||
data: t.arg({ type: this.documentDeltaInput(), required: true }),
|
||||
},
|
||||
resolve: async (_, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
const senderId = ctx.http?.me?.id
|
||||
if (!args.data.documentId) throw new Error('Document id not found')
|
||||
if (!senderId) throw new Error('User not found')
|
||||
if (!args.data.pageIndex) throw new Error('Page index not found')
|
||||
|
||||
// save delta to minio
|
||||
const delta = args.data.delta
|
||||
if (!delta) throw new Error('Delta not found')
|
||||
await this.minio.upsertDocumentPage(args.data.documentId, args.data.pageIndex, delta)
|
||||
const totalPage = await this.minio.countDocumentPages(args.data.documentId)
|
||||
return {
|
||||
...args.data,
|
||||
senderId: ctx.http?.me?.id,
|
||||
eventType: DocumentEvent.REQUEST_SYNC,
|
||||
totalPage,
|
||||
senderId,
|
||||
eventType: DocumentEvent.SERVER_REQUEST_SYNC,
|
||||
}
|
||||
},
|
||||
}),
|
||||
|
||||
updateDocument: t.prismaField({
|
||||
type: this.document(),
|
||||
args: {
|
||||
@@ -356,7 +392,16 @@ export class DocumentSchema extends PothosSchema {
|
||||
},
|
||||
resolve: async (payload: DocumentDelta, _args, ctx: SchemaContext) => {
|
||||
if (!ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (payload.senderId === ctx.websocket?.me?.id) return
|
||||
if (!payload.requestSync) {
|
||||
// using randomize sync mechanism to avoid performance issue
|
||||
const random = Math.random()
|
||||
// 0.5% chance to request sync
|
||||
if (random <= 0.005) {
|
||||
// set requestSync to true
|
||||
payload.requestSync = true
|
||||
return payload
|
||||
}
|
||||
}
|
||||
return payload
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user