This commit is contained in:
Peter Maquiran
2022-02-10 14:07:16 +01:00
parent a6823d9bae
commit 26a209502e
5 changed files with 122 additions and 128 deletions
+77 -88
View File
@@ -15,7 +15,7 @@ import { chatUser } from 'src/app/models/chatMethod';
import { environment } from 'src/environments/environment';
import { ChatService } from 'src/app/services/chat.service';
import { NfService } from 'src/app/services/chat/nf.service'
import alasql from 'alasql'
@Injectable({
providedIn: 'root'
})
@@ -30,7 +30,7 @@ export class RoomService {
t = ''
name = ''
_updatedAt = {}
private hasLoadHistory = false
hasLoadHistory = false
duration = ''
isTyping = false
otherUserType = false
@@ -67,15 +67,6 @@ export class RoomService {
private NfService: NfService
) {
this.NativeNotificationService.askForPermission()
this.WsChatService.registerCallback({
type: 'reConnect',
funx: ()=>{
this.hasLoadHistory = false
}
})
}
setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService), _updatedAt }) {
@@ -99,41 +90,33 @@ export class RoomService {
this.WsChatService.updateRoomEventss(
this.id,
"stream-room-messages",
(ChatMessage) => {
(_ChatMessage) => {
setTimeout(() => {
ChatMessage = ChatMessage.fields.args[0]
let ChatMessage = _ChatMessage.fields.args[0]
ChatMessage = this.fix_updatedAt(ChatMessage)
// console.log('recivemessage', ChatMessage)
const messageIsFound = this.messages.find((message) => {
return message._id == ChatMessage._id
})
if(!messageIsFound) {
const message = this.prepareMessage(ChatMessage)
this.lastMessage = message
if (message.t == 'r') { this.name = message.msg }
this.calDateDuration(ChatMessage._updatedAt)
setTimeout(() => {
this.scrollDown()
}, 100)
this.NativeNotificationService.sendNotificationChat({
message: message.msg,
title: this.name
});
ChatMessage = this.fix_updatedAt(ChatMessage)
console.log('recivemessage', JSON.stringify(_ChatMessage))
if(this.isSenderIsNotMe(ChatMessage)) {
this.addMessageDB(ChatMessage)
}
}
}, 150)
const message = this.prepareMessage(ChatMessage)
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.isSenderIsNotMe(ChatMessage)) {
// this.addMessageDB(ChatMessage)
}
this.scrollDown()
}
)
@@ -242,20 +225,20 @@ export class RoomService {
temporaryData
}
this.addMessageDB(offlineChatMessage)
const message: MessageService = this.prepareMessage(offlineChatMessage)
setTimeout(() => {
this.scrollDown()
}, 150)
this.lastMessage = message
const message: MessageService = this.prepareMessage(offlineChatMessage, environment.chatOffline)
message.send()
this.calDateDuration(message._updatedAt)
this.sortRoomList()
if (environment.chatOffline) {
this.addMessageDB(offlineChatMessage)
setTimeout(() => {
this.scrollDown()
}, 150)
this.lastMessage = message
this.calDateDuration(message._updatedAt)
this.sortRoomList()
}
this.message= ''
}
@@ -340,46 +323,50 @@ export class RoomService {
async restoreMessageFromDB() {
await this.storage.get('chatmsg' + this.id).then( async (messages = []) => {
if(!Array.isArray(messages)) {
messages = []
}
if(environment.chatOffline) {
await messages.forEach( async (ChatMessage, index) => {
const wewMessage = this.prepareMessage(ChatMessage, false)
if(wewMessage.offline == false) {
this.prepareMessage(ChatMessage)
await this.storage.get('chatmsg' + this.id).then( async (messages = []) => {
if(!Array.isArray(messages)) {
messages = []
}
await messages.forEach( async (ChatMessage, index) => {
const wewMessage = this.prepareMessage(ChatMessage, false)
if(wewMessage.offline == false) {
this.prepareMessage(ChatMessage)
}
});
setTimeout(()=> {
this.scrollDown()
}, 50)
})
});
setTimeout(()=> {
this.scrollDown()
}, 50)
})
}
}
// runs onces only
async loadHistory({limit = 100, forceUpdate = false }) {
if (this.hasLoadHistory || forceUpdate) { return false }
if(forceUpdate == false) {
if (this.hasLoadHistory) {
return false
}
}
this.restoreMessageFromDB()
await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => {
console.log('loadHistory', chatHistory)
this.messages = []
await chatHistory.result.messages.reverse().forEach( async (message) => {
this.prepareMessage(message)
const wewMessage = this.prepareMessage(message, false)
if(wewMessage.offline == false) {
this.prepareMessage(message)
}
this.messages = this.sortService.sortDate(this.messages, '_updatedAt')
})
@@ -399,22 +386,24 @@ export class RoomService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
if(!message._id && environment.chatOffline) {
this.messages.push(wewMessage)
return wewMessage
}
if (save) {
const found = this.messages.find((MessageService) => {
return MessageService._id == message._id
})
if (save && !found) {
this.messages.push(wewMessage)
}
return wewMessage
}
async returnData(res) {
return res;
}
ReactToMessage() { }
private calDateDuration(date = null) {
this.duration = showDateDuration(date || this._updatedAt);
this._updatedAt = date || this._updatedAt