This commit is contained in:
Peter Maquiran
2022-02-10 14:07:16 +01:00
parent a6823d9bae
commit 26a209502e
5 changed files with 122 additions and 128 deletions
+9 -5
View File
@@ -5,6 +5,7 @@ import { SessionStore } from 'src/app/store/session.service';
import { capitalizeTxt } from 'src/plugin/text' import { capitalizeTxt } from 'src/plugin/text'
import { NfService } from 'src/app/services/chat/nf.service' import { NfService } from 'src/app/services/chat/nf.service'
import { WsChatService } from 'src/app/services/chat/ws-chat.service'; import { WsChatService } from 'src/app/services/chat/ws-chat.service';
import { environment } from 'src/environments/environment';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
@@ -30,6 +31,7 @@ export class MessageService {
sendAttempt = 0 sendAttempt = 0
uploadingFile = false uploadingFile = false
errorUploadingAttachment = false errorUploadingAttachment = false
loadHistory = false
constructor(private storage: Storage, constructor(private storage: Storage,
private NfService: NfService, private NfService: NfService,
@@ -98,14 +100,15 @@ export class MessageService {
this.sendAttempt++; this.sendAttempt++;
if(!this.hasFile) { if(!this.hasFile) {
console.log('simple send')
this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => { this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => {
if (environment.chatOffline) {
let ChatMessage = data.result let ChatMessage = data.result
this.redefinedMessage(this, ChatMessage) this.redefinedMessage(this, ChatMessage)
this.offline = false this.offline = false
}
}) })
} else { } else {
console.log('complex send')
this.uploadingFile = true this.uploadingFile = true
@@ -122,10 +125,12 @@ export class MessageService {
this.temporaryData = {} this.temporaryData = {}
this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file}).then((data: any) => { this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file}).then((data: any) => {
console.log('send sucees', data.result) if (environment.chatOffline) {
// console.log('send sucees', data.result)
let ChatMessage = data.result let ChatMessage = data.result
this.redefinedMessage(this, ChatMessage) this.redefinedMessage(this, ChatMessage)
this.offline = false this.offline = false
}
}) })
} else if(this.WsChatService.isLogin == false) { } else if(this.WsChatService.isLogin == false) {
@@ -133,8 +138,7 @@ export class MessageService {
this.WsChatService.registerCallback({ this.WsChatService.registerCallback({
type: 'reConnect', type: 'reConnect',
funx:()=> { funx:()=> {
alert('reConnect') this.send()
// this.send()
return true return true
} }
}) })
+39 -50
View File
@@ -15,7 +15,7 @@ import { chatUser } from 'src/app/models/chatMethod';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
import { ChatService } from 'src/app/services/chat.service'; import { ChatService } from 'src/app/services/chat.service';
import { NfService } from 'src/app/services/chat/nf.service' import { NfService } from 'src/app/services/chat/nf.service'
import alasql from 'alasql'
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
@@ -30,7 +30,7 @@ export class RoomService {
t = '' t = ''
name = '' name = ''
_updatedAt = {} _updatedAt = {}
private hasLoadHistory = false hasLoadHistory = false
duration = '' duration = ''
isTyping = false isTyping = false
otherUserType = false otherUserType = false
@@ -67,15 +67,6 @@ export class RoomService {
private NfService: NfService private NfService: NfService
) { ) {
this.NativeNotificationService.askForPermission() this.NativeNotificationService.askForPermission()
this.WsChatService.registerCallback({
type: 'reConnect',
funx: ()=>{
this.hasLoadHistory = false
}
})
} }
setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService), _updatedAt }) { setData({ customFields, id, name, t, lastMessage = new MessageService(this.storage, this.NfService, this.WsChatService), _updatedAt }) {
@@ -99,41 +90,33 @@ export class RoomService {
this.WsChatService.updateRoomEventss( this.WsChatService.updateRoomEventss(
this.id, this.id,
"stream-room-messages", "stream-room-messages",
(ChatMessage) => { (_ChatMessage) => {
setTimeout(() => { let ChatMessage = _ChatMessage.fields.args[0]
ChatMessage = ChatMessage.fields.args[0]
ChatMessage = this.fix_updatedAt(ChatMessage) ChatMessage = this.fix_updatedAt(ChatMessage)
// console.log('recivemessage', ChatMessage) console.log('recivemessage', JSON.stringify(_ChatMessage))
const messageIsFound = this.messages.find((message) => {
return message._id == ChatMessage._id
})
if(!messageIsFound) {
const message = this.prepareMessage(ChatMessage) const message = this.prepareMessage(ChatMessage)
this.lastMessage = message this.lastMessage = message
if (message.t == 'r') { this.name = message.msg }
this.calDateDuration(ChatMessage._updatedAt) this.calDateDuration(ChatMessage._updatedAt)
setTimeout(() => { if (message.t == 'r') {
this.scrollDown() this.name = message.msg
}, 100) }
if(this.isSenderIsNotMe(ChatMessage)) {
this.NativeNotificationService.sendNotificationChat({ this.NativeNotificationService.sendNotificationChat({
message: message.msg, message: message.msg,
title: this.name title: this.name
}); });
}
if(this.isSenderIsNotMe(ChatMessage)) { if(this.isSenderIsNotMe(ChatMessage)) {
this.addMessageDB(ChatMessage) // this.addMessageDB(ChatMessage)
} }
} this.scrollDown()
}, 150)
} }
) )
@@ -242,19 +225,19 @@ export class RoomService {
temporaryData temporaryData
} }
this.addMessageDB(offlineChatMessage) const message: MessageService = this.prepareMessage(offlineChatMessage, environment.chatOffline)
const message: MessageService = this.prepareMessage(offlineChatMessage) message.send()
if (environment.chatOffline) {
this.addMessageDB(offlineChatMessage)
setTimeout(() => { setTimeout(() => {
this.scrollDown() this.scrollDown()
}, 150) }, 150)
this.lastMessage = message this.lastMessage = message
message.send()
this.calDateDuration(message._updatedAt) this.calDateDuration(message._updatedAt)
this.sortRoomList() this.sortRoomList()
}
this.message= '' this.message= ''
} }
@@ -340,6 +323,8 @@ export class RoomService {
async restoreMessageFromDB() { async restoreMessageFromDB() {
if(environment.chatOffline) {
await this.storage.get('chatmsg' + this.id).then( async (messages = []) => { await this.storage.get('chatmsg' + this.id).then( async (messages = []) => {
if(!Array.isArray(messages)) { if(!Array.isArray(messages)) {
messages = [] messages = []
@@ -359,27 +344,29 @@ export class RoomService {
}, 50) }, 50)
}) })
}
} }
// runs onces only // runs onces only
async loadHistory({limit = 100, forceUpdate = false }) { async loadHistory({limit = 100, forceUpdate = false }) {
if (this.hasLoadHistory || forceUpdate) { return false } if(forceUpdate == false) {
if (this.hasLoadHistory) {
return false
}
}
this.restoreMessageFromDB() this.restoreMessageFromDB()
await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => { await this.WsChatService.loadHistory(this.id, limit).then( async (chatHistory:chatHistory) => {
console.log('loadHistory', chatHistory) console.log('loadHistory', chatHistory)
this.messages = []
await chatHistory.result.messages.reverse().forEach( async (message) => { await chatHistory.result.messages.reverse().forEach( async (message) => {
this.prepareMessage(message) this.prepareMessage(message)
const wewMessage = this.prepareMessage(message, false) this.messages = this.sortService.sortDate(this.messages, '_updatedAt')
if(wewMessage.offline == false) {
this.prepareMessage(message)
}
}) })
@@ -399,22 +386,24 @@ export class RoomService {
message = this.fix_updatedAt(message) message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService) const wewMessage = new MessageService(this.storage, this.NfService, this.WsChatService)
wewMessage.setData(message) wewMessage.setData(message)
wewMessage.loadHistory = this.hasLoadHistory
if (save) { if(!message._id && environment.chatOffline) {
this.messages.push(wewMessage)
return wewMessage
}
const found = this.messages.find((MessageService) => {
return MessageService._id == message._id
})
if (save && !found) {
this.messages.push(wewMessage) this.messages.push(wewMessage)
} }
return wewMessage return wewMessage
} }
async returnData(res) {
return res;
}
ReactToMessage() { }
private calDateDuration(date = null) { private calDateDuration(date = null) {
this.duration = showDateDuration(date || this._updatedAt); this.duration = showDateDuration(date || this._updatedAt);
this._updatedAt = date || this._updatedAt this._updatedAt = date || this._updatedAt
@@ -19,9 +19,8 @@ import { NfService } from 'src/app/services/chat/nf.service'
}) })
export class WsChatMethodsService { export class WsChatMethodsService {
dm = {} dm: {[key: string]: RoomService} = {}
group = {} group: {[key: string]: RoomService} = {}
_dm = [] _dm = []
_group = [] _group = []
@@ -31,8 +30,7 @@ export class WsChatMethodsService {
dmCount = 0; dmCount = 0;
groupCount = 0; groupCount = 0;
currentRoom: RoomService = null
currentRoom = null
users: chatUser[] = [] users: chatUser[] = []
constructor( constructor(
@@ -57,20 +55,23 @@ export class WsChatMethodsService {
})() })()
this.WsChatService.registerCallback({
type: 'reConnect',
funx: ()=>{
/** /**
* @description when the phone is in the background for a long time it could disconnects from the socket then the socket reconnects automatically, * @description when the phone is in the background for a long time it could disconnects from the socket then the socket reconnects automatically,
* when the connection is lost the subscribe is also lost, so we have to subscribe again when reconnection is establish. * when the connection is lost the subscribe is also lost, so we have to subscribe again when reconnection is establish.
*/ */
this.WsChatService.registerCallback({
type: 'reConnect',
funx: ()=>{
this.subscribeToRoom() this.subscribeToRoom()
if(this.getDmRoom(this.currentRoom)) { this.currentRoom.loadHistory({forceUpdate: true})
this.getDmRoom(this.currentRoom).loadHistory({forceUpdate: true})
} else if(this.getGroupRoom(this.currentRoom)) { for (const id in this.dm) {
this.getGroupRoom(this.currentRoom).loadHistory({forceUpdate: true}) this.dm[id].hasLoadHistory = false
}
for (const id in this.group) {
this.group[id].hasLoadHistory = false
} }
} }
@@ -117,26 +118,21 @@ export class WsChatMethodsService {
this.prepareRoom(roomData); this.prepareRoom(roomData);
}); });
}) })
}; }
openRoom(roomId) { openRoom(roomId) {
if(this.currentRoom) { if(this.currentRoom) {
if(this.getDmRoom(this.currentRoom)) { this.currentRoom.roomLeave()
this.getDmRoom(this.currentRoom).roomLeave()
} else if(this.getGroupRoom(this.currentRoom)) {
this.getGroupRoom(this.currentRoom).roomLeave()
}
} }
this.currentRoom = roomId if(this.getDmRoom(roomId)) {
this.currentRoom = this.getDmRoom(roomId)
} else if(this.getGroupRoom(roomId)) {
this.currentRoom = this.getGroupRoom(roomId)
}
if(this.currentRoom) { this.currentRoom.open()
if(this.getDmRoom(this.currentRoom)) {
this.getDmRoom(this.currentRoom).open()
} else if(this.getGroupRoom(this.currentRoom)) {
this.getGroupRoom(this.currentRoom).open()
}
}
} }
+5
View File
@@ -677,6 +677,9 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
this.wsMsgQueue[requestId] = {message, requestId, loginRequired} this.wsMsgQueue[requestId] = {message, requestId, loginRequired}
} else { } else {
let messageStr = JSON.stringify(message) let messageStr = JSON.stringify(message)
console.log('send', messageStr)
this.socket.send(messageStr) this.socket.send(messageStr)
} }
return requestId return requestId
@@ -685,6 +688,8 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
onmessage: async (event: any)=> { onmessage: async (event: any)=> {
const data = JSON.parse(event.data) const data = JSON.parse(event.data)
console.log('onmessage', data)
for (const [key, value] of Object.entries(this.wsCallbacks)) { for (const [key, value] of Object.entries(this.wsCallbacks)) {
if(value.type== 'Onmessage') { if(value.type== 'Onmessage') {
const dontRepeat = await value.funx(data) const dontRepeat = await value.funx(data)
+1 -1
View File
@@ -13,7 +13,7 @@ export const environment = {
domain: 'gabinetedigital.local', //gabinetedigital.local domain: 'gabinetedigital.local', //gabinetedigital.local
defaultuser: 'paulo.pinto@gabinetedigital.local',//paulo.pinto paulo.pinto@gabinetedigital.local defaultuser: 'paulo.pinto@gabinetedigital.local',//paulo.pinto paulo.pinto@gabinetedigital.local
defaultuserpwd: 'tabteste@006', //tabteste@006, defaultuserpwd: 'tabteste@006', //tabteste@006,
chatOffline: true chatOffline: false
}; };
/* /*