This commit is contained in:
Peter Maquiran
2022-09-30 15:27:07 +01:00
parent 12cf5831a8
commit baff5d96ff
5 changed files with 6 additions and 220 deletions
@@ -1,16 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { ChatStorageService } from './chat-storage.service';
describe('ChatStorageService', () => {
let service: ChatStorageService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ChatStorageService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -1,192 +0,0 @@
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class ChatStorageService {
constructor(
private storage: Storage,
) { }
/**
* @description delete message in the DB. get all messages, delete then corresponding message and update the store
* @param id message ID
*/
async deleteMessageFromDb(messageId, roomId) {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
await messages.forEach( async (message, index) => {
if(message._id == messageId) {
messages.splice(index, 1)
}
})
this.storage.set('chatmsg' + roomId, messages).then((value) => {
//
});
})
}
}
async updateMessageDB(ChatMessage, roomId, identificator) {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
let index;
const find = messages.find((message, _index)=> {
if(message?.localReference == ChatMessage?.localReference ||
message?._id == ChatMessage?._id) {
index = _index
return true
}
return false
})
if(find) {
messages[index] = Object.assign(messages[index], ChatMessage)
await this.storage.set('chatmsg' + roomId, messages)
} else {
//
}
})
}
}
async updateChat(history, roomId, identificator = '_id') {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
history.forEach( async(ChatMessage)=>{
let index;
const find = messages.find((message, _index)=> {
if(message[identificator]) {
if(message[identificator] == ChatMessage[identificator]) {
index = _index
return true
}
}
return false
})
if(find) {
messages[index] = Object.assign(messages[index], ChatMessage)
// if(messages[index].msg.includes('***********')) {
//
//
//
// }
await this.storage.set('chatmsg' + roomId, messages)
} else {
}
})
})
}
}
async addMessageDB(ChatMessage, roomId) {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
if(!ChatMessage._id && environment.chatOffline) {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
await this.storage.set('chatmsg' + roomId, messages)
//
} else {
const find = messages.find((message)=> {
return message._id == ChatMessage._id
})
if(!find) {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
await this.storage.set('chatmsg' + roomId, messages)
//
} else {
}
}
})
}
}
async addManyMessageDB(_ChatMessage: any[], roomId) {
if (environment.chatOffline) {
await this.storage.get('chatmsg' + roomId).then(async(messages: any = []) => {
if(!Array.isArray(messages)) {
messages = []
}
await _ChatMessage.forEach(async(ChatMessage)=>{
if(!ChatMessage._id && environment.chatOffline) {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
//
} else {
const find = messages.find((message)=> {
return message._id == ChatMessage._id
})
if(!find) {
delete ChatMessage.temporaryData
messages.push(ChatMessage)
//
}
}
})
await this.storage.set('chatmsg' + roomId, messages)
})
}
}
}
+1 -3
View File
@@ -14,7 +14,6 @@ import { SortService } from '../functions/sort.service';
import { chatUser } from 'src/app/models/chatMethod';
import { NfService } from 'src/app/services/chat/nf.service'
import { ChangeProfileService } from '../change-profile.service';
import { ChatStorageService } from './chat-storage.service';
import { ChatMethodsService } from './chat-methods.service';
import { AESEncrypt } from '../aesencrypt.service'
import { AttachmentsService } from 'src/app/services/attachments.service';
@@ -55,7 +54,6 @@ export class ChatSystemService {
private NfService: NfService,
private changeProfileService: ChangeProfileService,
private chatService: ChatService,
private ChatStorageService: ChatStorageService,
private ChatMethodsService:ChatMethodsService,
private AESEncrypt: AESEncrypt,
private AttachmentsService:AttachmentsService,
@@ -389,7 +387,7 @@ export class ChatSystemService {
if (setData.name != 'Rocket Cat') {
// create room
if(!this.roomExist(roomId)) {
let room:RoomService = new RoomService(this.RochetChatConnectorService, new MessageService(this.storage, this.NfService, this.RochetChatConnectorService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService, this.NfService , this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this)
let room:RoomService = new RoomService(this.RochetChatConnectorService, new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService, this.NfService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService, this)
room.setData(setData)
room.receiveMessage()
room.getAllUsers = this.getUsers
+1 -3
View File
@@ -6,7 +6,6 @@ import { capitalizeTxt } from 'src/plugin/text'
import { NfService } from 'src/app/services/chat/nf.service'
import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service';
import { showDateDuration } from 'src/plugin/showDateDuration';
import { ChatStorageService } from './chat-storage.service'
import { ChatMethodsService } from './chat-methods.service'
import { MessageModel } from '../../models/beast-orm'
import { AESEncrypt } from '../aesencrypt.service'
@@ -62,10 +61,9 @@ export class MessageService {
downloadAttachmentsTemp = 0;
UploadAttachmentsTemp = 0;
constructor(private storage: Storage,
constructor(
private NfService: NfService,
private RochetChatConnectorService: RochetChatConnectorService,
private ChatStorageService: ChatStorageService,
private ChatMethodsService: ChatMethodsService,
private AESEncrypt: AESEncrypt,
private AttachmentsService: AttachmentsService,
+4 -6
View File
@@ -15,7 +15,6 @@ 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';
import { ChatMethodsService } from './chat-methods.service';
import { DeleteMessageModel, MessageModel } from '../../models/beast-orm';
import { AESEncrypt } from '../aesencrypt.service';
@@ -88,7 +87,6 @@ export class RoomService {
private sortService: SortService,
private chatService: ChatService,
private NfService: NfService,
private ChatStorageService: ChatStorageService,
private ChatMethodsService: ChatMethodsService,
private AESEncrypt: AESEncrypt,
private AttachmentsService: AttachmentsService,
@@ -181,7 +179,7 @@ export class RoomService {
}
}
setData({membersExcludeMe, members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.RochetChatConnectorService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService), _updatedAt }) {
setData({membersExcludeMe, members, u, customFields = {}, id, name, t, lastMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService), _updatedAt }) {
this.customFields = customFields
this.id = id
this.name = name
@@ -807,7 +805,7 @@ export class RoomService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.RochetChatConnectorService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService)
const wewMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
@@ -864,7 +862,7 @@ export class RoomService {
async prepareCreate({message, save = true}): Promise<MessageService> {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.RochetChatConnectorService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService)
const wewMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
@@ -902,7 +900,7 @@ export class RoomService {
simplePrepareMessage(message) {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.RochetChatConnectorService, this.ChatStorageService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService)
const wewMessage = new MessageService(this.NfService, this.RochetChatConnectorService, this.ChatMethodsService, this.AESEncrypt, this.AttachmentsService, this.NetworkServiceService)
wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory