Files
doneit-web/src/app/services/chat/room.service.ts
T

1291 lines
32 KiB
TypeScript
Raw Normal View History

2022-01-26 17:28:55 +01:00
import { Injectable } from '@angular/core';
2022-09-30 15:13:36 +01:00
import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service';
2023-09-11 21:57:14 +01:00
import { MessageService, MessageServiceDateLabel } from 'src/app/services/chat/message.service';
2022-01-26 17:28:55 +01:00
import { showDateDuration } from 'src/plugin/showDateDuration';
2022-02-08 17:44:15 +01:00
import { chatHistory } from 'src/app/models/chatMethod';
2022-01-19 09:12:30 +01:00
import { Storage } from '@ionic/storage';
2022-01-21 16:55:05 +01:00
import { Platform } from '@ionic/angular';
import { SqliteService } from 'src/app/services/sqlite.service';
2022-01-26 17:28:55 +01:00
import { NativeNotificationService } from 'src/app/services/native-notification.service';
import { SessionStore } from 'src/app/store/session.service';
2022-01-28 16:15:20 +01:00
import { capitalizeTxt } from 'src/plugin/text'
2022-01-28 15:59:01 +01:00
import { SortService } from '../functions/sort.service';
2022-01-29 19:21:46 +01:00
import { chatUser } from 'src/app/models/chatMethod';
2022-02-02 20:16:12 +01:00
import { environment } from 'src/environments/environment';
2022-02-03 21:01:53 +01:00
import { ChatService } from 'src/app/services/chat.service';
2022-02-10 14:56:06 +01:00
import { NfService } from 'src/app/services/chat/nf.service';
import { v4 as uuidv4 } from 'uuid'
2022-06-08 16:09:40 +01:00
import { ChatMethodsService } from './chat-methods.service';
import { DeleteMessageModel, MessageModel } from '../../models/beast-orm';
import { AESEncrypt } from '../aesencrypt.service';
2022-03-14 14:16:23 +01:00
import { IncomingChatMessage, ChatMessageInterface, falseTypingMethod } from 'src/app/models/message.model';
2022-03-22 16:11:30 +01:00
import { AttachmentsService } from 'src/app/services/attachments.service';
2023-10-19 15:03:12 +01:00
import { ConnectionStatus, NetworkServiceService } from 'src/app/services/network-service.service';
2022-09-30 15:13:36 +01:00
import { ChatSystemService } from './chat-system.service';
2023-01-05 12:11:50 +01:00
import { ViewedMessageService } from './viewed-message.service'
2023-01-09 10:49:58 +01:00
import * as FIFOProcessQueue from 'fifo-process-queue';
2023-02-02 18:24:26 +01:00
import { NotificationsService } from '../notifications.service';
2022-03-10 23:08:29 +01:00
2022-01-10 13:31:15 +01:00
@Injectable({
providedIn: 'root'
})
export class RoomService {
2022-01-28 15:59:01 +01:00
messages: MessageService[] = []
2022-01-20 14:31:49 +01:00
storageMessage: any[] = [];
2022-01-11 13:03:43 +01:00
lastMessage: MessageService;
2023-02-06 17:54:06 +01:00
messagesBeforeOfflineAPI: MessageService[] = []
2022-02-09 18:18:27 +01:00
2022-01-20 14:31:49 +01:00
customFields: any;
2022-01-11 13:03:43 +01:00
id = ''
2022-01-17 11:27:25 +01:00
t = ''
2022-01-13 21:24:43 +01:00
name = ''
2022-01-11 20:56:21 +01:00
_updatedAt = {}
2022-02-10 14:07:16 +01:00
hasLoadHistory = false
2023-09-29 16:40:50 +01:00
DoneLoadingHistory = false
2022-03-03 22:57:33 +01:00
restoreFromOffline = false
2022-01-20 14:31:49 +01:00
duration = ''
2022-01-28 16:15:20 +01:00
isTyping = false
otherUserType = false
lastTimeType = null
message = ''
lastMessageTxt = ''
userThatIsTyping = ''
2023-01-09 10:49:58 +01:00
messagesLocalReference: string[] = []
members: chatUser[] = []
membersExcludeMe: chatUser[] = []
2022-03-03 22:57:33 +01:00
u
2022-04-18 15:13:38 +01:00
sessionStore = SessionStore
2022-06-08 16:09:40 +01:00
countDownTime = ''
2023-10-19 15:03:12 +01:00
chatOpen = false
2022-07-13 16:08:57 +01:00
messageUnread = false
2022-02-10 18:07:06 +01:00
2022-09-26 18:15:41 +01:00
status = {
receive: {
message: false,
typing: false,
readMessage: false,
deleteMessage: false
}
}
2022-10-03 11:02:32 +01:00
isGroup: boolean
2022-09-30 16:55:09 +01:00
subscribeAttempt = false
2022-01-20 14:31:49 +01:00
scrollDown = () => { }
2022-01-29 20:16:39 +01:00
/**
* @description get user list from ws-chat-methods.service
* @returns chatUser[]
*/
2023-10-19 15:03:12 +01:00
getAllUsers = (): chatUser[] => {
2022-01-29 19:21:46 +01:00
return []
}
2022-01-14 14:50:47 +01:00
2023-10-19 15:03:12 +01:00
sortRoomList = () => { }
chatServiceDeleteRoom = (roomId) => { }
2022-02-02 20:16:12 +01:00
2022-01-10 23:52:33 +01:00
constructor(
2022-09-30 15:13:36 +01:00
public RochetChatConnectorService: RochetChatConnectorService,
2022-01-11 20:56:21 +01:00
private MessageService: MessageService,
2022-01-19 09:12:30 +01:00
private storage: Storage,
2022-01-21 16:55:05 +01:00
private platform: Platform,
private sqlservice: SqliteService,
2022-01-28 16:26:21 +01:00
private NativeNotificationService: NativeNotificationService,
2022-01-28 17:34:27 +01:00
private sortService: SortService,
2022-02-03 21:01:53 +01:00
private chatService: ChatService,
2022-03-03 22:57:33 +01:00
private NfService: NfService,
2022-03-10 23:08:29 +01:00
private ChatMethodsService: ChatMethodsService,
2022-03-22 16:11:30 +01:00
private AESEncrypt: AESEncrypt,
2022-03-29 16:48:24 +01:00
private AttachmentsService: AttachmentsService,
2022-06-08 16:09:40 +01:00
private NetworkServiceService: NetworkServiceService,
2023-01-05 12:11:50 +01:00
private ChatSystemService: ChatSystemService,
2023-02-02 18:24:26 +01:00
private ViewedMessageService: ViewedMessageService,
private notificationService: NotificationsService
2022-01-28 16:37:18 +01:00
) {
/* this.NativeNotificationService.askForPermission() */
2022-03-03 22:57:33 +01:00
2022-09-30 15:13:36 +01:00
this.RochetChatConnectorService.getUserStatus((d) => {
2022-03-03 22:57:33 +01:00
const userId = d.fields.args[0][0]
2022-03-18 12:32:21 +01:00
const statusNum = d.fields.args[0][2]
const statusText = this.statusNumberToText(statusNum)
2022-03-03 22:57:33 +01:00
2023-01-05 12:11:50 +01:00
this.ViewedMessageService.request(this, userId, statusNum, statusText)
2022-03-03 22:57:33 +01:00
})
2022-09-30 15:13:36 +01:00
this.RochetChatConnectorService.registerCallback({
2022-09-26 18:15:41 +01:00
type: 'Offline',
funx: () => {
/**
* @description when the phone is in the background for a long time it could disconnects from the socket then the socket reconnects automatically,
* when the connection is lost the subscribe is also lost, so we have to subscribe again when reconnection is establish.
*/
this.resetStatus();
2022-10-04 12:08:35 +01:00
this.hasLoadHistory = false
this.subscribeAttempt = false
2022-09-26 18:15:41 +01:00
}
2023-02-08 16:06:14 +01:00
});
2022-09-26 18:15:41 +01:00
2022-01-26 17:28:55 +01:00
}
2022-01-10 23:52:33 +01:00
2023-10-19 15:03:12 +01:00
/**
* @description convert rocketchat statues num to readable string
* @param text
* @returns
*/
2022-03-18 12:32:21 +01:00
statusNumberToText(text) {
2023-10-19 15:03:12 +01:00
if (text == '0') {
2022-03-18 12:32:21 +01:00
return "offline"
}
2023-10-19 15:03:12 +01:00
else if (text == '1') {
2022-03-18 12:32:21 +01:00
return "online"
}
2023-10-19 15:03:12 +01:00
else if (text == '2') {
2022-03-18 12:32:21 +01:00
return "away"
}
2023-10-19 15:03:12 +01:00
else if (text == '3') {
2022-03-18 12:32:21 +01:00
return "busy"
}
}
2022-09-26 18:15:41 +01:00
resetStatus() {
this.status = {
receive: {
message: false,
typing: false,
readMessage: false,
deleteMessage: false
}
}
}
2023-10-19 15:03:12 +01:00
setDataOnce = false
setData({ membersExcludeMe, members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this.ChatSystemService, this.notificationService), _updatedAt }) {
2022-01-13 21:24:43 +01:00
this.customFields = customFields
2022-01-11 13:03:43 +01:00
this.id = id
this.name = name
2022-01-17 11:27:25 +01:00
this.t = t
2022-01-11 13:03:43 +01:00
this.lastMessage = lastMessage
2022-01-11 20:56:21 +01:00
this._updatedAt = _updatedAt
2022-03-03 22:57:33 +01:00
this.u = u
2023-02-06 18:48:50 +01:00
this.calDateDuration();
2022-06-08 16:09:40 +01:00
2023-02-08 16:06:14 +01:00
2023-10-19 15:03:12 +01:00
if (this.customFields?.countDownDate) {
2022-06-08 16:09:40 +01:00
this.countDownDate(this.customFields.countDownDate);
}
2023-10-19 15:03:12 +01:00
if (this.setDataOnce == false) {
this.setDataOnce = true
this.updateContacts()
}
2022-06-08 16:09:40 +01:00
}
2022-10-12 11:38:04 +01:00
updateInfo() {
// this.chatService.getRoomInfo(this.id).toPromise();
}
2022-10-04 11:33:46 +01:00
get online() {
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
if (!this.isGroup) {
for (let user of this.ChatSystemService.users) {
2023-01-09 10:49:58 +01:00
for (const members of this.membersExcludeMe) {
2023-10-19 15:03:12 +01:00
if (members._id == user._id) {
2023-01-09 10:49:58 +01:00
return user.status
}
2022-10-04 11:33:46 +01:00
}
2023-09-11 21:57:14 +01:00
2022-10-04 11:33:46 +01:00
}
}
return 'offline'
}
2022-06-08 16:09:40 +01:00
countDownDate(date) {
let difference = new Date(date).getTime() - new Date().getTime();
2023-10-19 15:03:12 +01:00
let c_day = Math.floor(difference / (1000 * 60 * 60 * 24));
let c_hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let c_minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
let c_seconds = Math.floor((difference % (1000 * 60)) / 1000);
2022-06-08 16:09:40 +01:00
2023-10-19 15:03:12 +01:00
this.countDownTime = this.addZero(c_day) + " : " + this.addZero(c_hours) + " : " + this.addZero(c_minutes) + " : " + this.addZero(c_seconds);
2022-06-08 16:09:40 +01:00
2023-10-19 15:03:12 +01:00
if (difference < 0) {
2022-06-08 16:09:40 +01:00
2022-07-18 15:46:24 +01:00
this.deleteRoom();
2022-06-08 16:09:40 +01:00
} else {
setTimeout(() => {
this.countDownDate(date)
}, 1000)
}
}
addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
deleteRoom() {
this.countDownTime = "Expired";
let body = { "roomId": this.id }
2022-09-30 14:43:10 +01:00
// this.chatService.getRoomInfo(this.id).subscribe( room =>{});
2022-06-08 16:09:40 +01:00
2023-10-19 15:03:12 +01:00
if (this.t === 'p') {
this.chatService.deleteGroup(body).subscribe(res => {
2022-09-30 15:13:36 +01:00
this.ChatSystemService.deleteRoom(this.id);
this.ChatSystemService.getAllRooms();
2022-09-30 14:43:10 +01:00
this.chatServiceDeleteRoom(this.id);
2024-03-03 10:27:18 +01:00
}, (error)=> {
if(error.status != 0) {
this.ChatSystemService.deleteRoom(this.id);
this.ChatSystemService.getAllRooms();
this.chatServiceDeleteRoom(this.id);
}
2022-09-30 14:43:10 +01:00
});
}
else {
2023-10-19 15:03:12 +01:00
this.chatService.deleteChannel(body).subscribe(res => {
2022-09-30 15:13:36 +01:00
this.ChatSystemService.deleteRoom(this.id);
this.ChatSystemService.getAllRooms();
2022-09-30 14:43:10 +01:00
this.chatServiceDeleteRoom(this.id);
2024-03-03 10:27:18 +01:00
}, (error)=> {
if(error.status != 0) {
this.ChatSystemService.deleteRoom(this.id);
this.ChatSystemService.getAllRooms();
this.chatServiceDeleteRoom(this.id);
}
2022-09-30 14:43:10 +01:00
});
}
2022-01-10 23:52:33 +01:00
}
2022-02-03 15:52:21 +01:00
isSenderIsNotMe(ChatMessage) {
2022-04-19 16:03:59 +01:00
return SessionStore.user.UserName != ChatMessage.u.username
2022-02-03 15:52:21 +01:00
}
2022-03-03 22:57:33 +01:00
senderId(ChatMessage) {
return ChatMessage.u._id
}
2022-01-11 15:43:09 +01:00
receiveMessage() {
2022-01-14 11:47:55 +01:00
2022-09-30 15:13:36 +01:00
this.RochetChatConnectorService.updateRoomEventss(
2022-01-14 11:47:55 +01:00
this.id,
2022-01-28 15:31:52 +01:00
"stream-room-messages",
2023-10-19 15:03:12 +01:00
async (IncomingChatMessage: IncomingChatMessage) => {
2022-02-10 20:16:59 +01:00
2023-01-09 10:49:58 +01:00
this.appendReceiveMessage.push(IncomingChatMessage)
2022-02-09 14:07:34 +01:00
2022-01-14 11:47:55 +01:00
}
)
2022-01-28 16:15:20 +01:00
2022-09-30 15:13:36 +01:00
this.RochetChatConnectorService.receiveStreamNotifyRoom((message) => {
2022-01-28 16:15:20 +01:00
2023-10-19 15:03:12 +01:00
if (message.fields.eventName == this.id + '/' + 'typing') {
2022-01-28 16:15:20 +01:00
2022-03-03 22:57:33 +01:00
const args = message.fields.args
2023-01-24 15:56:47 +01:00
2022-03-18 12:32:21 +01:00
if (typeof args[1] != 'object') {
2022-03-03 22:57:33 +01:00
this.userThatIsTyping = this.usernameToDisplayName(args[0])
2023-09-11 21:57:14 +01:00
2023-01-24 15:56:47 +01:00
const user = args[0]
2023-10-19 15:03:12 +01:00
if (SessionStore.user.UserName != user) {
2023-01-24 15:56:47 +01:00
this.readAllMessage()
2024-02-29 15:56:21 +01:00
this.isTyping = args[1]
this.otherUserType = args[1]
2023-01-24 15:56:47 +01:00
}
2022-07-26 14:11:12 +01:00
2023-10-19 15:03:12 +01:00
} else if (args[0]?.method == 'viewMessage' || args[1]?.method == 'viewMessage') {
2023-01-24 15:56:47 +01:00
const user = args[0]
2023-10-19 15:03:12 +01:00
if (SessionStore.user.UserName != user) {
2023-01-24 15:56:47 +01:00
this.readAllMessage()
}
2023-10-19 15:03:12 +01:00
} else if (args[0]?.method == 'deleteMessage' || args[1]?.method == 'deleteMessage') {
2023-09-11 21:57:14 +01:00
2022-03-23 23:00:19 +01:00
this.deleteMessage(args[1]?.method?._id)
2023-09-11 21:57:14 +01:00
2022-03-18 12:32:21 +01:00
} else {
2022-07-26 14:11:12 +01:00
2022-02-10 14:56:06 +01:00
}
2022-02-16 15:52:59 +01:00
2023-10-19 15:03:12 +01:00
} else if (message.fields.eventName == this.id + '/' + 'deleteMessage') { }
2022-02-16 15:52:59 +01:00
2022-03-03 22:57:33 +01:00
})
2022-02-10 14:56:06 +01:00
}
2023-10-19 15:03:12 +01:00
appendReceiveMessage = FIFOProcessQueue(async (IncomingChatMessage: IncomingChatMessage, done) => {
2023-01-09 10:49:58 +01:00
let IncomingChatMessageArgs = IncomingChatMessage.fields.args[0]
2023-10-19 15:03:12 +01:00
let ChatMessage: ChatMessageInterface = this.fix_updatedAt(IncomingChatMessageArgs)
2023-01-09 10:49:58 +01:00
2023-09-11 21:57:14 +01:00
let found = this.findMessageBy_id(ChatMessage._id) ||
2023-10-19 15:03:12 +01:00
this.findMessageBy_localReference(ChatMessage?.localReference)
2023-01-09 10:49:58 +01:00
// || await this.findMessageInDBByData({_id:ChatMessage._id, localReference:ChatMessage.localReference })
2023-10-19 15:03:12 +01:00
if (!found) {
2023-01-09 10:49:58 +01:00
2023-09-11 21:57:14 +01:00
ChatMessage.origin = 'stream'
2023-10-19 15:03:12 +01:00
const message = await this.prepareCreate({ message: ChatMessage, save: false });
2023-01-09 10:49:58 +01:00
this.registerSendMessage(message)
message.from = 'stream'
2023-09-29 16:40:50 +01:00
message.loadHistory = this.DoneLoadingHistory
2023-01-09 10:49:58 +01:00
this.lastMessage = message;
2024-01-30 14:09:30 +01:00
try {
this.calDateDuration(ChatMessage._updatedAt);
} catch (error) {
console.log(error)
}
2023-01-09 10:49:58 +01:00
if (message.t == 'r') {
this.name = message.msg;
}
2024-01-30 14:09:30 +01:00
//if (this.isSenderIsNotMe(ChatMessage)) {
/* this.NativeNotificationService.sendNotificationChat({
2023-01-09 10:49:58 +01:00
message: message.msg,
title: this.name
}); */
2024-01-30 14:09:30 +01:00
//}
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
if (this.hasLoadHistory == true) {
2023-02-08 16:06:14 +01:00
this.messages.push(message)
2024-01-30 14:09:30 +01:00
message.addMessageDB()
2023-02-08 16:06:14 +01:00
} else {
this.messagesBeforeOfflineAPI.push(message)
2023-01-09 10:49:58 +01:00
}
2023-09-11 21:57:14 +01:00
this.messageUnread = true
2023-01-30 16:41:20 +01:00
setTimeout(() => {
2023-01-09 10:49:58 +01:00
this.scrollDown()
2023-09-11 21:57:14 +01:00
}, 50)
2023-01-09 10:49:58 +01:00
}
2023-10-19 15:03:12 +01:00
if (ChatMessage?.t == "au" || ChatMessage?.t == "ru" || ChatMessage?.t == "ul") {
2023-08-08 09:43:26 +01:00
this.updateContacts()
} else if (ChatMessage?.t == "r") {
this.name = ChatMessage.msg
}
2024-03-18 11:42:36 +01:00
this.sortRoomList()
2024-03-11 10:23:33 +01:00
// this.changeDetector()
2023-01-09 10:49:58 +01:00
setTimeout(() => {
done()
}, 5)
})
2024-03-01 14:42:16 +01:00
async info() {
// set unread messages
const response: any = await this.chatService.GetSubscriptionRoomUnreadM(this.id).toPromise()
if(response?.subscription?.unread >= 1) {
this.messageUnread = true
}
}
2023-01-09 10:49:58 +01:00
getUsersByStatus(status: 'offline' | 'online') {
return this.getAllUsers().filter((user => {
2023-10-19 15:03:12 +01:00
for (const member of this.membersExcludeMe) {
if (user._id == member._id && user.status == status) {
2023-01-09 10:49:58 +01:00
return true
}
}
}))
}
2022-02-02 20:16:12 +01:00
2022-03-10 23:08:29 +01:00
getRoomMembersIds(): string[] {
2022-03-26 08:52:33 +01:00
try {
2023-10-19 15:03:12 +01:00
return this.membersExcludeMe.map((user) => user._id)
} catch (error) {
2022-03-26 08:52:33 +01:00
return []
}
2023-09-11 21:57:14 +01:00
2022-03-10 23:08:29 +01:00
}
getAllMemberThatIsNotOffline(): string[] {
const membersIds = this.getRoomMembersIds()
const allChatUsers = this.getAllUsers()
const AllMemberThatIsNotOffline = []
2023-10-19 15:03:12 +01:00
for (let user of allChatUsers) {
if (membersIds.includes(user._id)) {
2022-03-10 23:08:29 +01:00
2023-10-19 15:03:12 +01:00
if (user.status != 'offline') {
2022-03-10 23:08:29 +01:00
AllMemberThatIsNotOffline.push(user._id)
}
}
}
return AllMemberThatIsNotOffline
}
2022-03-15 15:49:59 +01:00
getAllMemberThatIsOffline(): string[] {
const membersIds = this.getRoomMembersIds()
const allChatUsers = this.getAllUsers()
const AllMemberThatIsNotOffline = []
2023-10-19 15:03:12 +01:00
for (let user of allChatUsers) {
if (membersIds.includes(user._id)) {
2022-03-15 15:49:59 +01:00
2023-10-19 15:03:12 +01:00
if (user.status == 'offline') {
2022-03-15 15:49:59 +01:00
AllMemberThatIsNotOffline.push(user._id)
}
}
}
return AllMemberThatIsNotOffline
}
async deleteMessageToReceive(userId) {
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
const allDeleteMessages = await DeleteMessageModel.filter({ rid: this.id }).execute()
2022-03-18 12:32:21 +01:00
2023-10-19 15:03:12 +01:00
for (let message_ of allDeleteMessages) {
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
if (message_.needToReceiveBy.includes(userId)) {
2022-03-15 15:49:59 +01:00
2023-10-19 15:03:12 +01:00
message_.needToReceiveBy = message_.needToReceiveBy.filter((e) => e != userId)
this.sendFalseTypingReadMessage('deleteMessage', { _id: message_.messageId })
2022-03-15 15:49:59 +01:00
2023-10-19 15:03:12 +01:00
if (message_.needToReceiveBy.length == 0) {
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
const deleteMessage = await DeleteMessageModel.get({ messageId: message_.messageId })
2022-03-18 12:32:21 +01:00
await deleteMessage.delete()
2022-03-15 15:49:59 +01:00
2022-03-18 12:32:21 +01:00
} else {
await DeleteMessageModel.update(message_)
}
2022-03-15 15:49:59 +01:00
}
2023-09-11 21:57:14 +01:00
2022-03-15 15:49:59 +01:00
}
2023-09-11 21:57:14 +01:00
}
2022-03-15 15:49:59 +01:00
2022-01-28 15:31:52 +01:00
async receiveMessageDelete() {
2022-09-30 15:13:36 +01:00
this.RochetChatConnectorService.updateRoomEventss(
2022-01-28 15:31:52 +01:00
this.id,
"stream-notify-room",
async (ChatMessage) => {
2022-01-30 09:34:56 +01:00
const DeletedMessageId = ChatMessage.fields.args[0]._id;
2023-01-24 15:56:47 +01:00
const message = this.messages.find((e) => e._id == DeletedMessageId)
2023-10-19 15:03:12 +01:00
if (message) {
if (message.delate == false) {
2023-09-28 16:25:13 +01:00
this.deleteMessage(DeletedMessageId)
}
} else {
2023-10-19 15:03:12 +01:00
const message = await MessageModel.get({ _id: DeletedMessageId })
if (message) {
2023-09-28 16:25:13 +01:00
(message as MessageModel).delete()
DeleteMessageModel.create({
messageId: DeletedMessageId,
rid: this.id,
ts: "",
u: {},
needToReceiveBy: []
})
}
2023-01-24 15:56:47 +01:00
}
2023-09-11 21:57:14 +01:00
2023-09-28 16:25:13 +01:00
2022-01-28 15:31:52 +01:00
}
)
2022-01-11 15:43:09 +01:00
}
2022-01-30 09:34:56 +01:00
/**
* @description delete message in the view
* @param id message ID
*/
2023-10-19 15:03:12 +01:00
async deleteMessage(_id) {
2023-09-11 21:57:14 +01:00
2022-03-22 14:23:38 +01:00
const id = _id
2023-10-19 15:03:12 +01:00
for (let i = 0; i <= this.messages.length; i++) {
2022-03-22 14:23:38 +01:00
2023-10-19 15:03:12 +01:00
if (this.messages[i]?._id == id) {
2022-03-22 14:23:38 +01:00
2023-09-11 21:57:14 +01:00
2022-04-19 16:03:59 +01:00
if (SessionStore.user.UserName == this.messages[i]?.u?.username) {
2022-03-23 22:01:59 +01:00
const allMemberThatIsOffline = this.getAllMemberThatIsOffline()
2023-09-11 21:57:14 +01:00
2022-03-25 14:32:15 +01:00
DeleteMessageModel.create({
2022-03-23 22:01:59 +01:00
messageId: this.messages[i]._id,
rid: this.messages[i].rid,
ts: this.messages[i].ts,
u: this.messages[i].u,
needToReceiveBy: allMemberThatIsOffline
})
}
2023-09-11 21:57:14 +01:00
2022-03-25 17:39:51 +01:00
this.messages[i]?.delateDB()
this.messages.splice(i, 1)
2022-03-23 22:01:59 +01:00
2022-03-29 16:48:24 +01:00
//Get previous last message from room
const previousLastMessage = this.messages.slice(-1)[0];
2022-09-26 18:15:41 +01:00
2023-10-19 15:03:12 +01:00
if (previousLastMessage.dateLabel == false) {
if (previousLastMessage) {
2023-09-12 20:58:22 +01:00
this.lastMessage = previousLastMessage;
this.calDateDuration(previousLastMessage._updatedAt)
this.sortRoomList()
}
} else {
const previousLastMessage = this.messages.slice(-2)[0];
2023-10-19 15:03:12 +01:00
if (previousLastMessage) {
2023-09-12 20:58:22 +01:00
this.lastMessage = previousLastMessage;
this.calDateDuration(previousLastMessage._updatedAt)
this.sortRoomList()
}
2022-09-26 18:15:41 +01:00
}
2022-03-29 16:48:24 +01:00
2023-09-12 20:58:22 +01:00
2022-03-23 22:01:59 +01:00
return true
2022-01-30 09:34:56 +01:00
2022-03-23 22:01:59 +01:00
} else {
2023-09-11 21:57:14 +01:00
//
2022-01-30 09:34:56 +01:00
}
2022-03-22 14:23:38 +01:00
}
2022-03-03 22:57:33 +01:00
2022-01-30 09:34:56 +01:00
}
2022-03-21 21:06:54 +01:00
2023-10-19 15:03:12 +01:00
/**
* @description delete message in the view
* @param id message ID
*/
2023-09-29 16:40:50 +01:00
async deleteMessageFromArray(_id) {
const id = _id
2023-10-19 15:03:12 +01:00
for (let i = 0; i <= this.messages.length; i++) {
2023-09-29 16:40:50 +01:00
2023-10-19 15:03:12 +01:00
if (this.messages[i]?._id == id) {
2023-09-29 16:40:50 +01:00
this.messages.splice(i, 1)
return true
} else {
//
}
}
}
2022-03-21 21:06:54 +01:00
deleteAll() {
2022-07-21 18:05:29 +01:00
this.messages.forEach((message) => {
2023-10-19 15:03:12 +01:00
if (message?._id) {
2022-03-21 21:06:54 +01:00
this.sendDeleteRequest(message._id)
}
})
}
2022-03-15 15:49:59 +01:00
async delateMessageToSendToOthers(userId) {
2023-09-11 21:57:14 +01:00
2022-07-21 18:05:29 +01:00
const deleteMessage = await DeleteMessageModel.all();
2022-03-14 14:16:23 +01:00
2023-10-19 15:03:12 +01:00
const toSend = deleteMessage.filter((DeleteMessage: string[]) => !DeleteMessage.includes(userId))
2022-03-14 14:16:23 +01:00
}
2022-03-15 15:49:59 +01:00
async sendDeleteRequest(msgId) {
2023-10-19 15:03:12 +01:00
const message = this.messages.find((e) => e._id == msgId)
2022-03-23 23:01:03 +01:00
await message.delateStatusFalse()
2022-03-15 15:49:59 +01:00
2023-10-19 15:03:12 +01:00
if (this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Online) {
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
this.RochetChatConnectorService.deleteMessage(msgId).then(async () => {
2022-03-15 15:49:59 +01:00
message.delateRequest = true
2023-01-09 10:49:58 +01:00
await message.saveChanges();
2022-07-18 15:46:24 +01:00
this.deleteMessage(msgId);
2022-03-29 16:48:24 +01:00
})
2022-03-23 22:01:59 +01:00
2022-03-29 16:48:24 +01:00
} else {
2022-09-30 15:13:36 +01:00
this.RochetChatConnectorService.registerCallback({
2022-03-29 16:48:24 +01:00
type: 'reConnect',
2023-10-19 15:03:12 +01:00
funx: async () => {
2023-09-11 21:57:14 +01:00
2022-03-29 16:48:24 +01:00
this.sendDeleteRequest(msgId)
return true
2022-03-18 12:32:21 +01:00
}
2022-03-29 16:48:24 +01:00
})
}
2022-03-21 21:06:54 +01:00
2022-03-15 15:49:59 +01:00
}
2022-01-30 09:34:56 +01:00
/**
* @description sen text message
*/
2023-10-19 15:03:12 +01:00
async send({ file = null, attachments = null, temporaryData = {}, attachmentsModelData = {} }) {
2022-02-02 20:16:12 +01:00
2023-10-19 15:03:12 +01:00
this.message = this.message.replace(/(\n$)/, '')
2023-09-28 11:12:39 +01:00
2023-10-19 15:03:12 +01:00
if (file && this.message) {
2023-08-11 17:02:51 +01:00
this.send({})
}
2023-09-11 21:57:14 +01:00
2022-07-15 10:06:55 +01:00
const localReference = uuidv4();
2022-02-02 20:16:12 +01:00
2022-02-03 21:01:53 +01:00
let offlineChatMessage = {
rid: this.id,
msg: this.message,
attachments,
file,
2022-02-10 14:56:06 +01:00
temporaryData,
2023-01-24 15:56:47 +01:00
localReference,
2023-06-29 16:04:44 +01:00
origin: 'local',
attachmentsModelData
2022-02-03 21:01:53 +01:00
}
2022-03-04 14:50:13 +01:00
2023-06-29 16:04:44 +01:00
2023-09-29 16:40:50 +01:00
//console.log('offlineChatMessage', offlineChatMessage)
2023-06-29 16:04:44 +01:00
2023-10-19 15:03:12 +01:00
this.message = ''
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
this.hojeLabel({ ...offlineChatMessage })
2023-09-12 09:52:28 +01:00
2023-10-19 15:03:12 +01:00
const message: MessageService = await this.prepareCreate({ message: offlineChatMessage, save: environment.chatOffline })
2023-01-09 10:49:58 +01:00
this.registerSendMessage(message)
2022-02-07 20:18:48 +01:00
2023-10-19 15:03:12 +01:00
if (this.hasLoadHistory == true) {
2022-09-29 12:17:00 +01:00
await message.addMessageDB()
}
2023-09-11 21:57:14 +01:00
2022-03-03 22:57:33 +01:00
message.send()
2022-09-29 16:43:32 +01:00
message.from = 'send'
message.loadHistory = this.hasLoadHistory
2022-02-04 00:22:35 +01:00
if (environment.chatOffline) {
2023-09-11 21:57:14 +01:00
2022-02-10 14:07:16 +01:00
setTimeout(() => {
this.scrollDown()
}, 150)
this.lastMessage = message
this.calDateDuration(message._updatedAt)
this.sortRoomList()
}
2022-02-03 21:01:53 +01:00
}
2022-02-16 15:52:59 +01:00
2023-01-09 10:49:58 +01:00
/**
* Register all send message so that
* the incoming message wont be confuse to
* other user the localReference is the identifier
*/
registerSendMessage(message: MessageService) {
this.messagesLocalReference.push(message.localReference)
}
localReferenceExist(message: MessageService) {
2023-10-19 15:03:12 +01:00
for (const localReference of this.messagesLocalReference) {
if (localReference == message?.localReference) {
2023-01-09 10:49:58 +01:00
return true
}
}
return false
}
2022-01-28 16:15:20 +01:00
2023-10-19 15:03:12 +01:00
sendTyping(text: string = this.message) {
2022-01-28 16:15:20 +01:00
2023-10-19 15:03:12 +01:00
if (this.lastMessageTxt == text) { return false }
2022-01-28 16:15:20 +01:00
this.lastTimeType = new Date().getTime()
const lastIsTyping = this.isTyping
2023-10-19 15:03:12 +01:00
if (text.length >= 1) {
2022-01-28 17:34:27 +01:00
this.isTyping = true
2022-01-28 16:15:20 +01:00
} else {
this.isTyping = false
}
2023-10-19 15:03:12 +01:00
if (lastIsTyping != this.isTyping) {
2022-12-20 17:06:19 +01:00
this.RochetChatConnectorService.sendStreamNotifyRoom(this.id, SessionStore.user.UserName, 'typing', this.isTyping).catch((error) => console.error(error));
2022-01-28 16:15:20 +01:00
}
2022-01-28 17:34:27 +01:00
2022-01-28 16:15:20 +01:00
this.lastMessageTxt = this.message
this.typingWatch()
}
2023-10-19 15:03:12 +01:00
sendFalseTypingReadMessage(method, param: object) {
this.RochetChatConnectorService.sendStreamNotifyRoom(this.id, SessionStore.user.UserName, 'typing', { method: method, params: param } as falseTypingMethod).catch((error) => console.error(error))
2022-03-03 22:57:33 +01:00
this.setTypingOff()
}
2022-01-28 16:15:20 +01:00
private typingWatch() {
2022-09-29 15:23:27 +01:00
setTimeout(() => {
2022-01-28 16:15:20 +01:00
const now = new Date().getTime()
2023-10-19 15:03:12 +01:00
if ((now - this.lastTimeType) >= 2888) {
2022-01-28 17:34:27 +01:00
2023-10-19 15:03:12 +01:00
if (this.isTyping == true) {
2022-01-28 16:15:20 +01:00
this.isTyping = false
2022-12-20 17:06:19 +01:00
this.RochetChatConnectorService.sendStreamNotifyRoom(this.id, SessionStore.user.UserName, 'typing', this.isTyping).catch((error) => console.error(error))
2022-01-28 16:15:20 +01:00
}
}
}, 3000)
2022-01-11 15:43:09 +01:00
}
2022-01-29 19:21:46 +01:00
private setTypingOff() {
2022-02-10 16:14:25 +01:00
this.sendTyping('')
2022-01-29 19:21:46 +01:00
}
roomLeave() {
this.setTypingOff()
2022-07-13 16:08:57 +01:00
this.chatOpen = false
2022-01-29 19:21:46 +01:00
}
open() {
// this.typing(this.message)
2022-07-13 16:08:57 +01:00
this.chatOpen = true
this.messageUnread = false
2023-01-24 15:56:47 +01:00
this.sendReadMessage()
2022-01-29 19:21:46 +01:00
}
2022-01-29 19:21:46 +01:00
2022-01-26 09:19:54 +01:00
leave(rid?) {
2022-12-20 17:06:19 +01:00
this.RochetChatConnectorService.leaveRoom(this.id).catch((error) => console.error(error))
2022-01-26 09:19:54 +01:00
}
2022-01-21 16:55:05 +01:00
isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return "";
}
return JSON.parse(str);
}
2023-09-11 21:14:00 +01:00
formatDateToDDMMYYYY(dateStamp) {
const date = new Date(dateStamp);
2023-09-19 10:21:23 +01:00
2023-09-11 21:14:00 +01:00
const day = date.getDate().toString().padStart(2, '0');
const month = (date.getMonth() + 1).toString().padStart(2, '0'); // Month is zero-based
const year = date.getFullYear();
2023-09-19 10:21:23 +01:00
2023-09-11 21:14:00 +01:00
return `${day}-${month}-${year}`;
}
2023-01-09 10:49:58 +01:00
2023-09-11 21:57:14 +01:00
sortArrayISODate(messages: any): any[] {
2023-10-19 15:03:12 +01:00
return messages.sort((a, b) =>
new Date(b._updatedAt).getTime()
2023-09-11 21:57:14 +01:00
-
new Date(a._updatedAt).getTime())
}
2023-01-09 10:49:58 +01:00
restoreOnce = false
2023-09-11 21:57:14 +01:00
labelDates = []
goshPush(ChatMessage) {
let currentDateMessage = new Date(ChatMessage._updatedAt).toLocaleDateString()
2023-10-19 15:03:12 +01:00
if (currentDateMessage) {
if (!this.labelDates.find(e => e == currentDateMessage)) {
2023-09-11 21:57:14 +01:00
this.labelDates.push(currentDateMessage)
2023-10-19 15:03:12 +01:00
const newMessage = this.fix_updatedAt({ ...ChatMessage })
2023-09-11 21:57:14 +01:00
newMessage.msg = currentDateMessage
const cloneMessage = new MessageServiceDateLabel()
cloneMessage.ChatSystemService = this.ChatSystemService
2023-10-19 15:03:12 +01:00
cloneMessage.setData({ ...newMessage } as any)
2023-09-11 21:57:14 +01:00
//
cloneMessage._id = ""
cloneMessage._updatedAt = ""
2023-09-12 09:52:28 +01:00
cloneMessage.duration = ""
2023-09-11 21:57:14 +01:00
cloneMessage.msg = currentDateMessage
cloneMessage.dateLabel = true
cloneMessage.delate = false
2023-09-22 16:16:35 +01:00
cloneMessage.file = undefined
cloneMessage.attachments = undefined
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
if (new Date().toLocaleDateString() == currentDateMessage) {
2023-09-11 21:57:14 +01:00
cloneMessage.msg = "hoje"
} else {
cloneMessage.msg = currentDateMessage
}
this.messages.push(cloneMessage as any)
2023-09-29 16:40:50 +01:00
// console.log(currentDateMessage)
2023-09-11 21:57:14 +01:00
}
}
}
2023-09-26 14:06:03 +01:00
async deleteMessageDate(MessageService: MessageService) {
let currentDateMessageToDelete = new Date(MessageService._updatedAt).toLocaleDateString()
2023-10-19 15:03:12 +01:00
for (let i = 0; i <= this.messages.length; i++) {
2023-09-26 14:06:03 +01:00
let currentDateMessage = new Date(this.messages[i]._updatedAt).toLocaleDateString()
2023-10-19 15:03:12 +01:00
if (currentDateMessageToDelete == currentDateMessage) {
2023-09-26 14:06:03 +01:00
return
}
}
2023-10-19 15:03:12 +01:00
for (let i = 0; i <= this.messages.length; i++) {
if (this.messages[i].dateLabel) {
if (this.messages[i].msg == currentDateMessageToDelete) {
2023-09-26 14:06:03 +01:00
this.messages.splice(i, 1)
}
}
}
}
2023-09-12 09:52:28 +01:00
hojeLabel(offlineChatMessage: any) {
let currentDateMessage = new Date().toLocaleDateString()
2023-10-19 15:03:12 +01:00
if (!this.labelDates.find(e => e == currentDateMessage)) {
2023-09-12 09:52:28 +01:00
2023-09-19 10:21:23 +01:00
this.labelDates.push(currentDateMessage)
2023-09-12 09:52:28 +01:00
const cloneMessage = new MessageServiceDateLabel()
cloneMessage.ChatSystemService = this.ChatSystemService
2023-10-19 15:03:12 +01:00
cloneMessage.setData({ ...offlineChatMessage } as any)
2023-09-12 09:52:28 +01:00
cloneMessage.msg = "hoje"
cloneMessage._id = ""
cloneMessage._updatedAt = ""
cloneMessage.duration = ""
cloneMessage.dateLabel = true
cloneMessage.delate = false
2023-09-28 16:25:13 +01:00
cloneMessage.attachments = undefined
cloneMessage.file = undefined
2023-09-12 09:52:28 +01:00
this.messages.push(cloneMessage as any)
}
}
2022-02-07 20:18:48 +01:00
async restoreMessageFromDB() {
2023-10-19 15:03:12 +01:00
if (environment.chatOffline && this.restoreOnce == false) {
2023-01-09 10:49:58 +01:00
this.restoreOnce = true
2022-02-02 20:16:12 +01:00
2023-10-19 15:03:12 +01:00
const messages = this.sortArrayISODate(await MessageModel.filter({ rid: this.id }).execute()).reverse()
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
for (let ChatMessage of messages) {
2023-09-11 21:57:14 +01:00
this.goshPush(ChatMessage)
2022-03-10 23:08:29 +01:00
const wewMessage = await this.simplePrepareMessage(ChatMessage)
2023-09-11 21:14:00 +01:00
2022-09-29 16:43:32 +01:00
wewMessage.from = 'Offline'
wewMessage.loadHistory = this.hasLoadHistory
2023-06-29 16:04:44 +01:00
wewMessage.messageModelInstance = ChatMessage
2022-03-03 22:57:33 +01:00
2023-10-19 15:03:12 +01:00
if (wewMessage.offline == false) {
2022-09-30 10:26:54 +01:00
2023-10-19 15:03:12 +01:00
const message = await this.prepareCreate({ message: ChatMessage })
2023-09-11 21:14:00 +01:00
2023-01-09 10:49:58 +01:00
message.from = 'Offline'
message.loadHistory = this.hasLoadHistory
2023-06-29 16:04:44 +01:00
wewMessage.messageModelInstance = ChatMessage
2023-01-09 10:49:58 +01:00
message?.decryptMessage()
2023-09-11 21:57:14 +01:00
2022-03-10 23:08:29 +01:00
} else {
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
const offlineMessage = await this.prepareCreate({ message: ChatMessage })
2022-01-28 16:37:18 +01:00
2023-01-09 10:49:58 +01:00
offlineMessage.from = 'Offline'
offlineMessage.loadHistory = this.hasLoadHistory
2023-06-29 16:04:44 +01:00
wewMessage.messageModelInstance = ChatMessage
2023-01-09 10:49:58 +01:00
this.registerSendMessage(offlineMessage)
offlineMessage?.decryptMessage()
offlineMessage.send()
2023-05-26 14:23:37 +01:00
2022-03-10 23:08:29 +01:00
}
2023-10-19 15:03:12 +01:00
if (wewMessage.delate && !wewMessage.offline && !wewMessage.delateRequest) {
2022-03-18 12:32:21 +01:00
this.sendDeleteRequest(wewMessage._id)
}
2022-09-27 14:52:01 +01:00
}
2023-08-08 16:32:57 +01:00
2023-10-19 15:03:12 +01:00
if (!this.lastMessage) {
2023-09-11 21:57:14 +01:00
this.lastMessage = messages.pop();
2023-08-08 16:32:57 +01:00
}
2023-09-11 21:57:14 +01:00
2023-01-16 09:06:12 +01:00
setTimeout(() => {
2022-03-10 23:08:29 +01:00
this.scrollDown()
}, 50)
2022-02-10 14:07:16 +01:00
}
2022-02-02 20:16:12 +01:00
}
// runs onces only
2023-02-08 16:06:14 +01:00
loadHistoryCount = 0
2023-09-29 16:40:50 +01:00
localMessages = []
2023-10-19 15:03:12 +01:00
async loadHistory({ limit = 1000, forceUpdate = false }) {
2022-02-02 20:16:12 +01:00
2023-10-19 15:03:12 +01:00
if (forceUpdate == false) {
if (this.hasLoadHistory) {
return false
2022-02-10 14:07:16 +01:00
}
}
2022-01-28 15:28:21 +01:00
2023-02-06 18:48:50 +01:00
this.hasLoadHistory = true
2023-02-08 16:06:14 +01:00
await this.restoreMessageFromDB()
2022-10-05 10:49:17 +01:00
const chatHistory: chatHistory = await this.RochetChatConnectorService.loadHistory(this.id, limit)
2022-09-26 23:26:26 +01:00
2023-10-19 15:03:12 +01:00
this.localMessages = this.messages.map(e => e._id)
2023-09-29 16:40:50 +01:00
2023-10-19 15:03:12 +01:00
if (Array.isArray(chatHistory?.result?.messages)) {
2022-02-09 15:06:21 +01:00
2023-01-09 10:49:58 +01:00
const users = this.getUsersByStatus('online')
2024-03-01 14:42:16 +01:00
if(chatHistory.result.unreadNotLoaded >= 1) {
this.messageUnread = true
}
2023-10-19 15:03:12 +01:00
for (let message of chatHistory.result.messages.reverse()) {
2023-02-08 16:06:14 +01:00
message.origin = 'history'
message.from = 'History'
2023-10-19 15:03:12 +01:00
const messagesToSave = await this.prepareMessageCreateIfNotExist({ message: message });
if (messagesToSave != null) {
2023-02-08 16:06:14 +01:00
messagesToSave.received = users.map((user) => user._id)
messagesToSave.addMessageDB()
2022-03-28 16:39:54 +01:00
}
2023-09-11 21:57:14 +01:00
2022-09-26 23:26:26 +01:00
}
2022-01-28 15:31:52 +01:00
2022-01-14 14:58:47 +01:00
2023-10-19 15:03:12 +01:00
for (const message of this.messagesBeforeOfflineAPI) {
const messagesToSave = await this.prepareMessageCreateIfNotExist({ message: message });
if (messagesToSave != null) {
2023-02-08 16:06:14 +01:00
messagesToSave.received = users.map((user) => user._id)
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
this.addMessageDB.push(messagesToSave)
2023-02-08 16:06:14 +01:00
}
}
this.messagesBeforeOfflineAPI = []
2022-10-05 10:49:17 +01:00
setTimeout(() => {
this.scrollDown()
}, 50)
2023-09-11 21:57:14 +01:00
2022-09-29 12:17:00 +01:00
}
2023-09-29 16:40:50 +01:00
this.DoneLoadingHistory = true
2023-10-19 15:03:12 +01:00
if (chatHistory?.result?.messages) {
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
if (!this.lastMessage) {
2023-08-08 16:32:57 +01:00
this.lastMessage = chatHistory?.result?.messages.pop() as any
}
}
2023-09-29 16:40:50 +01:00
this.deletedMessages(chatHistory)
2022-01-11 15:43:09 +01:00
}
2023-09-11 21:57:14 +01:00
2023-09-29 16:40:50 +01:00
deletedMessages(chatHistory: chatHistory) {
2023-10-19 15:03:12 +01:00
const messagesToDelete = this.localMessages.filter(id => {
const found = chatHistory.result.messages.find(e => {
2023-09-29 16:40:50 +01:00
return e._id == id
})
2023-10-19 15:03:12 +01:00
if (!found) {
2023-09-29 16:40:50 +01:00
return true
}
return false
})
for (const id of messagesToDelete) {
2023-10-19 15:03:12 +01:00
if (id != '') {
2023-09-29 16:40:50 +01:00
this.deleteMessage(id)
}
}
}
2023-10-19 15:03:12 +01:00
addMessageDB = FIFOProcessQueue(async ({ messagesToSave }, callback) => {
2023-09-11 21:57:14 +01:00
await messagesToSave.addMessageDB()
callback()
})
2022-03-03 22:57:33 +01:00
async readAllMessage() {
2023-01-09 10:49:58 +01:00
this.ViewedMessageService.requestReadAll(this)
2022-03-03 22:57:33 +01:00
}
2022-01-20 14:31:49 +01:00
2022-02-09 14:07:34 +01:00
2022-03-04 14:20:39 +01:00
2023-10-19 15:03:12 +01:00
async ChatMessageIsPresentInTheView(ChatMessage: ChatMessageInterface) {
2022-03-10 23:08:29 +01:00
let foundIndex;
const found = this.messages.find((MessageService, index) => {
if (MessageService._id == ChatMessage._id) {
foundIndex = index
return true
} else {
return false
}
})
if (foundIndex) {
2023-10-19 15:03:12 +01:00
return { found, foundIndex }
2022-03-10 23:08:29 +01:00
} else {
return false
}
}
2023-01-09 10:49:58 +01:00
2023-10-19 15:03:12 +01:00
async prepareCreate({ message, save = true }): Promise<MessageService> {
2022-03-04 14:50:13 +01:00
message = this.fix_updatedAt(message)
2023-10-19 15:03:12 +01:00
const wewMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this.ChatSystemService, this.notificationService)
2022-03-04 14:50:13 +01:00
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
if (save) {
2023-02-08 16:06:14 +01:00
this.messages.push(wewMessage)
}
2023-09-11 21:57:14 +01:00
2022-03-21 15:08:43 +01:00
return wewMessage
2022-03-04 14:50:13 +01:00
}
2022-03-04 14:20:39 +01:00
simplePrepareMessage(message) {
message = this.fix_updatedAt(message)
2023-10-19 15:03:12 +01:00
const wewMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this.ChatSystemService, this.notificationService)
2022-03-04 14:20:39 +01:00
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
return wewMessage
}
2023-10-19 15:03:12 +01:00
private findMessageBy_id(id) {
for (let m of this.messages) {
if (m?._id == id) {
2022-03-04 14:20:39 +01:00
return true
2022-03-21 21:06:54 +01:00
}
2023-01-09 10:49:58 +01:00
}
2023-09-11 21:57:14 +01:00
2023-01-09 10:49:58 +01:00
return false
}
2023-10-19 15:03:12 +01:00
private findMessageBy_localReference(localReference) {
for (let m of this.messages) {
if (m?.localReference == localReference) {
2023-01-09 10:49:58 +01:00
return true
}
2022-03-21 21:06:54 +01:00
}
2023-01-09 10:49:58 +01:00
return false
}
2023-10-19 15:03:12 +01:00
private async findMessageInDBByLocalReference({ localReference }) {
const a = await MessageModel.get({ localReference: localReference })
2023-09-11 21:57:14 +01:00
2023-01-09 10:49:58 +01:00
return typeof a.id == 'number'
}
2023-10-19 15:03:12 +01:00
private async findMessageInDBByLocalId({ _id }) {
const a = await MessageModel.get({ _id: _id })
2023-01-09 10:49:58 +01:00
return typeof a.id == 'number'
2022-03-21 21:06:54 +01:00
}
2023-10-19 15:03:12 +01:00
private async findMessageInDBByData({ localReference, _id }) {
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
const a = await MessageModel.filter({ localReference: localReference }).execute()
if (a.length >= 1) {
2023-05-26 14:23:37 +01:00
2023-01-09 10:49:58 +01:00
return true
}
2022-03-21 21:06:54 +01:00
2023-10-19 15:03:12 +01:00
const c = await MessageModel.filter({ _id: _id }).execute()
if (c.length >= 1) {
2023-05-26 14:23:37 +01:00
2023-01-09 10:49:58 +01:00
return true
}
2023-05-26 14:23:37 +01:00
2023-01-09 10:49:58 +01:00
return false
}
2023-10-19 15:03:12 +01:00
async prepareMessageCreateIfNotExist({ message }) {
2022-03-21 21:06:54 +01:00
message = this.fix_updatedAt(message)
2023-02-08 16:06:14 +01:00
let found = await this.findMessageBy_id(message._id) || await this.findMessageBy_localReference(message.localReference)
2023-01-09 10:49:58 +01:00
// || await this.findMessageInDBByData({_id:message._id, localReference:message.localReference })
2022-03-04 14:20:39 +01:00
if (!found) {
const wewMessage = this.simplePrepareMessage(message)
2023-09-12 09:52:28 +01:00
this.goshPush(wewMessage)
2022-02-10 14:07:16 +01:00
this.messages.push(wewMessage)
2023-10-19 15:03:12 +01:00
return wewMessage
2022-03-04 14:20:39 +01:00
} else {
2022-03-10 23:08:29 +01:00
return null
2022-02-10 14:07:16 +01:00
}
}
2022-01-10 23:52:33 +01:00
2022-01-11 20:56:21 +01:00
private calDateDuration(date = null) {
this.duration = showDateDuration(date || this._updatedAt);
2022-01-28 19:01:24 +01:00
this._updatedAt = date || this._updatedAt
2022-01-11 20:56:21 +01:00
}
2022-03-10 23:08:29 +01:00
private fix_updatedAt(message): ChatMessageInterface {
2022-03-03 22:57:33 +01:00
if (message?.result) {
2022-01-11 20:56:21 +01:00
message.result._updatedAt = message.result._updatedAt['$date']
2023-10-19 15:03:12 +01:00
} else if (message?._updatedAt) {
if (message._updatedAt.hasOwnProperty('$date')) {
2022-02-03 21:01:53 +01:00
message._updatedAt = message._updatedAt['$date']
}
2022-01-11 20:56:21 +01:00
}
return message
}
2022-01-28 16:15:20 +01:00
usernameToDisplayName(username) {
2022-12-19 15:59:52 +01:00
try {
const firstName = capitalizeTxt(username.split('.')[0])
2023-10-19 15:03:12 +01:00
const lastName = capitalizeTxt(username.split('.')[1])
2022-12-19 15:59:52 +01:00
return firstName + ' ' + lastName
2023-10-19 15:03:12 +01:00
} catch (error) {
2022-12-19 15:59:52 +01:00
return username
}
2022-01-28 16:15:20 +01:00
}
2022-01-12 16:10:59 +01:00
2022-03-03 22:57:33 +01:00
sendReadMessage() {
2023-10-19 15:03:12 +01:00
this.RochetChatConnectorService.readMessage(this.id).catch((error) => {
2022-12-20 17:06:19 +01:00
console.error(error)
})
2022-03-15 15:49:59 +01:00
this.sendFalseTypingReadMessage('viewMessage', {})
2022-07-13 16:08:57 +01:00
this.messageUnread = false
2022-03-03 22:57:33 +01:00
}
2023-10-19 15:03:12 +01:00
async addContacts(userId: any) {
2023-09-11 21:57:14 +01:00
2023-06-22 12:53:35 +01:00
let body = {
"roomId": this.id,
"userId": userId,
}
2023-08-08 09:43:26 +01:00
await this.chatService.addUserToGroup(body).toPromise();
this.ChatSystemService.getGroupRoom(this.id).updateContacts()
2023-06-22 12:53:35 +01:00
}
2023-08-08 09:43:26 +01:00
async updateContacts() {
2023-09-11 21:57:14 +01:00
2023-08-08 09:43:26 +01:00
let res
let error = false
2023-10-19 15:03:12 +01:00
if (this.t == 'd') {
2023-09-11 21:57:14 +01:00
2023-08-08 09:43:26 +01:00
try {
res = await this.chatService.getMembers(this.id).toPromise();
} catch (e) {
await this.chatService.refreshtoken();
error = true
}
2023-10-19 15:03:12 +01:00
if (error) {
2023-08-08 09:43:26 +01:00
res = await this.chatService.getMembers(this.id).toPromise();
}
} else {
if (this.t === 'p') {
try {
res = await this.chatService.getGroupMembers(this.id).toPromise()
} catch (e) {
await this.chatService.refreshtoken();
error = true
}
2023-10-19 15:03:12 +01:00
if (error) {
2023-08-08 09:43:26 +01:00
res = await this.chatService.getGroupMembers(this.id).toPromise()
}
2023-10-19 15:03:12 +01:00
setTimeout(() => {
2024-03-01 14:42:16 +01:00
// console.log("getGroupMembers", this.membersExcludeMe)
2023-10-19 15:03:12 +01:00
}, 500)
2023-08-08 09:43:26 +01:00
}
else {
try {
res = await this.chatService.getChannelMembers(this.id).toPromise()
} catch (e) {
await this.chatService.refreshtoken();
error = true
}
2023-10-19 15:03:12 +01:00
if (error) {
2023-08-08 09:43:26 +01:00
res = await this.chatService.getChannelMembers(this.id).toPromise()
}
2023-09-11 21:57:14 +01:00
2023-10-19 15:03:12 +01:00
setTimeout(() => {
console.log("getChannelMembers", this.membersExcludeMe)
}, 500)
2023-08-08 09:43:26 +01:00
}
}
const members = res['members'];
const users = members.filter(data => data.username != this.sessionStore.user.UserName);
this.members = members
this.membersExcludeMe = users
}
2022-01-10 13:31:15 +01:00
}