add chat offline

This commit is contained in:
Peter Maquiran
2022-02-02 20:16:12 +01:00
parent 58e9698264
commit 33cce0fd44
7 changed files with 134 additions and 49 deletions
+107 -39
View File
@@ -13,6 +13,7 @@ import { SessionStore } from 'src/app/store/session.service';
import { capitalizeTxt } from 'src/plugin/text'
import { SortService } from '../functions/sort.service';
import { chatUser } from 'src/app/models/chatMethod';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
@@ -51,6 +52,8 @@ export class RoomService {
return []
}
sortRoomList = () => {}
constructor(
public WsChatService: WsChatService,
private MessageService: MessageService,
@@ -80,40 +83,34 @@ export class RoomService {
this.id,
"stream-room-messages",
(ChatMessage) => {
ChatMessage = ChatMessage.fields.args[0]
ChatMessage = this.fix_updatedAt(ChatMessage)
console.log('recivemessage', ChatMessage)
if(environment.chatOffline == false) {
ChatMessage = ChatMessage.fields.args[0]
ChatMessage = this.fix_updatedAt(ChatMessage)
console.log('recivemessage', ChatMessage)
const message = this.prepareMessage(ChatMessage)
const message = this.prepareMessage(ChatMessage)
this.lastMessage = message
if (message.t == 'r') { this.name = message.msg }
this.calDateDuration(ChatMessage._updatedAt)
setTimeout(() => {
this.scrollDown()
}, 100)
//this.sortService.sortDate(this.messages, '')
if(SessionStore.user.RochetChatUser != ChatMessage.u.username) {
this.NativeNotificationService.sendNotificationChat({
message: message.msg,
title: this.name
});
}
this.lastMessage = message
if (message.t == 'r') { this.name = message.msg }
this.calDateDuration(ChatMessage._updatedAt)
this.messages.push(message)
setTimeout(() => {
this.scrollDown()
}, 100)
//this.sortService.sortDate(this.messages, '')
if(SessionStore.user.RochetChatUser != ChatMessage.u.username) {
this.NativeNotificationService.sendNotificationChat({
message: message.msg,
title: this.name
});
this.addMessageDB(ChatMessage)
}
// save to ionic storage
this.storage.get('chatmsg' + this.id).then((messages: any) => {
const newListMessages = messages.push(ChatMessage)
this.storage.set('chatmsg' + this.id, newListMessages).then((value) => {
console.log('MSG SAVED ON STORAGE', value)
});
})
}
)
@@ -133,6 +130,15 @@ export class RoomService {
this.WsChatService.registerCallback
}
addMessageDB(ChatMessage) {
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
messages.push(ChatMessage)
this.storage.set('chatmsg' + this.id, messages)
})
}
async receiveMessageDelete() {
this.WsChatService.updateRoomEventss(
@@ -195,8 +201,60 @@ export class RoomService {
* @description sen text message
*/
send() {
this.WsChatService.send(this.id, this.message)
this.message= ''
if(environment.chatOffline == false) {
this.WsChatService.send(this.id, this.message)
this.message= ''
} else {
const offlineChatMessage = {
channels: [],
mentions: [],
rid: 'offline',
msg: this.message,
ts: { $date: new Date().getTime() },
u: {
name: this.usernameToDisplayName(SessionStore.user.RochetChatUser),
username: SessionStore.user.RochetChatUser,
_id: ""
},
_updatedAt: new Date().getTime()
}
this.addMessageDB(offlineChatMessage)
const message: MessageService = this.prepareMessage(offlineChatMessage)
setTimeout(() => {
this.scrollDown()
}, 150)
this.lastMessage = message
this.redefinedMessage(message)
this.calDateDuration(message._updatedAt)
this.sortRoomList()
this.message= ''
}
}
redefinedMessage = (message: MessageService) => {
this.WsChatService.send(this.id, message.msg).then((data: any) => {
let ChatMessage = data.result
ChatMessage = this.fix_updatedAt(ChatMessage)
message.setData(ChatMessage)
if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) {
this.lastMessage = message
this.calDateDuration(message._updatedAt)
}
this.sortRoomList()
})
}
@@ -266,22 +324,31 @@ export class RoomService {
}
// runs onces only
loadHistory(limit = 100) {
if (this.hasLoadHistory) { return false }
restoreMessageFromDB() {
this.storage.get('chatmsg' + this.id).then((messages = []) => {
let localMessages = []
messages.forEach(message => {
messages.forEach((message = []) => {
const wewMessage = this.prepareMessage(message)
if(wewMessage.rid == 'offline') {
this.redefinedMessage(wewMessage)
}
localMessages.push(wewMessage)
});
this.messages = localMessages
})
}
// runs onces only
loadHistory(limit = 100) {
if (this.hasLoadHistory) { return false }
this.restoreMessageFromDB()
this.WsChatService.loadHistory(this.id, limit).then((chatHistory:chatHistory) => {
console.log('loadHistory', chatHistory)
@@ -317,7 +384,8 @@ export class RoomService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage)
wewMessage.setData(message)
this.messages.push(wewMessage)
return wewMessage
}
@@ -343,7 +411,7 @@ export class RoomService {
if (message.result) {
console.log('FIX UPDATE ', message.result)
message.result._updatedAt = message.result._updatedAt['$date']
} else {
} else if(message._updatedAt.hasOwnProperty('$date')) {
// console.log('FIX UPDATE 11', message)
message._updatedAt = message._updatedAt['$date']
}