thang an pha qua

This commit is contained in:
2024-11-08 15:15:59 +07:00
parent 57037a59ec
commit 1897ccf677
4 changed files with 43 additions and 12 deletions

20
package-lock.json generated
View File

@@ -22,6 +22,7 @@
"@nestjs/common": "^10.0.0", "@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3", "@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",
"@nestjs/event-emitter": "^2.1.1",
"@nestjs/graphql": "^12.2.0", "@nestjs/graphql": "^12.2.0",
"@nestjs/jwt": "^10.2.0", "@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
@@ -4655,6 +4656,19 @@
"integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==", "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==",
"license": "0BSD" "license": "0BSD"
}, },
"node_modules/@nestjs/event-emitter": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/@nestjs/event-emitter/-/event-emitter-2.1.1.tgz",
"integrity": "sha512-6L6fBOZTyfFlL7Ih/JDdqlCzZeCW0RjCX28wnzGyg/ncv5F/EOeT1dfopQr1loBRQ3LTgu8OWM7n4zLN4xigsg==",
"license": "MIT",
"dependencies": {
"eventemitter2": "6.4.9"
},
"peerDependencies": {
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0",
"@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0"
}
},
"node_modules/@nestjs/graphql": { "node_modules/@nestjs/graphql": {
"version": "12.2.1", "version": "12.2.1",
"resolved": "https://registry.npmjs.org/@nestjs/graphql/-/graphql-12.2.1.tgz", "resolved": "https://registry.npmjs.org/@nestjs/graphql/-/graphql-12.2.1.tgz",
@@ -9553,6 +9567,12 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/eventemitter2": {
"version": "6.4.9",
"resolved": "https://registry.npmjs.org/eventemitter2/-/eventemitter2-6.4.9.tgz",
"integrity": "sha512-JEPTiaOt9f04oa6NOkc4aH+nVp5I3wEjpHbIPqfgCdD5v5bUzy7xQqwcVO2aDQgOWhI28da57HksMrzK9HlRxg==",
"license": "MIT"
},
"node_modules/eventemitter3": { "node_modules/eventemitter3": {
"version": "5.0.1", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz", "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz",

View File

@@ -44,6 +44,7 @@
"@nestjs/common": "^10.0.0", "@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3", "@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",
"@nestjs/event-emitter": "^2.1.1",
"@nestjs/graphql": "^12.2.0", "@nestjs/graphql": "^12.2.0",
"@nestjs/jwt": "^10.2.0", "@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.0.0", "@nestjs/platform-express": "^10.0.0",
@@ -133,13 +134,19 @@
"ws": "^8.18.0" "ws": "^8.18.0"
}, },
"jest": { "jest": {
"moduleFileExtensions": ["js", "json", "ts"], "moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src", "rootDir": "src",
"testRegex": ".*\\.spec\\.ts$", "testRegex": ".*\\.spec\\.ts$",
"transform": { "transform": {
"^.+\\.(t|j)s$": "ts-jest" "^.+\\.(t|j)s$": "ts-jest"
}, },
"collectCoverageFrom": ["**/*.(t|j)s"], "collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage", "coverageDirectory": "../coverage",
"testEnvironment": "node" "testEnvironment": "node"
}, },

View File

@@ -40,8 +40,6 @@ import { WorkshopOrganizationModule } from '../WorkshopOrganization/workshoporga
import { WorkshopSubscriptionModule } from '../WorkshopSubscription/workshopsubscription.module' import { WorkshopSubscriptionModule } from '../WorkshopSubscription/workshopsubscription.module'
import { initContextCache } from '@pothos/core' import { initContextCache } from '@pothos/core'
import { PubSub } from 'graphql-subscriptions' import { PubSub } from 'graphql-subscriptions'
import { isSubscription } from 'rxjs/internal/Subscription'
import { EventEmitter } from 'ws'
@Global() @Global()
@Module({ @Module({
@@ -90,7 +88,6 @@ import { EventEmitter } from 'ws'
debug: process.env.NODE_ENV === 'development' || false, debug: process.env.NODE_ENV === 'development' || false,
playground: process.env.NODE_ENV === 'development' || false, playground: process.env.NODE_ENV === 'development' || false,
introspection: process.env.NODE_ENV === 'development' || false, introspection: process.env.NODE_ENV === 'development' || false,
installSubscriptionHandlers: true,
subscriptions: { subscriptions: {
'graphql-ws': true, 'graphql-ws': true,
}, },
@@ -100,6 +97,7 @@ import { EventEmitter } from 'ws'
extra, extra,
// biome-ignore lint/suspicious/noExplicitAny: <explanation> // biome-ignore lint/suspicious/noExplicitAny: <explanation>
}: { req?: Request; subscriptions?: any; extra?: any }) => { }: { req?: Request; subscriptions?: any; extra?: any }) => {
initContextCache()
if (subscriptions) { if (subscriptions) {
return { return {
isSubscription: true, isSubscription: true,
@@ -148,10 +146,7 @@ import { EventEmitter } from 'ws'
}, },
{ {
provide: 'PUB_SUB', provide: 'PUB_SUB',
useFactory: () => useFactory: () => new PubSub(),
new PubSub({
eventEmitter: new EventEmitter({}),
}),
}, },
], ],
exports: [ exports: [

View File

@@ -147,6 +147,7 @@ export class MessageSchema extends PothosSchema {
'senderId', 'senderId',
'sender', 'sender',
'sentAt', 'sentAt',
'context',
]), ]),
description: 'The message to send.', description: 'The message to send.',
required: true, required: true,
@@ -156,6 +157,7 @@ export class MessageSchema extends PothosSchema {
if (ctx.isSubscription) { if (ctx.isSubscription) {
throw new Error('Not allowed') throw new Error('Not allowed')
} }
const messageContext = MessageContextType.CHAT
// get the sender from the context and add it to the input // get the sender from the context and add it to the input
args.input.sender = { args.input.sender = {
connect: { connect: {
@@ -165,6 +167,7 @@ export class MessageSchema extends PothosSchema {
if (!args.input.sender) { if (!args.input.sender) {
throw new Error('Cannot get sender from context') throw new Error('Cannot get sender from context')
} }
args.input.context = messageContext
const message = await this.prisma.message.create({ const message = await this.prisma.message.create({
...query, ...query,
data: args.input, data: args.input,
@@ -172,7 +175,7 @@ export class MessageSchema extends PothosSchema {
if (ctx.isSubscription) { if (ctx.isSubscription) {
throw new Error('Not allowed') throw new Error('Not allowed')
} }
ctx.http.pubSub.publish('MESSAGE_SENT', message) ctx.http.pubSub.publish(`MESSAGE_SENT_${message.chatRoomId}`, message)
return message return message
}, },
}), }),
@@ -182,13 +185,19 @@ export class MessageSchema extends PothosSchema {
messageSent: t.field({ messageSent: t.field({
description: 'Subscribe to messages sent by users.', description: 'Subscribe to messages sent by users.',
type: this.message(), type: this.message(),
subscribe: (_, __, ctx: SchemaContext) => { args: {
chatRoomId: t.arg({
type: 'String',
description: 'The ID of the chat room to subscribe to.',
}),
},
subscribe: (_, args, ctx: SchemaContext) => {
if (!ctx.isSubscription) throw new Error('Not allowed') if (!ctx.isSubscription) throw new Error('Not allowed')
const { const {
websocket: { pubSub }, websocket: { pubSub },
} = ctx } = ctx
return pubSub.asyncIterator( return pubSub.asyncIterator(
'MESSAGE_SENT', `MESSAGE_SENT_${args.chatRoomId}`,
) as unknown as AsyncIterable<Message> ) as unknown as AsyncIterable<Message>
}, },
resolve: (payload: Message) => payload, resolve: (payload: Message) => payload,