ChatStorage Done Done!

This commit is contained in:
Eudes Inácio
2022-01-21 16:55:05 +01:00
86 changed files with 458 additions and 622 deletions
+79 -22
View File
@@ -6,6 +6,8 @@ import { showDateDuration } from 'src/plugin/showDateDuration'
import { ToastsService } from '../toast.service';
import { chatHistory, ChatMessage } from 'src/app/models/chatMethod'
import { Storage } from '@ionic/storage';
import { Platform } from '@ionic/angular';
import { SqliteService } from 'src/app/services/sqlite.service';
@Injectable({
providedIn: 'root'
})
@@ -33,6 +35,8 @@ export class RoomService {
public WsChatService: WsChatService,
private MessageService: MessageService,
private storage: Storage,
private platform: Platform,
private sqlservice: SqliteService,
) { }
setData({ customFields, id, name, t, lastMessage, _updatedAt }) {
@@ -85,10 +89,55 @@ export class RoomService {
this.WsChatService.send(this.id, msg)
}
getMsgFromDBMobile() {
console.log('ALL MSG DBBB', this.id)
this.sqlservice.getAllChatMSG(this.id).then((msg: any = []) => {
let ad = [];
ad = msg
console.log('ALL MSG DBBB', ad.length)
msg.map(element => {
console.log('CHANNEL ELEMENT', element)
let msgChat = {
_id: element.Id,
attachments: this.isJson(element.Attachments),
channels: this.isJson(element.Channels),
file: {
guid: this.isJson(element.File).guid,
image_url: this.isJson(element.image_url),
type: this.isJson(element.File).type
},
mentions: this.isJson(element.Mentions),
msg: element.Msg,
rid: element.Rid,
ts: element.Ts,
u: this.isJson(element.U),
_updatedAt: this.isJson(element.UpdatedAt),
}
let mmessage = this.fix_updatedAt(msgChat)
console.log('FROM DB WEB', mmessage)
const wewMessage = new MessageService()
wewMessage.setData(mmessage)
this.massages.push(wewMessage)
console.log('loadHistory 222', this.massages)
});
});
}
isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return "";
}
return JSON.parse(str);
}
getMsgFromDB() {
this.storage.get('chatmsg' + this.id).then((message) => {
message.forEach(message => {
if (message.file) {
@@ -110,37 +159,45 @@ export class RoomService {
})
}
// runs onces only
loadHistory(limit = 100) {
if (this.hasLoadHistory) { return false }
// ionic store
/* this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => {
console.log('loadHistory', chatHistory)
// websocket
this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory: chatHistory) => {
//await this.transformData(chatHistory.result.messages.reverse());
//console.log('loadHistory 111', chatHistory.result.messages.reverse())
const mgsArray = chatHistory.result.messages.reverse();
await this.storage.remove('chatmsg' + this.id).then(() => {
console.log('MSG REMOVE ON STORAGE')
})
await this.storage.set('chatmsg' + this.id, mgsArray).then((value) => {
console.log('MSG SAVED ON STORAGE', value)
this.getMsgFromDB()
});
/* chatHistory.result.messages.reverse().forEach(message => {
chatHistory.result.messages.reverse().forEach(message => {
message = this.fix_updatedAt(message)
console.log('loadHistory', message)
this.storageMessage.push(message)
const wewMessage = new MessageService()
wewMessage.setData(message)
this.massages.push(wewMessage)
}); */
});
}) */
this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory: chatHistory) => {
const mgsArray = chatHistory.result.messages.reverse();
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
await this.storage.remove('chatmsg' + this.id).then(() => {
console.log('MSG REMOVE ON STORAGE')
})
await this.storage.set('chatmsg' + this.id, mgsArray).then((value) => {
console.log('MSG SAVED ON STORAGE', value)
this.getMsgFromDB()
});
} else {
mgsArray.forEach((element) => {
console.log('SQLITE WEBSOCKET', element)
this.sqlservice.addChatMSG(element)
})
this.getMsgFromDBMobile()
}
})