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