2024-08-02 16:20:26 +01:00
|
|
|
import { Injectable, Input } from '@angular/core';
|
2024-08-19 16:01:58 +01:00
|
|
|
import { MessageLocalDataSourceService } from '../../../data/repository/message/message-local-data-source.service';
|
2024-08-08 15:53:00 +01:00
|
|
|
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
|
|
|
|
|
import { ParamsValidation } from 'src/app/services/decorators/validate-schema.decorator';
|
2024-08-27 20:29:57 +01:00
|
|
|
import { MessageEntitySchema } from 'src/app/core/chat/entity/message';
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
|
|
|
|
const SocketMessageCreateOutputSchema = MessageEntitySchema.pick({
|
|
|
|
|
id: true,
|
|
|
|
|
attachments: true,
|
|
|
|
|
canEdit: true,
|
|
|
|
|
editedAt: true,
|
|
|
|
|
info: true,
|
|
|
|
|
isDeleted: true,
|
|
|
|
|
message: true,
|
|
|
|
|
messageType: true,
|
|
|
|
|
oneShot: true,
|
|
|
|
|
reactions: true,
|
|
|
|
|
receiverId: true,
|
|
|
|
|
requireUnlock: true,
|
|
|
|
|
roomId: true,
|
|
|
|
|
sender: true,
|
|
|
|
|
sending: true,
|
|
|
|
|
sentAt: true,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export type ISocketMessageCreateOutput = z.infer<typeof SocketMessageCreateOutputSchema>
|
2024-08-02 16:20:26 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class SocketMessageCreateUseCaseService {
|
|
|
|
|
|
2024-09-04 10:58:26 +01:00
|
|
|
private broadcastChannel: BroadcastChannel;
|
|
|
|
|
private processedMessages = new Set<string>();
|
|
|
|
|
|
2024-08-02 16:20:26 +01:00
|
|
|
constructor(
|
|
|
|
|
private messageLocalDataSourceService: MessageLocalDataSourceService,
|
2024-09-04 10:58:26 +01:00
|
|
|
) {
|
|
|
|
|
this.broadcastChannel = new BroadcastChannel('socket-message');
|
|
|
|
|
this.broadcastChannel.onmessage = (event) => {
|
|
|
|
|
console.log('hello', event.data)
|
|
|
|
|
const messageId = event.data;
|
|
|
|
|
this.processedMessages.add(messageId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// this.broadcastChannel.postMessage('incomingMessage.id');
|
|
|
|
|
}
|
2024-08-02 16:20:26 +01:00
|
|
|
|
2024-08-20 16:34:47 +01:00
|
|
|
@XTracerAsync({name:'Socket-Message-Create-UseCase', module:'chat', bugPrint: true})
|
2024-08-27 20:29:57 +01:00
|
|
|
async execute(input: ISocketMessageCreateOutput, tracing?: TracingType) {
|
2024-08-08 15:53:00 +01:00
|
|
|
|
2024-09-04 10:58:26 +01:00
|
|
|
this.broadcastChannel.postMessage(input.id);
|
2024-08-27 20:29:57 +01:00
|
|
|
ParamsValidation(SocketMessageCreateOutputSchema, input, tracing)
|
2024-08-02 16:20:26 +01:00
|
|
|
|
|
|
|
|
const incomingMessage = {
|
|
|
|
|
...input,
|
2024-08-08 15:53:00 +01:00
|
|
|
sending: false
|
2024-08-02 16:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 10:58:26 +01:00
|
|
|
// Check if the message ID already exists in the processedMessages set
|
|
|
|
|
if (this.processedMessages.has(incomingMessage.id)) {
|
|
|
|
|
console.warn(`Duplicate message detected: ${incomingMessage.id}`);
|
|
|
|
|
return; // Exit early to prevent duplicate handling
|
|
|
|
|
} else {
|
|
|
|
|
console.log('no duplicate')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the message ID to the processedMessages set and broadcast it
|
|
|
|
|
this.processedMessages.add(incomingMessage.id);
|
|
|
|
|
|
2024-08-06 11:24:00 +01:00
|
|
|
|
2024-09-04 10:58:26 +01:00
|
|
|
console.log('create message', { incomingMessage });
|
2024-08-02 16:20:26 +01:00
|
|
|
|
2024-09-04 10:58:26 +01:00
|
|
|
tracing?.addEvent("Message Create start");
|
|
|
|
|
const result = await this.messageLocalDataSourceService.insert(incomingMessage);
|
|
|
|
|
tracing?.addEvent("Message Create end");
|
2024-08-02 16:20:26 +01:00
|
|
|
|
2024-09-04 10:58:26 +01:00
|
|
|
if (result.isOk()) {
|
|
|
|
|
// Optionally, you can handle post-insertion logic here
|
2024-08-02 16:20:26 +01:00
|
|
|
} else {
|
2024-09-04 10:58:26 +01:00
|
|
|
tracing?.addEvent("error while creating message");
|
2024-08-08 15:53:00 +01:00
|
|
|
tracing.log("error while creating message", {
|
|
|
|
|
error: result.error
|
2024-09-04 10:58:26 +01:00
|
|
|
});
|
2024-08-02 16:20:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|