Files
doneit-web/src/app/module/chat/data/repository/message-respository.service.ts
T
2024-07-22 13:28:52 +01:00

122 lines
3.7 KiB
TypeScript

import { Injectable } from '@angular/core';
import { MessageRemoteDataSourceService } from '../data-source/message/message-remote-data-source.service';
import { MessageLiveDataSourceService } from '../data-source/message/message-live-data-source.service';
import { MessageInputDTO } from '../dto/message/messageInputDtO';
import { MessageLocalDataSourceService, TableMessage } from '../data-source/message/message-local-data-source.service';
import { SessionStore } from 'src/app/store/session.service';
import { SignalRService } from '../../infra/socket/signal-r.service';
import { v4 as uuidv4 } from 'uuid'
import { filter } from 'rxjs/operators';
import { err, ok } from 'neverthrow';
export const InstanceId = uuidv4();
@Injectable({
providedIn: 'root'
})
export class MessageRepositoryService {
constructor(
private messageRemoteDataSourceService: MessageRemoteDataSourceService,
private messageLiveDataSourceService: MessageLiveDataSourceService,
private messageLiveSignalRDataSourceService: SignalRService,
private messageLocalDataSourceService: MessageLocalDataSourceService
) {
// this.messageLiveDataSourceService.socket.messages$.subscribe(({payload, requestId, type}) => {
// })
}
async sendMessage(data: MessageInputDTO) {
(data as TableMessage).sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
data['requestId'] = InstanceId +'@'+ uuidv4();
const localActionResult = await this.messageLocalDataSourceService.sendMessage(data)
// this.messageLiveDataSourceService.sendMessage({
// type: 'sendMessage',
// payload: data
// })
if(localActionResult.isOk()) {
const sendMessageResult = await this.messageLiveSignalRDataSourceService.sendMessage(data)
//const sendMessageResult = await this.messageRemoteDataSourceService.sendMessage(data)
if(sendMessageResult.isOk()) {
if(sendMessageResult.value.sender == undefined || sendMessageResult.value.sender == null) {
console.log({sendMessageResult})
delete sendMessageResult.value.sender
}
let clone: TableMessage = {
...sendMessageResult.value,
messageId: sendMessageResult.value.id,
id : localActionResult.value
}
return this.messageLocalDataSourceService.update({...clone, sending: false})
}
} else {
return this.messageLocalDataSourceService.update({sending: false})
}
}
async sendReadAt({roomId}) {
const result = await this.messageLocalDataSourceService.getLastMessageByRoomId(roomId)
if(result.isOk()) {
if(result.value) {
return await this.messageLiveSignalRDataSourceService.sendReadAt({roomId, memberId: SessionStore.user.UserId, chatMessageId: result.value.messageId})
}
return ok(true)
}
return err(false)
}
async listAllMessagesByRoomId(id: string) {
const result = await this.messageRemoteDataSourceService.getMessagesFromRoom(id)
if(result.isOk()) {
for(const message of result.value.data) {
let clone: TableMessage = message
clone.messageId = message.id
clone.roomId = id
delete clone.id
await this.messageLocalDataSourceService.findOrUpdate(clone)
}
}
return result
}
getItemsLive (roomId: string) {
return this.messageLocalDataSourceService.getItemsLive(roomId)
}
subscribeToNewMessages(roomId: any) {
return this.messageLocalDataSourceService.subscribeToNewMessage(roomId)
}
sendTyping(roomId) {
return this.messageLiveSignalRDataSourceService.sendTyping({roomId, UserName:SessionStore.user.FullName})
}
}