Bug msg repeated

This commit is contained in:
Eudes Inácio
2022-01-28 15:28:21 +01:00
parent 33eddc7b73
commit f31d5fb0ff
4 changed files with 61 additions and 34 deletions
+13 -1
View File
@@ -1,6 +1,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Message } from 'src/app/models/chatMethod'; import { Message } from 'src/app/models/chatMethod';
import { chatHistory, ChatMessage, File } from 'src/app/models/chatMethod' import { chatHistory, ChatMessage, File } from 'src/app/models/chatMethod'
import { Storage } from '@ionic/storage';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@@ -20,7 +21,8 @@ export class MessageService {
file file
attachments attachments
constructor() { } constructor(private storage: Storage) {
}
setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments}:Message) { setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments}:Message) {
this.customFields = customFields this.customFields = customFields
@@ -35,6 +37,16 @@ export class MessageService {
this._updatedAt = _updatedAt this._updatedAt = _updatedAt
this.file = file this.file = file
this.attachments = attachments this.attachments = attachments
if (this.file) {
if (this.file.guid) {
this.storage.get(this.file.guid).then((image) => {
console.log('IMAGE FROM STORAGE', image)
this.file.image_url = image
});
}
}
} }
delete() {} delete() {}
+46 -31
View File
@@ -39,7 +39,7 @@ export class RoomService {
private sqlservice: SqliteService, private sqlservice: SqliteService,
) { } ) { }
setData({ customFields, id, name, t, lastMessage = new MessageService(), _updatedAt }) { setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage), _updatedAt }) {
this.customFields = customFields this.customFields = customFields
this.id = id this.id = id
this.name = name this.name = name
@@ -60,7 +60,7 @@ export class RoomService {
console.log('recivemessage', ChatMessage) console.log('recivemessage', ChatMessage)
/* this.ToastService._chatMessage({message:'Nova mensagem', sender:'Gilson'}) */ /* this.ToastService._chatMessage({message:'Nova mensagem', sender:'Gilson'}) */
const message = new MessageService() const message = new MessageService(this.storage)
message.setData(ChatMessage) message.setData(ChatMessage)
this.lastMessage.msg = message.msg this.lastMessage.msg = message.msg
@@ -122,7 +122,7 @@ export class RoomService {
let mmessage = this.fix_updatedAt(msgChat) let mmessage = this.fix_updatedAt(msgChat)
console.log('FROM DB WEB', mmessage) console.log('FROM DB WEB', mmessage)
const wewMessage = new MessageService() const wewMessage = new MessageService(this.storage)
wewMessage.setData(mmessage) wewMessage.setData(mmessage)
this.massages.push(wewMessage) this.massages.push(wewMessage)
console.log('loadHistory 222', this.massages) console.log('loadHistory 222', this.massages)
@@ -141,8 +141,8 @@ export class RoomService {
getMsgFromDB() { getMsgFromDB() {
this.storage.get('chatmsg' + this.id).then((message) => { /* this.storage.get('chatmsg' + this.id).then((message) => {
console.log('ALL MESSAGE WEB', message)
message.forEach(message => { message.forEach(message => {
if (message.file) { if (message.file) {
@@ -156,12 +156,12 @@ export class RoomService {
let mmessage = this.fix_updatedAt(message) let mmessage = this.fix_updatedAt(message)
console.log('FROM DB WEB', mmessage) console.log('FROM DB WEB', mmessage)
const wewMessage = new MessageService() const wewMessage = new MessageService(this.storage)
wewMessage.setData(mmessage) wewMessage.setData(mmessage)
this.massages.push(wewMessage) this.massages.push(wewMessage)
console.log('loadHistory 222', this.massages) console.log('loadHistory 222', this.massages)
}); });
}) }) */
} }
@@ -171,42 +171,57 @@ export class RoomService {
if (this.hasLoadHistory) { return false } if (this.hasLoadHistory) { return false }
/* this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => { this.storage.get('chatmsg' + this.id).then((messages = [])=>{
console.log('loadHistory', chatHistory) messages.forEach(message => {
chatHistory.result.messages.reverse().forEach(message => {
message = this.fix_updatedAt(message) message = this.fix_updatedAt(message)
const wewMessage = new MessageService() const wewMessage = new MessageService(this.storage)
wewMessage.setData(message) wewMessage.setData(message)
this.massages.push(wewMessage) this.massages.push(wewMessage)
}); });
})
this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => {
console.log('loadHistory', chatHistory)
}) */ let localMessages = []
chatHistory.result.messages.reverse().forEach(message => {
this.WsChatService.loadHistory(this.id, limit).then(async (chatHistory: chatHistory) => { message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage)
wewMessage.setData(message)
localMessages.push(wewMessage)
});
const mgsArray = chatHistory.result.messages.reverse(); this.massages = localMessages
if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
await this.storage.remove('chatmsg' + this.id).then(() => { this.storage.set('chatmsg' + this.id, chatHistory.result.messages.reverse())
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()
}
}) })
/* 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()
}
}) */
setTimeout(() => { setTimeout(() => {
this.scrollDown() this.scrollDown()
}, 50) }, 50)
@@ -92,7 +92,7 @@ export class WsChatMethodsService {
prepareRoom(roomData){ prepareRoom(roomData){
let room:RoomService; let room:RoomService;
room = new RoomService(this.WsChatService, new MessageService(), this.storage, this.platform, this.sqlservice) room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice)
room.setData({ room.setData({
customFields: roomData.customFields, customFields: roomData.customFields,
id: this.getRoomId(roomData), id: this.getRoomId(roomData),
@@ -101,7 +101,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
} }
ngOnChanges(changes: SimpleChanges): void { ngOnChanges(changes: SimpleChanges): void {
this.wsChatMethodsService.getDmRoom(this.roomId).loadHistory() this.wsChatMethodsService.getDmRoom(this.roomId).loadHistory()
this.wsChatMethodsService.getDmRoom(this.roomId).getMsgFromDB() //this.wsChatMethodsService.getDmRoom(this.roomId).getMsgFromDB()
this.wsChatMethodsService.getDmRoom(this.roomId).scrollDown = this.scrollToBottomClicked this.wsChatMethodsService.getDmRoom(this.roomId).scrollDown = this.scrollToBottomClicked
/* console.log('DATATATA', this.wsChatMethodsService.getDmRoom(this.roomId).massages) /* console.log('DATATATA', this.wsChatMethodsService.getDmRoom(this.roomId).massages)
this.wsChatMethodsService.getDmRoom(this.roomId).massages.forEach((element) => { this.wsChatMethodsService.getDmRoom(this.roomId).massages.forEach((element) => {