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

149 lines
4.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 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-18 16:19:30 +01:00
import { err, ok } from 'neverthrow';
2024-07-31 17:23:44 +01:00
import { MessageDeleteInputDTO } from '../../domain/use-case/message-delete-live-use-case.service';
2024-08-02 12:40:34 +01:00
import { MessageUpdateInput } from '../../domain/use-case/message-update-use-case.service';
2024-08-06 16:53:13 +01:00
import { messageListDetermineChanges } from '../async/list/rooms/messageListChangedetector';
import { MessageEntity } from '../../domain/entity/message';
import { InstanceId } from '../../domain/chat-service.service';
import { MessageMapper } from '../../domain/mapper/messageMapper';
2024-08-07 15:36:15 +01:00
import { MessageOutPutDataDTO } from '../dto/message/messageOutputDTO';
2024-07-11 10:28:21 +01:00
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-08-02 16:20:26 +01:00
) {}
2024-06-04 16:21:11 +01:00
2024-08-07 15:36:15 +01:00
async createMessageLocally(entity: MessageEntity) {
const requestId = InstanceId +'@'+ uuidv4();
const roomId = entity.roomId
return await this.messageLocalDataSourceService.sendMessage(entity)
}
async sendMessage(entity: MessageEntity) {
2024-06-05 10:28:38 +01:00
const requestId = InstanceId +'@'+ uuidv4();
const roomId = entity.roomId
2024-06-10 16:34:43 +01:00
entity.sending = true
const localActionResult = await this.messageLocalDataSourceService.sendMessage(entity)
2024-06-05 10:28:38 +01:00
if(localActionResult.isOk()) {
const DTO = MessageMapper.fromDomain(entity, requestId)
2024-08-07 15:36:15 +01:00
const sendMessageResult = await this.messageLiveSignalRDataSourceService.sendMessage<MessageOutPutDataDTO>(DTO)
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) {
2024-07-11 10:28:21 +01:00
delete sendMessageResult.value.sender
2024-06-10 16:34:43 +01:00
}
2024-08-01 16:38:08 +01:00
let clone: TableMessage = {
...sendMessageResult.value,
2024-08-06 11:24:00 +01:00
id: sendMessageResult.value.id,
$id : localActionResult.value
2024-08-01 16:38:08 +01:00
}
2024-06-05 14:31:26 +01:00
return this.messageLocalDataSourceService.update({...clone, sending: false, roomId: entity.roomId})
2024-08-01 16:38:08 +01:00
} else {
2024-08-07 15:36:15 +01:00
await this.messageLocalDataSourceService.update({sending: false, $id: localActionResult.value})
return err('no connection')
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-31 17:23:44 +01:00
sendMessageDelete(data: MessageDeleteInputDTO) {
2024-08-01 16:38:08 +01:00
2024-07-31 17:23:44 +01:00
data['requestId'] = InstanceId +'@'+ uuidv4();
return this.messageLiveSignalRDataSourceService.sendMessageDelete(data)
}
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-08-01 16:38:08 +01:00
2024-08-06 11:24:00 +01:00
return await this.messageLiveSignalRDataSourceService.sendReadAt({roomId, memberId: SessionStore.user.UserId, chatMessageId: result.value.id})
2024-07-18 16:19:30 +01:00
}
return ok(true)
}
return err(false)
}
2024-08-02 11:34:57 +01:00
reactToMessage(data) {
this.messageLiveSignalRDataSourceService.sendData({
method: 'ReactMessage',
data
})
}
2024-08-02 12:40:34 +01:00
updateMessage(input: MessageUpdateInput) {
this.messageLiveSignalRDataSourceService.sendData({
method: 'EditMessage',
data: input,
})
}
2024-06-05 15:35:38 +01:00
async listAllMessagesByRoomId(id: string) {
const result = await this.messageRemoteDataSourceService.getMessagesFromRoom(id)
2024-08-06 16:53:13 +01:00
const localResult = await this.messageLocalDataSourceService.getItems(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-08-06 16:53:13 +01:00
const { addedItems, changedItems } = messageListDetermineChanges(result.value.data, localResult)
for(const message of changedItems) {
2024-06-05 15:35:38 +01:00
let clone: TableMessage = message
clone.roomId = id
await this.messageLocalDataSourceService.findOrUpdate(clone)
}
2024-08-06 16:53:13 +01:00
for(const message of addedItems) {
let clone: TableMessage = message
clone.roomId = id
await this.messageLocalDataSourceService.createMessage(clone)
}
2024-06-05 15:35:38 +01:00
}
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({
2024-08-01 16:38:08 +01:00
roomId,
2024-07-25 08:51:04 +01:00
UserName:SessionStore.user.FullName,
userId: SessionStore.user.UserId
})
}
2024-06-03 13:34:40 +01:00
}