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

134 lines
3.9 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-18 16:19:30 +01:00
import { err, ok } from 'neverthrow';
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-07-22 13:28:52 +01:00
// this.messageLiveDataSourceService.socket.messages$.subscribe(({payload, requestId, type}) => {
2024-06-14 09:30:14 +01:00
2024-06-13 12:11:17 +01:00
2024-07-22 13:28:52 +01:00
// })
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-07-25 08:51:04 +01:00
const localActionResult = await this.messageLocalDataSourceService.sendMessage({...data})
console.log('create message', data)
2024-06-13 12:25:06 +01:00
2024-07-22 13:28:52 +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
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-07-25 08:51:04 +01:00
// let clone: TableMessage = {
// ...sendMessageResult.value,
// messageId: sendMessageResult.value.id,
// id : localActionResult.value
// }
2024-06-05 14:31:26 +01:00
2024-07-25 08:51:04 +01:00
// console.log({clone})
console.log('update message')
//return this.messageLocalDataSourceService.update({...clone, sending: false})
return ok(true)
2024-06-05 14:31:26 +01:00
}
2024-06-11 08:37:52 +01:00
} else {
2024-07-25 08:51:04 +01:00
// return this.messageLocalDataSourceService.update({sending: false})
2024-06-05 10:28:38 +01:00
}
2024-06-04 16:21:11 +01:00
}
2024-07-18 16:19:30 +01:00
async sendReadAt({roomId}) {
const result = await this.messageLocalDataSourceService.getLastMessageByRoomId(roomId)
if(result.isOk()) {
if(result.value) {
2024-07-25 08:51:04 +01:00
// return await this.messageLiveSignalRDataSourceService.sendReadAt({roomId, memberId: SessionStore.user.UserId, chatMessageId: result.value.messageId})
2024-07-18 16:19:30 +01:00
}
return ok(true)
}
return err(false)
}
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
2024-07-18 16:19:30 +01:00
sendTyping(roomId) {
2024-07-25 08:51:04 +01:00
return this.messageLiveSignalRDataSourceService.sendTyping({
roomId,
UserName:SessionStore.user.FullName,
userId: SessionStore.user.UserId
})
}
getMemberByLive({roomId, userId}) {
2024-07-17 16:39:18 +01:00
}
2024-06-03 13:34:40 +01:00
}