chore: update biome configuration and enhance error handling in schema files
- Enabled useIgnoreFile in biome.json for better file management. - Updated various correctness and style rules in biome.json to enforce stricter coding standards. - Added new biome lint command in package.json for improved code quality checks. - Refactored error handling in multiple schema files to use consistent error throwing patterns, enhancing readability and maintainability. - Improved user authentication checks across various schemas to ensure proper access control.
This commit is contained in:
@@ -94,8 +94,12 @@ export class DocumentSchema extends PothosSchema {
|
||||
type: [this.document()],
|
||||
args: this.builder.generator.findManyArgs('Document'),
|
||||
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')
|
||||
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,
|
||||
@@ -133,9 +137,13 @@ export class DocumentSchema extends PothosSchema {
|
||||
type: this.document(),
|
||||
args: {},
|
||||
resolve: async (query, _args, ctx, _info) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
const userId = ctx.http?.me?.id
|
||||
if (!userId) throw new Error('User not found')
|
||||
if (!userId) {
|
||||
throw new Error('User not found')
|
||||
}
|
||||
// const fileUrl = await this.minio.getFileUrl('document', 'document', 'document')
|
||||
// if (!fileUrl) throw new Error('File not found')
|
||||
const document = await this.prisma.document.create({
|
||||
@@ -156,10 +164,18 @@ export class DocumentSchema extends PothosSchema {
|
||||
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 === undefined || args.pageIndex === null) throw new Error('Page index not found')
|
||||
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 === undefined || args.pageIndex === null) {
|
||||
throw new Error('Page index not found')
|
||||
}
|
||||
let delta = null
|
||||
try {
|
||||
delta = await this.minio.getDocumentPage(args.documentId, args.pageIndex)
|
||||
@@ -197,9 +213,13 @@ export class DocumentSchema extends PothosSchema {
|
||||
}),
|
||||
},
|
||||
resolve: async (query, _parent, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
const userId = ctx.http?.me?.id
|
||||
if (!userId) throw new Error('Unauthorized')
|
||||
if (!userId) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
return await this.prisma.document.create({
|
||||
...query,
|
||||
data: {
|
||||
@@ -225,12 +245,16 @@ export class DocumentSchema extends PothosSchema {
|
||||
}),
|
||||
},
|
||||
resolve: async (_, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
const {
|
||||
http: { pubSub },
|
||||
} = ctx
|
||||
const senderId = ctx.http?.me?.id
|
||||
if (!senderId) throw new Error('User not found')
|
||||
if (!senderId) {
|
||||
throw new Error('User not found')
|
||||
}
|
||||
pubSub.publish(`${DocumentEvent.CHANGED}.${args.data.documentId}`, {
|
||||
...args.data,
|
||||
senderId,
|
||||
@@ -245,14 +269,24 @@ export class DocumentSchema extends PothosSchema {
|
||||
data: t.arg({ type: this.documentDeltaInput(), required: true }),
|
||||
},
|
||||
resolve: async (_, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
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 === undefined || args.data.pageIndex === null) throw new Error('Page index not found')
|
||||
if (!args.data.documentId) {
|
||||
throw new Error('Document id not found')
|
||||
}
|
||||
if (!senderId) {
|
||||
throw new Error('User not found')
|
||||
}
|
||||
if (args.data.pageIndex === undefined || args.data.pageIndex === null) {
|
||||
throw new Error('Page index not found')
|
||||
}
|
||||
// save delta to minio
|
||||
const delta = args.data.delta
|
||||
if (!delta) throw new Error('Delta not found')
|
||||
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 {
|
||||
@@ -283,8 +317,12 @@ export class DocumentSchema extends PothosSchema {
|
||||
}),
|
||||
},
|
||||
resolve: async (query, _parent, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (!ctx.http?.me?.id) throw new Error('Unauthorized')
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
if (!ctx.http?.me?.id) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
// check if user is owner or collaborator
|
||||
const document = await this.prisma.document.findUnique({
|
||||
where: { id: args.documentId },
|
||||
@@ -292,7 +330,9 @@ export class DocumentSchema extends PothosSchema {
|
||||
collaborators: true,
|
||||
},
|
||||
})
|
||||
if (!document) throw new Error('Document not found')
|
||||
if (!document) {
|
||||
throw new Error('Document not found')
|
||||
}
|
||||
if (
|
||||
!document.isPublic &&
|
||||
!document.collaborators.some((c) => c.userId === ctx.http?.me?.id && c.writable) &&
|
||||
@@ -316,13 +356,19 @@ export class DocumentSchema extends PothosSchema {
|
||||
writable: t.arg({ type: 'Boolean', required: true }),
|
||||
},
|
||||
resolve: async (_, __, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
// check if ctx user is owner of document
|
||||
const document = await this.prisma.document.findUnique({
|
||||
where: { id: args.documentId },
|
||||
})
|
||||
if (!document) throw new Error('Document not found')
|
||||
if (document.ownerId !== ctx.http?.me?.id) throw new Error('User is not owner of document')
|
||||
if (!document) {
|
||||
throw new Error('Document not found')
|
||||
}
|
||||
if (document.ownerId !== ctx.http?.me?.id) {
|
||||
throw new Error('User is not owner of document')
|
||||
}
|
||||
return await this.prisma.documentCollaborator.create({
|
||||
data: {
|
||||
documentId: args.documentId,
|
||||
@@ -340,13 +386,19 @@ export class DocumentSchema extends PothosSchema {
|
||||
userId: t.arg({ type: 'String', required: true }),
|
||||
},
|
||||
resolve: async (_, __, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
// check if ctx user is owner of document
|
||||
const document = await this.prisma.document.findUnique({
|
||||
where: { id: args.documentId },
|
||||
})
|
||||
if (!document) throw new Error('Document not found')
|
||||
if (document.ownerId !== ctx.http?.me?.id) throw new Error('User is not owner of document')
|
||||
if (!document) {
|
||||
throw new Error('Document not found')
|
||||
}
|
||||
if (document.ownerId !== ctx.http?.me?.id) {
|
||||
throw new Error('User is not owner of document')
|
||||
}
|
||||
return await this.prisma.documentCollaborator.delete({
|
||||
where: { documentId_userId: { documentId: args.documentId, userId: args.userId } },
|
||||
})
|
||||
@@ -361,13 +413,19 @@ export class DocumentSchema extends PothosSchema {
|
||||
writable: t.arg({ type: 'Boolean', required: true }),
|
||||
},
|
||||
resolve: async (_, __, args, ctx: SchemaContext) => {
|
||||
if (ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
// check if ctx user is owner of document
|
||||
const document = await this.prisma.document.findUnique({
|
||||
where: { id: args.documentId },
|
||||
})
|
||||
if (!document) throw new Error('Document not found')
|
||||
if (document.ownerId !== ctx.http?.me?.id) throw new Error('User is not owner of document')
|
||||
if (!document) {
|
||||
throw new Error('Document not found')
|
||||
}
|
||||
if (document.ownerId !== ctx.http?.me?.id) {
|
||||
throw new Error('User is not owner of document')
|
||||
}
|
||||
return await this.prisma.documentCollaborator.update({
|
||||
where: { documentId_userId: { documentId: args.documentId, userId: args.userId } },
|
||||
data: { readable: args.readable, writable: args.writable },
|
||||
@@ -386,7 +444,9 @@ export class DocumentSchema extends PothosSchema {
|
||||
}),
|
||||
},
|
||||
subscribe: async (_, args, ctx: SchemaContext) => {
|
||||
if (!ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (!ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
const {
|
||||
websocket: { pubSub },
|
||||
} = ctx
|
||||
@@ -398,13 +458,16 @@ export class DocumentSchema extends PothosSchema {
|
||||
collaborators: true,
|
||||
},
|
||||
})
|
||||
if (!document) throw new Error('Document not found')
|
||||
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 && c.readable)
|
||||
)
|
||||
) {
|
||||
throw new Error('User is not owner or collaborator of document')
|
||||
}
|
||||
}
|
||||
return pubSub.asyncIterableIterator([
|
||||
`${DocumentEvent.CHANGED}.${documentId}`,
|
||||
@@ -416,7 +479,9 @@ export class DocumentSchema extends PothosSchema {
|
||||
]) as unknown as AsyncIterable<DocumentDelta>
|
||||
},
|
||||
resolve: async (payload: DocumentDelta, _args, ctx: SchemaContext) => {
|
||||
if (!ctx.isSubscription) throw new Error('Not allowed')
|
||||
if (!ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
if (!payload.requestSync) {
|
||||
// using randomize sync mechanism to avoid performance issue
|
||||
const random = Math.random()
|
||||
|
||||
Reference in New Issue
Block a user