mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 21:06:06 +00:00
merge chat
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { filter, map } from 'rxjs/operators';
|
||||
import { IMessageSocketRepository } from 'src/app/core/chat/repository/message/message-socket-repository';
|
||||
import { MessageEntity } from 'src/app/core/chat/entity/message';
|
||||
import { IBoldLocalRepository } from 'src/app/core/chat/repository/bold/bold-local-repository';
|
||||
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
|
||||
import { InstanceId } from '../../chat-service.service';
|
||||
import { HttpAdapter } from 'src/app/infra/http/adapter';
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
|
||||
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomBoldSyncUseCaseService {
|
||||
|
||||
constructor(
|
||||
private MessageSocketRepositoryService: IMessageSocketRepository,
|
||||
private boldLocalRepository: IBoldLocalRepository,
|
||||
private http: HttpAdapter,
|
||||
private messageLocalRepository: IMessageLocalRepository,
|
||||
private roomLocalDataSourceService: IRoomLocalRepository,
|
||||
) {
|
||||
this.listenToIncomingMessage();
|
||||
// this.loadHistory()
|
||||
this.onInsertToDB()
|
||||
this.listenToUpdateMessages();
|
||||
}
|
||||
|
||||
private listenToIncomingMessage() {
|
||||
return this.MessageSocketRepositoryService.listenToMessages().pipe(
|
||||
filter((message) => !message?.requestId?.startsWith(InstanceId)),
|
||||
map(message => Object.assign(new MessageEntity(), message)),
|
||||
filter((message) => !message.meSender())
|
||||
).subscribe(async (message) => {
|
||||
|
||||
const result = await this.boldLocalRepository.findOne({roomId: message.roomId})
|
||||
|
||||
if(result.isOk() && !result.value) {
|
||||
const result = await this.boldLocalRepository.insert({roomId: message.roomId, bold: 1})
|
||||
} else if(result.isOk() && result.value.bold == 0) {
|
||||
const result = await this.boldLocalRepository.update(message.roomId, {bold: 1})
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private listenToUpdateMessages() {
|
||||
return this.MessageSocketRepositoryService.listenToUpdateMessages().pipe(
|
||||
filter((message) => !message?.requestId?.startsWith(InstanceId)),
|
||||
map(message => Object.assign(new MessageEntity(), message))
|
||||
).subscribe(async (message) => {
|
||||
|
||||
const haveSeen = MessageEntity.haveSeen(message.info)
|
||||
|
||||
if(haveSeen) {
|
||||
|
||||
const result = await this.boldLocalRepository.findOne({roomId: message.roomId})
|
||||
|
||||
if(result.isOk() && result.value?.bold == 1) {
|
||||
const lastMessage = await this.roomLocalDataSourceService.findOne({id: message.roomId})
|
||||
if(lastMessage.isOk()) {
|
||||
if(lastMessage.value.messages[0].id == message.id) {
|
||||
const result = await this.boldLocalRepository.update(message.roomId, {bold: 0})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@XTracerAsync({name:'RoomBoldSyncUseCaseService/onInsertToDB', module:'chat', bugPrint: true})
|
||||
private onInsertToDB(tracing?: TracingType) {
|
||||
|
||||
let delay = 2000
|
||||
this.messageLocalRepository.onCreateObservable().pipe(
|
||||
filter(e => e?.origin == 'history'),
|
||||
filter(e => e.sender.wxUserId != SessionStore.user.UserId),
|
||||
).subscribe(async (newMessage)=> {
|
||||
|
||||
setTimeout(async ()=> {
|
||||
const haveSeen = MessageEntity.haveSeen(newMessage.info)
|
||||
if(!haveSeen) {
|
||||
await this.boldLocalRepository.open()
|
||||
const result = await this.boldLocalRepository.findOne({roomId: newMessage.roomId})
|
||||
|
||||
if(result.isOk() && !result.value?.bold) {
|
||||
const result = await this.boldLocalRepository.insert({roomId: newMessage.roomId, bold: 1})
|
||||
} else if(result.isOk() && result.value.bold == 0) {
|
||||
const result = await this.boldLocalRepository.update(newMessage.roomId, {bold: 1})
|
||||
} else {
|
||||
// tracing.hasError("failed to set bold",{})
|
||||
}
|
||||
}
|
||||
|
||||
}, delay);
|
||||
delay = 0
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { SessionStore } from 'src/app/store/session.service';
|
||||
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
|
||||
import { z } from "zod";
|
||||
import { IRoomRemoteRepository } from 'src/app/core/chat/repository/room/room-remote-repository';
|
||||
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
||||
import { IRoomSocketRepository } from 'src/app/core/chat/repository/room/room-socket-repository';
|
||||
|
||||
export const CreateRoomInputDTOSchema = z.object({
|
||||
roomName: z.string(),
|
||||
createdBy: z.number(),
|
||||
roomType: z.number(),
|
||||
expirationDate: z.string().nullable().optional(),
|
||||
members: z.array(z.number())
|
||||
});
|
||||
export type CreateRoomInputDTO = z.infer<typeof CreateRoomInputDTOSchema>
|
||||
|
||||
|
||||
export const RoomOutPutDTOSchema = z.object({
|
||||
success: z.boolean(),
|
||||
message: z.string(),
|
||||
data: z.object({
|
||||
id: z.string(),
|
||||
roomName: z.string(),
|
||||
createdBy: z.any().nullable(),
|
||||
createdAt: z.string(),
|
||||
expirationDate: z.string().nullable(),
|
||||
roomType: z.any()
|
||||
})
|
||||
});
|
||||
export type RoomOutPutDTO = z.infer<typeof RoomOutPutDTOSchema>
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class CreateRoomUseCaseService {
|
||||
|
||||
constructor(
|
||||
private roomRemoteDataSourceService: IRoomRemoteRepository,
|
||||
private roomLocalDataSourceService: IRoomLocalRepository,
|
||||
private RoomSocketRepositoryService: IRoomSocketRepository
|
||||
) { }
|
||||
|
||||
@XTracerAsync({name:'room-create-use-case.service', module:'chat', bugPrint: true, waitNThrow: 5000})
|
||||
async execute(data: CreateRoomInputDTO, tracing?: TracingType) {
|
||||
|
||||
const result = await this.RoomSocketRepositoryService.CreateGroup(data)
|
||||
|
||||
console.log('history', result)
|
||||
// const result = await this.roomRemoteDataSourceService.createRoom(data)
|
||||
|
||||
if(result.isOk()) {
|
||||
|
||||
return result
|
||||
} else {
|
||||
tracing.hasError("socket close");
|
||||
console.log(result.error)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { captureAndReraiseAsync } from 'src/app/services/decorators/captureAndReraiseAsync';
|
||||
import { z } from "zod";
|
||||
import { IRoomRemoteRepository } from 'src/app/core/chat/repository/room/room-remote-repository';
|
||||
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
||||
import { isHttpResponse } from 'src/app/infra/http/http.service';
|
||||
|
||||
export const DeleteRoomByIdInputDTOSchema = z.string()
|
||||
export type DeleteRoomByIdInputDTO = z.infer<typeof DeleteRoomByIdInputDTOSchema>
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DeleteRoomUseCaseService {
|
||||
|
||||
constructor(
|
||||
private roomRemoteDataSourceService: IRoomRemoteRepository,
|
||||
// private roomMemoryDataSourceService: Store<RoomRemoteDataSourceState>,
|
||||
private roomLocalDataSourceService: IRoomLocalRepository,
|
||||
) { }
|
||||
|
||||
|
||||
@captureAndReraiseAsync('RoomRepositoryService/deleteRoomById')
|
||||
async execute(id: DeleteRoomByIdInputDTO) {
|
||||
const result = await this.roomRemoteDataSourceService.deleteRoom(id)
|
||||
|
||||
if(result.isOk()) {
|
||||
|
||||
const result = await this.roomLocalDataSourceService.delete(id)
|
||||
// this.messageLiveDataSourceService.sendMessage({
|
||||
// type: 'createRoom',
|
||||
// payload: {a: '5'}
|
||||
// })
|
||||
|
||||
return result
|
||||
} else if (isHttpResponse(result.error)) {
|
||||
if(result.error.status == 404) {
|
||||
await this.roomLocalDataSourceService.delete(id)
|
||||
}
|
||||
// this.httpErrorHandle.httpStatusHandle(result.error)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { isHttpResponse } from 'src/app/infra/http/http.service';
|
||||
import { captureAndReraiseAsync } from 'src/app/services/decorators/captureAndReraiseAsync';
|
||||
import { z } from 'zod';
|
||||
import { IRoomRemoteRepository } from 'src/app/core/chat/repository/room/room-remote-repository';
|
||||
import { IMemberLocalRepository } from 'src/app/core/chat/repository/member/member-local-repository';
|
||||
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
||||
import { GetRoomByIdMapper } from 'src/app/core/chat/mapper/getRoomByIdMapper';
|
||||
import { RoomEntity, RoomType } from 'src/app/core/chat/entity/group';
|
||||
import { diff, addedDiff, deletedDiff, updatedDiff, detailedDiff } from 'deep-object-diff';
|
||||
import { zodSafeValidation } from 'src/app/utils/zodValidation';
|
||||
|
||||
const UserSchema = z.object({
|
||||
wxUserId: z.number(),
|
||||
wxFullName: z.string(),
|
||||
wxeMail: z.string(),
|
||||
userPhoto: z.string().nullable(),
|
||||
});
|
||||
|
||||
const MemberSchema = z.object({
|
||||
id: z.string(),
|
||||
user: UserSchema,
|
||||
joinAt: z.string(),
|
||||
isAdmin: z.boolean()
|
||||
});
|
||||
|
||||
export const RoomByIdOutputDTOSchema = z.object({
|
||||
success: z.boolean(),
|
||||
message: z.string(),
|
||||
data: z.object({
|
||||
id: z.string(),
|
||||
roomName: z.string(),
|
||||
createdBy: UserSchema,
|
||||
createdAt: z.string(),
|
||||
expirationDate: z.string().nullable(),
|
||||
roomType: z.number(),
|
||||
members: z.array(MemberSchema),
|
||||
}),
|
||||
})
|
||||
|
||||
export type RoomByIdMemberItemOutputDTO = z.infer<typeof MemberSchema>
|
||||
export type RoomByIdOutputDTO = z.infer<typeof RoomByIdOutputDTOSchema>
|
||||
|
||||
|
||||
|
||||
export const RoomByIdInputDTOSchema = z.string()
|
||||
export type RoomByIdInputDTO = z.infer<typeof RoomByIdInputDTOSchema>
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class GetRoomByIdUseCaseService {
|
||||
|
||||
constructor(
|
||||
private roomRemoteDataSourceService: IRoomRemoteRepository,
|
||||
private roomLocalDataSourceService: IRoomLocalRepository,
|
||||
private MemberListLocalRepository: IMemberLocalRepository
|
||||
) { }
|
||||
|
||||
@captureAndReraiseAsync('RoomRepositoryService/getRoomById')
|
||||
async execute(id: RoomByIdInputDTO) {
|
||||
const result = await this.roomRemoteDataSourceService.getRoom(id)
|
||||
|
||||
if(result.isOk()) {
|
||||
const validData = zodSafeValidation<RoomByIdOutputDTO>(RoomByIdOutputDTOSchema, result.value)
|
||||
|
||||
if(validData.isOk()) {
|
||||
const localListRoom = await this.roomLocalDataSourceService.findAll()
|
||||
if(localListRoom.isOk()) {
|
||||
|
||||
const getRoomById = await this.roomLocalDataSourceService.findOne({id:validData.value.data.id})
|
||||
if(getRoomById.isOk() && getRoomById.value) {
|
||||
const room = GetRoomByIdMapper.toDomain(validData.value)
|
||||
|
||||
const added: Partial<RoomEntity> = addedDiff(getRoomById.value, room);
|
||||
const deleted: Partial<RoomEntity> = deletedDiff(getRoomById.value, room);
|
||||
const updated: Partial<RoomEntity> = updatedDiff(getRoomById.value, room);
|
||||
|
||||
delete added.members
|
||||
if(room.roomType == RoomType.Direct) {
|
||||
delete updated.roomName
|
||||
}
|
||||
|
||||
if(Object.keys(added).length >= 1 || Object.keys(updated).length >= 1) {
|
||||
console.log('added', added);
|
||||
console.log('deleted', deleted);
|
||||
console.log('updated', updated);
|
||||
this.roomLocalDataSourceService.update(room.id, room)
|
||||
}
|
||||
|
||||
} else if (getRoomById.isOk() && !getRoomById.value) {
|
||||
const room = GetRoomByIdMapper.toDomain(validData.value)
|
||||
this.roomLocalDataSourceService.insert(room)
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
} else if (isHttpResponse(result.error) ) {
|
||||
if(result.error.status == 404) {
|
||||
await this.roomLocalDataSourceService.delete(id)
|
||||
}
|
||||
// this.httpErrorHandle.httpStatusHandle(result.error)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { GetRoomListUseCaseService } from './room-get-list-use-case.service'
|
||||
import { RoomSocketRepositoryService } from '../../../data/repository/room/room-socket-repository.service'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomGetListOnCreateUseCaseService {
|
||||
|
||||
constructor(
|
||||
private RoomSocketRepositoryService: RoomSocketRepositoryService,
|
||||
private getRoomListUseCaseService: GetRoomListUseCaseService
|
||||
) {
|
||||
this.init()
|
||||
}
|
||||
|
||||
|
||||
private init() {
|
||||
this.OnReceiveCreateRoom()
|
||||
this.OnDeleteCreateRoom()
|
||||
}
|
||||
|
||||
|
||||
private OnReceiveCreateRoom() {
|
||||
this.RoomSocketRepositoryService.listenToCreateRoom().subscribe((data)=> {
|
||||
console.log('OnReceiveCreateRoom', data)
|
||||
this.getRoomListUseCaseService.execute()
|
||||
})
|
||||
}
|
||||
|
||||
private OnDeleteCreateRoom() {
|
||||
this.RoomSocketRepositoryService.listenToDeleteRoom().subscribe((data)=> {
|
||||
console.log('OnDeleteCreateRoom7', data)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { roomListDetermineChanges } from '../../../data/async/list/rooms/roomListChangeDetector';
|
||||
import { captureAndReraiseAsync } from 'src/app/services/decorators/captureAndReraiseAsync';
|
||||
import { CronJobService } from 'src/app/utils/task-scheduler'
|
||||
import { z } from "zod";
|
||||
import { IRoomRemoteRepository } from 'src/app/core/chat/repository/room/room-remote-repository';
|
||||
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
||||
import { MessageEntitySchema } from 'src/app/core/chat/entity/message';
|
||||
import { GetRoomListMapper } from 'src/app/core/chat/mapper/getRoomListMapper';
|
||||
|
||||
|
||||
const CreatedBySchema = z.object({
|
||||
wxUserId: z.number(),
|
||||
wxFullName: z.string(),
|
||||
wxeMail: z.string().email(),
|
||||
userPhoto: z.string().nullable()// api check
|
||||
});
|
||||
|
||||
const RoomListItemOutPutDTOSchema = z.object({
|
||||
chatRoom: z.object({
|
||||
id: z.string(),
|
||||
roomName: z.string(),
|
||||
createdBy: CreatedBySchema,
|
||||
createdAt: z.string(),
|
||||
expirationDate: z.string().nullable(), // api check
|
||||
roomType: z.number(),
|
||||
messages: MessageEntitySchema.array(),
|
||||
user1: CreatedBySchema.nullable(),
|
||||
user2: CreatedBySchema.nullable()
|
||||
}),
|
||||
joinAt: z.string()
|
||||
})
|
||||
|
||||
|
||||
// Define the schema for the entire response
|
||||
export const RoomListOutPutDTOSchema = z.object({
|
||||
success: z.boolean(),
|
||||
message: z.string(),
|
||||
data: z.array(RoomListItemOutPutDTOSchema),
|
||||
});
|
||||
|
||||
export type RoomListItemOutPutDTO = z.infer<typeof RoomListItemOutPutDTOSchema>
|
||||
|
||||
export type RoomListOutPutDTO = z.infer<typeof RoomListOutPutDTOSchema>
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class GetRoomListUseCaseService {
|
||||
|
||||
constructor(
|
||||
private roomRemoteDataSourceService: IRoomRemoteRepository,
|
||||
private roomLocalDataSourceService: IRoomLocalRepository,
|
||||
private CronJobService: CronJobService
|
||||
) { }
|
||||
|
||||
@captureAndReraiseAsync('RoomRepositoryService/list')
|
||||
async execute() {
|
||||
const result = await this.roomRemoteDataSourceService.getRoomList()
|
||||
|
||||
const localList = await this.roomLocalDataSourceService.findAll()
|
||||
|
||||
if(localList.isOk()) {
|
||||
if(result.isOk()) {
|
||||
|
||||
const { roomsToDelete, roomsToInsert, roomsToUpdate } = roomListDetermineChanges(result.value.data, localList.value)
|
||||
|
||||
if(roomsToInsert) {
|
||||
const roomsToInsertEntity = GetRoomListMapper.toDomain(roomsToInsert)
|
||||
for( const room of roomsToInsertEntity) {
|
||||
this.roomLocalDataSourceService.insert(room)
|
||||
if(room.expirationDate) {
|
||||
this.CronJobService.createCronJob('remove expired room', new Date(room.expirationDate), this.execute)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const roomsToUpdateEntity = GetRoomListMapper.toDomain(roomsToUpdate)
|
||||
this.roomLocalDataSourceService.updateMany(roomsToUpdateEntity)
|
||||
|
||||
for( const room of roomsToDelete) {
|
||||
this.roomLocalDataSourceService.delete(room.id)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { z } from "zod";
|
||||
import { IMemberRemoteRepository } from 'src/app/core/chat/repository/member/member-remote-repository';
|
||||
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
||||
import { isHttpResponse } from 'src/app/infra/http/http.service';
|
||||
|
||||
// Define the schema for the entire response
|
||||
export const UserRemoveListInputDTOSchema = z.object({
|
||||
id: z.string(),
|
||||
members: z.array(z.number())
|
||||
});
|
||||
|
||||
export type UserRemoveListInputDTO = z.infer<typeof UserRemoveListInputDTOSchema>
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomLeaveUseCase {
|
||||
|
||||
constructor(
|
||||
private memberRemoteDataSourceService: IMemberRemoteRepository,
|
||||
private roomLocalDataSourceService: IRoomLocalRepository,
|
||||
) { }
|
||||
|
||||
|
||||
|
||||
async execute(data: UserRemoveListInputDTO) {
|
||||
const result = await this.memberRemoteDataSourceService.removeMemberFromRoom(data)
|
||||
|
||||
if(result.isOk()) {
|
||||
this.roomLocalDataSourceService.delete(data.id)
|
||||
} else if (isHttpResponse(result.error)) {
|
||||
if(result.error.status == 404) {
|
||||
await this.roomLocalDataSourceService.delete(data.id)
|
||||
|
||||
}
|
||||
// this.httpErrorHandle.httpStatusHandle(result.error)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { IMessageSocketRepository } from 'src/app/core/chat/repository/message/message-socket-repository';
|
||||
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
||||
import { filter, map, tap } from 'rxjs/operators';
|
||||
import { MessageEntity } from 'src/app/core/chat/entity/message';
|
||||
import { HttpAdapter } from 'src/app/infra/http/adapter';
|
||||
import { IMessageGetAllByRoomIdOutPut } from 'src/app/core/chat/usecase/message/message-get-all-by-room-Id';
|
||||
import { RoomEntity } from 'src/app/core/chat/entity/group';
|
||||
import { RoomTable } from 'src/app/infra/database/dexie/instance/chat/schema/room';
|
||||
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RoomSetLastMessageService {
|
||||
|
||||
constructor(
|
||||
private roomLocalRepository: IRoomLocalRepository,
|
||||
private messageSocketRepository: IMessageSocketRepository,
|
||||
private messageLocalRepository: IMessageLocalRepository,
|
||||
private http: HttpAdapter,
|
||||
) {
|
||||
this.listenToIncomingMessage()
|
||||
this.listenToOnSendDataToSocket()
|
||||
this.loadHistory()
|
||||
this.listenToUpdateMessage()
|
||||
}
|
||||
|
||||
private listenToUpdateMessage() {
|
||||
let roomList: RoomTable[] = []
|
||||
this.roomLocalRepository.getItemsLive().pipe(
|
||||
tap((_roomList) => {
|
||||
|
||||
roomList = _roomList
|
||||
|
||||
})
|
||||
).subscribe();
|
||||
|
||||
this.messageSocketRepository.listenToUpdateMessages().subscribe(async(_message) => {
|
||||
const message = {..._message}
|
||||
|
||||
for(const room of roomList) {
|
||||
if(room.messages?.[0]?.id == message.id) {
|
||||
console.log('listenToUpdateMessage', message.roomId)
|
||||
const result = await this.roomLocalRepository.update(message.roomId, {
|
||||
messages: [message]
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
this.messageSocketRepository.listenToDeleteMessages().subscribe(async(_message) => {
|
||||
const message = {..._message}
|
||||
|
||||
for(const room of roomList) {
|
||||
if(room.messages?.[0]?.id == message.id) {
|
||||
console.log({...room.messages?.[0],isDeleted: true})
|
||||
const result = await this.roomLocalRepository.update(message.roomId, {
|
||||
messages: [{...room.messages?.[0],isDeleted: true}]
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private listenToIncomingMessage() {
|
||||
return this.messageSocketRepository.listenToMessages().pipe(
|
||||
map(message => Object.assign(new MessageEntity(), message))
|
||||
).subscribe(async (message) => {
|
||||
if(message?.roomId) {
|
||||
console.log('listenToIncomingMessage', message.roomId)
|
||||
const result = await this.roomLocalRepository.update(message.roomId, {
|
||||
messages: [message]
|
||||
})
|
||||
|
||||
if(result.isErr()) {
|
||||
console.log('failed to update last message')
|
||||
} else {
|
||||
console.log('set last message')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private listenToOnSendDataToSocket() {
|
||||
this.messageLocalRepository.onCreateObservable().subscribe(async (message) => {
|
||||
if(message?.roomId) {
|
||||
|
||||
setTimeout(async() => {
|
||||
if(message.origin != 'history') {
|
||||
const result = await this.roomLocalRepository.update(message.roomId, {
|
||||
messages: [message]
|
||||
})
|
||||
}
|
||||
}, 100)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
private loadHistory() {
|
||||
const regex = new RegExp("Room\\/([0-9a-fA-F]{8})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{12})\\/Messages");
|
||||
|
||||
return this.http.listen().pipe(
|
||||
filter((response: any)=> {
|
||||
return response?.isOk() && regex.test(response.value.url) && response.value.url.endsWith('/Messages')
|
||||
}),
|
||||
map((response: any) => response.value.data as IMessageGetAllByRoomIdOutPut)
|
||||
).subscribe(async (data)=> {
|
||||
const loadHistoryFirstMessage = data.data[0]
|
||||
if(loadHistoryFirstMessage) {
|
||||
|
||||
const roomId = loadHistoryFirstMessage.roomId
|
||||
|
||||
const room = await this.roomLocalRepository.findOne({id: roomId})
|
||||
|
||||
if(room.isOk()) {
|
||||
const roomEntity = new RoomEntity(room.value)
|
||||
if(!roomEntity.hasLastMessage()) {
|
||||
await this.roomLocalRepository.update(loadHistoryFirstMessage.roomId, {
|
||||
messages: [loadHistoryFirstMessage]
|
||||
})
|
||||
} else if (roomEntity.hasLastMessage()) {
|
||||
const localLastMessageDate = new Date(room.value.messages[0].sentAt).getTime()
|
||||
const loadHistoryLastMessageDate = new Date(loadHistoryFirstMessage.sentAt).getTime()
|
||||
|
||||
if(loadHistoryFirstMessage.id == room.value.messages?.[0]?.id) {
|
||||
// do nothing
|
||||
} else if(loadHistoryLastMessageDate>localLastMessageDate) {
|
||||
await this.roomLocalRepository.update(loadHistoryFirstMessage.roomId, {
|
||||
messages: [loadHistoryFirstMessage]
|
||||
})
|
||||
} else if(loadHistoryLastMessageDate == localLastMessageDate) {
|
||||
// do nothing
|
||||
} else if(room.value.messages[0].isDeleted != loadHistoryFirstMessage.isDeleted) {
|
||||
await this.roomLocalRepository.update(loadHistoryFirstMessage.roomId, {
|
||||
messages: [loadHistoryFirstMessage]
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { captureAndReraiseAsync } from 'src/app/services/decorators/captureAndReraiseAsync';
|
||||
import { z } from "zod";
|
||||
import { DataSourceReturn } from 'src/app/services/Repositorys/type';
|
||||
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
|
||||
import { IRoomRemoteRepository } from 'src/app/core/chat/repository/room/room-remote-repository';
|
||||
|
||||
export const RoomUpdateInputDTOSchema = z.object({
|
||||
roomName: z.string(),
|
||||
roomId: z.string(),
|
||||
roomType: z.number(),
|
||||
});
|
||||
export type RoomUpdateInputDTO = z.infer<typeof RoomUpdateInputDTOSchema>
|
||||
|
||||
|
||||
|
||||
const UserSchema = z.object({
|
||||
wxUserId: z.number(),
|
||||
wxFullName: z.string(),
|
||||
wxeMail: z.string(),
|
||||
userPhoto: z.string().nullable(),
|
||||
});
|
||||
const MemberSchema = z.object({
|
||||
id: z.string(),
|
||||
user: UserSchema,
|
||||
joinAt: z.string(),
|
||||
});
|
||||
|
||||
export const RoomUpdateOutputDTOSchema = z.object({
|
||||
success: z.boolean(),
|
||||
message: z.string(),
|
||||
data: z.object({
|
||||
id: z.string(),
|
||||
roomName: z.string(),
|
||||
createdBy: UserSchema,
|
||||
createdAt: z.string(),
|
||||
expirationDate: z.string().nullable(),
|
||||
roomType: z.number(),
|
||||
members: z.array(MemberSchema),
|
||||
}),
|
||||
});
|
||||
export type RoomUpdateOutputDTO = z.infer<typeof RoomUpdateOutputDTOSchema>
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UpdateRoomByIdUseCaseService {
|
||||
|
||||
constructor(
|
||||
private roomRemoteDataSourceService: IRoomRemoteRepository,
|
||||
private roomLocalDataSourceService: IRoomLocalRepository,
|
||||
) { }
|
||||
|
||||
|
||||
@captureAndReraiseAsync('RoomRepositoryService/updateRoomBy')
|
||||
async execute(data: RoomUpdateInputDTO): Promise<DataSourceReturn<RoomUpdateOutputDTO>> {
|
||||
|
||||
const result = await this.roomRemoteDataSourceService.updateRoom(data)
|
||||
|
||||
if(result.isOk()) {
|
||||
const localList = await this.roomLocalDataSourceService.findAll()
|
||||
// const { roomsToDelete, roomsToInsert, roomsToUpdate } = roomListDetermineChanges([result.value.data], localList)
|
||||
|
||||
// for( const roomData of roomsToUpdate) {
|
||||
// if(!roomData.chatRoom.createdBy?.wxUserId) {
|
||||
// delete roomData.chatRoom.createdBy;
|
||||
// }
|
||||
|
||||
// this.roomLocalDataSourceService.updateRoom(roomData.chatRoom)
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user