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()
}
})
@@ -6,6 +6,8 @@ import { SessionStore } from 'src/app/store/session.service';
import { capitalizeTxt } from 'src/plugin/text'
import { Rooms, Update as room } 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'
@@ -24,7 +26,9 @@ export class WsChatMethodsService {
constructor(
private WsChatService: WsChatService,
private storage: Storage
private storage: Storage,
private platform: Platform,
private sqlservice: SqliteService,
) {
(async()=>{
await this.getAllRooms();
@@ -47,7 +51,7 @@ export class WsChatMethodsService {
//console.log(roomData);
room = new RoomService(this.WsChatService, new MessageService(), this.storage)
room = new RoomService(this.WsChatService, new MessageService(), this.storage, this.platform, this.sqlservice)
room.setData({
customFields: roomData.customFields,
id: this.getRoomId(roomData),
@@ -51,13 +51,13 @@
<ion-item>
<ion-label position="stacked">Data Início</ion-label>
<ion-datetime value="{{loadedEvent.StartDate}}" [(ngModel)]="loadedEvent.StartDate" min="2020" max="2100"
displayFormat="D MMM YYYY H:mm" minuteValues="0,15,30,45"
displayFormat="D MMM YYYY H:mm" minuteValues="0,5,10,15,20,25,30,35,40,45,50,55"
monthShortNames="Jan, Fev, Mar, Abr, Mai, Jun, Jul, Aug, Sep, Out, Nov, Dez"></ion-datetime>
</ion-item>
<ion-item>
<ion-label position="stacked">Data Fim</ion-label>
<ion-datetime value="{{loadedEvent.EndDate}}" [(ngModel)]="loadedEvent.EndDate" min="2020" max="2100"
displayFormat="D MMM YYYY HH:mm" minuteValues="0,15,30,45"
displayFormat="D MMM YYYY HH:mm" minuteValues="0,5,10,15,20,25,30,35,40,45,50,55"
monthShortNames="Jan, Fev, Mar, Abr, Mai, Jun, Jul, Aug, Sep, Out, Nov, Dez"></ion-datetime>
</ion-item>
@@ -53,13 +53,13 @@
<ion-item>
<ion-label position="stacked">Data Início</ion-label>
<ion-datetime value="{{loadedEvent.StartDate}}" [(ngModel)]="loadedEvent.StartDate" min="2020" max="2100"
displayFormat="D MMM YYYY H:mm" minuteValues="0,15,30,45"
displayFormat="D MMM YYYY H:mm" minuteValues="0,5,10,15,20,25,30,35,40,45,50,55"
monthShortNames="Jan, Fev, Mar, Abr, Mai, Jun, Jul, Aug, Sep, Out, Nov, Dez"></ion-datetime>
</ion-item>
<ion-item>
<ion-label position="stacked">Data Fim</ion-label>
<ion-datetime value="{{loadedEvent.EndDate}}" [(ngModel)]="loadedEvent.EndDate" min="2020" max="2100"
displayFormat="D MMM YYYY HH:mm" minuteValues="0,15,30,45"
displayFormat="D MMM YYYY HH:mm" minuteValues="0,5,10,15,20,25,30,35,40,45,50,55"
monthShortNames="Jan, Fev, Mar, Abr, Mai, Jun, Jul, Aug, Sep, Out, Nov, Dez"></ion-datetime>
</ion-item>
@@ -53,13 +53,13 @@
<ion-item>
<ion-label position="stacked">Data Início</ion-label>
<ion-datetime value="{{loadedEvent.StartDate}}" [(ngModel)]="loadedEvent.StartDate" min="2020" max="2100"
displayFormat="D MMM YYYY H:mm" minuteValues="0,15,30,45"
displayFormat="D MMM YYYY H:mm" minuteValues="0,5,10,15,20,25,30,35,40,45,50,55"
monthShortNames="Jan, Fev, Mar, Abr, Mai, Jun, Jul, Aug, Sep, Out, Nov, Dez"></ion-datetime>
</ion-item>
<ion-item>
<ion-label position="stacked">Data Fim</ion-label>
<ion-datetime value="{{loadedEvent.EndDate}}" [(ngModel)]="loadedEvent.EndDate" min="2020" max="2100"
displayFormat="D MMM YYYY HH:mm" minuteValues="0,15,30,45"
displayFormat="D MMM YYYY HH:mm" minuteValues="0,5,10,15,20,25,30,35,40,45,50,55"
monthShortNames="Jan, Fev, Mar, Abr, Mai, Jun, Jul, Aug, Sep, Out, Nov, Dez"></ion-datetime>
</ion-item>
+1 -1
View File
@@ -280,7 +280,7 @@ export class SqliteService {
console.log('INSIDE DB CHAT MSG',data,)
this.dbInstance.executeSql(`
INSERT OR IGNORE INTO ${this.chatmsg} (Id,Attachments,Channels,File,Mentions,Msg,Rid, Ts ,U, UpdatedAt)
VALUES ('${data._id}','${JSON.stringify(data.attachments)}','${JSON.stringify(data.channels)}','${JSON.stringify(data.file)}','${JSON.stringify(data.mentions)}','${data.msg}','${data.rid}','${data.ts}','${JSON.stringify(data.u)}','${data._updatedAt}')`, [])
VALUES ('${data._id}','${JSON.stringify(data.attachments)}','${JSON.stringify(data.channels)}','${JSON.stringify(data.file)}','${JSON.stringify(data.mentions)}','${data.msg}','${data.rid}','${data.ts}','${JSON.stringify(data.u)}','${JSON.stringify(data._updatedAt)}')`, [])
.then(() => {
console.log("chat msg add with Success");