add implementation

This commit is contained in:
Peter Maquiran
2023-09-11 21:57:14 +01:00
parent 680f57313f
commit c04fc2a15f
12 changed files with 529 additions and 140 deletions
+113 -54
View File
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service';
import { MessageService } from 'src/app/services/chat/message.service';
import { MessageService, MessageServiceDateLabel } from 'src/app/services/chat/message.service';
import { showDateDuration } from 'src/plugin/showDateDuration';
import { chatHistory } from 'src/app/models/chatMethod';
import { Storage } from '@ionic/storage';
@@ -187,7 +187,7 @@ export class RoomService {
}
get online() {
if(!this.isGroup) {
for(let user of this.ChatSystemService.users) {
@@ -196,7 +196,7 @@ export class RoomService {
return user.status
}
}
}
}
@@ -244,7 +244,7 @@ export class RoomService {
});
}
else {
this.chatService.deleteChannel(body).subscribe(res=>{
this.chatService.deleteChannel(body).subscribe(res=>{
this.ChatSystemService.deleteRoom(this.id);
this.ChatSystemService.getAllRooms();
this.chatServiceDeleteRoom(this.id);
@@ -281,7 +281,7 @@ export class RoomService {
if (typeof args[1] != 'object') {
this.userThatIsTyping = this.usernameToDisplayName(args[0])
this.isTyping = args[1]
this.otherUserType = args[1]
@@ -298,9 +298,9 @@ export class RoomService {
}
} else if(args[0]?.method == 'deleteMessage' || args[1]?.method == 'deleteMessage') {
this.deleteMessage(args[1]?.method?._id)
} else {
}
@@ -316,13 +316,13 @@ export class RoomService {
let ChatMessage : ChatMessageInterface = this.fix_updatedAt(IncomingChatMessageArgs)
let found = this.findMessageBy_id(ChatMessage._id) ||
let found = this.findMessageBy_id(ChatMessage._id) ||
this.findMessageBy_localReference(ChatMessage?.localReference)
// || await this.findMessageInDBByData({_id:ChatMessage._id, localReference:ChatMessage.localReference })
if(!found) {
ChatMessage.origin = 'stream'
ChatMessage.origin = 'stream'
const message = await this.prepareCreate({message: ChatMessage, save: false});
this.registerSendMessage(message)
@@ -342,7 +342,7 @@ export class RoomService {
title: this.name
}); */
}
if(this.hasLoadHistory == true) {
this.messages.push(message)
await message.addMessageDB()
@@ -350,14 +350,12 @@ export class RoomService {
this.messagesBeforeOfflineAPI.push(message)
}
if(this.chatOpen == false) {
this.messageUnread = true
}
this.messageUnread = true
setTimeout(() => {
this.scrollDown()
}, 50)
}, 50)
}
if(ChatMessage?.t == "au" || ChatMessage?.t == "ru" || ChatMessage?.t == "ul") {
@@ -389,7 +387,7 @@ export class RoomService {
} catch(error) {
return []
}
}
getAllMemberThatIsNotOffline(): string[] {
@@ -432,18 +430,18 @@ export class RoomService {
async deleteMessageToReceive(userId) {
const allDeleteMessages = await DeleteMessageModel.filter({rid: this.id}).execute()
for(let message_ of allDeleteMessages) {
if(message_.needToReceiveBy.includes(userId)) {
message_.needToReceiveBy = message_.needToReceiveBy.filter((e)=> e != userId)
this.sendFalseTypingReadMessage('deleteMessage',{_id:message_.messageId})
if(message_.needToReceiveBy.length == 0) {
const deleteMessage = await DeleteMessageModel.get({messageId: message_.messageId})
await deleteMessage.delete()
@@ -452,10 +450,10 @@ export class RoomService {
}
}
}
}
}
async receiveMessageDelete() {
@@ -470,7 +468,7 @@ export class RoomService {
if(message.delate == false) {
this.deleteMessage(DeletedMessageId)
}
}
)
}
@@ -480,17 +478,17 @@ export class RoomService {
* @param id message ID
*/
async deleteMessage(_id) {
const id = _id
for (let i =0; i <= this.messages.length; i++) {
if(this.messages[i]?._id == id ) {
if (SessionStore.user.UserName == this.messages[i]?.u?.username) {
const allMemberThatIsOffline = this.getAllMemberThatIsOffline()
DeleteMessageModel.create({
messageId: this.messages[i]._id,
rid: this.messages[i].rid,
@@ -499,7 +497,7 @@ export class RoomService {
needToReceiveBy: allMemberThatIsOffline
})
}
this.messages[i]?.delateDB()
this.messages.splice(i, 1)
@@ -516,7 +514,7 @@ export class RoomService {
return true
} else {
//
//
}
}
@@ -532,7 +530,7 @@ export class RoomService {
}
async delateMessageToSendToOthers(userId) {
const deleteMessage = await DeleteMessageModel.all();
const toSend = deleteMessage.filter((DeleteMessage:string[])=> ! DeleteMessage.includes(userId))
@@ -546,7 +544,7 @@ export class RoomService {
await message.delateStatusFalse()
if(this.NetworkServiceService.getCurrentNetworkStatus() == ConnectionStatus.Online) {
this.RochetChatConnectorService.deleteMessage(msgId).then(async() => {
message.delateRequest = true
await message.saveChanges();
@@ -557,7 +555,7 @@ export class RoomService {
this.RochetChatConnectorService.registerCallback({
type: 'reConnect',
funx: async ()=> {
this.sendDeleteRequest(msgId)
return true
}
@@ -575,7 +573,7 @@ export class RoomService {
if(file && this.message) {
this.send({})
}
const localReference = uuidv4();
let offlineChatMessage = {
@@ -593,20 +591,20 @@ export class RoomService {
console.log('offlineChatMessage', offlineChatMessage)
this.message= ''
const message: MessageService = await this.prepareCreate({message:offlineChatMessage, save: environment.chatOffline})
this.registerSendMessage(message)
if(this.hasLoadHistory == true) {
await message.addMessageDB()
}
message.send()
message.from = 'send'
message.loadHistory = this.hasLoadHistory
if (environment.chatOffline) {
setTimeout(() => {
this.scrollDown()
}, 150)
@@ -708,17 +706,68 @@ export class RoomService {
}
sortArrayISODate(messages: any): any[] {
return messages.sort((a,b) =>
new Date(b._updatedAt ).getTime()
-
new Date(a._updatedAt).getTime())
}
restoreOnce = false
labelDates = []
goshPush(ChatMessage) {
let currentDateMessage = new Date(ChatMessage._updatedAt).toLocaleDateString()
if(currentDateMessage) {
if(!this.labelDates.find( e => e == currentDateMessage)) {
this.labelDates.push(currentDateMessage)
const newMessage = this.fix_updatedAt({...ChatMessage})
newMessage.msg = currentDateMessage
const cloneMessage = new MessageServiceDateLabel()
cloneMessage.ChatSystemService = this.ChatSystemService
cloneMessage.setData({...newMessage} as any)
//
cloneMessage._id = ""
cloneMessage._updatedAt = ""
cloneMessage.msg = currentDateMessage
cloneMessage.dateLabel = true
cloneMessage.delate = false
if(new Date().toLocaleDateString() == currentDateMessage) {
cloneMessage.msg = "hoje"
} else {
cloneMessage.msg = currentDateMessage
}
this.messages.push(cloneMessage as any)
console.log(currentDateMessage)
}
}
}
async restoreMessageFromDB() {
if(environment.chatOffline && this.restoreOnce == false) {
this.restoreOnce = true
const messages = await MessageModel.filter({rid:this.id}).execute()
const messages = this.sortArrayISODate(await MessageModel.filter({rid:this.id}).execute()).reverse()
console.log('MessageModel', messages)
for (let ChatMessage of messages) {
for (let ChatMessage of messages) {
this.goshPush(ChatMessage)
const wewMessage = await this.simplePrepareMessage(ChatMessage)
wewMessage.from = 'Offline'
wewMessage.loadHistory = this.hasLoadHistory
@@ -731,9 +780,9 @@ export class RoomService {
message.loadHistory = this.hasLoadHistory
wewMessage.messageModelInstance = ChatMessage
message?.decryptMessage()
} else {
const offlineMessage = await this.prepareCreate({message:ChatMessage})
offlineMessage.from = 'Offline'
@@ -751,10 +800,10 @@ export class RoomService {
}
if(!this.lastMessage) {
this.lastMessage = messages.pop();
this.lastMessage = messages.pop();
}
setTimeout(() => {
this.scrollDown()
}, 50)
@@ -790,7 +839,7 @@ export class RoomService {
messagesToSave.received = users.map((user) => user._id)
messagesToSave.addMessageDB()
}
}
@@ -799,7 +848,10 @@ export class RoomService {
if(messagesToSave != null) {
messagesToSave.received = users.map((user) => user._id)
messagesToSave.addMessageDB()
this.goshPush(messagesToSave)
this.addMessageDB.push(messagesToSave)
}
}
this.messagesBeforeOfflineAPI = []
@@ -807,11 +859,11 @@ export class RoomService {
setTimeout(() => {
this.scrollDown()
}, 50)
}
if(chatHistory?.result?.messages) {
if(!this.lastMessage) {
this.lastMessage = chatHistory?.result?.messages.pop() as any
}
@@ -820,6 +872,13 @@ export class RoomService {
}
addMessageDB = FIFOProcessQueue(async ({messagesToSave}, callback) => {
await messagesToSave.addMessageDB()
callback()
})
async readAllMessage() {
this.ViewedMessageService.requestReadAll(this)
@@ -856,11 +915,11 @@ export class RoomService {
const wewMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this.ChatSystemService,this.notificationService)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
if(save) {
this.messages.push(wewMessage)
}
return wewMessage
}
@@ -881,7 +940,7 @@ export class RoomService {
return true
}
}
return false
}
@@ -897,7 +956,7 @@ export class RoomService {
private async findMessageInDBByLocalReference({localReference}) {
const a = await MessageModel.get({localReference: localReference})
return typeof a.id == 'number'
}
@@ -907,7 +966,7 @@ export class RoomService {
}
private async findMessageInDBByData({localReference, _id}) {
const a = await MessageModel.filter({localReference: localReference}).execute()
if(a.length >= 1) {
@@ -976,7 +1035,7 @@ export class RoomService {
}
async addContacts(userId:any) {
let body = {
"roomId": this.id,
"userId": userId,
@@ -988,12 +1047,12 @@ export class RoomService {
async updateContacts() {
let res
let error = false
if(this.t == 'd') {
try {
res = await this.chatService.getMembers(this.id).toPromise();
} catch (e) {
@@ -1035,7 +1094,7 @@ export class RoomService {
if(error) {
res = await this.chatService.getChannelMembers(this.id).toPromise()
}
}
}