2024-12-30 08:26:40 +08:00
|
|
|
import { Injectable, OnModuleInit } from "@nestjs/common";
|
|
|
|
import { WebSocketType } from "../types";
|
|
|
|
import { BaseWebSocketServer } from "../base/base-websocket-server";
|
|
|
|
import EventBus, { CrudOperation } from "@server/utils/event-bus";
|
2024-12-30 09:22:38 +08:00
|
|
|
import { ObjectType, SocketMsgType, MessageDto, PostDto, PostType } from "@nicestack/common";
|
2024-12-30 08:26:40 +08:00
|
|
|
@Injectable()
|
|
|
|
export class RealtimeServer extends BaseWebSocketServer implements OnModuleInit {
|
|
|
|
onModuleInit() {
|
|
|
|
EventBus.on("dataChanged", ({ data, type, operation }) => {
|
|
|
|
if (type === ObjectType.MESSAGE && operation === CrudOperation.CREATED) {
|
|
|
|
const receiverIds = (data as Partial<MessageDto>).receivers.map(receiver => receiver.id)
|
|
|
|
this.sendToUsers(receiverIds, { type: SocketMsgType.NOTIFY, payload: { objectType: ObjectType.MESSAGE } })
|
|
|
|
}
|
2024-12-30 09:22:38 +08:00
|
|
|
|
2024-12-30 08:26:40 +08:00
|
|
|
if (type === ObjectType.POST) {
|
|
|
|
const post = data as Partial<PostDto>
|
|
|
|
if (post.type === PostType.TROUBLE_INSTRUCTION || post.type === PostType.TROUBLE_PROGRESS) {
|
|
|
|
this.sendToRoom(post.referenceId, { type: SocketMsgType.NOTIFY, payload: { objectType: ObjectType.POST } })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
public get serverType(): WebSocketType {
|
|
|
|
return WebSocketType.REALTIME;
|
|
|
|
}
|
|
|
|
}
|