toi cam thay minh la sieu nhan
This commit is contained in:
8
src/Document/document.event.ts
Normal file
8
src/Document/document.event.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export enum DocumentEvent {
|
||||
CREATED = 'document_created',
|
||||
CHANGED = 'document_changed',
|
||||
DELETED = 'document_deleted',
|
||||
SAVED = 'document_saved',
|
||||
PAGE_CREATED = 'document_page_created',
|
||||
PAGE_DELETED = 'document_page_deleted',
|
||||
}
|
||||
9
src/Document/document.module.ts
Normal file
9
src/Document/document.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { DocumentService } from './document.service'
|
||||
import { DocumentSchema } from './document.schema'
|
||||
import { JSDOM } from 'jsdom'
|
||||
@Module({
|
||||
providers: [JSDOM, DocumentService, DocumentSchema],
|
||||
exports: [DocumentService, DocumentSchema],
|
||||
})
|
||||
export class DocumentModule {}
|
||||
110
src/Document/document.schema.ts
Normal file
110
src/Document/document.schema.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { Inject, Injectable, Logger } from '@nestjs/common'
|
||||
import {
|
||||
Pothos,
|
||||
PothosRef,
|
||||
PothosSchema,
|
||||
SchemaBuilderToken,
|
||||
} from '@smatch-corp/nestjs-pothos'
|
||||
import { Builder, SchemaContext } from '../Graphql/graphql.builder'
|
||||
import { PrismaService } from '../Prisma/prisma.service'
|
||||
import { DocumentEvent } from './document.event'
|
||||
import { Document } from '@prisma/client'
|
||||
@Injectable()
|
||||
export class DocumentSchema extends PothosSchema {
|
||||
constructor(
|
||||
@Inject(SchemaBuilderToken) private readonly builder: Builder,
|
||||
private readonly prisma: PrismaService,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
@PothosRef()
|
||||
document() {
|
||||
return this.builder.prismaObject('Document', {
|
||||
fields: (t) => ({
|
||||
id: t.exposeID('id'),
|
||||
name: t.exposeString('name'),
|
||||
fileUrl: t.exposeString('fileUrl'),
|
||||
createdAt: t.expose('createdAt', { type: 'DateTime' }),
|
||||
updatedAt: t.expose('updatedAt', { type: 'DateTime' }),
|
||||
owner: t.relation('owner'),
|
||||
ownerId: t.exposeID('ownerId'),
|
||||
collaborators: t.exposeStringList('collaborators'),
|
||||
isPublic: t.exposeBoolean('isPublic'),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
@Pothos()
|
||||
init(): void {
|
||||
this.builder.queryFields((t) => ({
|
||||
document: t.prismaField({
|
||||
type: this.document(),
|
||||
args: this.builder.generator.findUniqueArgs('Document'),
|
||||
resolve: async (query, _root, args) => {
|
||||
return await this.prisma.document.findUnique({
|
||||
...query,
|
||||
where: args.where,
|
||||
})
|
||||
},
|
||||
}),
|
||||
|
||||
documents: t.prismaField({
|
||||
type: [this.document()],
|
||||
args: this.builder.generator.findManyArgs('Document'),
|
||||
resolve: async (query, _root, args) => {
|
||||
return await this.prisma.document.findMany({
|
||||
...query,
|
||||
skip: args.skip ?? undefined,
|
||||
take: args.take ?? undefined,
|
||||
orderBy: args.orderBy ?? undefined,
|
||||
where: args.filter ?? undefined,
|
||||
})
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
this.builder.mutationFields((t) => ({
|
||||
createDocument: t.prismaField({
|
||||
type: this.document(),
|
||||
args: {
|
||||
data: t.arg({
|
||||
type: this.builder.generator.getCreateInput('Document'),
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, _root, args) => {
|
||||
return await this.prisma.document.create({
|
||||
...query,
|
||||
data: args.data,
|
||||
})
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
this.builder.subscriptionFields((t) => ({
|
||||
document: t.field({
|
||||
type: this.document(),
|
||||
args: {
|
||||
documentId: t.arg({
|
||||
type: 'String',
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
subscribe: (_, args, ctx: SchemaContext) => {
|
||||
if (!ctx.isSubscription) throw new Error('Not allowed')
|
||||
const {
|
||||
websocket: { pubSub },
|
||||
} = ctx
|
||||
return pubSub.asyncIterator([
|
||||
`${DocumentEvent.CHANGED}.${args.documentId}`,
|
||||
`${DocumentEvent.CREATED}.${args.documentId}`,
|
||||
`${DocumentEvent.DELETED}.${args.documentId}`,
|
||||
`${DocumentEvent.SAVED}.${args.documentId}`,
|
||||
]) as unknown as AsyncIterable<Document>
|
||||
},
|
||||
resolve: async (payload: Document) => payload,
|
||||
}),
|
||||
}))
|
||||
}
|
||||
}
|
||||
42
src/Document/document.service.ts
Normal file
42
src/Document/document.service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { forwardRef, Inject, Injectable } from '@nestjs/common'
|
||||
|
||||
import Delta, { Op } from 'quill-delta'
|
||||
import { MinioService } from '../Minio/minio.service'
|
||||
import { DocumentDelta } from './document.type'
|
||||
import { JSDOM } from 'jsdom'
|
||||
@Injectable()
|
||||
export class DocumentService {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
private quill: any
|
||||
constructor(
|
||||
private readonly minio: MinioService,
|
||||
public document: JSDOM,
|
||||
) {
|
||||
;(async () => {
|
||||
await this.loadQuill()
|
||||
})()
|
||||
}
|
||||
|
||||
private async loadQuill() {
|
||||
if (typeof window === 'undefined' || typeof document === 'undefined') {
|
||||
const { window } = new JSDOM('<!doctype html><html><body></body></html>')
|
||||
const { navigator } = window
|
||||
const { Node } = window
|
||||
global.window = window
|
||||
global.document = window.document
|
||||
global.navigator = navigator
|
||||
global.Node = Node
|
||||
}
|
||||
|
||||
const { default: Quill } = await import('quill')
|
||||
this.quill = Quill
|
||||
}
|
||||
|
||||
async handleOnChange(delta: DocumentDelta) {}
|
||||
|
||||
async handleOnSave() {}
|
||||
|
||||
async handleOnSync() {}
|
||||
|
||||
async requestSync() {}
|
||||
}
|
||||
6
src/Document/document.type.ts
Normal file
6
src/Document/document.type.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import Delta from 'quill-delta'
|
||||
|
||||
export type DocumentDelta = Delta & {
|
||||
pageIndex: number
|
||||
documentId: string
|
||||
}
|
||||
@@ -39,7 +39,8 @@ import { WorkshopModule } from '../Workshop/workshop.module'
|
||||
import { WorkshopOrganizationModule } from '../WorkshopOrganization/workshoporganization.module'
|
||||
import { WorkshopSubscriptionModule } from '../WorkshopSubscription/workshopsubscription.module'
|
||||
import { initContextCache } from '@pothos/core'
|
||||
import { PubSub, PubSubOptions } from 'graphql-subscriptions'
|
||||
import { PubSub } from 'graphql-subscriptions'
|
||||
import { DocumentModule } from 'src/Document/document.module'
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
@@ -74,6 +75,7 @@ import { PubSub, PubSubOptions } from 'graphql-subscriptions'
|
||||
ManagedServiceModule,
|
||||
WorkshopMeetingRoomModule,
|
||||
AdminNoteModule,
|
||||
DocumentModule,
|
||||
PothosModule.forRoot({
|
||||
builder: {
|
||||
inject: [PrismaService],
|
||||
|
||||
@@ -173,6 +173,7 @@ export class MessageSchema extends PothosSchema {
|
||||
if (args.input.content.length > 1024) {
|
||||
throw new Error('Content cannot be longer than 1024 characters')
|
||||
}
|
||||
args.input.context = messageContext
|
||||
const message = await this.prisma.message.create({
|
||||
...query,
|
||||
data: args.input,
|
||||
|
||||
@@ -13,7 +13,6 @@ export class MinioService {
|
||||
) {}
|
||||
|
||||
async uploadFile(file: FileUpload, category: string) {
|
||||
// @ts-nocheck
|
||||
const { mimetype, createReadStream, filename: actualFileName } = await file
|
||||
const filename = this.fileName()
|
||||
const Name = `${category}/${filename}`
|
||||
@@ -88,6 +87,8 @@ export class MinioService {
|
||||
)
|
||||
}
|
||||
|
||||
async streamFile(id: string, category: string) {}
|
||||
|
||||
fileName() {
|
||||
// generate a unique file name using uuid
|
||||
return uuidv4()
|
||||
|
||||
@@ -3,4 +3,5 @@ export enum PubSubEvent {
|
||||
MESSAGE_SENT = 'MESSAGE_SENT',
|
||||
NEW_MESSAGE = 'NEW_MESSAGE',
|
||||
NOTIFICATION = 'NOTIFICATION',
|
||||
DOCUMENT_UPDATED = 'DOCUMENT_UPDATED',
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user