This commit is contained in:
tiago.kayaya
2022-02-10 14:55:32 +01:00
5 changed files with 137 additions and 132 deletions
+92 -93
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,30 @@ 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)
ChatMessage = this.fix_updatedAt(ChatMessage)
console.log('recivemessage', JSON.stringify(_ChatMessage))
const messageIsFound = this.messages.find((message) => {
return message._id == ChatMessage._id
})
const message = this.prepareMessage(ChatMessage)
this.lastMessage = message
this.calDateDuration(ChatMessage._updatedAt)
if(!messageIsFound) {
const message = this.prepareMessage(ChatMessage)
if (message.t == 'r') {
this.name = message.msg
}
this.lastMessage = message
if (message.t == 'r') { this.name = message.msg }
this.calDateDuration(ChatMessage._updatedAt)
if(this.isSenderIsNotMe(ChatMessage)) {
this.NativeNotificationService.sendNotificationChat({
message: message.msg,
title: this.name
});
}
setTimeout(() => {
this.scrollDown()
}, 100)
this.NativeNotificationService.sendNotificationChat({
message: message.msg,
title: this.name
});
if(this.isSenderIsNotMe(ChatMessage)) {
this.addMessageDB(ChatMessage)
}
}
}, 150)
this.addMessageDB(ChatMessage)
this.scrollDown()
}
)
@@ -154,17 +134,34 @@ export class RoomService {
}
addMessageDB(ChatMessage) {
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
async addMessageDB(ChatMessage) {
if (environment.chatOffline) {
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
delete ChatMessage.temporaryData
messages.push(ChatMessage)
if(!ChatMessage._id && environment.chatOffline) {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
this.storage.set('chatmsg' + this.id, messages)
} else {
const find = messages.find((message)=> {
return message._id == ChatMessage._id
})
if(!find) {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
this.storage.set('chatmsg' + this.id, messages)
}
}
})
}
this.storage.set('chatmsg' + this.id, messages)
})
}
async receiveMessageDelete() {
@@ -242,19 +239,19 @@ 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,49 +337,49 @@ 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)
setTimeout(()=> {
this.scrollDown()
}, 50)
if(wewMessage.offline == false) {
this.prepareMessage(ChatMessage)
}
})
});
setTimeout(()=> {
this.scrollDown()
}, 50)
})
}
}
// runs onces only
async loadHistory({limit = 50, forceUpdate = false }) {
if (this.hasLoadHistory || forceUpdate) {
return false
if(forceUpdate == false) {
if (this.hasLoadHistory) {
return false
}
}
await 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')
})
@@ -402,22 +399,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 (save) {
if(!message._id && environment.chatOffline) {
this.messages.push(wewMessage)
return wewMessage
}
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