create offline direct message

This commit is contained in:
Peter Maquiran
2024-09-17 16:02:12 +01:00
parent 9be19bfc78
commit 1bc5707321
266 changed files with 860 additions and 2771 deletions
+35 -3
View File
@@ -2,6 +2,7 @@ import { SessionStore } from "src/app/store/session.service";
import { BaseEntity } from "src/app/utils/entity";
import { z } from "zod"
import { MessageEntitySchema } from "./message";
import { IDBoolean } from "src/app/infra/database/dexie/type";
export enum RoomType {
Group = 1,
@@ -23,7 +24,8 @@ const MemberSchema = z.object({
});
export const RoomEntitySchema = z.object({
id: z.string(),
$id: z.string(),
id: z.string().uuid().optional(),
roomName: z.string(),
createdBy: z.object({
wxUserId: z.number(),
@@ -32,16 +34,19 @@ export const RoomEntitySchema = z.object({
userPhoto: z.string().nullable().optional()// api check
}),
createdAt: z.any(),
expirationDate: z.any().nullable(),
expirationDate: z.any().nullable().optional(),
roomType: z.nativeEnum(RoomType),
members: z.array(MemberSchema).optional(),
messages: MessageEntitySchema.array().optional()
messages: MessageEntitySchema.array().optional(),
local: z.nativeEnum(IDBoolean).optional(),
receiverId: z.number().optional()
})
export type IRoom = z.infer<typeof RoomEntitySchema>
export class RoomEntity extends BaseEntity<RoomEntity>(RoomEntitySchema) implements IRoom{
$id: typeof RoomEntitySchema._input.$id
id: typeof RoomEntitySchema._input.id
roomName: typeof RoomEntitySchema._input.roomName
createdBy: typeof RoomEntitySchema._input.createdBy
@@ -50,13 +55,40 @@ export class RoomEntity extends BaseEntity<RoomEntity>(RoomEntitySchema) implem
roomType: typeof RoomEntitySchema._input.roomType
members: typeof RoomEntitySchema._input.members
messages: typeof RoomEntitySchema._input.messages
receiverId: typeof RoomEntitySchema._input.receiverId
constructor(data: IRoom) {
super();
Object.assign(this, data)
if(data.roomType == RoomType.Direct) {
this.setName()
}
if(!this.$id) {
this.setLocalId()
}
if(this.roomType == RoomType.Direct && !this.receiverId && this.members.length == 2) {
this.setReceiver()
}
}
setLocalId() {
const receiver = this.members?.find((e) => e.user.wxUserId != SessionStore.user.UserId)
if(receiver) {
this.$id =receiver.user.wxUserId.toString()
}
}
setReceiver() {
const receiver = this.members?.find((e) => e.user.wxUserId != SessionStore.user.UserId)
if(receiver) {
this.receiverId = receiver.user.wxUserId
}
}
setName() {
+1 -1
View File
@@ -35,7 +35,7 @@ export const MessageEntityAttachmentSchema = z.object({
export const MessageEntitySchema = z.object({
$id: z.any().optional(),
id: z.string().uuid().optional(),
roomId: z.string().uuid().optional(),
roomId: z.string().optional(),
receiverId: z.number().optional(),
message: z.string().nullable().optional(),
messageType: z.nativeEnum(IMessageType),
@@ -3,11 +3,17 @@ import { DexieRepository } from "src/app/infra/repository/dexie/dexie-repository
import { MessageEntity } from "../../entity/message";
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
import { Observable } from "rxjs";
import { Result } from "neverthrow";
export interface IDirectMessages {
receiverId: string,
roomId: string
}
export abstract class IMessageLocalRepository extends DexieRepository<MessageTable, MessageEntity> {
abstract setAllSenderToFalse(): void
abstract getItems(roomId: string): PromiseExtended<MessageEntity[]>
abstract getItemsLive(roomId: string): DexieObservable<MessageEntity[]>
abstract getOfflineMessages(): Promise<MessageEntity[]>
abstract onCreateObservable(): Observable<MessageTable>
abstract getDirectMessages(input: IDirectMessages): Promise<Result<MessageEntity[], any>>
}
@@ -6,7 +6,7 @@ export const MessageTableSchema = z.object({
$id: z.string().optional(),
$createAt: z.number().optional(),
id: z.string().uuid().optional(),
roomId: z.string().uuid().optional(),
roomId: z.string().optional(),
message: z.string().nullable().optional(),
requestId: z.string().nullable().optional(),
messageType: z.number(),
@@ -50,5 +50,5 @@ export const MessageTableSchema = z.object({
export type MessageTable = z.infer<typeof MessageTableSchema>
export type DexieMessageTable = EntityTable<MessageTable, '$id'>;
export const messageTableColumn = '$id, id, roomId, senderId, message, messageType, canEdit, oneShot, requireUnlock, sending'
export const messageTableColumn = '$id, id, roomId, senderId, receiverId, message, messageType, canEdit, oneShot, requireUnlock, sending'
@@ -1,11 +1,12 @@
import { z } from "zod";
import { EntityTable } from 'Dexie';
import { RoomType } from "src/app/core/chat/entity/group";
import { MessageEntity, MessageEntitySchema } from "src/app/core/chat/entity/message";
import { MessageTableSchema } from "./message";
import { IDBoolean } from "../../../type";
export const RoomTableSchema = z.object({
id: z.string().uuid(),
$id: z.string().optional(),
id: z.string().optional(),
roomName: z.string(),
createdBy: z.object({
wxUserId: z.number(),
@@ -14,12 +15,14 @@ export const RoomTableSchema = z.object({
userPhoto: z.string().nullable().optional()// api check
}),
createdAt: z.any(),
expirationDate: z.any().nullable(),
expirationDate: z.string().nullable().optional(),
roomType: z.nativeEnum(RoomType),
messages: MessageTableSchema.array().optional(),
bold: z.number().optional()
bold: z.number().optional(),
local: z.nativeEnum(IDBoolean).optional(),
receiverId: z.number().optional()
})
export type RoomTable = z.infer<typeof RoomTableSchema>
export type DexieRoomsTable = EntityTable<RoomTable, 'id'>;
export const RoomTableColumn = 'id, createdBy, roomName, roomType, expirationDate, lastMessage'
export type DexieRoomsTable = EntityTable<RoomTable, '$id'>;
export const RoomTableColumn = '$id, id, createdBy, receiverId, roomName, roomType, expirationDate, lastMessage'
+4
View File
@@ -0,0 +1,4 @@
export enum IDBoolean {
false = 0,
true = 1
}
@@ -3,20 +3,22 @@ import Dexie, { EntityTable, Table } from 'Dexie';
import { ZodError, ZodObject, ZodSchema } from 'zod';
import { Logger } from 'src/app/services/logger/main/service';
import { IDexieRepository, RepositoryResult } from '../adapter'
import { IDBError, IDexieError } from '../types';
import { IDBError, IDBErrorParams, IDexieError } from '../types';
// Define a type for the Result of repository operations
class DBError<T> extends Error implements IDBError<T> {
zodError?: ZodError<T>;
parameters: T;
error?: IDexieError;
error: IDexieError;
DBErrorName?: 'NotFoundError' | 'ConstraintError'
constructor(data: IDBError<T>) {
constructor(data: IDBErrorParams<T>) {
super(data.message);
this.zodError = data.zodError;
this.parameters = data.parameters;
this.error = data.error;
this.DBErrorName = data.error?.name as any;
Logger.error(data.message, {
zodError: this.zodError,
@@ -74,7 +76,7 @@ export class DexieRepository<T, R, I = EntityTable<any, any>> implements IDexieR
return err(new DBError({
message: `dexie.js failed to insert into ${this.table.name}, ${error.message}`,
parameters: document,
error: error
error: error,
}))
}
} else {
+8
View File
@@ -38,6 +38,14 @@ import { ZodError } from "zod"
// };
export type IDBError<T> = {
message: string,
zodError?: ZodError<T>,
parameters?: T,
error?: IDexieError,
DBErrorName?: 'NotFoundError' | 'ConstraintError'
}
export type IDBErrorParams<T> = {
message: string,
zodError?: ZodError<T>,
parameters?: T,
@@ -4,7 +4,7 @@ import { Platform } from '@ionic/angular';
import { SignalRConnection, SocketMessage } from './signalR';
import { Plugins } from '@capacitor/core';
import { switchMap } from 'rxjs/operators';
import { Result } from 'neverthrow';
import { err, Result } from 'neverthrow';
import { HubConnection } from '@microsoft/signalr';
import { ISignalRInput } from '../type';
@@ -81,8 +81,12 @@ export class SignalRService {
}
}
sendData<T>(input: ISignalRInput) {
return this.connection.sendData<T>(input)
async sendData<T>(input: ISignalRInput) {
try {
return await this.connection.sendData<T>(input)
} catch (e) {
return err('havent connected yet')
}
}
join() {
+1 -1
View File
@@ -145,7 +145,7 @@ export class SignalRConnection {
} else {
this.sendLaterSubject.next({method: 'SendMessage', args: input})
return reject(err(false))
return resolve(err(false))
}
})
@@ -26,7 +26,7 @@ export function messageListDetermineChanges(serverList: MessageTable[], localLis
item.$id = localItem.$id
}
return localItem && (item.editedAt !== localItem.editedAt || item.sentAt != item.sentAt || item.reactions.some((r, index) => {
return localItem && (item.editedAt !== localItem.editedAt || item.isDeleted !== localItem.isDeleted || item.sentAt != item.sentAt || item.reactions.some((r, index) => {
const localReaction = localItem.reactions[index];
return !localReaction || r.reactedAt !== localReaction.reactedAt;
}));
@@ -1,6 +1,7 @@
import { RoomType } from "src/app/core/chat/entity/group";
import { RoomTable } from "src/app/infra/database/dexie/instance/chat/schema/room";
import { RoomListItemOutPutDTO } from "src/app/module/chat/domain/use-case/room/room-get-list-use-case.service";
import { SessionStore } from "src/app/store/session.service";
export function roomListDetermineChanges(serverRooms: RoomListItemOutPutDTO[], localRooms: RoomTable[]) {
const serverRoomMap = new Map(serverRooms.map(room => [room.chatRoom.id, room]));
@@ -8,7 +9,7 @@ export function roomListDetermineChanges(serverRooms: RoomListItemOutPutDTO[], l
const roomsToInsert = serverRooms.filter(room => !localRoomMap.has(room.chatRoom.id));
const roomsToUpdate = serverRooms.filter(room => {
const localRoom = localRoomMap.get(room.chatRoom.id);
const localRoom = localRoomMap.get(room.chatRoom.id);
return localRoom && (
room.chatRoom.roomName !== localRoom.roomName && room.chatRoom.roomType == RoomType.Group ||
@@ -5,10 +5,11 @@ import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository
import { Observable as DexieObservable, PromiseExtended } from 'Dexie';
import { DexieMessageTable, MessageTable, MessageTableSchema } from 'src/app/infra/database/dexie/instance/chat/schema/message';
import { chatDatabase } from 'src/app/infra/database/dexie/service';
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
import { IDirectMessages, IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
import { BehaviorSubject, combineLatest, from, Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { v4 as uuidv4 } from 'uuid'
import { err, ok } from 'neverthrow';
@Injectable({
providedIn: 'root'
@@ -79,9 +80,6 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable,
return chatDatabase.message.where('roomId').equals(roomId).sortBy('sentAt') as any
}
getItemsLive(roomId: string): DexieObservable<MessageEntity[]> {
return liveQuery(() => chatDatabase.message.where('roomId').equals(roomId).sortBy('sentAt') as any)
}
async getOfflineMessages () {
try {
@@ -96,7 +94,6 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable,
}
getLastMessageForRooms(roomIds: string[]): Observable<any[]> {
const observables = roomIds.map(roomId =>
from (liveQuery(async() =>{
@@ -114,5 +111,20 @@ export class MessageLocalDataSourceService extends DexieRepository<MessageTable,
return combineLatest(observables); // Combine all observables into one array of results
}
async getDirectMessages(input: IDirectMessages) {
try {
const result = await chatDatabase.message
.where('receiverId')
.equals(input.receiverId)
.or('roomId')
.equals(input.roomId)
.toArray()
return ok(result as MessageEntity[])
} catch (e) {
return err(e)
}
}
}
@@ -1,19 +1,38 @@
import { Injectable } from '@angular/core';
import { liveQuery, Observable } from 'Dexie';
import { DexieRepository } from 'src/app/infra/repository/dexie/dexie-repository.service';
import { from } from 'rxjs';
import { BehaviorSubject, from } from 'rxjs';
import { RoomTable, RoomTableSchema } from 'src/app/infra/database/dexie/instance/chat/schema/room';
import { chatDatabase } from 'src/app/infra/database/dexie/service';
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
import { IDBoolean } from 'src/app/infra/database/dexie/type';
import { RoomType } from 'src/app/core/chat/entity/group';
@Injectable({
providedIn: 'root'
})
export class RoomLocalRepository extends DexieRepository<RoomTable, RoomTable> implements IRoomLocalRepository {
private listenCreateSyncSubject : BehaviorSubject<RoomTable> = new BehaviorSubject<RoomTable>(null);
constructor() {
super(chatDatabase.room, RoomTableSchema)
chatDatabase.room.hook('creating', (primaryKey, obj, transaction) => {
if(obj.id && !obj.$id && obj.roomType == RoomType.Group) {
obj.$id = obj.id
}
if(obj.id) {
obj.local = IDBoolean.false
} else {
obj.local = IDBoolean.true
}
});
chatDatabase.room.hook('updating', (modifications, primKey, oldValue, transaction) => {
// if((modifications as Partial<RoomTable>).messages?.[0].requestId == oldValue.messages?.[0].requestId) {
@@ -22,6 +41,12 @@ export class RoomLocalRepository extends DexieRepository<RoomTable, RoomTable> i
// (modifications as Partial<RoomTable>).messages[0].sentAt = oldValue.messages?.[0]?.sentAt
// }
if((modifications as Partial<RoomTable>).id || oldValue.id) {
(modifications as Partial<RoomTable>).local = IDBoolean.false
} else {
(modifications as Partial<RoomTable>).local = IDBoolean.true
}
return modifications
});
@@ -42,8 +42,13 @@ import { BoldRemoveByRoomIdInput, BoldRemoveByRoomIdService } from 'src/app/modu
import { MemberListHttpSyncUseCase } from 'src/app/module/chat/domain/use-case/member/member-list-http-sync-use-case.ts.service'
import { RoomBoldSyncUseCaseService } from 'src/app/module/chat/domain/use-case/room/room-bold-sync-use-case.service'
import { RoomSetLastMessageService } from 'src/app/module/chat/domain/use-case/room/room-set-last-message.service';
import { RoomCreateLocalDirectMessageInputDTOInputDTO, RoomCreateLocalDirectMessageService } from 'src/app/module/chat/domain/use-case/room/room-create-local-direct-message.service';
import { RoomGetListOnCreateUseCaseService } from 'src/app/module/chat/domain/use-case/room/room-get-list-on-create-use-case.service';
import { IRoomSetLocalToFalseByIdInput, RoomSetLocalToFalseByIdService } from 'src/app/module/chat/domain/use-case/room/room-set-local-to-false-by-id.service';
import { IUserPhotoGetByIdInput, UserPhotoGetByIdUseCase } from 'src/app/module/chat/domain/use-case/user-photo/user-photo-get-by-id-use-case.service'
import { IMessageLocalGetByIdServiceInput, MessageLocalGetByIdService } from 'src/app/module/chat/domain/use-case/message/message-local-get-by-id.service'
import { ContactListService } from './use-case/contact/contact-list.service';
import { IRoomGetLocalByIdServiceInput, RoomGetLocalByIdService } from './use-case/room/room-getlocal-by-id.service';
export const getInstanceId = (): string => {
@@ -103,7 +108,12 @@ export class ChatServiceService {
private RoomBoldSyncUseCaseService: RoomBoldSyncUseCaseService, // dont remove
private RoomSetLastMessageService: RoomSetLastMessageService, // dont remove
private UserPhotoGetByIdUseCase: UserPhotoGetByIdUseCase,
private RoomGetListOnCreateUseCaseService: RoomGetListOnCreateUseCaseService
private RoomGetListOnCreateUseCaseService: RoomGetListOnCreateUseCaseService,
private RoomCreateLocalDirectMessageService: RoomCreateLocalDirectMessageService,
private contactListService: ContactListService,
private messageLocalGetByIdService: MessageLocalGetByIdService,
private roomGetLocalByIdService: RoomGetLocalByIdService,
private roomSetLocalToFalseByIdService: RoomSetLocalToFalseByIdService
) {
this.MessageSocketRepositoryService.listenToDeleteMessages()
.pipe()
@@ -160,6 +170,17 @@ export class ChatServiceService {
await this.asyncAllRoomMessage();
}
messageLocalGetById(input: IMessageLocalGetByIdServiceInput) {
return this.messageLocalGetByIdService.execute(input)
}
roomGetLocalById(input: IRoomGetLocalByIdServiceInput) {
return this.roomGetLocalByIdService.execute(input)
}
roomSetLocalToFalseById(input: IRoomSetLocalToFalseByIdInput) {
return this.roomSetLocalToFalseByIdService.execute(input)
}
removeMemberToRoom(data: UserRemoveListInputDTO) {
return this.RemoveMemberUseCaseService.execute(data)
}
@@ -262,6 +283,15 @@ export class ChatServiceService {
return this.MessageMarkAsReadUseCaseService.execute(sendReadAt)
}
roomCreateLocalDirectMessage(input: RoomCreateLocalDirectMessageInputDTOInputDTO) {
return this.RoomCreateLocalDirectMessageService.execute(input)
}
getContactList() {
return this.contactListService.execute()
}
markAllMessagesAsRead(input: MessageMarkAllMessageAsReadByRoomIdInputSchema) {
return this.MessageMarkAllMessageAsReadByRoomIdService.execute(input)
}
@@ -0,0 +1,37 @@
import { Injectable } from '@angular/core';
import { RoomType } from 'src/app/core/chat/entity/group';
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
import { ContactRepositoryService } from 'src/app/services/Repositorys/contacts/repository/contacts-repository.service';
@Injectable({
providedIn: 'root'
})
export class ContactListService {
constructor(
private contactsRepositoryService: ContactRepositoryService,
private roomLocalRepository: IRoomLocalRepository
) {}
async execute() {
const [userContact, localDirectRooms] = await Promise.all([
this.contactsRepositoryService.getUsers(),
this.roomLocalRepository.find({roomType: RoomType.Direct})
])
if(userContact.isOk() && localDirectRooms.isOk()) {
const existNames = localDirectRooms.value.map(e => e.roomName);
return userContact.map((list) => {
return list.data.result.filter((e) => {
return !existNames.includes(e.wxFullName)
})
})
} else if (userContact.isErr()) {
return userContact
} else if (localDirectRooms.isErr()) {
return localDirectRooms
}
}
}
@@ -157,25 +157,9 @@ export class MessageCreateUseCaseService {
const DTO = MessageMapper.fromDomain(message, message.requestId)
sendMessageResult = await this.messageSocketRepositoryService.sendGroupMessage(DTO)
} else {
if(message.receiverId) {
const DTO = MessageMapper.fromDomain(message, message.requestId)
sendMessageResult = await this.messageSocketRepositoryService.sendDirectMessage(DTO)
} else {
const getRoomMembers = await this.MemberListLocalRepository.directMember({
roomId:message.roomId,
currentUserId: SessionStore.user.UserId
})
if(getRoomMembers.isOk()) {
message.receiverId = getRoomMembers.value.wxUserId
const DTO = MessageMapper.fromDomain(message, message.requestId)
sendMessageResult = await this.messageSocketRepositoryService.sendGroupMessage(DTO)
} else {
console.log('not found direct users', getRoomMembers.error)
}
}
const DTO = MessageMapper.fromDomain(message, message.requestId)
delete DTO.roomId
sendMessageResult = await this.messageSocketRepositoryService.sendDirectMessage(DTO)
}
// return this sendMessageResult
@@ -0,0 +1,36 @@
import { Injectable } from '@angular/core';
import { IMessageLocalRepository } from 'src/app/core/chat/repository/message/message-local-repository';
import { z } from 'zod';
const messageLocalGetByIdServiceInputSchema = z.object({
roomId: z.string().uuid().optional(),
receiverId: z.string().optional(),
})
export type IMessageLocalGetByIdServiceInput = z.infer<typeof messageLocalGetByIdServiceInputSchema>
@Injectable({
providedIn: 'root'
})
export class MessageLocalGetByIdService {
constructor(
private messageLocalRepository: IMessageLocalRepository
) { }
execute(input: IMessageLocalGetByIdServiceInput) {
if(input.roomId && input.receiverId) {
return this.messageLocalRepository.getDirectMessages({
roomId: input.roomId,
receiverId: input.receiverId
})
}
else if(input.roomId) {
return this.messageLocalRepository.find({roomId: input.roomId})
} else if (input.receiverId) {
return this.messageLocalRepository.find({receiverId: parseInt(input.receiverId)})
}
}
}
@@ -9,9 +9,9 @@ import { RoomLocalRepository } from '../../../data/repository/room/room-local-re
import { MemberListLocalRepository } from 'src/app/module/chat/data/repository/member/member-list-local-repository.service'
import { Result } from 'neverthrow';
import { RoomType } from 'src/app/core/chat/entity/group';
import { SessionStore } from 'src/app/store/session.service';
import { MessageTable } from 'src/app/infra/database/dexie/instance/chat/schema/message';
import { MessageOutPutDataDTO } from 'src/app/core/chat/repository/dto/messageOutputDTO';
import { IDBoolean } from 'src/app/infra/database/dexie/type';
@Injectable({
providedIn: 'root'
@@ -36,9 +36,12 @@ export class SendLocalMessagesUseCaseService {
if(allRooms.value.length == 0) {
} else {
//const removeLocalRooms = allRooms.value.filter((e)=> e.local == IDBoolean.false)
console.log({localM: messages});
for(const message of messages) {
const room = allRooms.value.find(e => e.id == message.roomId)
const room = allRooms.value.find(e => e.id == message.roomId || e.$id == message.roomId)
if(room) {
@@ -68,21 +71,16 @@ export class SendLocalMessagesUseCaseService {
sendMessageResult = await this.MessageSocketRepositoryService.sendGroupMessage(DTO)
} else {
if(message.receiverId) {
const DTO = MessageMapper.fromDomain(message, message.requestId)
sendMessageResult = await this.messageSocketRepositoryService.sendDirectMessage(DTO)
} else {
const getRoomMembers = await this.MemberListLocalRepository.directMember({
roomId:message.roomId,
currentUserId: SessionStore.user.UserId
const DTO = MessageMapper.fromDomain(message, message.requestId)
sendMessageResult = await this.messageSocketRepositoryService.sendDirectMessage(DTO)
if(sendMessageResult.isOk() && room.local == IDBoolean.true) {
this.roomLocalDataSourceService.update(room.$id, {
local: IDBoolean.false,
id: sendMessageResult.value.roomId
})
if(getRoomMembers.isOk() && getRoomMembers.value) {
message.receiverId = getRoomMembers.value.wxUserId
const DTO = MessageMapper.fromDomain(message, message.requestId)
sendMessageResult = await this.messageSocketRepositoryService.sendGroupMessage(DTO)
} else {
console.error('direct users not found', getRoomMembers)
}
room.local = IDBoolean.false
}
}
@@ -8,6 +8,7 @@ import { RoomLocalRepository } from '../../../data/repository/room/room-local-re
import { Logger } from 'src/app/services/logger/main/service';
import { XTracerAsync, TracingType } from 'src/app/services/monitoring/opentelemetry/tracer';
import { MessageTable } from 'src/app/infra/database/dexie/instance/chat/schema/message';
import { IDBoolean } from 'src/app/infra/database/dexie/type';
@Injectable({
providedIn: 'root'
@@ -34,7 +35,10 @@ export class SyncAllRoomMessagesService {
}
let n =0
const roomPromises = allRooms.value.map(async (room) => {
const removeLocalRooms = allRooms.value.filter((e)=> e.local != IDBoolean.true)
const roomPromises = removeLocalRooms.map(async (room) => {
const [result, localResult] = await Promise.all([
this.messageRemoteDataSourceService.getMessagesFromRoom(room.id),
this.messageLocalDataSourceService.getItems(room.id)
@@ -46,8 +50,9 @@ export class SyncAllRoomMessagesService {
if (result.isOk()) {
const { addedItems, changedItems, deletedItems } = messageListDetermineChanges(result.value.data, localResult);
console.log({changedItems})
for (const message of changedItems) {
delete message.sentAt
let clone: MessageTable = { ...message, roomId: room.id };
this.messageLocalDataSourceService.update(clone.$id, clone);
}
@@ -0,0 +1,44 @@
import { Injectable } from '@angular/core';
import { z } from 'zod';
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
import { SessionStore } from 'src/app/store/session.service';
import { RoomType } from 'src/app/core/chat/entity/group';
import { IDBoolean } from 'src/app/infra/database/dexie/type';
export const RoomCreateLocalDirectMessageInputDTOSchema = z.object({
roomName: z.string(),
members: z.array(z.number()),
receiverId: z.number().optional(),
});
export type RoomCreateLocalDirectMessageInputDTOInputDTO = z.infer<typeof RoomCreateLocalDirectMessageInputDTOSchema>
@Injectable({
providedIn: 'root'
})
export class RoomCreateLocalDirectMessageService {
constructor(
private roomLocalRepository: IRoomLocalRepository
) { }
async execute(input: RoomCreateLocalDirectMessageInputDTOInputDTO) {
return await this.roomLocalRepository.insert({
roomName: input.roomName,
bold: 0,
createdAt: new Date().toISOString(),
createdBy: {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
},
$id: input.receiverId.toString(),
messages: [],
roomType: RoomType.Direct,
local: IDBoolean.true,
receiverId: input.receiverId,
})
}
}
@@ -27,16 +27,24 @@ export class DeleteRoomUseCaseService {
if(result.isOk()) {
const result = await this.roomLocalDataSourceService.delete(id)
// this.messageLiveDataSourceService.sendMessage({
// type: 'createRoom',
// payload: {a: '5'}
// })
const room = await this.roomLocalDataSourceService.findOne({
id: id
})
if(room.isOk()) {
const result = await this.roomLocalDataSourceService.delete(room.value.$id)
}
return result
} else if (isHttpResponse(result.error)) {
if(result.error.status == 404) {
await this.roomLocalDataSourceService.delete(id)
const room = await this.roomLocalDataSourceService.findOne({
id: id
})
if(room.isOk()) {
const result = await this.roomLocalDataSourceService.delete(room.value.$id)
}
}
// this.httpErrorHandle.httpStatusHandle(result.error)
}
@@ -59,6 +59,8 @@ export class GetRoomByIdUseCaseService {
@captureAndReraiseAsync('RoomRepositoryService/getRoomById')
async execute(id: RoomByIdInputDTO) {
console.log('iiiiiiiiiiii', id)
const result = await this.roomRemoteDataSourceService.getRoom(id)
if(result.isOk()) {
@@ -7,6 +7,9 @@ import { IRoomRemoteRepository } from 'src/app/core/chat/repository/room/room-re
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';
import { IDBoolean } from 'src/app/infra/database/dexie/type';
import { RoomType } from 'src/app/core/chat/entity/group';
import { Logger } from 'src/app/services/logger/main/service';
const CreatedBySchema = z.object({
@@ -64,12 +67,44 @@ export class GetRoomListUseCaseService {
if(localList.isOk()) {
if(result.isOk()) {
const { roomsToDelete, roomsToInsert, roomsToUpdate } = roomListDetermineChanges(result.value.data, localList.value)
const filterValidateRooms = result.value.data.filter(e => {
if(e.chatRoom.roomType == RoomType.Direct) {
if(e.chatRoom.user1 != null && e.chatRoom.user2 != null) {
return true
} else {
Logger.error("direct room invalid data", {
data: e.chatRoom,
user1: e.chatRoom.user1,
user2: e.chatRoom.user2
})
return false
}
}
return true
})
const { roomsToDelete, roomsToInsert, roomsToUpdate } = roomListDetermineChanges(filterValidateRooms, localList.value)
console.log({roomsToDelete, roomsToInsert, roomsToUpdate})
if(roomsToInsert) {
const roomsToInsertEntity = GetRoomListMapper.toDomain(roomsToInsert)
for( const room of roomsToInsertEntity) {
this.roomLocalDataSourceService.insert(room)
this.roomLocalDataSourceService.insert(room).then((result) => {
if( result.isErr() &&
room.roomType == RoomType.Direct &&
result.error.DBErrorName == 'ConstraintError') {
this.roomLocalDataSourceService.update(room.$id, room).then((result) => {
if(result.isErr()) {
console.log('failed to update room id')
}
})
}
})
if(room.expirationDate) {
this.CronJobService.createCronJob('remove expired room', new Date(room.expirationDate), this.execute)
}
@@ -80,7 +115,9 @@ export class GetRoomListUseCaseService {
this.roomLocalDataSourceService.updateMany(roomsToUpdateEntity)
for( const room of roomsToDelete) {
this.roomLocalDataSourceService.delete(room.id)
if(room.local == IDBoolean.false) {
this.roomLocalDataSourceService.delete(room.id)
}
}
}
@@ -0,0 +1,22 @@
import { Injectable } from '@angular/core';
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
import { z } from 'zod';
const RoomGetLocalByIdServiceInputSchema = z.object({
$roomId: z.string()
})
export type IRoomGetLocalByIdServiceInput = z.infer<typeof RoomGetLocalByIdServiceInputSchema >
@Injectable({
providedIn: 'root'
})
export class RoomGetLocalByIdService {
constructor(
private roomLocalDataSourceService: IRoomLocalRepository,
) { }
async execute(input: IRoomGetLocalByIdServiceInput) {
return await this.roomLocalDataSourceService.findOne({$id: input.$roomId})
}
}
@@ -8,6 +8,7 @@ import { IMessageGetAllByRoomIdOutPut } from 'src/app/core/chat/usecase/message/
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';
import { messageListDetermineChanges } from '../../../data/async/list/rooms/messageListChangedetector';
@Injectable({
providedIn: 'root'
@@ -42,9 +43,16 @@ export class RoomSetLastMessageService {
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, {
const result = await this.roomLocalRepository.update(room.$id, {
messages: [message]
})
if(result.isErr()) {
console.log('failed to update last message')
} else {
console.log('set last message')
}
}
}
})
@@ -54,10 +62,19 @@ export class RoomSetLastMessageService {
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}]
// incoming _message does not have sender
const messageToUpdate = ({...room.messages?.[0],isDeleted: true})
const result = await this.roomLocalRepository.update(room.$id, {
messages: [messageToUpdate]
})
if(result.isErr()) {
console.log('failed to update last message')
} else {
console.log('set last message')
}
}
}
})
@@ -69,31 +86,61 @@ export class RoomSetLastMessageService {
).subscribe(async (message) => {
if(message?.roomId) {
console.log('listenToIncomingMessage', message.roomId)
const result = await this.roomLocalRepository.update(message.roomId, {
messages: [message]
const findRoom = await this.roomLocalRepository.findOne({
id: message.roomId
})
if(result.isErr()) {
console.log('failed to update last message')
} else {
console.log('set last message')
if(findRoom.isOk() && findRoom.value) {
const result = await this.roomLocalRepository.update(findRoom.value.$id, {
messages: [message]
})
if(result.isErr()) {
console.log('failed to update last message')
} else {
console.log('set last message')
}
}
}
});
}
// local storage
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]
const findRoom = await this.roomLocalRepository.findOne({
id: message.roomId
})
if(findRoom.isOk() && findRoom.value) {
const result = await this.roomLocalRepository.update(findRoom.value.$id, {
messages: [message]
})
if(result.isErr()) {
console.log('failed to update last message')
} else {
console.log('set last message')
}
} else if (message.receiverId) {
// direct message and first message
const findRoom = await this.roomLocalRepository.findOne({
receiverId: message.receiverId
})
if(findRoom.isOk() && findRoom.value) {
const result = await this.roomLocalRepository.update(findRoom.value.$id, {
messages: [message]
})
}
}
}
}, 100)
}
@@ -128,17 +175,47 @@ export class RoomSetLastMessageService {
const loadHistoryLastMessageDate = new Date(loadHistoryFirstMessage.sentAt).getTime()
if(loadHistoryFirstMessage.id == room.value.messages?.[0]?.id) {
const { addedItems, changedItems, deletedItems } = messageListDetermineChanges([loadHistoryFirstMessage], room.value.messages);
for (const message of changedItems) {
const result = await this.roomLocalRepository.update(room.value.$id, {
messages: [message]
})
}
// do nothing
} else if(loadHistoryLastMessageDate>localLastMessageDate) {
await this.roomLocalRepository.update(loadHistoryFirstMessage.roomId, {
// await this.roomLocalRepository.update(loadHistoryFirstMessage.roomId, {
// messages: [loadHistoryFirstMessage]
// })
const result = await this.roomLocalRepository.update(room.value.$id, {
messages: [loadHistoryFirstMessage]
})
if(result.isErr()) {
console.log('failed to update last message')
} else {
console.log('set last message')
}
} else if(loadHistoryLastMessageDate == localLastMessageDate) {
// do nothing
} else if(room.value.messages[0].isDeleted != loadHistoryFirstMessage.isDeleted) {
await this.roomLocalRepository.update(loadHistoryFirstMessage.roomId, {
// await this.roomLocalRepository.update(loadHistoryFirstMessage.roomId, {
// messages: [loadHistoryFirstMessage]
// })
const result = await this.roomLocalRepository.update(room.value.$id, {
messages: [loadHistoryFirstMessage]
})
if(result.isErr()) {
console.log('failed to update last message')
} else {
console.log('set last message')
}
}
}
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import { IRoomLocalRepository } from 'src/app/core/chat/repository/room/room-local-repository';
import { IDBoolean } from 'src/app/infra/database/dexie/type';
import { z } from 'zod';
const RoomSetLocalToFalseByIdInputSchema = z.object({
$roomId: z.string(),
roomId: z.string()
})
export type IRoomSetLocalToFalseByIdInput = z.infer<typeof RoomSetLocalToFalseByIdInputSchema >
@Injectable({
providedIn: 'root'
})
export class RoomSetLocalToFalseByIdService {
constructor(
private roomLocalDataSourceService: IRoomLocalRepository,
) { }
execute(input: IRoomSetLocalToFalseByIdInput) {
return this.roomLocalDataSourceService.update(input.$roomId, {
local: IDBoolean.false,
id: input.roomId
})
}
}
@@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { MessageLocalDataSourceService } from '../../../data/repository/message/message-local-data-source.service';
import { MessageEntitySchema } from 'src/app/core/chat/entity/message';
import { z } from 'zod';
import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer';
const SocketMessageDeleteOutputSchema = MessageEntitySchema.pick({
id: true,
@@ -35,13 +36,17 @@ export class SocketMessageDeleteUseCaseService {
) { }
async execute(input: ISocketMessageDeleteOutput) {
const result = await this.messageLocalDataSourceService.update(input.id, { isDeleted: true})
@XTracerAsync({name:'Socket-Message-Delete-UseCase', module:'chat', bugPrint: true})
async execute(input: ISocketMessageDeleteOutput, tracing?: TracingType) {
const result = await this.messageLocalDataSourceService.findOne({id: input.id})
if(result.isOk()) {
console.log('deleled', result.value)
} else {
console.log(result.error)
if(result.isOk() && result.value) {
tracing?.addEvent("Message found")
const updateResult = await this.messageLocalDataSourceService.update(result.value.$id, { isDeleted: true })
return updateResult
}else {
tracing.hasError("failed to delete message")
}
}
}
-1
View File
@@ -64,7 +64,6 @@ export class LoginPage implements OnInit {
private ChatServiceService: ChatServiceService,
private RoomLocalRepository: RoomLocalRepository,
private MessageLocalDataSourceService: MessageLocalDataSourceService
) { }
ngOnInit() { }
-16
View File
@@ -1,16 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { ChatService } from './chat.service';
describe('ChatService', () => {
let service: ChatService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ChatService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+26 -23
View File
@@ -42,59 +42,59 @@
<div class="width-100">
<div class="item item-hover width-100 d-flex ion-no-padding ion-no-margin"
*ngFor="let room of rooms"
[class.item-active]="room.id == idSelected" [class.hide-room]="room.roomType != segment">
[class.item-active]="room.$id == selectedRoomId" [class.hide-room]="room.roomType != segment">
<div class="item-icon">
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' && room.id != idSelected && room.roomType == RoomType.Group " class="icon" slot="start" src="assets/images/theme/gov/icons-chat-chat-40.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' && room.id == idSelected && room.roomType == RoomType.Group" class="icon" slot="start" src="assets/images/theme/gov/icons-chat-chat-40-hover.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' && room.$id != selectedRoomId && room.roomType == RoomType.Group " class="icon" slot="start" src="assets/images/theme/gov/icons-chat-chat-40.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' && room.$id == selectedRoomId && room.roomType == RoomType.Group" class="icon" slot="start" src="assets/images/theme/gov/icons-chat-chat-40-hover.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'default' && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/icons-chat-group-chat-40.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' && room.id != idSelected && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/theme/gov/icons-chat-group-chat-40.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' && room.id == idSelected && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/theme/gov/icons-chat-group-chat-40-hover.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'doneIt' && room.id != idSelected && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/theme/{{ThemeService.currentTheme}}/icons-chat-group-chat-40.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'doneIt' && room.id == idSelected && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/theme/{{ThemeService.currentTheme}}/icons-chat-group-chat-40-hover.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' && room.$id != selectedRoomId && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/theme/gov/icons-chat-group-chat-40.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' && room.$id == selectedRoomId && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/theme/gov/icons-chat-group-chat-40-hover.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'doneIt' && room.$id != selectedRoomId && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/theme/{{ThemeService.currentTheme}}/icons-chat-group-chat-40.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'doneIt' && room.$id == selectedRoomId && room.roomType == RoomType.Direct" class="icon" slot="start" src="assets/images/theme/{{ThemeService.currentTheme}}/icons-chat-group-chat-40-hover.svg"></ion-icon>
</div>
<div
(click)="openMessagesPage(room.id)" class="item-content flex-grow-1 cursor-pointer"><!-- (click)="openMessages(dm)" -->
(click)="openMessagesPage(room.$id)" class="item-content flex-grow-1 cursor-pointer"><!-- (click)="openMessages(dm)" -->
<div class="item-title-time">
<div class="item-title add-ellipsis" [class.item-title-active]="room.id == idSelected">
<div class="item-title add-ellipsis" [class.item-title-active]="room.$id == selectedRoomId">
<ion-label >
<span >
<div >
<div class="font-15-em add-ellipsis" [class.bold-message]="boldTable?.[room.id]?.bold">
<div class="font-15-em add-ellipsis" [class.bold-message]="boldTable?.[room.$id]?.bold">
{{room.roomName}}
</div>
</div>
</span>
</ion-label>
</div>
<div class="item-date font-13-em" [class.item-date-active]="room.id == idSelected">
<div class="item-date font-13-em" [class.item-date-active]="room.$id == selectedRoomId">
{{ room.displayDate }}
</div>
</div>
<div class="item-date font-13-em" [class.item-date-active]="room.id == idSelected">
<!-- {{ expirationDate[room.id] !== null ? expirationDate[room.id] + ' seconds left' : 'No expiration' }} -->
{{ expirationDate[room.id] !== null ? expirationDate[room.id] + ' seconds left' : '' }}
<div class="item-date font-13-em" [class.item-date-active]="room.$id == selectedRoomId">
<!-- {{ expirationDate[room.$id] !== null ? expirationDate[room.$id] + ' seconds left' : 'No expiration' }} -->
<span *ngIf="expirationDate[room.$id]"> {{ expirationDate[room.$id] !== null ? expirationDate[room.$id] + ' seconds left' : '' }} </span>
</div>
<div *ngIf="room?.messages?.length >= 1 && room.messages[0].isDeleted!=true" class="item-description d-flex align-items-center" [class.item-description-active]="room.id ==idSelected">
<div *ngIf="room?.messages?.length >= 1 && room.messages[0].isDeleted!=true" class="item-description d-flex align-items-center" [class.item-description-active]="room.$id ==selectedRoomId">
<!-- -->
<!-- <div *ngIf="room?.messages?.[0]?.message" class="item-description d-flex align-items-center" [class.item-description-active]="room.id ==idSelected"> -->
<!-- <div *ngIf="room?.messages?.[0]?.message" class="item-description d-flex align-items-center" [class.item-description-active]="room.$id ==selectedRoomId"> -->
<!-- <div class="item-message" *ngIf="group.otherUserType == false">{{room?.messages?.[0]?.message.u.name}}: {{room?.messages?.[0]?.message.msg}} </div> -->
<div class="item-message font-13-em add-ellipsis white-space-nowrap"> {{room.messages[0].message}} </div>
<!-- <div *ngIf="group.otherUserType == true">{{group.userThatIsTyping}} está escrever ...</div> -->
<div class="item-files add-ellipsis" *ngIf="room.messages[0]?.attachments?.length >= 1">
<fa-icon *ngIf="room.lastMessageDocument" icon="file-alt" class="file-icon" [class.set-active-item-font-to-white]="room.id == idSelected"></fa-icon>
<fa-icon *ngIf="room?.messages?.[0]?.attachments[0]?.mimeType == 'application/audio'" icon="file-audio" class="file-icon" [class.set-active-item-font-to-white]="room.id == idSelected"></fa-icon>
<fa-icon *ngIf="room.lastMessageDocument" icon="file-alt" class="file-icon" [class.set-active-item-font-to-white]="room.$id == selectedRoomId"></fa-icon>
<fa-icon *ngIf="room?.messages?.[0]?.attachments[0]?.mimeType == 'application/audio'" icon="file-audio" class="file-icon" [class.set-active-item-font-to-white]="room.$id == selectedRoomId"></fa-icon>
<span *ngIf="room?.messages?.[0]?.attachments[0]?.mimeType == 'application/audio'" class="item-files-title"> audio </span>
<fa-icon *ngIf="room?.messages?.[0]?.attachments[0]?.mimeType == 'application/meeting'" icon="calendar-alt" class="file-icon" [class.set-active-item-font-to-white]="room.id == idSelected"></fa-icon>
<fa-icon *ngIf="room?.messages?.[0]?.attachments[0]?.mimeType == 'application/meeting'" icon="calendar-alt" class="file-icon" [class.set-active-item-font-to-white]="room.$id == selectedRoomId"></fa-icon>
<fa-icon *ngIf="room.lastMessageImage" icon="image"></fa-icon>
<span class="pl-2 font-13-em add-ellipsis">{{ room.messages[0].attachments[0].description }}</span>
</div>
<!-- <div class="item-files" *ngIf="room.attachments">
<div *ngIf="room.value.lastMessage.attachments[0].image_url">
<fa-icon icon="image" class="file-icon" [class.set-active-item-font-to-white]="room.id == idSelected"></fa-icon>
<fa-icon icon="image" class="file-icon" [class.set-active-item-font-to-white]="room.$id == selectedRoomId"></fa-icon>
<span> Fotografia</span>
</div>
</div> -->
@@ -127,7 +127,8 @@
</app-empty-chat>
<app-messages class=" height-100 flex-column"
[room]="RoomSelected"
[_room]="RoomSelected"
*ngIf="showMessages"
[style.display]="showMessages ? 'flex' : 'none'"
(closeAllDesktopComponents)="closeAllDesktopComponents()"
@@ -139,13 +140,15 @@
[style.display]="showMessages ? 'flex' : 'none'"
[roomId]="roomId"
[receiverId]="selectedReceiverId"
[showMessages]="showMessages" #messagecontainer>
</app-messages>
<app-contacts
(openMessage)="openMessagesPage($event)"
(backToChat)="backToChat($event)"
(backToChat)="openMessagesWithOutValidation($event)"
(openMessagesToStartDirectConversation)="openMessagesToStartDirectConversation($event)"
(emptyTextDescriptionOpen)="emptyTextDescriptionOpen()"
(closeAllDesktopComponents)="closeAllDesktopComponents()"
*ngIf="showContacts"
@@ -157,7 +160,7 @@
<app-new-group
(addGroupMessage)="openGroupContactsPage($event)"
[roomId]="roomId"
(backToChat)="backToChat($event)"
(backToChat)="openMessagesWithOutValidation($event)"
(closeAllDesktopComponents)="closeAllDesktopComponents()"
(openGroupContacts)="openGroupContactsPage($event)"
(openEditGroupPage)="openEditGroupPage($event)"
+66 -30
View File
@@ -1,4 +1,5 @@
import {
ChangeDetectorRef,
Component,
OnInit,
} from '@angular/core';
@@ -25,6 +26,7 @@ import { BoldTable } from 'src/app/infra/database/dexie/instance/chat/schema/bol
import { RoomViewModel } from './store/model/room';
import { MessageLocalDataSourceService } from 'src/app/module/chat/data/repository/message/message-local-data-source.service'
import { ToastService } from 'src/app/services/toast.service';
import { Logger } from 'src/app/services/logger/main/service';
@Component({
selector: 'app-chat',
templateUrl: './chat.page.html',
@@ -34,7 +36,8 @@ export class ChatPage implements OnInit {
showLoader: boolean;
segment: RoomType = RoomType.Direct
idSelected: string;
selectedRoomId: string;
selectedReceiverId: string;
roomId: any;
groupRoomId: any;
@@ -83,7 +86,7 @@ export class ChatPage implements OnInit {
private roomLocalDataSourceService: RoomLocalRepository,
private boldLocalRepository: BoldLocalRepository,
private MessageLocalDataSourceService: MessageLocalDataSourceService,
private toastService: ToastService,
private toastService: ToastService
) {
window.onresize = (event) => {
@@ -111,12 +114,11 @@ export class ChatPage implements OnInit {
this.rooms = sortedRooms
this.rooms.sort((a, b) =>
new Date(b.messages?.[0]?.sentAt as string).getTime() -
new Date(a.messages?.[0]?.sentAt as string).getTime()
new Date(b.messages?.[0]?.sentAt as string || b.createdAt ).getTime() -
new Date(a.messages?.[0]?.sentAt as string || a.createdAt).getTime()
);
// this.RoomSelected = this.rooms.filter(e => e.id == this.idSelected)[0]
// this.RoomSelected = this.rooms.filter(e => e.id == this.selectedRoomId)[0]
}
ngOnInit() {
// this.subscription = this.roomListSubject.pipe(
@@ -143,14 +145,7 @@ export class ChatPage implements OnInit {
this.roomLocalDataSourceService.getItemsLive().pipe(
map((roomList) => roomList.map((room)=> new RoomViewModel(room))),
tap((roomList) => {
roomList.sort((a, b) =>
new Date(b.messages?.[0]?.sentAt as string).getTime() -
new Date(a.messages?.[0]?.sentAt as string).getTime()
);
console.log('update')
this.updatemessage(roomList)
})
).subscribe()
@@ -159,7 +154,10 @@ export class ChatPage implements OnInit {
const interval$ = interval(1000).pipe(
tap(() => {
for (const room of this.rooms) {
this.expirationDate[room.id] = this.getSecondsLeft(room.expirationDate);
if(room.expirationDate) {
this.expirationDate[room.$id] = this.getSecondsLeft(room.expirationDate);
}
}
})
);
@@ -290,7 +288,7 @@ export class ChatPage implements OnInit {
hideRefreshButton() {
window.onresize = (event) => {
if (window.innerWidth < 701) {
this.idSelected = '';
this.selectedRoomId = '';
this.hideRefreshBtn = false;
this.closeAllDesktopComponents()
@@ -298,13 +296,13 @@ export class ChatPage implements OnInit {
}
else {
this.hideRefreshBtn = true;
if (this.idSelected == '') {
if (this.selectedRoomId == '') {
this.showEmptyComponent = true;
}
}
}
if (window.innerWidth < 701) {
this.idSelected = '';
this.selectedRoomId = '';
this.hideRefreshBtn = false;
}
}
@@ -323,13 +321,13 @@ export class ChatPage implements OnInit {
}
showEmptyContainer() {
this.idSelected = '';
this.selectedRoomId = '';
this.showEmptyComponent = true;
}
openGroupContactsPage(roomId) {
this.idSelected = '';
this.selectedRoomId = '';
this.groupRoomId = roomId;
this.RoomSelected = this.rooms.filter(e => e.id == roomId)[0]
this.RoomSelected = this.rooms.filter(e => e.$id == roomId)[0]
this.closeAllDesktopComponents();
if (window.innerWidth < 701) {
@@ -340,17 +338,19 @@ export class ChatPage implements OnInit {
}
openMessagesPage(roomId) {
openMessagesPage($roomId: string) {
// this.chatService.refreshtoken();
this.roomId = roomId;
this.RoomSelected = this.rooms.filter(e => e.id == roomId)[0]
this.roomId = $roomId;
const exist = this.rooms.filter(e => e.$id == $roomId)[0]
if(this.RoomSelected?.id) {
if(exist) {
this.RoomSelected = exist
this.selectedRoomId = exist.$id;
if (window.innerWidth < 701) {
this.openMessagesModal(roomId);
this.openMessagesModal($roomId);
}
else {
this.idSelected = roomId;
this.selectedRoomId = $roomId;
this.closeAllDesktopComponents();
this.showEmptyComponent = false;
this.showMessages = true;
@@ -360,10 +360,40 @@ export class ChatPage implements OnInit {
// this.toastService._badRequest("Pedimos desculpa mas não foi possível executar a acção. Por favor, contacte o apoio técnico.")
}
// Trigger change detection manually
}
openMessagesWithOutValidation(roomId: string) {
if (window.innerWidth < 701) {
// this.openMessagesModal(roomId);
}
else {
this.selectedRoomId = roomId;
this.selectedReceiverId = undefined
this.closeAllDesktopComponents();
this.showEmptyComponent = false;
this.showMessages = true;
}
}
openMessagesToStartDirectConversation(room: RoomViewModel) {
if (window.innerWidth < 701) {
// this.openMessagesModal(roomId);
}
else {
this.roomId = null;
this.selectedRoomId = room.$id;
console.log('RoomSelected', room)
this.RoomSelected = room
this.closeAllDesktopComponents();
this.showEmptyComponent = false;
this.showMessages = true;
}
}
openContactsPage() {
this.segment = RoomType.Direct;
this.idSelected = '';
this.selectedRoomId = '';
this.closeAllDesktopComponents();
if (window.innerWidth < 701) {
@@ -377,7 +407,7 @@ export class ChatPage implements OnInit {
openNewGroupPage() {
this.segment = RoomType.Group;
this.idSelected = '';
this.selectedRoomId = '';
if (window.innerWidth < 701) {
this.newGroup();
}
@@ -492,8 +522,14 @@ export class ChatPage implements OnInit {
// this.segment = "Grupos"
// this.openGroupMessagesPage(room.id);
//} else {
// this.segment = RoomType.Direct
this.openMessagesPage(roomId);
// this.segment = RoomType.Direct
this.openMessagesPage(roomId);
if(typeof roomId != 'string') {
Logger.error('roomId must be string', {
roomId
})
}
// }
}
@@ -11,6 +11,7 @@ import { SessionStore } from 'src/app/store/session.service';
import { MessageEntity } from 'src/app/core/chat/entity/message';
// import { ChatSystemService } from 'src/app/services/chat/chat-system.service'
import { RoomType } from "src/app/core/chat/entity/group";
import { RoomViewModel } from '../../store/model/room';
@Component({
selector: 'app-contacts',
@@ -30,6 +31,7 @@ export class ContactsPage implements OnInit {
@Output() emptyTextDescriptionOpen: EventEmitter<any> = new EventEmitter<any>();
@Output() backToChat: EventEmitter<any> = new EventEmitter<any>();
@Output() closeAllDesktopComponents: EventEmitter<any> = new EventEmitter<any>();
@Output() openMessagesToStartDirectConversation = new EventEmitter<RoomViewModel>();
@Input() roomId: string;
@@ -39,7 +41,7 @@ export class ContactsPage implements OnInit {
private contactsRepositoryService: ContactRepositoryService,
private httpErrorHandle: HttpErrorHandle,
private toastService: ToastService,
private chatServiceService: ChatServiceService
private chatServiceService: ChatServiceService,
)
{}
@@ -51,11 +53,12 @@ export class ContactsPage implements OnInit {
async loadUsers() {
this.loading = true
const getallChatUsers = await this.contactsRepositoryService.getUsers()
const getallChatUsers = await this.chatServiceService.getContactList()
if(getallChatUsers.isOk()) {
this.allChatUsers = getallChatUsers.value.data.result.sort((a,b) => {
this.allChatUsers = getallChatUsers.value.sort((a,b) => {
if(a.wxFullName < b.wxFullName) {
return -1;
}
@@ -111,14 +114,19 @@ export class ContactsPage implements OnInit {
doRefresh(event){
}
close(roomId) {
close(roomId?: string) {
if (roomId) {
this.backToChat.emit({ roomId: roomId });
this.backToChat.emit( roomId );
} else {
this.closeAllDesktopComponents.emit();
}
}
openMessageComponent(room: RoomViewModel) {
this.openMessagesToStartDirectConversation.emit(room)
}
onChange(event) {
const textSearch = event.detail.value;
@@ -155,29 +163,34 @@ export class ContactsPage implements OnInit {
selectOnce = true
async select(user: UserContacts) {
const message = new MessageEntity();
// const result = await this.chatServiceService.sendMessage(message, RoomType.Direct)
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.receiverId = user.wxUserId
message.message = null
const result = await this.chatServiceService.sendMessage(message, RoomType.Direct)
console.log('result', result);
const result = await this.chatServiceService.roomCreateLocalDirectMessage({
roomName: user.wxFullName,
receiverId: user.wxUserId,
});
if(result.isOk()) {
await this.chatServiceService.getRoomById(result.value.roomId)
this.close(result.value.roomId)
const room = await this.chatServiceService.roomGetLocalById({
$roomId: result.value
})
if(room.isOk()) {
console.log('room', room)
console.log('result.value', result.value)
console.log('receiverId', user.wxUserId)
// await this.chatServiceService.getRoomById(user.wxUserId.toString())
this.close(user.wxUserId.toString())
this.openMessageComponent(new RoomViewModel(room.value))
}
} else {
console.log(result.error)
this.close(user.wxUserId.toString())
}
}
}
@@ -3,13 +3,12 @@
<div class="main-header">
<div class="header-top">
<div class="middle" >
<!-- <ion-label class="title" *ngIf="(roomData$ | async) === null"> {{ room.roomName }}</ion-label> -->
<ion-label class="title" *ngIf="roomData$ | async as roomData"> {{ roomData.roomName }}</ion-label>
<ion-label class="title"> {{ room?.roomName }}</ion-label>
<!-- <button (click)="ChatMessageDebuggingPage()">Dev</button> -->
<span *ngIf="roomStatus$ | async as roomStatus"><ion-icon *ngIf="roomStatus" class="online" name="ellipse"></ion-icon></span>
</div>
<div class="right">
<button title="Menu" class="btn-no-color" (click)="_openMessagesOptions()" *ngIf="roomType == RoomTypeEnum.Group">
<button title="Menu" class="btn-no-color" (click)="_openMessagesOptions()" >
<ion-icon *ngIf="ThemeService.currentTheme == 'default' " src="assets/images/theme/blue/icons-menu.svg"></ion-icon>
<ion-icon *ngIf="ThemeService.currentTheme == 'gov' " src="assets/images/theme/gov/icons-menu.svg">
</ion-icon>
@@ -44,7 +43,7 @@
<div class="messages height-100 width-100 d-flex flex-column rotate-div" #scrollMe >
<div
*ngFor="let message of messages1[roomId]; let messageIndex = index" class="messages-list-item-wrapper px-10-em"
*ngFor="let message of messages1[room?.$id]; let messageIndex = index" class="messages-list-item-wrapper px-10-em"
[ngClass]="{
'info-meeting': message.messageType == IMessageType.information && !message.ballon,
'info-ballon': message.ballon == true,
@@ -3,11 +3,8 @@ import { AnimationController, GestureController, IonRange, ModalController, Popo
import { ToastService } from 'src/app/services/toast.service';
import { ContactsPage } from '../contacts/contacts.page';
import { ChatOptionsFeaturesPage } from 'src/app/modals/chat-options-features/chat-options-features.page';
import { TimeService } from 'src/app/services/functions/time.service';
import { FileService } from 'src/app/services/functions/file.service';
import { ViewDocumentPage } from 'src/app/modals/view-document/view-document.page';
import { ThemeService } from 'src/app/services/theme.service';
import { ViewEventPage } from 'src/app/modals/view-event/view-event.page';
import { FileType } from 'src/app/models/fileType';
import { SearchPage } from 'src/app/pages/search/search.page';
import { CameraResultType } from '@capacitor/camera';
@@ -53,6 +50,9 @@ import { RoomViewModel } from '../../store/model/room';
import { MessageViewModal } from '../../store/model/message';
import { XBallon } from '../../utils/messageBallon';
import { whatsappDate } from 'src/app/ui/shared/utils/whatappdate';
import { IDBoolean } from 'src/app/infra/database/dexie/type';
import { Result } from 'neverthrow';
import { MessageOutPutDataDTO } from 'src/app/core/chat/repository/dto/messageOutputDTO';
@Component({
selector: 'app-messages',
templateUrl: './messages.page.html',
@@ -72,7 +72,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
roomType!: RoomType
RoomTypeEnum = RoomType
@Input() room!: RoomViewModel
@Input() _room!: RoomViewModel
room!: RoomViewModel
@Input() roomId: string;
@Input() showMessages: string;
@@ -114,8 +116,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
@ViewChild('range', { static: false }) range: IonRange;
@ViewChild('array') myInputRef!: ElementRef;
userName = "";
roomName: any;
isAdmin = true;
roomCountDownDate: string;
audioMimeType = ''
@@ -152,9 +152,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
totalMembers = 0
members: MemberTable[] = []
private worker: SharedWorker;
private port: MessagePort;
date: {[key: string]: Object} = {}
handleClickActive = true
@@ -163,8 +160,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
private modalController: ModalController,
private animationController: AnimationController,
private toastService: ToastService,
private timeService: TimeService,
private fileService: FileService,
private gestureController: GestureController,
public ThemeService: ThemeService,
private sanitiser: DomSanitizer,
@@ -192,21 +187,32 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
ngOnChanges(changes: SimpleChanges): void {
this.roomData$ = this.RoomLocalRepository.getRoomByIdLive(this.roomId)
this.room = {...this._room} as any
console.log('message', this.room)
if(this.room.local == IDBoolean.false) {
this.getMessages()
this.subscribeToChanges()
} else {
this.getMessages()
}
this.roomData$ = this.RoomLocalRepository.getRoomByIdLive(this.room.$id)
this.roomData$.subscribe(e => {
if(e) {
this.roomType = e.roomType
this.room = new RoomViewModel(e)
this.roomType = e.roomType;
}
})
}
this.getMessages();
subscribeToChanges() {
this.listenToIncomingMessage();
this.listenToDeleteMessage();
this.listenToUpdateMessage();
this.listenToSendMessage();
this.roomMembers$ = this.MemberListLocalRepository.getRoomMemberByIdLive(this.roomId).pipe(
this.roomMembers$ = this.MemberListLocalRepository.getRoomMemberByIdLive(this.room.id).pipe(
tap((members) => {
this.totalMembers = members.length
this.members = members
@@ -218,10 +224,10 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
})
)
this.roomStatus$ = this.MemberListLocalRepository.allMemberOnline(this.roomId)
this.roomStatus$ = this.MemberListLocalRepository.allMemberOnline(this.room.id)
this.messageTypingSubject?.unsubscribe()
this.messageTypingSubject = this.userTypingLocalRepository.getUserTypingLiveByRoomId(this.roomId).subscribe((e) => {
this.messageTypingSubject = this.userTypingLocalRepository.getUserTypingLiveByRoomId(this.room.id).subscribe((e) => {
const arrayNames = e.filter((b)=> b.userId != SessionStore.user.UserId).map(e => e.userName)
this.userTyping$ = e as any
@@ -235,12 +241,22 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}) as any
this.chatServiceService.removeBoldFromRoom({roomId: this.roomId})
this.chatServiceService.getRoomById(this.roomId)
this.updateRoomDetails()
}
updateRoomDetails() {
if(!this.room?.local) {
this.chatServiceService.getRoomById(this.room.id)
}
}
removeBold() {
if(!this.room?.local) {
this.chatServiceService.removeBoldFromRoom({roomId: this.room.$id})
}
}
@HostListener('document:click', ['$event'])
handleClickOutside(event: Event) {
if (!this.handleClickActive) return;
@@ -274,76 +290,74 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
async getMessages() {
// dont remove this line
this.messages1[this.roomId] = []
let messages = await this.messageLocalDataSourceService.getItems(this.roomId)
this.messages1[this.room.$id] = []
let messages = await this.chatServiceService.messageLocalGetById({
roomId: this.room.id,
receiverId: this.room?.receiverId?.toString()
})
this.messages1[this.roomId] = []
this.date = {}
const allMessage = [];
if(messages.isOk()) {
this.messages1[this.room.$id] = []
this.date = {}
const allMessage = [];
// let ids = {}
// messages = messages.filter((message: any) => {
// if (message.$createAt) {
// if (!ids[message.$createAt]) {
// ids[message.$createAt] = true;
// return true; // Keep this message
// } else {
// console.log('delete');
// return false; // Remove this message
// }
// }
// return true; // Keep messages without an id
// });
console.time("mappingTime");
for(const message of messages) {
const date = whatsappDate(message.sentAt, false)
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(message)
allMessage.push(Ballon)
console.time("mappingTime");
const sortMessages = messages.value.sort((a, b) => new Date(a.sentAt).getTime() - new Date(b.sentAt).getTime())
for(const message of sortMessages) {
const date = whatsappDate(message.sentAt, false)
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(message)
allMessage.push(Ballon)
}
allMessage.push(new MessageViewModal(message))
}
console.timeEnd("mappingTime");
this.messages1[this.room.$id]
this.messages1[this.room.$id] = allMessage
// if(messages.length >= 1) {
// this.messages1[this.room.$id].push(LastMessage)
// }
this.loadAttachment()
setTimeout(() => {
this.sendReadMessage()
}, 1000)
if(this.room.$id) {
this.onReconnectGetMessages()
}
allMessage.push(new MessageViewModal(message))
}
console.timeEnd("mappingTime");
this.messages1[this.roomId] = allMessage
// if(messages.length >= 1) {
// this.messages1[this.roomId].push(LastMessage)
// }
this.loadAttachment()
setTimeout(() => {
this.sendReadMessage()
}, 1000)
}
onReconnectGetMessages() {
this.messageOnReconnectSubject?.unsubscribe()
this.messageOnReconnectSubject = this.chatServiceService.listenToMessageLoadHistory({roomId: this.roomId}).subscribe((messages) => {
this.messageOnReconnectSubject = this.chatServiceService.listenToMessageLoadHistory({roomId: this.room.id}).subscribe((messages) => {
for(const message of messages.data) {
const found = this.messages1[this.roomId].find((e) => e.id == message.id)
const found = this.messages1[this.room.$id].find((e) => e.id == message.id)
if(!found) {
const msg = new MessageViewModal(message as any)
Object.assign(msg, message)
this.messages1[this.roomId].push(msg)
this.messages1[this.room.$id].push(msg)
}
}
this.messages1[this.roomId].sort((a, b) => {
this.messages1[this.room.$id].sort((a, b) => {
return new Date(a.sentAt).getTime() - new Date(b.sentAt).getTime()
})
})
}
async loadAttachment() {
for(const message of this.messages1[this.roomId]) {
for(const message of this.messages1[this.room.$id]) {
if(message.hasAttachment && message.attachments[0].source != MessageAttachmentSource.Webtrix) {
this.chatServiceService.getMessageAttachmentByMessageId(message).then((result)=> {
@@ -368,7 +382,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
this.scrollToBottom();
}, 100)
this.messages1[this.roomId].splice(index, 1);
this.messages1[this.room.$id].splice(index, 1);
}
}
@@ -393,39 +407,40 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
sendReadMessage() {
for(const message of this.messages1[this.roomId]) {
if(this.room.local == IDBoolean.false) {
for(const message of this.messages1[this.room.$id]) {
if(!message.meSender()) {
const me = message.haveSeen(message.info)
if(!message.meSender()) {
const me = message.haveSeen(message.info)
if(!me) {
Logger.info('send read at, sender '+ message.sender.wxFullName+ ' '+ message.message +'message id'+ message.id)
if(!me) {
Logger.info('send read at, sender '+ message.sender.wxFullName+ ' '+ message.message +'message id'+ message.id)
this.chatServiceService.sendReadAt({
memberId: SessionStore.user.UserId,
messageId: message.id,
requestId: '',
roomId: this.roomId
})
this.chatServiceService.sendReadAt({
memberId: SessionStore.user.UserId,
messageId: message.id,
requestId: '',
roomId: this.room.$id
})
}
}
}
}
}
listenToIncomingMessage() {
this.messageReceiveSubject?.unsubscribe();
this.messageReceiveSubject = this.chatServiceService.listenToIncomingMessage(this.roomId).subscribe(async (_message) => {
this.messageReceiveSubject = this.chatServiceService.listenToIncomingMessage(this.room.id).subscribe(async (_message) => {
const date = whatsappDate(_message.sentAt, false)
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(_message)
this.messages1[this.roomId].push(Ballon)
this.messages1[this.room.$id].push(Ballon)
}
const message = new MessageViewModal(_message)
this.messages1[this.roomId].push(new MessageViewModal(message))
this.messages1[this.room.$id].push(new MessageViewModal(message))
if(message.hasAttachment) {
@@ -450,7 +465,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
setTimeout(() => {
this.chatServiceService.removeBoldFromRoom({roomId: this.roomId})
this.removeBold()
}, 1000)
});
@@ -459,23 +474,23 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
listenToDeleteMessage() {
this.messageDeleteSubject?.unsubscribe();
this.messageDeleteSubject = this.chatServiceService.listenToDeleteMessage(this.roomId).subscribe((deleteMessage) => {
this.messageDeleteSubject = this.chatServiceService.listenToDeleteMessage(this.room.id).subscribe((deleteMessage) => {
console.log('delete class', deleteMessage);
const index = this.messages1[this.roomId].findIndex(e =>
const index = this.messages1[this.room.$id].findIndex(e =>
typeof e?.id == 'string' && e?.id === deleteMessage.id ||
typeof e?.requestId == 'string' && e?.requestId == deleteMessage.requestId);
try {
console.log(this.messages1[this.roomId][index])
this.messages1[this.roomId][index].delete()
} catch (e) {
console.log('delete', e)
}
console.log(this.messages1[this.room.$id][index])
this.messages1[this.room.$id][index].delete()
} catch (e) {
console.log('delete', e)
}
// if (index !== -1) { // Check if the item was found
// console.log('delete ==')
// this.messages1[this.roomId].splice(index, 1);
// this.messages1[this.room.$id].splice(index, 1);
// // console.log('removed index', index);
// } else {
// // console.log('message not found');
@@ -486,14 +501,17 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
listenToUpdateMessage() {
this.messageUpdateSubject?.unsubscribe();
this.messageUpdateSubject = this.chatServiceService.listenToUpdateMessage(this.roomId).subscribe((updateMessage) => {
console.log('liste to update')
this.messageUpdateSubject = this.chatServiceService.listenToUpdateMessage(this.room.id).subscribe((updateMessage) => {
const index = this.messages1[this.roomId].findIndex(e => e?.id === updateMessage.id); // Use triple equals for comparison
const index = this.messages1[this.room.$id].findIndex(e => e?.id === updateMessage.id); // Use triple equals for comparison
console.log('updateMessage', updateMessage)
if (index !== -1) { // Check if the item was found
this.messages1[this.roomId][index].info = updateMessage.info
this.messages1[this.roomId][index].message = updateMessage.message
this.messages1[this.roomId][index].reactions = updateMessage.reactions
this.messages1[this.room.$id][index].info = updateMessage.info
this.messages1[this.room.$id][index].message = updateMessage.message
this.messages1[this.room.$id][index].reactions = updateMessage.reactions
} else {
// console.log('message not found');
}
@@ -504,20 +522,20 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
listenToSendMessage() {
this.messageSendSubject?.unsubscribe();
this.messageSendSubject = this.chatServiceService.listenToSendMessage(this.roomId).subscribe((updateMessage) => {
this.messageSendSubject = this.chatServiceService.listenToSendMessage(this.room.id).subscribe((updateMessage) => {
const index = this.messages1[this.roomId].findIndex(e => e?.requestId === updateMessage.requestId); // Use triple equals for comparison
const index = this.messages1[this.room.$id].findIndex(e => e?.requestId === updateMessage.requestId); // Use triple equals for comparison
if (index !== -1) { // Check if the item was found
this.messages1[this.roomId][index].id = updateMessage.id
this.messages1[this.roomId][index].info = updateMessage.info
this.messages1[this.room.$id][index].id = updateMessage.id
this.messages1[this.room.$id][index].info = updateMessage.info
let attachmentIndex = 0;
for(const message of updateMessage.attachments) {
this.messages1[this.roomId][index].attachments[attachmentIndex].id = message.id
this.messages1[this.room.$id][index].attachments[attachmentIndex].id = message.id
attachmentIndex++;
}
@@ -545,7 +563,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
this.chatServiceService.reactToMessage({
memberId: SessionStore.user.UserId,
messageId: message.id,
roomId: this.roomId,
roomId: this.room.id,
reaction: emoji,
requestId: ''
})
@@ -554,13 +572,15 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
sendReadAt() {
// this.chatServiceService.messageMarkAsRead({roomId: this.roomId}).then((e) => {
// this.chatServiceService.messageMarkAsRead({roomId: this.room.$id}).then((e) => {
// console.log(e)
// })
}
sendTyping() {
this.UserTypingRemoteRepositoryService.sendTyping(this.roomId)
if(this.room.local == IDBoolean.false) {
this.UserTypingRemoteRepositoryService.sendTyping(this.room.id)
}
}
async editMessage(message: MessageViewModal) {
@@ -570,7 +590,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
cssClass: 'edit-message',
componentProps: {
message: message.message,
roomId: this.roomId,
roomId: this.room.$id,
}
});
@@ -584,7 +604,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
message: res.data.message,
messageId: message.id,
requestId: '',
roomId: this.roomId
roomId: this.room.id
})
}
@@ -607,7 +627,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
ngOnInit() {
this.scrollToBottom();
this.getChatMembers();
this.deleteRecording();
}
@@ -628,14 +647,10 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
});
}
load = () => {
this.getChatMembers();
}
doRefresh(ev: any) {
this.load();
ev.target.complete();
// ev.target.complete();
}
scrollToBottom = () => {
@@ -747,13 +762,15 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}
async sendAudio(fileName) {
const roomId = this.roomId
//Converting base64 to blob
const encodedData = this.audioRecordedDataUrl;
createMessage() {
const message = new MessageViewModal();
message.roomId = this.roomId
if(this.room.id) {
message.roomId = this.room.id
} else {
message.roomId = this.room.$id
}
message.sentAt = new Date().toISOString()
@@ -764,6 +781,40 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
wxUserId: SessionStore.user.UserId
}
message.sentAt = new Date().toISOString()
if(this.room.receiverId) {
message.receiverId = this.room.receiverId
}
return message
}
async messageResult(result: Promise<Result<MessageOutPutDataDTO, any>> ) {
let message = await result
console.log('result', message)
if(message.isOk() && this.room.local == IDBoolean.true) {
this.room.local = IDBoolean.false;
// console.log('enter')
// await this.chatServiceService.roomSetLocalToFalseById({
// $roomId: this.room.$id,
// roomId: message.value.roomId
// })
this.room.id = message.value.roomId
this.subscribeToChanges()
}
}
async sendAudio(fileName) {
const roomId = this.room.$id
//Converting base64 to blob
const encodedData = this.audioRecordedDataUrl;
const message = this.createMessage();
message.attachments = [{
file: encodedData.split(',')[1],
fileName: "audio",
@@ -773,18 +824,17 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
safeFile: this.sanitiser.bypassSecurityTrustResourceUrl(this.audioRecordedDataUrl)
}]
message.sentAt = new Date().toISOString()
const date = whatsappDate(message.sentAt, false)
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(message)
this.messages1[this.roomId].push(Ballon)
this.messages1[this.room.$id].push(Ballon)
}
this.messages1[this.roomId].push(message)
this.chatServiceService.sendMessage(message, this.roomType)
this.messages1[this.room.$id].push(message)
let sendMessage = this.chatServiceService.sendMessage(message, this.roomType)
this.messageResult(sendMessage)
setTimeout(() => {
this.scrollToBottomClicked()
@@ -800,45 +850,10 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
this.audioRecordedDataUrl = ''
}
showDateDuration(start: any) {
return this.timeService.showDateDuration(start);
}
async goToEvent(event: any) {
let classs;
if (window.innerWidth < 701) {
classs = 'modal modal-desktop'
} else {
classs = 'modal modal-desktop showAsideOptions'
}
const modal = await this.modalController.create({
component: ViewEventPage,
componentProps: {
eventId: event.id,
CalendarId: event.calendarId
},
cssClass: classs,
});
modal.onDidDismiss().then((res) => {
});
await modal.present();
}
async sendMessage() {
const message = new MessageViewModal();
const message = this.createMessage();
message.message = this.textField
message.roomId = this.roomId
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.sentAt = new Date().toISOString()
this.textField = ''
@@ -846,14 +861,15 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(message)
this.messages1[this.roomId].push(Ballon)
this.messages1[this.room.$id].push(Ballon)
}
this.messages1[this.roomId].push(message)
this.messages1[this.room.$id].push(message)
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
const data = await this.chatServiceService.sendMessage(message, this.roomType)
let sendMessage = this.chatServiceService.sendMessage(message, this.roomType)
this.messageResult(sendMessage)
}
@@ -911,9 +927,6 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
await modal.present();
}
getChatMembers() {}
async addContacts() {
const modal = await this.modalController.create({
component: ContactsPage,
@@ -956,9 +969,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
component: ChatPopoverPage,
cssClass: 'model search-submodal chat-option-aside',
componentProps: {
roomId: this.roomId,
roomId: this.room.id,
members: [],
isAdmin: this.isAdmin,
isAdmin: true,
roomType: this.roomType
}
});
@@ -969,7 +982,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.room.$id).catch((error) => console.error(error));
}
else if (res.data == 'delete') {
this.closeAllDesktopComponents.emit();
@@ -981,7 +994,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
else if (res.data == 'edit') {
//this.closeAllDesktopComponents.emit();
this.openEditGroupPage.emit(this.roomId);
this.openEditGroupPage.emit(this.room.$id);
} else if (res.data == 'addUser') {
this.openGroupContactsPage();
@@ -992,7 +1005,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
}
openGroupContactsPage() {
this.openGroupContacts.emit(this.roomId);
this.openGroupContacts.emit(this.room.$id);
}
async takePictureMobile() {
@@ -1016,19 +1029,10 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if(compressedImage.isOk()) {
const message = new MessageViewModal();
message.roomId = this.roomId
const message = this.createMessage();
message.oneShot = oneShot
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.sentAt = new Date().toISOString()
message.attachments = [{
file: compressedImage.value.split(',')[1],
fileName: "foto",
@@ -1042,13 +1046,14 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(message)
this.messages1[this.roomId].push(Ballon)
this.messages1[this.room.$id].push(Ballon)
}
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
this.chatServiceService.sendMessage(message, this.roomType)
let sendMessage = this.chatServiceService.sendMessage(message, this.roomType)
this.messageResult(sendMessage)
}
@@ -1082,18 +1087,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
// "title": res.data.selected.Assunto,
// "description": res.data.selected.DocTypeDesc,
const message = new MessageViewModal();
const message = this.createMessage();
message.message = this.textField
message.roomId = this.roomId
message.sentAt = new Date().toISOString()
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.attachments = [{
fileName: res.data.selected.Assunto,
source: MessageAttachmentSource.Webtrix,
@@ -1107,14 +1103,15 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(message)
this.messages1[this.roomId].push(Ballon)
this.messages1[this.room.$id].push(Ballon)
}
this.messages1[this.roomId].push(message)
this.messages1[this.room.$id].push(message)
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
this.chatServiceService.sendMessage(message, this.roomType)
let sendMessage = this.chatServiceService.sendMessage(message, this.roomType)
this.messageResult(sendMessage)
this.textField = ''
}
@@ -1178,19 +1175,9 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if(compressedImage.isOk()) {
const message = new MessageViewModal();
message.roomId = this.roomId
message.sentAt = new Date().toISOString()
const message = this.createMessage();
message.oneShot = oneShot
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
message.attachments = [{
file: file.value.base64String,
fileName: "foto",
@@ -1204,14 +1191,15 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(message)
this.messages1[this.roomId].push(Ballon)
this.messages1[this.room.$id].push(Ballon)
}
this.messages1[this.roomId].push(message)
this.messages1[this.room.$id].push(message)
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
this.chatServiceService.sendMessage(message, this.roomType)
let sendMessage = this.chatServiceService.sendMessage(message, this.roomType)
this.messageResult(sendMessage)
}
}
@@ -1224,7 +1212,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
// this.messageRepositoryService.sendMessageDelete()
this.chatServiceService.messageDelete({
messageId: message.id,
roomId: this.roomId,
roomId: this.room.id,
})
}
@@ -1243,17 +1231,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if(fileBase64.isOk()) {
const message = new MessageViewModal();
message.roomId = this.roomId
message.sentAt = new Date().toISOString()
message.sender = {
userPhoto: '',
wxeMail: SessionStore.user.Email,
wxFullName: SessionStore.user.FullName,
wxUserId: SessionStore.user.UserId
}
const message = this.createMessage();
message.attachments = [{
file: fileBase64.value.split(',')[1],
@@ -1268,14 +1246,15 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if(!this.date[date]) {
this.date[date] = true
const Ballon = XBallon(message)
this.messages1[this.roomId].push(Ballon)
this.messages1[this.room.$id].push(Ballon)
}
this.messages1[this.roomId].push(message)
this.messages1[this.room.$id].push(message)
setTimeout(() => {
this.scrollToBottomClicked()
}, 100)
this.chatServiceService.sendMessage(message, this.roomType)
let sendMessage = this.chatServiceService.sendMessage(message, this.roomType)
this.messageResult(sendMessage)
}
} else {
@@ -1286,7 +1265,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
async _openChatOptions() {
const roomId = this.roomId;
const roomId = this.room.$id;
const enterAnimation = (baseEl: any) => {
@@ -1318,7 +1297,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
component: ChatOptionsFeaturesPage,
cssClass: 'model profile-modal search-submodal',
componentProps: {
roomId: this.roomId,
roomId: this.room.$id,
members: [],
}
});
@@ -1330,7 +1309,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
if (res['data'] == 'meeting') {
//this.closeAllDesktopComponents.emit();
let data = {
roomId: this.roomId,
roomId: this.room.$id,
members: []
}
this.openNewEventPage.emit(data);
@@ -27,6 +27,6 @@
<button *ngIf="isAdmin && roomType == EnumRoomType.Group" (click)="setRoomOwner()" class="btn-cancel" shape="round">Adicionar admin</button>
<div class="solid"></div>
<button (click)="close('cancel')" full class="btn-cancel mobile-only" shape="round">Cancelar</button>
<button *ngIf="isAdmin && roomType == EnumRoomType.Group" (click)="deleteGroup()" class="btn-delete" shape="round">Apagar grupo</button>
<button (click)="deleteGroup()" class="btn-delete" shape="round">Apagar grupo</button>
</div>
</ion-content>
@@ -1,24 +0,0 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';
import { ViewOncesPage } from './view-onces.page';
describe('ViewOncesPage', () => {
let component: ViewOncesPage;
let fixture: ComponentFixture<ViewOncesPage>;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [ ViewOncesPage ],
imports: [IonicModule.forRoot()]
}).compileComponents();
fixture = TestBed.createComponent(ViewOncesPage);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});
View File
+1
View File
@@ -35,6 +35,7 @@ export class MessageViewModal {
$id?: string
id?: string
roomId?: string
$roomId: string
receiverId?: number
message?: string
canEdit: boolean = false
+4 -1
View File
@@ -4,7 +4,8 @@ import { isDocument } from "src/app/utils/document-mimetype";
export class RoomViewModel implements IRoom {
id: typeof RoomEntitySchema._input.id
$id: typeof RoomEntitySchema._input.$id
id?: typeof RoomEntitySchema._input.id
roomName: typeof RoomEntitySchema._input.roomName
createdBy: typeof RoomEntitySchema._input.createdBy
createdAt: typeof RoomEntitySchema._input.createdAt
@@ -12,6 +13,8 @@ export class RoomViewModel implements IRoom {
roomType: typeof RoomEntitySchema._input.roomType
members: typeof RoomEntitySchema._input.members
messages: typeof RoomEntitySchema._input.messages
local: typeof RoomEntitySchema._input.local
receiverId?: typeof RoomEntitySchema._input.receiverId
displayDate = ''
lastMessageImage = false
lastMessageDocument = false