mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 13:26:08 +00:00
fix bug
This commit is contained in:
@@ -16,6 +16,7 @@ import { environment } from 'src/environments/environment';
|
||||
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'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -32,6 +33,7 @@ export class RoomService {
|
||||
name = ''
|
||||
_updatedAt = {}
|
||||
hasLoadHistory = false
|
||||
restoreFromOffline = false
|
||||
duration = ''
|
||||
isTyping = false
|
||||
otherUserType = false
|
||||
@@ -39,10 +41,10 @@ export class RoomService {
|
||||
message = ''
|
||||
lastMessageTxt = ''
|
||||
userThatIsTyping = ''
|
||||
private ToastService = ToastsService
|
||||
mgsArray = [];
|
||||
|
||||
messagesLocalReference = []
|
||||
members = []
|
||||
u
|
||||
|
||||
scrollDown = () => { }
|
||||
|
||||
@@ -66,21 +68,60 @@ export class RoomService {
|
||||
private NativeNotificationService: NativeNotificationService,
|
||||
private sortService: SortService,
|
||||
private chatService: ChatService,
|
||||
private NfService: NfService
|
||||
private NfService: NfService,
|
||||
private ChatStorageService: ChatStorageService
|
||||
) {
|
||||
this.NativeNotificationService.askForPermission()
|
||||
|
||||
this.restoreMessageFromDB()
|
||||
|
||||
this.WsChatService.getUserStatus((d) => {
|
||||
|
||||
const userId = d.fields.args[0][0]
|
||||
|
||||
if(this.members?.map) {
|
||||
const membersIds = this.members.map((user)=> user._id)
|
||||
|
||||
if(membersIds.includes(userId)) {
|
||||
this.messages.forEach((message, index) => {
|
||||
if(!message.messageOwnerById(userId)) {
|
||||
|
||||
if(!this.messages[index]?.received?.includes(userId)) {
|
||||
|
||||
|
||||
if(this.messages[index]._id) {
|
||||
try {
|
||||
this.messages[index].received.push(userId)
|
||||
} catch(e) {
|
||||
this.messages[index].received = [userId]
|
||||
}
|
||||
|
||||
this.messages[index].save()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
setData({ customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService), _updatedAt }) {
|
||||
setData({members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService), _updatedAt }) {
|
||||
this.customFields = customFields
|
||||
this.id = id
|
||||
this.name = name
|
||||
this.t = t
|
||||
this.lastMessage = lastMessage
|
||||
this._updatedAt = _updatedAt
|
||||
this.u = u
|
||||
this.members = members
|
||||
|
||||
this.calDateDuration()
|
||||
|
||||
this.restoreMessageFromDB()
|
||||
}
|
||||
|
||||
@@ -89,19 +130,26 @@ export class RoomService {
|
||||
return SessionStore.user.RochetChatUser != ChatMessage.u.username
|
||||
}
|
||||
|
||||
senderId(ChatMessage) {
|
||||
return ChatMessage.u._id
|
||||
}
|
||||
|
||||
receiveMessage() {
|
||||
|
||||
this.WsChatService.updateRoomEventss(
|
||||
this.id,
|
||||
"stream-room-messages",
|
||||
(_ChatMessage) => {
|
||||
async (_ChatMessage) => {
|
||||
console.log('recivemessage', _ChatMessage)
|
||||
|
||||
let ChatMessage = _ChatMessage.fields.args[0]
|
||||
ChatMessage = this.fix_updatedAt(ChatMessage)
|
||||
|
||||
if(!this.messagesLocalReference.includes(ChatMessage.localReference)) {
|
||||
const message = this.prepareMessage(ChatMessage)
|
||||
|
||||
const message = await this.prepareMessage({message: ChatMessage, save: true})
|
||||
message.messageSend = true
|
||||
|
||||
this.lastMessage = message
|
||||
this.calDateDuration(ChatMessage._updatedAt)
|
||||
|
||||
@@ -114,154 +162,63 @@ export class RoomService {
|
||||
message: message.msg,
|
||||
title: this.name
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
this.addMessageDB(ChatMessage)
|
||||
|
||||
message.addMessageDB()
|
||||
|
||||
setTimeout(()=>{
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
}
|
||||
} else {
|
||||
|
||||
this.messages.forEach((message, index)=> {
|
||||
if(message.localReference == ChatMessage.localReference) {
|
||||
|
||||
const membersIds = this.members.map((user)=> user._id)
|
||||
|
||||
this.getAllUsers().forEach( async (users) => {
|
||||
|
||||
if(membersIds.includes(users._id)) {
|
||||
|
||||
if(users.status != 'offline') {
|
||||
|
||||
this.messages[index].received.push(users._id)
|
||||
setTimeout(() => {
|
||||
message.save()
|
||||
}, 150)
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
this.WsChatService.receiveStreamNotifyRoom((message) => {
|
||||
|
||||
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]
|
||||
const args = message.fields.args
|
||||
if (typeof args[0] != 'object') {
|
||||
this.userThatIsTyping = this.usernameToDisplayName(args[0])
|
||||
console.log(this.userThatIsTyping, 'this.userThatIsTyping')
|
||||
this.isTyping = args[1]
|
||||
this.otherUserType = args[1]
|
||||
this.readAllMessage()
|
||||
} else {
|
||||
this.readAllMessage()
|
||||
}
|
||||
|
||||
|
||||
} else if (message.fields.eventName == this.id+'/'+'deleteMessage') {}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
async addMessageDB(ChatMessage) {
|
||||
if (environment.chatOffline) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(!Array.isArray(messages)) {
|
||||
messages = []
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async updateViewedMessage(id, userId) {
|
||||
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._id == id) {
|
||||
index = _index
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
if(find) {
|
||||
|
||||
if(!messages[index].hasOwnProperty('viewed') || !Array.isArray(messages[index].viewed)) {
|
||||
messages.viewed = []
|
||||
}
|
||||
|
||||
messages.viewed.push(userId)
|
||||
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() {
|
||||
|
||||
@@ -286,9 +243,10 @@ export class RoomService {
|
||||
deleteMessage(id) {
|
||||
this.messages.forEach((message, index) => {
|
||||
if(message._id == id) {
|
||||
|
||||
this.messages.splice(index, 1)
|
||||
message.delete()
|
||||
|
||||
this.deleteMessageFromDb(id)
|
||||
//Get previous last message from room
|
||||
const previousLastMessage = this.messages.slice(-1)[0];
|
||||
|
||||
@@ -298,6 +256,7 @@ export class RoomService {
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -314,24 +273,17 @@ export class RoomService {
|
||||
attachments,
|
||||
file,
|
||||
temporaryData,
|
||||
localReference,
|
||||
viewed: [],
|
||||
received: []
|
||||
localReference
|
||||
}
|
||||
|
||||
const message: MessageService = await this.prepareMessage({message:offlineChatMessage, save: environment.chatOffline})
|
||||
|
||||
const message: MessageService = this.prepareMessage(offlineChatMessage, environment.chatOffline)
|
||||
|
||||
/**
|
||||
* @description redefine message offline data "offlineChatMessage" with live ChatMessage
|
||||
*/
|
||||
message.send().then((ChatMessage) => {
|
||||
this.updateMessageDB(ChatMessage, localReference)
|
||||
})
|
||||
|
||||
this.messagesLocalReference.push(localReference)
|
||||
await message.addMessageDB()
|
||||
message.send()
|
||||
|
||||
|
||||
if (environment.chatOffline) {
|
||||
this.messagesLocalReference.push(localReference)
|
||||
this.addMessageDB(offlineChatMessage)
|
||||
|
||||
setTimeout(() => {
|
||||
this.scrollDown()
|
||||
@@ -346,25 +298,6 @@ export class RoomService {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param message
|
||||
* @param ChatMessage
|
||||
* @description when creating message we use offline data, then we need redefined with live data
|
||||
*/
|
||||
redefinedMessage (message: MessageService, ChatMessage) {
|
||||
|
||||
ChatMessage = this.fix_updatedAt(ChatMessage)
|
||||
|
||||
message.setData(ChatMessage)
|
||||
|
||||
if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) {
|
||||
this.lastMessage = message
|
||||
this.calDateDuration(message._updatedAt)
|
||||
}
|
||||
this.sortRoomList()
|
||||
}
|
||||
|
||||
|
||||
sendTyping(text:string = this.message) {
|
||||
|
||||
@@ -387,6 +320,10 @@ export class RoomService {
|
||||
this.typingWatch()
|
||||
}
|
||||
|
||||
sendFalseTypingReadMessage() {
|
||||
this.WsChatService.sendStreamNotifyRoom(this.id, SessionStore.user.RochetChatUser, 'typing', {})
|
||||
this.setTypingOff()
|
||||
}
|
||||
|
||||
private typingWatch() {
|
||||
setTimeout(()=>{
|
||||
@@ -440,19 +377,25 @@ export class RoomService {
|
||||
messages = []
|
||||
}
|
||||
|
||||
// console.log('offline messages', messages)
|
||||
|
||||
await messages.forEach( async (ChatMessage, index) => {
|
||||
const wewMessage = this.prepareMessage(ChatMessage, false)
|
||||
|
||||
|
||||
// if(ChatMessage.msg.includes('***********')) {
|
||||
// console.log('restore ========')
|
||||
// console.log(JSON.stringify(ChatMessage))
|
||||
// }
|
||||
|
||||
const wewMessage = await this.prepareMessage({message:ChatMessage, save: false})
|
||||
|
||||
if(wewMessage.offline == false) {
|
||||
this.prepareMessage(ChatMessage)
|
||||
} else {
|
||||
const offlineMessage = this.prepareMessage(ChatMessage)
|
||||
const offlineMessage = await this.prepareMessage({message:ChatMessage, save: true})
|
||||
|
||||
this.messagesLocalReference.push(offlineMessage.localReference)
|
||||
offlineMessage.send().then((newChatMessage) => {
|
||||
this.updateMessageDB(newChatMessage, ChatMessage.localReference)
|
||||
})
|
||||
|
||||
// offlineMessage.send()
|
||||
}
|
||||
|
||||
});
|
||||
@@ -467,7 +410,7 @@ export class RoomService {
|
||||
}
|
||||
|
||||
// runs onces only
|
||||
async loadHistory({limit = 50, forceUpdate = false }) {
|
||||
async loadHistory({limit = 10000000, forceUpdate = false }) {
|
||||
|
||||
if(forceUpdate == false) {
|
||||
if (this.hasLoadHistory) {
|
||||
@@ -475,15 +418,23 @@ export class RoomService {
|
||||
}
|
||||
}
|
||||
|
||||
if(this.restoreFromOffline == false) {
|
||||
this.restoreFromOffline = true
|
||||
await this.restoreMessageFromDB()
|
||||
}
|
||||
|
||||
await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => {
|
||||
|
||||
await chatHistory.result.messages.reverse().forEach( async (message) => {
|
||||
|
||||
this.prepareMessage(message)
|
||||
// this.messages = this.sortService.sortDate(this.messages, '_updatedAt')
|
||||
// await this.prepareMessage({message})
|
||||
|
||||
})
|
||||
|
||||
// console.log('load history ',chatHistory)
|
||||
await this.readMessage(chatHistory)
|
||||
//await this.updateAllMessages()
|
||||
|
||||
this.storage.set('chatmsg' + this.id, chatHistory.result.messages)
|
||||
})
|
||||
|
||||
@@ -494,6 +445,72 @@ export class RoomService {
|
||||
this.hasLoadHistory = true
|
||||
}
|
||||
|
||||
async readMessage(chatHistory) {
|
||||
|
||||
const firstUnread = chatHistory.result.firstUnread
|
||||
let foundUnread = false
|
||||
|
||||
// if (chatHistory.result.unreadNotLoaded == 0) {
|
||||
// const membersIds = this.members.map((user)=> user._id)
|
||||
|
||||
// this.messages.forEach((message, index) => {
|
||||
// if(message.viewed.length == 0) {
|
||||
// this.messages[index].viewed = membersIds
|
||||
// }
|
||||
// })
|
||||
|
||||
// } else {
|
||||
// chatHistory.result.messages.forEach((message) => {
|
||||
// console.log(message._id == firstUnread._id)
|
||||
// if(message._id == firstUnread._id) {
|
||||
// foundUnread = true
|
||||
// console.log('found=============================')
|
||||
// } else {
|
||||
// console.log('message')
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
async readAllMessage() {
|
||||
|
||||
|
||||
const membersIds = this.members.map((user)=> user._id)
|
||||
|
||||
await this.messages.forEach( async (message, index) => {
|
||||
if(message._id) {
|
||||
if(message.viewed.length == 0) {
|
||||
this.messages[index].viewed = membersIds
|
||||
this.messages[index].received = membersIds
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
this.updateAllMessages()
|
||||
}
|
||||
|
||||
|
||||
updateAllMessages () {
|
||||
const newHistory = this.messages.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
|
||||
}
|
||||
})
|
||||
|
||||
this.ChatStorageService.updateChat(newHistory, this.id)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description find or create message
|
||||
@@ -501,32 +518,40 @@ export class RoomService {
|
||||
* @param save
|
||||
* @returns
|
||||
*/
|
||||
prepareMessage(message, save = true): MessageService {
|
||||
async prepareMessage({message, save = true, redefined = false}): Promise<MessageService> {
|
||||
message = this.fix_updatedAt(message)
|
||||
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService)
|
||||
|
||||
|
||||
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService)
|
||||
wewMessage.setData(message)
|
||||
wewMessage.loadHistory = this.hasLoadHistory
|
||||
|
||||
if(!message._id && environment.chatOffline && save) {
|
||||
|
||||
if(!message?._id && environment.chatOffline && save) {
|
||||
this.messages.push(wewMessage)
|
||||
return wewMessage
|
||||
}
|
||||
|
||||
const found = this.messages.find((MessageService) => {
|
||||
let foundIndex;
|
||||
|
||||
const found = this.messages.find((MessageService, index) => {
|
||||
if (MessageService._id == message._id) {
|
||||
if(this.hasLoadHistory) console.log(`${MessageService._id} == ${message._id}`)
|
||||
foundIndex = index
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
if (save && !found) {
|
||||
if (save || !found) {
|
||||
this.messages.push(wewMessage)
|
||||
return wewMessage
|
||||
} else{
|
||||
|
||||
if(redefined) {
|
||||
}
|
||||
return this.messages[foundIndex]
|
||||
}
|
||||
|
||||
return wewMessage
|
||||
}
|
||||
|
||||
private calDateDuration(date = null) {
|
||||
@@ -534,11 +559,10 @@ export class RoomService {
|
||||
this._updatedAt = date || this._updatedAt
|
||||
}
|
||||
|
||||
|
||||
private fix_updatedAt(message) {
|
||||
if (message.result) {
|
||||
if (message?.result) {
|
||||
message.result._updatedAt = message.result._updatedAt['$date']
|
||||
} else if(message._updatedAt) {
|
||||
} else if(message?._updatedAt) {
|
||||
if(message._updatedAt.hasOwnProperty('$date')) {
|
||||
message._updatedAt = message._updatedAt['$date']
|
||||
}
|
||||
@@ -546,7 +570,6 @@ export class RoomService {
|
||||
return message
|
||||
}
|
||||
|
||||
|
||||
usernameToDisplayName(username) {
|
||||
|
||||
const firstName = capitalizeTxt(username.split('.')[0])
|
||||
@@ -554,4 +577,9 @@ export class RoomService {
|
||||
return firstName + ' ' + lastName
|
||||
}
|
||||
|
||||
sendReadMessage() {
|
||||
this.WsChatService.readMessage(this.id)
|
||||
this.sendFalseTypingReadMessage()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user