Files
doneit-web/src/app/module/chat/data/repository/message-respository.service.ts
T

112 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-06-03 13:34:40 +01:00
import { Injectable } from '@angular/core';
2024-06-04 16:21:11 +01:00
import { MessageRemoteDataSourceService } from '../data-source/message/message-remote-data-source.service';
import { MessageLiveDataSourceService } from '../data-source/message/message-live-data-source.service';
2024-06-05 10:28:38 +01:00
import { MessageInputDTO } from '../dto/message/messageInputDtO';
2024-06-05 14:31:26 +01:00
import { MessageLocalDataSourceService, TableMessage } from '../data-source/message/message-local-data-source.service';
2024-06-10 16:34:43 +01:00
import { SessionStore } from 'src/app/store/session.service';
2024-07-11 10:28:21 +01:00
import { SignalRService } from '../../infra/socket/signal-r.service';
import { v4 as uuidv4 } from 'uuid'
2024-07-16 14:15:58 +01:00
import { filter } from 'rxjs/operators';
2024-07-11 10:28:21 +01:00
export const InstanceId = uuidv4();
2024-06-03 13:34:40 +01:00
@Injectable({
providedIn: 'root'
})
export class MessageRepositoryService {
constructor(
2024-06-04 16:21:11 +01:00
private messageRemoteDataSourceService: MessageRemoteDataSourceService,
2024-06-05 10:28:38 +01:00
private messageLiveDataSourceService: MessageLiveDataSourceService,
2024-07-11 10:28:21 +01:00
private messageLiveSignalRDataSourceService: SignalRService,
2024-06-05 10:28:38 +01:00
private messageLocalDataSourceService: MessageLocalDataSourceService
2024-06-13 12:11:17 +01:00
) {
2024-06-14 09:30:14 +01:00
this.messageLiveDataSourceService.socket.messages$.subscribe(({payload, requestId, type}) => {
2024-06-13 12:11:17 +01:00
})
}
2024-06-04 16:21:11 +01:00
2024-06-05 10:28:38 +01:00
async sendMessage(data: MessageInputDTO) {
2024-06-10 16:34:43 +01:00
(data as TableMessage).sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
2024-07-11 10:28:21 +01:00
data['requestId'] = InstanceId +'@'+ uuidv4();
2024-06-11 08:37:52 +01:00
const localActionResult = await this.messageLocalDataSourceService.sendMessage(data)
2024-06-13 12:25:06 +01:00
2024-06-13 12:11:17 +01:00
this.messageLiveDataSourceService.sendMessage({
type: 'sendMessage',
payload: data
})
2024-06-05 10:28:38 +01:00
if(localActionResult.isOk()) {
2024-07-11 10:28:21 +01:00
2024-07-17 16:39:18 +01:00
(await this.sendTyping(data.roomId)).map((e) => {
console.log('map', e)
})
2024-07-11 10:28:21 +01:00
const sendMessageResult = await this.messageLiveSignalRDataSourceService.sendMessage(data)
//const sendMessageResult = await this.messageRemoteDataSourceService.sendMessage(data)
2024-06-05 14:31:26 +01:00
if(sendMessageResult.isOk()) {
2024-07-11 10:28:21 +01:00
if(sendMessageResult.value.sender == undefined || sendMessageResult.value.sender == null) {
console.log({sendMessageResult})
delete sendMessageResult.value.sender
2024-06-10 16:34:43 +01:00
}
2024-06-05 15:35:38 +01:00
let clone: TableMessage = {
2024-07-11 10:28:21 +01:00
...sendMessageResult.value,
messageId: sendMessageResult.value.id,
2024-06-05 15:35:38 +01:00
id : localActionResult.value
}
2024-06-05 14:31:26 +01:00
2024-06-11 08:37:52 +01:00
return this.messageLocalDataSourceService.update({...clone, sending: false})
2024-06-05 14:31:26 +01:00
}
2024-06-11 08:37:52 +01:00
} else {
return this.messageLocalDataSourceService.update({sending: false})
2024-06-05 10:28:38 +01:00
}
2024-06-04 16:21:11 +01:00
}
2024-06-05 15:35:38 +01:00
async listAllMessagesByRoomId(id: string) {
const result = await this.messageRemoteDataSourceService.getMessagesFromRoom(id)
2024-06-05 14:31:26 +01:00
2024-06-05 15:35:38 +01:00
if(result.isOk()) {
2024-06-04 16:21:11 +01:00
2024-06-05 15:35:38 +01:00
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)
}
}
2024-06-04 16:21:11 +01:00
return result
}
2024-06-05 14:31:26 +01:00
getItemsLive (roomId: string) {
return this.messageLocalDataSourceService.getItemsLive(roomId)
}
2024-06-13 12:11:17 +01:00
subscribeToNewMessages(roomId: any) {
2024-06-14 09:30:14 +01:00
return this.messageLocalDataSourceService.subscribeToNewMessage(roomId)
2024-06-13 12:11:17 +01:00
}
2024-07-17 16:39:18 +01:00
sendTyping(ChatRoomId) {
return this.messageLiveSignalRDataSourceService.sendTyping({ChatRoomId, UserName:SessionStore.user.FullName})
}
2024-06-03 13:34:40 +01:00
}