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
+4 -4
View File
@@ -59,7 +59,7 @@ interface FirstUnread {
msg: string;
ts: Ts2;
u: U2;
_updatedAt: UpdatedAt2;
_updatedAt: string;
mentions: any[];
channels: any[];
}
@@ -72,7 +72,7 @@ export interface Message {
ts: Ts;
u: U;
t: string;
_updatedAt: UpdatedAt;
_updatedAt: '';
mentions: any[];
channels: any[];
attachments: Attachment[];
@@ -232,7 +232,7 @@ interface FirstUnread {
msg: string;
ts: Ts2;
u: U2;
_updatedAt: UpdatedAt2;
_updatedAt: string;
mentions: any[];
channels: any[];
}
@@ -245,7 +245,7 @@ export interface Message {
ts: Ts;
u: U;
t: string;
_updatedAt: UpdatedAt;
_updatedAt: '';
mentions: any[];
channels: any[];
attachments: Attachment[];
+15 -1
View File
@@ -17,9 +17,10 @@ export class MessageService {
u = {}
t = ''
_id =''
_updatedAt = {}
_updatedAt = ''
file
attachments
offline = false
constructor(private storage: Storage) {
}
@@ -47,10 +48,23 @@ export class MessageService {
});
}
}
if(this.rid == 'offline') {
this.offline = true
} else {
this.offline = false
}
}
delete() {}
showDateDuration() {}
resend() {
}
}
+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']
}
@@ -131,7 +131,7 @@ export class WsChatMethodsService {
/**
* @description sort room list by last message date
*/
sortRoomList() {
sortRoomList =() => {
this._dm = this.sortService.sortDate(this._dm,'_updatedAt').reverse()
this._group = this.sortService.sortDate(this._group,'_updatedAt').reverse()
}
@@ -207,6 +207,7 @@ export class WsChatMethodsService {
room.receiveMessage()
room.getAllUsers = this.getUsers
room.receiveMessageDelete();
room.sortRoomList = this.sortRoomList
let roomId = this.getRoomId(roomData)
+4 -4
View File
@@ -30,8 +30,8 @@ export class WsChatService {
support: ["1"]
}
this.ws.send({message, loginRequired: false})
this.ws.send({message:{msg:"pong"}, loginRequired: false})
this.ws.send({message, loginRequired: false, requestId: 'connectMessage'})
this.ws.send({message:{msg:"pong"}, loginRequired: false, requestId: 'connectPong'})
this.ws.registerCallback({
type:'Onmessage',
@@ -65,7 +65,7 @@ export class WsChatService {
}
]
}
this.ws.send({message, requestId, loginRequired: false})
this.ws.send({message, requestId: 'login', loginRequired: false})
return new Promise((resolve, reject) => {
@@ -633,7 +633,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
if (this.ws.connected == false || loginRequired == true && this.isLogin == false) { // save data to send when back online
// console.log('save msgQueue this.ws.connected == false || loginRequired == true && this.isLogin == false',this.ws.connected, loginRequired, this.isLogin)
console.log('save msgQueue', requestId)
console.log('save msgQueue', requestId, message)
this.wsMsgQueue[requestId] = {message, requestId, loginRequired}
} else {
+1
View File
@@ -6,4 +6,5 @@ export const environment = {
domain: 'gabinetedigital.local',
defaultuser: '',//paulo.pinto paulo.pinto@gabinetedigital.local
defaultuserpwd: '', //tabteste@006,
chatOffline: true
};
+1
View File
@@ -13,6 +13,7 @@ export const environment = {
domain: 'gabinetedigital.local', //gabinetedigital.local
defaultuser: 'paulo.pinto@gabinetedigital.local',//paulo.pinto paulo.pinto@gabinetedigital.local
defaultuserpwd: 'tabteste@006', //tabteste@006,
chatOffline: true
};
/*