This commit is contained in:
Peter Maquiran
2022-03-04 14:20:39 +01:00
parent f5880fec2a
commit c112080917
6 changed files with 205 additions and 89 deletions
+80 -32
View File
@@ -17,7 +17,7 @@ import { ChatService } from 'src/app/services/chat.service';
import { NfService } from 'src/app/services/chat/nf.service';
import { v4 as uuidv4 } from 'uuid'
import { ChatStorageService } from './chat-storage.service'
import { ChatMethodsService } from './chat-methods.service'
@Injectable({
providedIn: 'root'
})
@@ -69,7 +69,8 @@ export class RoomService {
private sortService: SortService,
private chatService: ChatService,
private NfService: NfService,
private ChatStorageService: ChatStorageService
private ChatStorageService: ChatStorageService,
private ChatMethodsService: ChatMethodsService
) {
this.NativeNotificationService.askForPermission()
@@ -104,14 +105,11 @@ export class RoomService {
})
}
}
})
}
setData({members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService), _updatedAt }) {
setData({members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService), _updatedAt }) {
this.customFields = customFields
this.id = id
this.name = name
@@ -368,7 +366,6 @@ export class RoomService {
return JSON.parse(str);
}
async restoreMessageFromDB() {
if(environment.chatOffline) {
@@ -377,25 +374,18 @@ export class RoomService {
messages = []
}
// console.log('offline messages', messages)
await messages.forEach( async (ChatMessage, index) => {
// if(ChatMessage.msg.includes('***********')) {
// console.log('restore ========')
// console.log(JSON.stringify(ChatMessage))
// }
const wewMessage = await this.prepareMessage({message:ChatMessage, save: false})
const wewMessage = await this.simplePrepareMessage(ChatMessage)
if(wewMessage.offline == false) {
this.prepareMessage(ChatMessage)
this.prepareMessage({message:ChatMessage})
} else {
const offlineMessage = await this.prepareMessage({message:ChatMessage, save: true})
const offlineMessage = await this.prepareMessageCreateIfNotExist({message:ChatMessage})
this.messagesLocalReference.push(offlineMessage.localReference)
// offlineMessage.send()
offlineMessage.send()
}
});
@@ -425,15 +415,39 @@ export class RoomService {
await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => {
await chatHistory.result.messages.reverse().forEach( async (message) => {
// await chatHistory.result.messages.reverse().forEach( async (message) => {})
// await this.prepareMessage({message})
const messagesId = this.messages.map((message)=> message._id)
const createdMsgIds = []
chatHistory.result.messages.reverse().forEach(async(message) => {
if (!messagesId.includes(message._id)) {
createdMsgIds.push(message._id)
await this.prepareMessageCreateIfNotExist({message: message});
}
const messagesToSave = this.messages.filter((message)=> createdMsgIds.includes(message._id)).map((message)=>{
return {
channels: message.channels,
mentions: message.mentions,
msg: message.msg,
rid: message.rid,
ts: message.ts,
u: message.u,
_id: message._id,
_updatedAt: message._updatedAt,
messageSend: message.messageSend,
offline: message.offline,
viewed: message.viewed,
received: message.received
}
})
await this.ChatStorageService.addManyMessageDB(messagesToSave, this.id)
})
// console.log('load history ',chatHistory)
await this.readMessage(chatHistory)
//await this.updateAllMessages()
this.storage.set('chatmsg' + this.id, chatHistory.result.messages)
})
@@ -490,7 +504,6 @@ export class RoomService {
this.updateAllMessages()
}
updateAllMessages () {
const newHistory = this.messages.map((message) => {
return {
@@ -522,11 +535,13 @@ export class RoomService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
if(!message?._id && environment.chatOffline && save) {
console.log('wewMessage.localReference ::', wewMessage.localReference)
this.messages.push(wewMessage)
return wewMessage
}
@@ -542,18 +557,51 @@ export class RoomService {
}
})
if (save || !found) {
this.messages.push(wewMessage)
return wewMessage
} else{
if(redefined) {
}
if(save) {
if (!found) {
this.messages.push(wewMessage)
return wewMessage
}
} else if(foundIndex) {
return this.messages[foundIndex]
} else {
return wewMessage
}
}
simplePrepareMessage(message) {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
return wewMessage
}
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 this.messages[foundIndex]
}
}
private calDateDuration(date = null) {
this.duration = showDateDuration(date || this._updatedAt);
this._updatedAt = date || this._updatedAt