mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 21:06:06 +00:00
improve
This commit is contained in:
@@ -14,8 +14,9 @@ import { SortService } from '../functions/sort.service';
|
||||
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'
|
||||
import { NfService } from 'src/app/services/chat/nf.service';
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -24,13 +25,13 @@ export class RoomService {
|
||||
messages: MessageService[] = []
|
||||
storageMessage: any[] = [];
|
||||
lastMessage: MessageService;
|
||||
|
||||
|
||||
customFields: any;
|
||||
id = ''
|
||||
t = ''
|
||||
name = ''
|
||||
_updatedAt = {}
|
||||
private hasLoadHistory = false
|
||||
hasLoadHistory = false
|
||||
duration = ''
|
||||
isTyping = false
|
||||
otherUserType = false
|
||||
@@ -38,10 +39,11 @@ export class RoomService {
|
||||
message = ''
|
||||
lastMessageTxt = ''
|
||||
userThatIsTyping = ''
|
||||
|
||||
private ToastService = ToastsService
|
||||
mgsArray = [];
|
||||
|
||||
messagesLocalReference = []
|
||||
|
||||
scrollDown = () => { }
|
||||
|
||||
/**
|
||||
@@ -90,40 +92,35 @@ export class RoomService {
|
||||
this.WsChatService.updateRoomEventss(
|
||||
this.id,
|
||||
"stream-room-messages",
|
||||
(ChatMessage) => {
|
||||
|
||||
setTimeout(() => {
|
||||
ChatMessage = ChatMessage.fields.args[0]
|
||||
|
||||
(_ChatMessage) => {
|
||||
console.log('recivemessage', _ChatMessage)
|
||||
alert('receive')
|
||||
|
||||
setTimeout(()=>{
|
||||
let ChatMessage = _ChatMessage.fields.args[0]
|
||||
ChatMessage = this.fix_updatedAt(ChatMessage)
|
||||
// console.log('recivemessage', ChatMessage)
|
||||
|
||||
const message = this.prepareMessage(ChatMessage)
|
||||
this.lastMessage = message
|
||||
this.calDateDuration(ChatMessage._updatedAt)
|
||||
|
||||
const messageIsFound = this.messages.find((message) => {
|
||||
return message._id == ChatMessage._id
|
||||
})
|
||||
if (message.t == 'r') {
|
||||
this.name = message.msg
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if(this.isSenderIsNotMe(ChatMessage)) {
|
||||
this.NativeNotificationService.sendNotificationChat({
|
||||
message: message.msg,
|
||||
title: this.name
|
||||
});
|
||||
|
||||
|
||||
if(this.isSenderIsNotMe(ChatMessage)) {
|
||||
//this.addMessageDB(ChatMessage)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.addMessageDB(ChatMessage)
|
||||
|
||||
setTimeout(()=>{
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
}, 150)
|
||||
|
||||
}
|
||||
@@ -136,6 +133,7 @@ export class RoomService {
|
||||
if(message.fields.eventName == this.id+'/'+'typing') {
|
||||
|
||||
this.userThatIsTyping = this.usernameToDisplayName(message.fields.args[0])
|
||||
console.log(this.userThatIsTyping, 'this.userThatIsTyping')
|
||||
this.isTyping = message.fields.args[1]
|
||||
this.otherUserType = message.fields.args[1]
|
||||
|
||||
@@ -144,17 +142,90 @@ export class RoomService {
|
||||
})
|
||||
}
|
||||
|
||||
async addMessageDB(ChatMessage) {
|
||||
if (environment.chatOffline) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(!Array.isArray(messages)) {
|
||||
messages = []
|
||||
}
|
||||
|
||||
/* addMessageDB(ChatMessage) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(messages==null) messages = []
|
||||
|
||||
delete ChatMessage.temporaryData
|
||||
messages.push(ChatMessage)
|
||||
if(!ChatMessage._id && environment.chatOffline) {
|
||||
|
||||
this.storage.set('chatmsg' + this.id, messages)
|
||||
})
|
||||
} */
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async updateMessageDB(ChatMessage, localReference) {
|
||||
if (environment.chatOffline) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(!Array.isArray(messages)) {
|
||||
messages = []
|
||||
}
|
||||
|
||||
let index;
|
||||
const find = messages.find((message, _index)=> {
|
||||
if(message.localReference) {
|
||||
if(message.localReference == ChatMessage.localReference) {
|
||||
index = _index
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
if(find) {
|
||||
messages[index] = ChatMessage
|
||||
this.storage.set('chatmsg' + this.id, messages)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @description delete message in the DB. get all messages, delete then corresponding message and update the store
|
||||
* @param id message ID
|
||||
*/
|
||||
private deleteMessageFromDb(id) {
|
||||
if (environment.chatOffline) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(!Array.isArray(messages)) {
|
||||
messages = []
|
||||
}
|
||||
|
||||
messages.forEach((message, index) => {
|
||||
|
||||
if(message._id == id) {
|
||||
messages.splice(index, 1)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
this.storage.set('chatmsg' + this.id, messages).then((value) => {
|
||||
console.log('MSG SAVED ON STORAGE', value)
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async receiveMessageDelete() {
|
||||
|
||||
@@ -193,58 +264,47 @@ export class RoomService {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description delete message in the DB. get all messages, delete then corresponding message and update the store
|
||||
* @param id message ID
|
||||
*/
|
||||
private deleteMessageFromDb(id) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(messages==null) messages = []
|
||||
|
||||
messages.forEach((message, index) => {
|
||||
|
||||
if(message._id == id) {
|
||||
messages.splice(index, 1)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
this.storage.set('chatmsg' + this.id, messages).then((value) => {
|
||||
console.log('MSG SAVED ON STORAGE', value)
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @description sen text message
|
||||
*/
|
||||
async send({file = null, attachments = null, temporaryData = {}}) {
|
||||
|
||||
const localReference = uuidv4() + 'peter'
|
||||
|
||||
let offlineChatMessage = {
|
||||
rid: this.id,
|
||||
msg: this.message,
|
||||
attachments,
|
||||
file,
|
||||
temporaryData
|
||||
temporaryData,
|
||||
localReference
|
||||
}
|
||||
|
||||
console.log('offlineChatMessage', offlineChatMessage)
|
||||
const message: MessageService = this.prepareMessage(offlineChatMessage, environment.chatOffline)
|
||||
|
||||
//this.addMessageDB(offlineChatMessage)
|
||||
const message: MessageService = this.prepareMessage(offlineChatMessage)
|
||||
|
||||
setTimeout(() => {
|
||||
this.scrollDown()
|
||||
}, 150)
|
||||
|
||||
this.lastMessage = message
|
||||
message.send().then((ChatMessage) => {
|
||||
this.updateMessageDB(ChatMessage, localReference)
|
||||
})
|
||||
|
||||
message.send()
|
||||
|
||||
this.calDateDuration(message._updatedAt)
|
||||
this.sortRoomList()
|
||||
|
||||
if (environment.chatOffline) {
|
||||
this.messagesLocalReference.push(localReference)
|
||||
this.addMessageDB(offlineChatMessage)
|
||||
|
||||
setTimeout(() => {
|
||||
this.scrollDown()
|
||||
}, 150)
|
||||
|
||||
this.lastMessage = message
|
||||
this.calDateDuration(message._updatedAt)
|
||||
this.sortRoomList()
|
||||
}
|
||||
|
||||
this.message= ''
|
||||
}
|
||||
|
||||
@@ -262,7 +322,7 @@ export class RoomService {
|
||||
}
|
||||
|
||||
|
||||
typing(text:string = this.message) {
|
||||
sendTyping(text:string = this.message) {
|
||||
|
||||
if(this.lastMessageTxt == text) { return false }
|
||||
this.lastTimeType = new Date().getTime()
|
||||
@@ -302,7 +362,7 @@ export class RoomService {
|
||||
}
|
||||
|
||||
private setTypingOff() {
|
||||
this.typing('')
|
||||
this.sendTyping('')
|
||||
}
|
||||
|
||||
roomLeave() {
|
||||
@@ -329,48 +389,59 @@ export class RoomService {
|
||||
|
||||
|
||||
async restoreMessageFromDB() {
|
||||
await this.storage.get('chatmsg' + this.id).then( async (messages = []) => {
|
||||
if(messages==null) 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)
|
||||
} else {
|
||||
if(environment.chatOffline) alert('create offline')
|
||||
const offlineMessage = this.prepareMessage(ChatMessage)
|
||||
offlineMessage.send().then(()=>{
|
||||
this.updateMessageDB(ChatMessage, ChatMessage.localReference)
|
||||
})
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
setTimeout(()=> {
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// runs onces only
|
||||
async loadHistory(limit = 100) {
|
||||
async loadHistory({limit = 50, forceUpdate = false }) {
|
||||
|
||||
if (this.hasLoadHistory) { return false }
|
||||
|
||||
this.restoreMessageFromDB()
|
||||
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)
|
||||
this.messages = this.sortService.sortDate(this.messages, '_updatedAt')
|
||||
|
||||
const wewMessage = this.prepareMessage(message, false)
|
||||
})
|
||||
|
||||
if(wewMessage.offline == false) {
|
||||
this.prepareMessage(message)
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
this.storage.set('chatmsg' + this.id, chatHistory)
|
||||
this.storage.set('chatmsg' + this.id, chatHistory.result.messages)
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
@@ -386,22 +457,32 @@ 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) {
|
||||
if(this.hasLoadHistory && environment.chatOffline) alert('create offline')
|
||||
|
||||
this.messages.push(wewMessage)
|
||||
return wewMessage
|
||||
}
|
||||
|
||||
const found = this.messages.find((MessageService) => {
|
||||
if (MessageService._id == message._id) {
|
||||
if(this.hasLoadHistory) console.log(`${MessageService._id} == ${message._id}`)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
if (save && !found) {
|
||||
if(this.hasLoadHistory && environment.chatOffline) alert('not 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
|
||||
@@ -410,11 +491,9 @@ export class RoomService {
|
||||
|
||||
private fix_updatedAt(message) {
|
||||
if (message.result) {
|
||||
//console.log('FIX UPDATE ', message.result)
|
||||
message.result._updatedAt = message.result._updatedAt['$date']
|
||||
} else if(message._updatedAt) {
|
||||
if(message._updatedAt.hasOwnProperty('$date')) {
|
||||
// console.log('FIX UPDATE 11', message)
|
||||
message._updatedAt = message._updatedAt['$date']
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user