mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
improve chat
This commit is contained in:
@@ -283,11 +283,12 @@ export class ChatSystemService {
|
||||
this.loadingWholeList = false
|
||||
await this.storage.set('Rooms', rooms);
|
||||
|
||||
this.sortRoomList()
|
||||
|
||||
setTimeout(() => {
|
||||
this.sortRoomList()
|
||||
}, 1000)
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
this.sortRoomList()
|
||||
}, 10000)
|
||||
@@ -480,19 +481,19 @@ export class ChatSystemService {
|
||||
*/
|
||||
private getUserStatus(id?:string) {
|
||||
|
||||
this.RochetChatConnectorService.getUserStatus((d) => {
|
||||
this.RochetChatConnectorService.getUserStatus(async (d) => {
|
||||
|
||||
const userId = d.fields.args[0][0]
|
||||
const username = d.fields.args[0][1]
|
||||
const statusNum = d.fields.args[0][2]
|
||||
|
||||
|
||||
const statusText = this.statusNumberToText(statusNum)
|
||||
|
||||
this.users.forEach((user, index) => {
|
||||
if(user.username == username) {
|
||||
this.users[index].status = statusText
|
||||
for ( const user of this.users) {
|
||||
if(user._id == userId) {
|
||||
user.status = statusText
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@ import { HttpEventType } from '@angular/common/http';
|
||||
import { AttachmentsService } from 'src/app/services/attachments.service';
|
||||
import { NetworkServiceService , ConnectionStatus} from 'src/app/services/network-service.service';
|
||||
import { ChatSystemService } from './chat-system.service';
|
||||
import { resolve } from 'dns';
|
||||
import { async } from '@angular/core/testing';
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -38,7 +38,6 @@ export class MessageService {
|
||||
_updatedAt
|
||||
file
|
||||
attachments
|
||||
offline = true
|
||||
displayType = ''
|
||||
temporaryData: any = {}
|
||||
hasFile = false
|
||||
@@ -50,8 +49,8 @@ export class MessageService {
|
||||
from: 'Offline'|'History'|'stream'| 'send'
|
||||
duration = ''
|
||||
localReference = null
|
||||
viewed = []
|
||||
received = []
|
||||
viewed: string[] = []
|
||||
received: string[]= []
|
||||
addToDb = false
|
||||
|
||||
messageSend = false
|
||||
@@ -73,7 +72,7 @@ export class MessageService {
|
||||
private ChatSystemService: ChatSystemService) {
|
||||
}
|
||||
|
||||
setData({customFields = {}, channels, mentions, msg ,rid ,ts, u, t, _id, id, _updatedAt, file, attachments, temporaryData, localReference , viewed = [], received = [], delate = false, delateRequest =false, from, sendAttempt = 0 }:Message) {
|
||||
setData({customFields = {}, channels, mentions, msg ,rid ,ts, u, t, _id, id, _updatedAt, file, attachments, temporaryData, localReference = 'out-'+uuidv4() , viewed = [], received = [], delate = false, delateRequest =false, from, sendAttempt = 0 }:Message) {
|
||||
|
||||
this.channels = channels || []
|
||||
this.mentions = mentions || []
|
||||
@@ -102,11 +101,9 @@ export class MessageService {
|
||||
this.received = [...new Set([...received,...this.received])];
|
||||
|
||||
if(!this.ts) {
|
||||
this.offline = true
|
||||
this.messageSend = false
|
||||
} else {
|
||||
this.messageSend = true
|
||||
this.offline = false
|
||||
}
|
||||
|
||||
if (this.file) {
|
||||
@@ -137,6 +134,34 @@ export class MessageService {
|
||||
this.calDateDuration()
|
||||
}
|
||||
|
||||
|
||||
get offline () {
|
||||
if(!this._id) {
|
||||
return true
|
||||
}
|
||||
|
||||
if(!this.ts) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Message is on the server
|
||||
*/
|
||||
get online() {
|
||||
return !this.offline
|
||||
}
|
||||
|
||||
/**
|
||||
* if Message is already saved on database
|
||||
*/
|
||||
get save() {
|
||||
return this.id != ''
|
||||
}
|
||||
|
||||
|
||||
private usernameToDisplayName(username) {
|
||||
|
||||
try {
|
||||
@@ -150,7 +175,6 @@ export class MessageService {
|
||||
}
|
||||
|
||||
async send(): Promise<any> {
|
||||
|
||||
if(this.messageSend) {
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve('solve')
|
||||
@@ -183,7 +207,7 @@ export class MessageService {
|
||||
|
||||
this.RochetChatConnectorService.registerCallback({
|
||||
type: 'reConnect',
|
||||
funx: async ()=> {
|
||||
funx: async () => {
|
||||
this.send().catch((error) =>{
|
||||
console.error(error)
|
||||
})
|
||||
@@ -250,7 +274,6 @@ export class MessageService {
|
||||
}
|
||||
|
||||
if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Online) {
|
||||
// console.log('online send')
|
||||
|
||||
if(this.msg == '<script></script>') {
|
||||
if(this.sendAttempt >= 4) {
|
||||
@@ -259,7 +282,6 @@ export class MessageService {
|
||||
ChatMessage = ChatMessage.message.result
|
||||
clearTimeout(this.functionTimer);
|
||||
|
||||
this.messageSend = true
|
||||
this.redefinedMessage(ChatMessage)
|
||||
|
||||
}
|
||||
@@ -270,12 +292,12 @@ export class MessageService {
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
this.RochetChatConnectorService.send(params).then(
|
||||
(ChatMessage: any) => {
|
||||
ChatMessage = ChatMessage.message.result
|
||||
clearTimeout(this.functionTimer);
|
||||
|
||||
this.messageSend = true
|
||||
this.redefinedMessage(ChatMessage)
|
||||
|
||||
}
|
||||
@@ -286,7 +308,6 @@ export class MessageService {
|
||||
}
|
||||
|
||||
this.functionTimer = setTimeout(() => {
|
||||
|
||||
this.RochetChatConnectorService.registerCallback({
|
||||
type:'Onmessage',
|
||||
key:'ping-pong-message',
|
||||
@@ -300,7 +321,7 @@ export class MessageService {
|
||||
return true
|
||||
}
|
||||
|
||||
this.save()
|
||||
this.saveChanges()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -312,7 +333,6 @@ export class MessageService {
|
||||
this.RochetChatConnectorService.registerCallback({
|
||||
type: 'reConnect',
|
||||
funx: async ()=> {
|
||||
|
||||
this.send().catch((error) =>{
|
||||
console.error(error)
|
||||
})
|
||||
@@ -329,11 +349,18 @@ export class MessageService {
|
||||
const message = this.getChatObj()
|
||||
|
||||
this.manualRetry = false
|
||||
this.messageSend = true
|
||||
|
||||
ChatMessage = Object.assign(message, ChatMessage)
|
||||
this.setData(ChatMessage)
|
||||
|
||||
await this.save()
|
||||
const roomObject = this.ChatSystemService.getRoomById(this.rid)
|
||||
const users = roomObject.getUsersByStatus('online')
|
||||
for(const user of users) {
|
||||
this.addReceived(user._id)
|
||||
}
|
||||
|
||||
await this.saveChanges()
|
||||
}
|
||||
|
||||
downloadFileMsg() {
|
||||
@@ -363,7 +390,7 @@ export class MessageService {
|
||||
}
|
||||
|
||||
// save the changes to the storage
|
||||
this.save()
|
||||
this.saveChanges()
|
||||
this.downloadLoader = false;
|
||||
this.downloadAttachments = true
|
||||
this.downloadAttachmentsTemp++;
|
||||
@@ -384,30 +411,55 @@ export class MessageService {
|
||||
|
||||
async delateStatusFalse() {
|
||||
this.delate = true
|
||||
this.save()
|
||||
this.saveChanges()
|
||||
}
|
||||
|
||||
addViewed(id: string) {
|
||||
if(this.messageOwner()) {
|
||||
let found = this.viewed.find((UserId) => UserId == id)
|
||||
|
||||
if(!found) {
|
||||
this.viewed.push(id)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
addReceived(id: string) {
|
||||
if(this.messageOwner()) {
|
||||
let found = this.received.find((UserId) => UserId == id)
|
||||
|
||||
if(!found) {
|
||||
this.received.push(id)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
async delateDB() {
|
||||
|
||||
const message = await MessageModel.get({_id: this._id})
|
||||
const message = await MessageModel.get({id: this.id})
|
||||
await message.delete()
|
||||
|
||||
}
|
||||
|
||||
|
||||
isSenderIsNotMe(ChatMessage) {
|
||||
return SessionStore.user.UserName != ChatMessage.u.username
|
||||
return SessionStore.user.ChatData.data.userId != ChatMessage.u._id
|
||||
}
|
||||
|
||||
messageOwnerById(id) {
|
||||
return SessionStore.user.UserName != this.u.username
|
||||
messageOwner() {
|
||||
return SessionStore.user.ChatData.data.userId == this.u._id
|
||||
}
|
||||
|
||||
private getChatObj() {
|
||||
return {
|
||||
channels: this.channels,
|
||||
mentions: this.mentions,
|
||||
//msg: this.AESEncrypt.encrypt(this.msg, SessionStore.user.UserName),
|
||||
// msg: this.AESEncrypt.encrypt(this.msg, SessionStore.user.UserName),
|
||||
sendAttempt: this.sendAttempt,
|
||||
msg:this.msg,
|
||||
rid: this.rid,
|
||||
@@ -427,8 +479,10 @@ export class MessageService {
|
||||
}
|
||||
}
|
||||
|
||||
earlySave = false
|
||||
|
||||
async addMessageDB() {
|
||||
if(!this.addToDb) {
|
||||
if(!this.addToDb && this.save) {
|
||||
this.addToDb = true
|
||||
const message = this.getChatObj()
|
||||
|
||||
@@ -436,15 +490,39 @@ export class MessageService {
|
||||
const createdMessage = await MessageModel.create(message)
|
||||
|
||||
this.id = createdMessage.id
|
||||
|
||||
if(this.earlySave) {
|
||||
this.saveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async save() {
|
||||
const message = this.getChatObj()
|
||||
async saveChanges() {
|
||||
|
||||
|
||||
if(this.save) {
|
||||
const message = this.getChatObj()
|
||||
let a
|
||||
|
||||
if (this.localReference) {
|
||||
a = await MessageModel.get({localReference: this.localReference})
|
||||
} else if (this._id) {
|
||||
a = await MessageModel.get({id: this.id})
|
||||
} else if(this.id) {
|
||||
a = await MessageModel.get({_id: this._id})
|
||||
}
|
||||
|
||||
for( const [name, value] of Object.entries(message)) {
|
||||
a[name] = value
|
||||
}
|
||||
|
||||
await a.save()
|
||||
|
||||
} else {
|
||||
this.earlySave = true
|
||||
console.log('save change to early')
|
||||
}
|
||||
|
||||
|
||||
await MessageModel.update(message)
|
||||
|
||||
}
|
||||
|
||||
decryptMessage() {
|
||||
|
||||
@@ -23,6 +23,7 @@ import { AttachmentsService } from 'src/app/services/attachments.service';
|
||||
import { ConnectionStatus, NetworkServiceService} from 'src/app/services/network-service.service';
|
||||
import { ChatSystemService } from './chat-system.service';
|
||||
import { ViewedMessageService } from './viewed-message.service'
|
||||
import * as FIFOProcessQueue from 'fifo-process-queue';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -47,9 +48,9 @@ export class RoomService {
|
||||
message = ''
|
||||
lastMessageTxt = ''
|
||||
userThatIsTyping = ''
|
||||
messagesLocalReference = []
|
||||
members = []
|
||||
membersExcludeMe = []
|
||||
messagesLocalReference: string[] = []
|
||||
members: chatUser[] = []
|
||||
membersExcludeMe: chatUser[] = []
|
||||
u
|
||||
sessionStore = SessionStore
|
||||
countDownTime = ''
|
||||
@@ -186,9 +187,13 @@ export class RoomService {
|
||||
|
||||
if(!this.isGroup) {
|
||||
for(let user of this.ChatSystemService.users) {
|
||||
if(this.membersExcludeMe[0]._id == user._id) {
|
||||
return user.status
|
||||
|
||||
for (const members of this.membersExcludeMe) {
|
||||
if(members._id == user._id) {
|
||||
return user.status
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,71 +265,7 @@ export class RoomService {
|
||||
"stream-room-messages",
|
||||
async (IncomingChatMessage:IncomingChatMessage) => {
|
||||
|
||||
let IncomingChatMessageArgs = IncomingChatMessage.fields.args[0]
|
||||
let ChatMessage : ChatMessageInterface = this.fix_updatedAt(IncomingChatMessageArgs)
|
||||
|
||||
const allMessageIds = this.messages.map((e)=> e._id);
|
||||
|
||||
if(!this.messagesLocalReference.includes(ChatMessage.localReference) && !allMessageIds.includes(ChatMessage?._id)) {
|
||||
|
||||
this.messagesLocalReference.push(ChatMessage.localReference);
|
||||
|
||||
const message = await this.prepareCreate({message: ChatMessage, save: true});
|
||||
message.messageSend = true
|
||||
message.from = 'stream'
|
||||
message.loadHistory = this.hasLoadHistory
|
||||
|
||||
this.lastMessage = message;
|
||||
this.calDateDuration(ChatMessage._updatedAt);
|
||||
|
||||
if (message.t == 'r') {
|
||||
this.name = message.msg;
|
||||
}
|
||||
|
||||
if(this.isSenderIsNotMe(ChatMessage)) {
|
||||
this.NativeNotificationService.sendNotificationChat({
|
||||
message: message.msg,
|
||||
title: this.name
|
||||
});
|
||||
}
|
||||
|
||||
if(this.hasLoadHistory == true) {
|
||||
await message.addMessageDB()
|
||||
}
|
||||
|
||||
if(this.chatOpen == false) {
|
||||
this.messageUnread = true
|
||||
}
|
||||
|
||||
setTimeout(()=>{
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
} else {
|
||||
|
||||
this.messages.forEach((message, index)=> {
|
||||
if(message.localReference == ChatMessage.localReference) {
|
||||
|
||||
const membersIds = this.membersExcludeMe.map((user)=> user._id)
|
||||
|
||||
this.getAllUsers().forEach( async (users) => {
|
||||
|
||||
if(membersIds.includes(users._id)) {
|
||||
|
||||
if(users.status != 'offline') {
|
||||
|
||||
this.messages[index].received.push(users._id)
|
||||
|
||||
setTimeout(() => {
|
||||
message.save()
|
||||
}, 150)
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
this.appendReceiveMessage.push(IncomingChatMessage)
|
||||
|
||||
}
|
||||
)
|
||||
@@ -334,9 +275,7 @@ export class RoomService {
|
||||
if(message.fields.eventName == this.id+'/'+'typing') {
|
||||
|
||||
const args = message.fields.args
|
||||
|
||||
|
||||
|
||||
|
||||
if (typeof args[1] != 'object') {
|
||||
this.userThatIsTyping = this.usernameToDisplayName(args[0])
|
||||
|
||||
@@ -350,7 +289,6 @@ export class RoomService {
|
||||
this.readAllMessage()
|
||||
} else if(args[0]?.method == 'deleteMessage' || args[1]?.method == 'deleteMessage') {
|
||||
|
||||
|
||||
this.deleteMessage(args[1]?.method?._id)
|
||||
|
||||
} else {
|
||||
@@ -363,6 +301,68 @@ export class RoomService {
|
||||
})
|
||||
}
|
||||
|
||||
appendReceiveMessage = FIFOProcessQueue(async (IncomingChatMessage:IncomingChatMessage, done) => {
|
||||
|
||||
let IncomingChatMessageArgs = IncomingChatMessage.fields.args[0]
|
||||
let ChatMessage : ChatMessageInterface = this.fix_updatedAt(IncomingChatMessageArgs)
|
||||
|
||||
|
||||
let found = this.findMessageBy_id(ChatMessage._id) ||
|
||||
this.findMessageBy_localReference(ChatMessage?.localReference)
|
||||
// || await this.findMessageInDBByData({_id:ChatMessage._id, localReference:ChatMessage.localReference })
|
||||
|
||||
if(!found) {
|
||||
|
||||
const message = await this.prepareCreate({message: ChatMessage, save: true});
|
||||
this.registerSendMessage(message)
|
||||
|
||||
message.from = 'stream'
|
||||
message.loadHistory = this.hasLoadHistory
|
||||
|
||||
this.lastMessage = message;
|
||||
this.calDateDuration(ChatMessage._updatedAt);
|
||||
|
||||
if (message.t == 'r') {
|
||||
this.name = message.msg;
|
||||
}
|
||||
|
||||
if(this.isSenderIsNotMe(ChatMessage)) {
|
||||
this.NativeNotificationService.sendNotificationChat({
|
||||
message: message.msg,
|
||||
title: this.name
|
||||
});
|
||||
}
|
||||
|
||||
if(this.hasLoadHistory == true) {
|
||||
await message.addMessageDB()
|
||||
}
|
||||
|
||||
if(this.chatOpen == false) {
|
||||
this.messageUnread = true
|
||||
}
|
||||
|
||||
setTimeout(()=>{
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
done()
|
||||
}, 5)
|
||||
})
|
||||
|
||||
getUsersByStatus(status: 'offline' | 'online') {
|
||||
|
||||
return this.getAllUsers().filter((user => {
|
||||
for(const member of this.membersExcludeMe) {
|
||||
if(user._id == member._id && user.status == status) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
}
|
||||
|
||||
getRoomMembersIds(): string[] {
|
||||
try {
|
||||
@@ -528,7 +528,7 @@ export class RoomService {
|
||||
|
||||
this.RochetChatConnectorService.deleteMessage(msgId).then(async() => {
|
||||
message.delateRequest = true
|
||||
await message.save();
|
||||
await message.saveChanges();
|
||||
this.deleteMessage(msgId);
|
||||
})
|
||||
|
||||
@@ -564,8 +564,8 @@ export class RoomService {
|
||||
|
||||
this.message= ''
|
||||
|
||||
this.messagesLocalReference.push(localReference)
|
||||
const message: MessageService = await this.prepareCreate({message:offlineChatMessage, save: environment.chatOffline})
|
||||
this.registerSendMessage(message)
|
||||
|
||||
if(this.hasLoadHistory == true) {
|
||||
await message.addMessageDB()
|
||||
@@ -574,7 +574,6 @@ export class RoomService {
|
||||
message.send()
|
||||
message.from = 'send'
|
||||
message.loadHistory = this.hasLoadHistory
|
||||
|
||||
|
||||
if (environment.chatOffline) {
|
||||
|
||||
@@ -589,6 +588,24 @@ export class RoomService {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
for( const localReference of this.messagesLocalReference) {
|
||||
if(localReference == message?.localReference) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
sendTyping(text:string = this.message) {
|
||||
|
||||
@@ -660,8 +677,11 @@ export class RoomService {
|
||||
return JSON.parse(str);
|
||||
}
|
||||
|
||||
|
||||
restoreOnce = false
|
||||
async restoreMessageFromDB() {
|
||||
if(environment.chatOffline) {
|
||||
if(environment.chatOffline && this.restoreOnce == false) {
|
||||
this.restoreOnce = true
|
||||
|
||||
const messages = await MessageModel.filter({rid:this.id}).execute()
|
||||
|
||||
@@ -669,31 +689,24 @@ export class RoomService {
|
||||
const wewMessage = await this.simplePrepareMessage(ChatMessage)
|
||||
wewMessage.from = 'Offline'
|
||||
wewMessage.loadHistory = this.hasLoadHistory
|
||||
|
||||
|
||||
if(wewMessage.offline == false) {
|
||||
const message = await this.prepareMessageCreateIfNotExist_iD({message:ChatMessage})
|
||||
|
||||
if(message) {
|
||||
message.from = 'Offline'
|
||||
message.loadHistory = this.hasLoadHistory
|
||||
message?.decryptMessage()
|
||||
}
|
||||
const message = await this.prepareCreate({message:ChatMessage})
|
||||
message.from = 'Offline'
|
||||
message.loadHistory = this.hasLoadHistory
|
||||
message?.decryptMessage()
|
||||
|
||||
} else {
|
||||
|
||||
const offlineMessage = await this.prepareCreate({message:ChatMessage})
|
||||
|
||||
const offlineMessage = await this.prepareMessageCreateIfNotExist({message:ChatMessage})
|
||||
|
||||
if(offlineMessage) {
|
||||
offlineMessage.from = 'Offline'
|
||||
offlineMessage.loadHistory = this.hasLoadHistory
|
||||
|
||||
this.messagesLocalReference.push(offlineMessage.localReference)
|
||||
|
||||
offlineMessage?.decryptMessage()
|
||||
offlineMessage.send()
|
||||
|
||||
}
|
||||
offlineMessage.from = 'Offline'
|
||||
offlineMessage.loadHistory = this.hasLoadHistory
|
||||
this.registerSendMessage(offlineMessage)
|
||||
offlineMessage?.decryptMessage()
|
||||
offlineMessage.send()
|
||||
// console.log('send offline', ChatMessage)
|
||||
|
||||
}
|
||||
|
||||
@@ -710,6 +723,7 @@ export class RoomService {
|
||||
}
|
||||
|
||||
// runs onces only
|
||||
|
||||
async loadHistory({limit = 1000, forceUpdate = false }) {
|
||||
|
||||
if(forceUpdate == false) {
|
||||
@@ -718,23 +732,27 @@ export class RoomService {
|
||||
}
|
||||
}
|
||||
|
||||
if(this.restoreFromOffline == false) {
|
||||
this.restoreFromOffline = true
|
||||
await this.restoreMessageFromDB()
|
||||
}
|
||||
|
||||
|
||||
const chatHistory: chatHistory = await this.RochetChatConnectorService.loadHistory(this.id, limit)
|
||||
|
||||
if(chatHistory?.result?.messages) {
|
||||
const messagesId = this.messages.map((message)=> message._id)
|
||||
|
||||
const users = this.getUsersByStatus('online')
|
||||
|
||||
for(let message of chatHistory.result.messages.reverse()) {
|
||||
if (!messagesId.includes(message._id)) {
|
||||
const messagesToSave = await this.prepareMessageCreateIfNotExist_iD({message: message});
|
||||
if(messagesToSave) {
|
||||
const messagesToSave = await this.prepareMessageCreateIfNotExist({message: message});
|
||||
if(messagesToSave != null) {
|
||||
|
||||
messagesToSave.received = users.map((user) => user._id)
|
||||
messagesToSave.addMessageDB()
|
||||
|
||||
// console.log('add history', message)
|
||||
} else {
|
||||
// console.log('exit')
|
||||
}
|
||||
} else {
|
||||
// console.log('exit')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -744,28 +762,6 @@ export class RoomService {
|
||||
}, 50)
|
||||
|
||||
this.hasLoadHistory = true
|
||||
this.messageReorder();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async messageReorder() {
|
||||
const reorderMessage: MessageService[] = this.messages.filter((message) =>
|
||||
message.from == 'send' && !message.loadHistory || message.from == 'stream' && !message.loadHistory
|
||||
);
|
||||
|
||||
|
||||
let i = 0
|
||||
for(let message of this.messages) {
|
||||
if(message.from == 'send' && !message.loadHistory || message.from == 'stream' && !message.loadHistory) {
|
||||
this.messages.splice(i, 1)
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
for(let message of reorderMessage) {
|
||||
this.messages.push(message)
|
||||
message.addMessageDB()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -774,60 +770,11 @@ export class RoomService {
|
||||
|
||||
async readAllMessage() {
|
||||
|
||||
|
||||
const membersIds = this.membersExcludeMe.map((user)=> user._id)
|
||||
|
||||
|
||||
await this.messages.forEach( async (message, index) => {
|
||||
if(message._id) {
|
||||
if(message.viewed.length == 0) {
|
||||
this.messages[index].viewed = membersIds;
|
||||
this.messages[index].received = membersIds;
|
||||
|
||||
await this.messages[index].save()
|
||||
}
|
||||
}
|
||||
})
|
||||
this.ViewedMessageService.requestReadAll(this)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @description find or create message
|
||||
* @param message
|
||||
* @param save
|
||||
* @returns
|
||||
*/
|
||||
async prepareMessage({message, save = true, redefined = false}): Promise<MessageService> {
|
||||
message = this.fix_updatedAt(message)
|
||||
|
||||
|
||||
const wewMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this.ChatSystemService)
|
||||
wewMessage.setData(message)
|
||||
wewMessage.loadHistory = this.hasLoadHistory
|
||||
|
||||
let foundIndex;
|
||||
|
||||
const found = this.messages.find((MessageService, index) => {
|
||||
if (MessageService._id == message._id) {
|
||||
foundIndex = index
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
if(save) {
|
||||
if (!found) {
|
||||
this.messages.push(wewMessage)
|
||||
return wewMessage
|
||||
}
|
||||
} else if(foundIndex) {
|
||||
return this.messages[foundIndex]
|
||||
} else {
|
||||
return wewMessage
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async ChatMessageIsPresentInTheView(ChatMessage:ChatMessageInterface) {
|
||||
let foundIndex;
|
||||
@@ -849,48 +796,18 @@ export class RoomService {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description find or create message
|
||||
* @param message
|
||||
* @param save
|
||||
* @returns
|
||||
*/
|
||||
async prepareCreate({message, save = true}): Promise<MessageService> {
|
||||
|
||||
|
||||
async prepareCreate({message, save = true}): Promise<MessageService> {
|
||||
message = this.fix_updatedAt(message)
|
||||
|
||||
const wewMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this.ChatSystemService)
|
||||
wewMessage.setData(message)
|
||||
wewMessage.loadHistory = this.hasLoadHistory
|
||||
|
||||
|
||||
let found;
|
||||
|
||||
if(wewMessage.localReference != null) {
|
||||
found = this.messages.find((MessageService, index) => {
|
||||
if ( MessageService.localReference == wewMessage.localReference ) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
} else {
|
||||
found = this.messages.find((MessageService, index) => {
|
||||
if ( MessageService._id == wewMessage._id) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
if (!found) {
|
||||
this.messages.push(wewMessage)
|
||||
return wewMessage
|
||||
}
|
||||
this.messages.push(wewMessage)
|
||||
|
||||
return wewMessage
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -903,40 +820,62 @@ export class RoomService {
|
||||
return wewMessage
|
||||
}
|
||||
|
||||
|
||||
private findMessageBy_id (id) {
|
||||
for( let m of this.messages) {
|
||||
if(m._id == id) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
private findMessageBy_localReference (localReference) {
|
||||
for( let m of this.messages) {
|
||||
if(m.localReference == localReference) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
private async findMessageInDBByLocalReference({localReference}) {
|
||||
const a = await MessageModel.get({localReference: localReference})
|
||||
|
||||
return typeof a.id == 'number'
|
||||
}
|
||||
|
||||
private async findMessageInDBByLocalId({_id}) {
|
||||
const a = await MessageModel.get({_id: _id})
|
||||
return typeof a.id == 'number'
|
||||
}
|
||||
|
||||
private async findMessageInDBByData({localReference, _id}) {
|
||||
|
||||
// console.log('look')
|
||||
const a = await MessageModel.filter({localReference: localReference})
|
||||
if(a.length >= 1) {
|
||||
// console.log('localReference', localReference)
|
||||
return true
|
||||
}
|
||||
|
||||
const c = await MessageModel.filter({_id: _id})
|
||||
if(c.length >= 1) {
|
||||
// console.log('_id', _id)
|
||||
return true
|
||||
}
|
||||
|
||||
// console.log('look not found')
|
||||
return false
|
||||
}
|
||||
|
||||
async prepareMessageCreateIfNotExist({message}) {
|
||||
message = this.fix_updatedAt(message)
|
||||
|
||||
let foundIndex;
|
||||
const found = this.messages.find((MessageService, index) => {
|
||||
if (MessageService._id == message._id ||
|
||||
MessageService.localReference == message.localReference ) {
|
||||
foundIndex = index
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
if (!found) {
|
||||
const wewMessage = this.simplePrepareMessage(message)
|
||||
this.messages.push(wewMessage)
|
||||
return wewMessage
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async prepareMessageCreateIfNotExist_iD({message}) {
|
||||
message = this.fix_updatedAt(message)
|
||||
|
||||
const found = this.messages.find((MessageService, index) => {
|
||||
if (MessageService._id == message._id) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
let found = await this.findMessageBy_id(message._id) || this.findMessageBy_localReference(message.localReference)
|
||||
// || await this.findMessageInDBByData({_id:message._id, localReference:message.localReference })
|
||||
|
||||
if (!found) {
|
||||
const wewMessage = this.simplePrepareMessage(message)
|
||||
|
||||
@@ -7,7 +7,8 @@ import { RoomService } from './room.service';
|
||||
})
|
||||
export class ViewedMessageService {
|
||||
|
||||
constructor() { }
|
||||
constructor() {
|
||||
}
|
||||
|
||||
viewQueue = FIFOProcessQueue(async ({room, userId, statusNum, statusText}, callback) => {
|
||||
|
||||
@@ -20,25 +21,15 @@ export class ViewedMessageService {
|
||||
room.deleteMessageToReceive(userId)
|
||||
}
|
||||
|
||||
await room.messages.forEach(async (message, index) => {
|
||||
if(!message.messageOwnerById(userId)) {
|
||||
|
||||
if(!room.messages[index]?.received?.includes(userId)) {
|
||||
|
||||
if(room.messages[index]._id) {
|
||||
try {
|
||||
if(!room.messages[index].received.includes(userId)) {
|
||||
room.messages[index].received.push(userId)
|
||||
}
|
||||
} catch(e) {
|
||||
room.messages[index].received = [userId]
|
||||
}
|
||||
room.messages[index].save()
|
||||
for (const message of room.messages) {
|
||||
if(message.online) {
|
||||
for(let id of membersIds) {
|
||||
if(message.addReceived(id)) {
|
||||
await message.saveChanges()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
callback();
|
||||
@@ -47,8 +38,35 @@ export class ViewedMessageService {
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
viewReadQueue = FIFOProcessQueue(async ({room}, callback) => {
|
||||
|
||||
const membersIds: string[] = room.membersExcludeMe.map((user)=> user._id)
|
||||
|
||||
for (const message of room.messages) {
|
||||
if(message.online) {
|
||||
for(let id of membersIds) {
|
||||
|
||||
if(message.addViewed(id)) {
|
||||
message.addReceived(id)
|
||||
await message.saveChanges()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
callback()
|
||||
},100)
|
||||
|
||||
})
|
||||
|
||||
request(room:RoomService, userId, statusNum, statusText) {
|
||||
this.viewQueue.push({room, userId, statusNum, statusText})
|
||||
}
|
||||
|
||||
requestReadAll(room:RoomService) {
|
||||
this.viewReadQueue.push({room})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user