change storage package to beast-rom

This commit is contained in:
Peter Maquiran
2022-03-10 23:08:29 +01:00
parent f3aabae9c7
commit c2b1a54a6e
18 changed files with 256 additions and 185 deletions
+100 -111
View File
@@ -18,6 +18,10 @@ 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'
import { MessageModel } from '../../models/beast-orm'
import { AESEncrypt } from '../aesencrypt.service'
import { IncomingChatMessage, ChatMessageInterface } from 'src/app/models/message.model';
@Injectable({
providedIn: 'root'
})
@@ -70,11 +74,12 @@ export class RoomService {
private chatService: ChatService,
private NfService: NfService,
private ChatStorageService: ChatStorageService,
private ChatMethodsService: ChatMethodsService
private ChatMethodsService: ChatMethodsService,
private AESEncrypt: AESEncrypt
) {
this.NativeNotificationService.askForPermission()
this.restoreMessageFromDB()
// this.restoreMessageFromDB()
this.WsChatService.getUserStatus((d) => {
@@ -92,11 +97,12 @@ export class RoomService {
if(this.messages[index]._id) {
try {
this.messages[index].received.push(userId)
if(!this.messages[index].received.includes(userId)) {
this.messages[index].received.push(userId)
}
} catch(e) {
this.messages[index].received = [userId]
}
}
this.messages[index].save()
}
@@ -109,7 +115,7 @@ export class RoomService {
}
setData({members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService), _updatedAt }) {
setData({members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt), _updatedAt }) {
this.customFields = customFields
this.id = id
this.name = name
@@ -137,11 +143,11 @@ export class RoomService {
this.WsChatService.updateRoomEventss(
this.id,
"stream-room-messages",
async (_ChatMessage) => {
console.log('recivemessage', _ChatMessage)
async (IncomingChatMessage:IncomingChatMessage) => {
// console.log('recivemessage========', JSON.stringify(IncomingChatMessage))
let ChatMessage = _ChatMessage.fields.args[0]
ChatMessage = this.fix_updatedAt(ChatMessage)
let IncomingChatMessageArgs = IncomingChatMessage.fields.args[0]
let ChatMessage : ChatMessageInterface = this.fix_updatedAt(IncomingChatMessageArgs)
if(!this.messagesLocalReference.includes(ChatMessage.localReference)) {
@@ -208,6 +214,7 @@ export class RoomService {
this.otherUserType = args[1]
this.readAllMessage()
} else {
this.readAllMessage()
}
@@ -218,6 +225,30 @@ export class RoomService {
}
getRoomMembersIds(): string[] {
return this.members.map((user)=> user._id)
}
getAllMemberThatIsNotOffline(): string[] {
const membersIds = this.getRoomMembersIds()
const allChatUsers = this.getAllUsers()
const AllMemberThatIsNotOffline = []
for(let user of allChatUsers) {
if(membersIds.includes(user._id)) {
if(user.status != 'offline') {
AllMemberThatIsNotOffline.push(user._id)
}
}
}
return AllMemberThatIsNotOffline
}
async receiveMessageDelete() {
this.WsChatService.updateRoomEventss(
@@ -243,7 +274,8 @@ export class RoomService {
if(message._id == id) {
this.messages.splice(index, 1)
message.delete()
const allMemberThatIsNotOffline = this.getAllMemberThatIsNotOffline()
message.delete(allMemberThatIsNotOffline)
//Get previous last message from room
const previousLastMessage = this.messages.slice(-1)[0];
@@ -371,32 +403,36 @@ export class RoomService {
async restoreMessageFromDB() {
if(environment.chatOffline) {
await this.storage.get('chatmsg' + this.id).then( async (messages = []) => {
if(!Array.isArray(messages)) {
messages = []
}
const messages = await MessageModel.filter({rid:this.id}).execute()
await messages.forEach( async (ChatMessage, index) => {
await messages.forEach( async (ChatMessage, index) => {
const wewMessage = await this.simplePrepareMessage(ChatMessage)
const wewMessage = await this.simplePrepareMessage(ChatMessage)
if(wewMessage.offline == false) {
this.prepareMessage({message:ChatMessage})
} else {
if(wewMessage.offline == false) {
const message = await this.prepareMessageCreateIfNotExist({message:ChatMessage})
const offlineMessage = await this.prepareMessageCreateIfNotExist({message:ChatMessage})
message?.decryptMessage()
} else {
const offlineMessage = await this.prepareMessageCreateIfNotExist({message:ChatMessage})
if(offlineMessage) {
this.messagesLocalReference.push(offlineMessage.localReference)
offlineMessage.send()
offlineMessage?.decryptMessage()
offlineMessage.send()
}
});
}
setTimeout(()=> {
this.scrollDown()
}, 50)
});
})
setTimeout(()=> {
this.scrollDown()
}, 50)
}
}
@@ -417,41 +453,20 @@ export class RoomService {
await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => {
// await chatHistory.result.messages.reverse().forEach( async (message) => {})
const messagesId = this.messages.map((message)=> message._id)
const createdMsgIds = []
chatHistory.result.messages.reverse().forEach(async(message) => {
chatHistory.result.messages.reverse().forEach(async(message: any) => {
if (!messagesId.includes(message._id)) {
createdMsgIds.push(message._id)
await this.prepareMessageCreateIfNotExist({message: message});
const messagesToSave = await this.prepareMessageCreateIfNotExist({message: message});
if(messagesToSave) {
await messagesToSave.addMessageDB()
}
}
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)
})
await this.readMessage(chatHistory)
this.storage.set('chatmsg' + this.id, chatHistory.result.messages)
})
setTimeout(() => {
@@ -461,70 +476,26 @@ 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)
console.log('read all ===========')
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
await this.messages[index].save()
}
}
})
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)
}
/**
@@ -537,13 +508,11 @@ export class RoomService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt)
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
}
@@ -573,6 +542,27 @@ export class RoomService {
}
async ChatMessageIsPresentInTheView(ChatMessage:ChatMessageInterface) {
let foundIndex;
const found = this.messages.find((MessageService, index) => {
if (MessageService._id == ChatMessage._id) {
foundIndex = index
return true
} else {
return false
}
})
if (foundIndex) {
return { found, foundIndex}
} else {
return false
}
}
/**
* @description find or create message
* @param message
@@ -582,7 +572,7 @@ export class RoomService {
async prepareCreate({message}): Promise<MessageService> {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
@@ -595,10 +585,9 @@ export class RoomService {
}
simplePrepareMessage(message) {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
@@ -624,7 +613,7 @@ export class RoomService {
this.messages.push(wewMessage)
return wewMessage
} else {
return this.messages[foundIndex]
return null
}
}
@@ -633,7 +622,7 @@ export class RoomService {
this._updatedAt = date || this._updatedAt
}
private fix_updatedAt(message) {
private fix_updatedAt(message): ChatMessageInterface {
if (message?.result) {
message.result._updatedAt = message.result._updatedAt['$date']
} else if(message?._updatedAt) {