diff --git a/src/app/services/Repositorys/chat/async/rooms/roomMembersChangeDetector.ts b/src/app/services/Repositorys/chat/async/rooms/roomMembersChangeDetector.ts new file mode 100644 index 000000000..455e8f58b --- /dev/null +++ b/src/app/services/Repositorys/chat/async/rooms/roomMembersChangeDetector.ts @@ -0,0 +1,30 @@ +import { TableMemberList } from "../../data-source/room/rooom-local-data-source.service"; +import { RoomByIdMemberItemOutputDTO } from "../../dto/room/roomByIdOutputDTO"; + +export function roomMemberListDetermineChanges(serverRooms: RoomByIdMemberItemOutputDTO[], localRooms: TableMemberList[], roomId: string) { + + const PServerRooms: (RoomByIdMemberItemOutputDTO & {$roomIdUserId: string}[]) = serverRooms.map( e=> { + + return { + ...e, + $roomIdUserId: roomId + e.user.wxUserId + } + }) + + const serverRoomMap = new Map(PServerRooms.map(room => [room.$roomIdUserId, room])); + const localRoomMap = new Map(localRooms.map(room => [room.$roomIdUserId, room])); + + const membersToInsert = serverRooms.filter(room => !localRoomMap.has(room.id)); + const membersToUpdate = serverRooms.filter(room => { + const localRoom = localRoomMap.get(room.id); + return localRoom && ( + room.user.wxUserId !== localRoom.user.wxUserId || + room.user.userPhoto !== localRoom.user.userPhoto || + room.joinAt !== localRoom.joinAt + ) + }); + + const membersToDelete = localRooms.filter(room => !serverRoomMap.has(room.id)); + + return { membersToInsert, membersToUpdate, membersToDelete }; +} diff --git a/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts b/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts index d33ac4cff..4d07dc695 100644 --- a/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts +++ b/src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service.ts @@ -3,8 +3,6 @@ import { RoomListItemOutPutDTO, RoomListOutPutDTO } from '../../dto/room/roomLis import { Dexie, EntityTable, liveQuery, Observable } from 'Dexie'; import { err, ok } from 'neverthrow'; import { z } from 'zod'; -import { UserList } from '../../../contacts/data-source/contacts-data-source.service'; - const tableSchema = z.object({ id: z.string(), @@ -105,6 +103,7 @@ export class RoomLocalDataSourceService { return await roomDataSource.room.toArray() } + getItemsLive(): Observable { return liveQuery(() => roomDataSource.room.toArray()) as any; } diff --git a/src/app/services/Repositorys/chat/dto/room/roomByIdOutputDTO.ts b/src/app/services/Repositorys/chat/dto/room/roomByIdOutputDTO.ts index 461ef931f..e3e9046d1 100644 --- a/src/app/services/Repositorys/chat/dto/room/roomByIdOutputDTO.ts +++ b/src/app/services/Repositorys/chat/dto/room/roomByIdOutputDTO.ts @@ -28,4 +28,5 @@ export const RoomByIdOutputDTOSchema = z.object({ }); +export type RoomByIdMemberItemOutputDTO = z.infer export type RoomByIdOutputDTO = z.infer diff --git a/src/app/services/Repositorys/chat/repository/message-respository.service.ts b/src/app/services/Repositorys/chat/repository/message-respository.service.ts index 9547538ec..ea8c46db6 100644 --- a/src/app/services/Repositorys/chat/repository/message-respository.service.ts +++ b/src/app/services/Repositorys/chat/repository/message-respository.service.ts @@ -1,7 +1,6 @@ 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 { MessageListInputDTO } from '../dto/message/messageListInputDTO'; 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'; diff --git a/src/app/services/Repositorys/chat/repository/room-repository.service.ts b/src/app/services/Repositorys/chat/repository/room-repository.service.ts index acd0da1fe..8ac7df9aa 100644 --- a/src/app/services/Repositorys/chat/repository/room-repository.service.ts +++ b/src/app/services/Repositorys/chat/repository/room-repository.service.ts @@ -8,6 +8,7 @@ import { RoomLocalDataSourceService } from '../data-source/room/rooom-local-data import { RoomByIdInputDTO } from '../dto/room/roomByIdInputDTO'; import { roomListDetermineChanges } from '../async/rooms/roomListChangeDetector'; import { UserRemoveListInputDTO } from '../dto/room/userRemoveListInputDTO'; +import { roomMemberListDetermineChanges } from '../async/rooms/roomMembersChangeDetector'; @Injectable({ providedIn: 'root' @@ -45,6 +46,10 @@ export class RoomRepositoryService { const result = await this.roomRemoteDataSourceService.getRoom(id) if(result.isOk()) { + const localList = await this.roomLocalDataSourceService.getRoomMemberById(id) + + const { membersToInsert, membersToUpdate, membersToDelete } = roomMemberListDetermineChanges(result.value.data.members, localList, id) + for (const user of result.value.data.members) { this.roomLocalDataSourceService.addMember({...user, roomId:id}) } diff --git a/src/app/shared/chat/messages/messages.page.ts b/src/app/shared/chat/messages/messages.page.ts index b2177fe1a..159fb353c 100644 --- a/src/app/shared/chat/messages/messages.page.ts +++ b/src/app/shared/chat/messages/messages.page.ts @@ -14,7 +14,6 @@ import { ViewDocumentPage } from 'src/app/modals/view-document/view-document.pag import { ThemeService } from 'src/app/services/theme.service'; import { ViewEventPage } from 'src/app/modals/view-event/view-event.page'; import { Storage } from '@ionic/storage'; -import { ChatSystemService } from 'src/app/services/chat/chat-system.service' import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service' import { MessageService } from 'src/app/services/chat/message.service'; import { CameraService } from 'src/app/services/camera.service'; @@ -35,11 +34,10 @@ import { ChatMessageDebuggingPage } from 'src/app/shared/popover/chat-message-de import { PermissionService } from 'src/app/services/permission.service'; import { FileValidatorService } from "src/app/services/file/file-validator.service" import { RoomRepositoryService } from 'src/app/services/Repositorys/chat/repository/room-repository.service'; -import { RoomListItemOutPutDTO, RoomListOutPutDTO } from 'src/app/services/Repositorys/chat/dto/room/roomListOutputDTO'; +import { RoomListItemOutPutDTO } from 'src/app/services/Repositorys/chat/dto/room/roomListOutputDTO'; import { MessageRepositoryService } from 'src/app/services/Repositorys/chat/repository/message-respository.service'; import { MessageInputDTO } from 'src/app/services/Repositorys/chat/dto/message/messageInputDtO'; import { TableMemberList } from 'src/app/services/Repositorys/chat/data-source/room/rooom-local-data-source.service'; -import { Observable } from 'rxjs'; import { TableMessage } from 'src/app/services/Repositorys/chat/data-source/message/message-local-data-source.service'; import { ChatPopoverPage } from '../../popover/chat-popover/chat-popover.page'; import { Observable as DexieObservable } from 'Dexie'; @@ -125,7 +123,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy public popoverController: PopoverController, private modalController: ModalController, /* private navParams: NavParams, */ - private chatService: ChatService, private animationController: AnimationController, private toastService: ToastService, private timeService: TimeService, @@ -133,7 +130,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy private gestureController: GestureController, public ThemeService: ThemeService, private storage: Storage, - public ChatSystemService: ChatSystemService, public RochetChatConnectorService: RochetChatConnectorService, private CameraService: CameraService, private sanitiser: DomSanitizer, @@ -186,7 +182,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } ngOnInit() { - this.ChatSystemService.getAllRooms(); + // this.ChatSystemService.getAllRooms(); // this.chatService.refreshtoken(); this.scrollToBottom(); this.getChatMembers(); @@ -451,22 +447,22 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy const formData = new FormData(); formData.append("blobFile", blob); - this.ChatSystemService.getDmRoom(roomId).send({ - file: { - "type": "application/audio", - "msDuration": audioFile.value.msDuration, - "mimeType": audioFile.value.mimeType, - }, - attachments: [{ - "title": fileName, - "title_link_download": true, - "type": "audio" - }], - temporaryData: formData, - attachmentsModelData: { - fileBase64: encodedData, - } - }) + // this.ChatSystemService.getDmRoom(roomId).send({ + // file: { + // "type": "application/audio", + // "msDuration": audioFile.value.msDuration, + // "mimeType": audioFile.value.mimeType, + // }, + // attachments: [{ + // "title": fileName, + // "title_link_download": true, + // "type": "audio" + // }], + // temporaryData: formData, + // attachmentsModelData: { + // fileBase64: encodedData, + // } + // }) }); this.deleteRecording(); @@ -474,7 +470,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy deleteMessage(msgId: string, msg: MessageService) { - this.ChatSystemService.getDmRoom(this.roomId).sendDeleteRequest(msgId) + // this.ChatSystemService.getDmRoom(this.roomId).sendDeleteRequest(msgId) } @@ -537,7 +533,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy // this.showLoader = false; // }); - this.dmUsers = this.ChatSystemService.getDmRoom(this.roomId).membersExcludeMe + // this.dmUsers = this.ChatSystemService.getDmRoom(this.roomId).membersExcludeMe } async openMessagesOptions(ev: any) { @@ -632,7 +628,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy this.getRoomInfo(); this.closeAllDesktopComponents.emit(); this.showEmptyContainer.emit(); - this.ChatSystemService.hidingRoom(this.roomId).catch((error) => console.error(error)); + // this.ChatSystemService.hidingRoom(this.roomId).catch((error) => console.error(error)); } else if (res.data == 'delete') { this.closeAllDesktopComponents.emit(); @@ -713,23 +709,23 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy const formData = new FormData(); formData.append("blobFile", blob); - this.ChatSystemService.getDmRoom(roomId).send({ - file: { - "type": "application/img", - "guid": '', - }, - temporaryData: formData, - attachments: [{ - "title": file.path, - // "image_url": "", - //"image_url": 'data:image/jpeg;base64,' + file.base64String, - "text": "description", - "title_link_download": false, - }], - attachmentsModelData: { - fileBase64: base64, - } - }) + // this.ChatSystemService.getDmRoom(roomId).send({ + // file: { + // "type": "application/img", + // "guid": '', + // }, + // temporaryData: formData, + // attachments: [{ + // "title": file.path, + // // "image_url": "", + // //"image_url": 'data:image/jpeg;base64,' + file.base64String, + // "text": "description", + // "title_link_download": false, + // }], + // attachmentsModelData: { + // fileBase64: base64, + // } + // }) } @@ -755,21 +751,21 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy formData.append("blobFile", blob); - this.ChatSystemService.getDmRoom(roomId).send({ - file: { - "type": "application/img", - "guid": '' - }, - temporaryData: formData, - attachments: [{ - "title": "file.jpg", - "text": "description", - "title_link_download": false, - }], - attachmentsModelData: { - fileBase64: imageBase64, - } - }) + // this.ChatSystemService.getDmRoom(roomId).send({ + // file: { + // "type": "application/img", + // "guid": '' + // }, + // temporaryData: formData, + // attachments: [{ + // "title": "file.jpg", + // "text": "description", + // "title_link_download": false, + // }], + // attachmentsModelData: { + // fileBase64: imageBase64, + // } + // }) } @@ -799,24 +795,24 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy if (data.selected) { - this.ChatSystemService.getDmRoom(roomId).send({ - file: { - "name": res.data.selected.Assunto, - "type": "application/webtrix", - "ApplicationId": res.data.selected.ApplicationType, - "DocId": res.data.selected.Id, - "Assunto": res.data.selected.Assunto, - }, - temporaryData: res, - attachments: [{ - "title": res.data.selected.Assunto, - "description": res.data.selected.DocTypeDesc, - "title_link_download": true, - "type": "webtrix", - "text": res.data.selected.DocTypeDesc, - "thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png", - }], - }) + // this.ChatSystemService.getDmRoom(roomId).send({ + // file: { + // "name": res.data.selected.Assunto, + // "type": "application/webtrix", + // "ApplicationId": res.data.selected.ApplicationType, + // "DocId": res.data.selected.Id, + // "Assunto": res.data.selected.Assunto, + // }, + // temporaryData: res, + // attachments: [{ + // "title": res.data.selected.Assunto, + // "description": res.data.selected.DocTypeDesc, + // "title_link_download": true, + // "type": "webtrix", + // "text": res.data.selected.DocTypeDesc, + // "thumb_url": "https://static.ichimura.ed.jp/uploads/2017/10/pdf-icon.png", + // }], + // }) } @@ -859,22 +855,22 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy const formData = new FormData(); formData.append("blobFile", blob); - this.ChatSystemService.getDmRoom(roomId).send({ - file: { - "type": "application/img", - "guid": '' - }, - temporaryData: formData, - attachments: [{ - "title": file.path, - //"image_url": 'data:image/jpeg;base64,' + file.base64String, - "text": "description", - "title_link_download": false, - }], - attachmentsModelData: { - fileBase64: base64, - } - }) + // this.ChatSystemService.getDmRoom(roomId).send({ + // file: { + // "type": "application/img", + // "guid": '' + // }, + // temporaryData: formData, + // attachments: [{ + // "title": file.path, + // //"image_url": 'data:image/jpeg;base64,' + file.base64String, + // "text": "description", + // "title_link_download": false, + // }], + // attachmentsModelData: { + // fileBase64: base64, + // } + // }) } @@ -930,23 +926,23 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy - this.ChatSystemService.getDmRoom(roomId).send({ - file: { - "type": file.type, - "guid": '', - }, - attachments: [{ - "title": file.name, - "name": file.name, - //"image_url": res, - // "text": "description", - "title_link_download": false, - }], - temporaryData: formData, - attachmentsModelData: { - fileBase64: fileBase64, - } - }) + // this.ChatSystemService.getDmRoom(roomId).send({ + // file: { + // "type": file.type, + // "guid": '', + // }, + // attachments: [{ + // "title": file.name, + // "name": file.name, + // //"image_url": res, + // // "text": "description", + // "title_link_download": false, + // }], + // temporaryData: formData, + // attachmentsModelData: { + // fileBase64: fileBase64, + // } + // }) } else { this.toastService._badRequest("Ficheiro inválido") } @@ -1257,27 +1253,27 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy } async getRoomInfo() { - let room = await this.chatService.getRoomInfo(this.roomId).toPromise(); - this.room = room['room']; - if (this.room.name) { - try { - this.roomName = this.room.name.split('-').join(' '); - } catch (error) { - this.roomName = this.room.name; - } + // let room = await this.chatService.getRoomInfo(this.roomId).toPromise(); + // this.room = room['room']; + // if (this.room.name) { + // try { + // this.roomName = this.room.name.split('-').join(' '); + // } catch (error) { + // this.roomName = this.room.name; + // } - } + // } - if (SessionStore.user.ChatData.data.userId == this.room.u._id) { - this.isAdmin = true - } else { - this.isAdmin = false - } + // if (SessionStore.user.ChatData.data.userId == this.room.u._id) { + // this.isAdmin = true + // } else { + // this.isAdmin = false + // } - if (this.room.customFields.countDownDate) { - this.roomCountDownDate = this.room.customFields.countDownDate; - } + // if (this.room.customFields.countDownDate) { + // this.roomCountDownDate = this.room.customFields.countDownDate; + // } }