mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 20:47:54 +00:00
Websocket with storage
This commit is contained in:
@@ -12,28 +12,30 @@ import { Storage } from '@ionic/storage';
|
||||
export class RoomService {
|
||||
|
||||
massages: MessageService[] = []
|
||||
storageMessage: any[] = [];
|
||||
lastMessage: MessageService;
|
||||
|
||||
chatUser: ChatUserService[] = []
|
||||
customFields:any;
|
||||
customFields: any;
|
||||
id = ''
|
||||
t = ''
|
||||
name = ''
|
||||
_updatedAt = {}
|
||||
private hasLoadHistory = false
|
||||
duration = ''
|
||||
duration = ''
|
||||
|
||||
private ToastService = ToastsService
|
||||
mgsArray = [];
|
||||
|
||||
scrollDown = () => {}
|
||||
scrollDown = () => { }
|
||||
|
||||
constructor(
|
||||
public WsChatService: WsChatService,
|
||||
private MessageService: MessageService,
|
||||
private storage: Storage,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
setData({customFields, id, name, t, lastMessage, _updatedAt}) {
|
||||
setData({ customFields, id, name, t, lastMessage, _updatedAt }) {
|
||||
this.customFields = customFields
|
||||
this.id = id
|
||||
this.name = name
|
||||
@@ -57,11 +59,11 @@ export class RoomService {
|
||||
const message = new MessageService()
|
||||
message.setData(ChatMessage)
|
||||
this.lastMessage.msg = message.msg
|
||||
if(message.t == 'r'){this.name = message.msg}
|
||||
if (message.t == 'r') { this.name = message.msg }
|
||||
this.calDateDuration(ChatMessage._updatedAt)
|
||||
this.massages.push(message)
|
||||
|
||||
setTimeout(()=>{
|
||||
setTimeout(() => {
|
||||
this.scrollDown()
|
||||
}, 100)
|
||||
|
||||
@@ -74,19 +76,34 @@ export class RoomService {
|
||||
}
|
||||
|
||||
// runs onces only
|
||||
loadHistory(limit= 100) {
|
||||
loadHistory(limit = 100) {
|
||||
|
||||
if(this.hasLoadHistory){ return false}
|
||||
if (this.hasLoadHistory) { return false }
|
||||
|
||||
this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory:chatHistory) => {
|
||||
this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory: chatHistory) => {
|
||||
|
||||
await this.transformData(chatHistory.result.messages.reverse());
|
||||
//await this.transformData(chatHistory.result.messages.reverse());
|
||||
/* console.log('loadHistory 111', chatHistory.result.messages.reverse())
|
||||
this.mgsArray = chatHistory.result.messages.reverse();
|
||||
await this.storage.set('chatmsg', this.mgsArray).then((value) => {
|
||||
console.log('MSG SAVED ON STORAGE', value)
|
||||
}); */
|
||||
|
||||
/* await this.storage.get('chatmsg').then((message) => {
|
||||
message.forEach(message => {
|
||||
console.log('FROM DB WEB', message)
|
||||
message = this.fix_updatedAt(message)
|
||||
const wewMessage = new MessageService()
|
||||
wewMessage.setData(message)
|
||||
this.massages.push(wewMessage)
|
||||
console.log('loadHistory 222', this.massages)
|
||||
});
|
||||
}) */
|
||||
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)
|
||||
@@ -95,18 +112,27 @@ export class RoomService {
|
||||
|
||||
})
|
||||
|
||||
setTimeout(()=>{
|
||||
setTimeout(() => {
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
|
||||
this.hasLoadHistory = true
|
||||
}
|
||||
|
||||
|
||||
updateMeessage(messageID, imgbase64) {
|
||||
|
||||
}
|
||||
|
||||
async returnData(res) {
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async transformData(res) {
|
||||
|
||||
let mgsArray = [];
|
||||
|
||||
this.mgsArray = [];
|
||||
res.forEach(async element => {
|
||||
console.log('TRANSFORM DATA ELEMENT' ,element)
|
||||
|
||||
if (element.file) {
|
||||
if (element.file.guid) {
|
||||
@@ -125,11 +151,11 @@ export class RoomService {
|
||||
rid: element.rid,
|
||||
ts: element.ts,
|
||||
u: element.u,
|
||||
_updatedAt: element._updatedAt,
|
||||
_updatedAt: element._updatedAt ,
|
||||
|
||||
}
|
||||
|
||||
mgsArray.push(this.fix_updatedAt(chatmsg));
|
||||
this.mgsArray.push(chatmsg);
|
||||
|
||||
})
|
||||
} else {
|
||||
@@ -145,8 +171,8 @@ export class RoomService {
|
||||
u: element.u,
|
||||
_updatedAt: element._updatedAt,
|
||||
}
|
||||
|
||||
mgsArray.push(this.fix_updatedAt(chatmsg))
|
||||
|
||||
this.mgsArray.push(chatmsg)
|
||||
}
|
||||
} else {
|
||||
let chatmsg = {
|
||||
@@ -161,20 +187,20 @@ export class RoomService {
|
||||
_updatedAt: element._updatedAt,
|
||||
}
|
||||
|
||||
mgsArray.push(this.fix_updatedAt(chatmsg))
|
||||
this.mgsArray.push(chatmsg)
|
||||
}
|
||||
|
||||
});
|
||||
await this.storage.remove('chatmsg').then(() => {
|
||||
console.log('MSG REMOVE FROM STORAGE')
|
||||
});
|
||||
await this.storage.set('chatmsg', mgsArray).then((value) => {
|
||||
await this.storage.set('chatmsg', this.mgsArray).then((value) => {
|
||||
console.log('MSG SAVED ON STORAGE', value)
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
ReactToMessage() {}
|
||||
ReactToMessage() { }
|
||||
|
||||
private calDateDuration(date = null) {
|
||||
this.duration = showDateDuration(date || this._updatedAt);
|
||||
@@ -182,9 +208,11 @@ export class RoomService {
|
||||
|
||||
|
||||
private fix_updatedAt(message) {
|
||||
if(message.result) {
|
||||
if (message.result) {
|
||||
console.log('FIX UPDATE ', message.result)
|
||||
message.result._updatedAt = message.result._updatedAt['$date']
|
||||
} else{
|
||||
} else {
|
||||
console.log('FIX UPDATE 11', message)
|
||||
message._updatedAt = message._updatedAt['$date']
|
||||
}
|
||||
return message
|
||||
@@ -192,6 +220,6 @@ export class RoomService {
|
||||
|
||||
|
||||
// to add
|
||||
countDownDate(){}
|
||||
countDownDate() { }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user