mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 13:02:56 +00:00
improve
This commit is contained in:
@@ -5,6 +5,8 @@ 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';
|
||||
import { showDateDuration } from 'src/plugin/showDateDuration';
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -30,14 +32,17 @@ export class MessageService {
|
||||
sendAttempt = 0
|
||||
uploadingFile = false
|
||||
errorUploadingAttachment = false
|
||||
loadHistory = false
|
||||
duration = ''
|
||||
localReference = null
|
||||
|
||||
constructor(private storage: Storage,
|
||||
private NfService: NfService,
|
||||
private WsChatService: WsChatService) {
|
||||
}
|
||||
|
||||
setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData}:Message) {
|
||||
this.customFields = customFields
|
||||
setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData, localReference}:Message) {
|
||||
this.customFields = customFields
|
||||
this.channels = channels || []
|
||||
this.mentions = mentions || []
|
||||
this.msg = msg || ""
|
||||
@@ -50,6 +55,7 @@ export class MessageService {
|
||||
this.file = file
|
||||
this.attachments = attachments
|
||||
this.temporaryData = temporaryData
|
||||
this.localReference = localReference || null
|
||||
|
||||
if(!this.ts) {
|
||||
this.offline = true
|
||||
@@ -71,6 +77,8 @@ export class MessageService {
|
||||
this.displayType = this.file.type.replace('application/','').toUpperCase()
|
||||
}
|
||||
}
|
||||
|
||||
this.calDateDuration()
|
||||
}
|
||||
|
||||
private usernameToDisplayName(username) {
|
||||
@@ -78,7 +86,7 @@ export class MessageService {
|
||||
const firstName = capitalizeTxt(username.split('.')[0])
|
||||
const lastName = capitalizeTxt(username.split('.')[1])
|
||||
return firstName + ' ' + lastName
|
||||
}
|
||||
}
|
||||
|
||||
getFileFromDb() {
|
||||
|
||||
@@ -93,19 +101,26 @@ export class MessageService {
|
||||
}
|
||||
}
|
||||
|
||||
async send() {
|
||||
async send(): Promise<any> {
|
||||
|
||||
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
|
||||
this.WsChatService.send({roomId:this.rid, msg:this.msg, requestId: this.localReference}).then(({message, requestId}) => {
|
||||
let ChatMessage = message.result
|
||||
|
||||
if (environment.chatOffline) {
|
||||
|
||||
this.redefinedMessage(ChatMessage)
|
||||
alert('redifie')
|
||||
this.offline = false
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject)=>{
|
||||
resolve(ChatMessage)
|
||||
})
|
||||
})
|
||||
} else {
|
||||
console.log('complex send')
|
||||
|
||||
this.uploadingFile = true
|
||||
|
||||
@@ -113,41 +128,52 @@ export class MessageService {
|
||||
if(this.hasSendAttachment == false) {
|
||||
uploadSuccessfully = await this.NfService.beforeSendAttachment(this)
|
||||
}
|
||||
|
||||
|
||||
this.uploadingFile = false
|
||||
|
||||
if(uploadSuccessfully || this.hasSendAttachment == false) {
|
||||
this.hasSendAttachment = true
|
||||
this.errorUploadingAttachment = false
|
||||
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
|
||||
|
||||
alert('to send')
|
||||
this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file, requestId: this.localReference}).then(({message, requestId}) => {
|
||||
alert('receive11')
|
||||
console.log('message', message)
|
||||
let ChatMessage = message.result
|
||||
|
||||
if (environment.chatOffline) {
|
||||
this.redefinedMessage(ChatMessage)
|
||||
this.offline = false
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject)=>{
|
||||
resolve(ChatMessage)
|
||||
})
|
||||
})
|
||||
} else if(this.WsChatService.isLogin == false) {
|
||||
|
||||
|
||||
this.WsChatService.registerCallback({
|
||||
type: 'reConnect',
|
||||
funx:()=> {
|
||||
alert('reConnect')
|
||||
// this.send()
|
||||
return true
|
||||
funx: async ()=> {
|
||||
return await this.send()
|
||||
}
|
||||
})
|
||||
|
||||
} else if(uploadSuccessfully == false) {
|
||||
|
||||
this.errorUploadingAttachment = true
|
||||
|
||||
return new Promise((resolve, reject)=>{
|
||||
reject(false)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
redefinedMessage(messagem, ChatMessage) {
|
||||
redefinedMessage(ChatMessage) {
|
||||
ChatMessage = this.NfService.fix_updatedAt(ChatMessage)
|
||||
this.setData(ChatMessage)
|
||||
}
|
||||
|
||||
@@ -160,4 +186,9 @@ export class MessageService {
|
||||
|
||||
}
|
||||
|
||||
private calDateDuration(date = null) {
|
||||
this.duration = showDateDuration(date || this._updatedAt);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,8 +7,19 @@ import { RoomService } from './room.service';
|
||||
})
|
||||
export class NfService {
|
||||
|
||||
beforeSendAttachment = async (message: MessageService, room?: RoomService): Promise<boolean> => new Promise ((resolve, reject)=> (resolve(true)))
|
||||
downloadFileMsg = async (message: MessageService, room?: RoomService): Promise<boolean> => new Promise ((resolve, reject)=> (resolve(true)))
|
||||
beforeSendAttachment = async (message: MessageService, room?: RoomService): Promise<boolean> => new Promise ((resolve, reject)=> (resolve(true)));
|
||||
downloadFileMsg = async (message: MessageService, room?: RoomService): Promise<boolean> => new Promise ((resolve, reject)=> (resolve(true)));
|
||||
|
||||
fix_updatedAt(message) {
|
||||
if (message.result) {
|
||||
message.result._updatedAt = message.result._updatedAt['$date']
|
||||
} else if(message._updatedAt) {
|
||||
if(message._updatedAt.hasOwnProperty('$date')) {
|
||||
message._updatedAt = message._updatedAt['$date']
|
||||
}
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
constructor() { }
|
||||
}
|
||||
|
||||
@@ -14,8 +14,9 @@ import { SortService } from '../functions/sort.service';
|
||||
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'
|
||||
import { NfService } from 'src/app/services/chat/nf.service';
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -24,13 +25,13 @@ export class RoomService {
|
||||
messages: MessageService[] = []
|
||||
storageMessage: any[] = [];
|
||||
lastMessage: MessageService;
|
||||
|
||||
|
||||
customFields: any;
|
||||
id = ''
|
||||
t = ''
|
||||
name = ''
|
||||
_updatedAt = {}
|
||||
private hasLoadHistory = false
|
||||
hasLoadHistory = false
|
||||
duration = ''
|
||||
isTyping = false
|
||||
otherUserType = false
|
||||
@@ -38,10 +39,11 @@ export class RoomService {
|
||||
message = ''
|
||||
lastMessageTxt = ''
|
||||
userThatIsTyping = ''
|
||||
|
||||
private ToastService = ToastsService
|
||||
mgsArray = [];
|
||||
|
||||
messagesLocalReference = []
|
||||
|
||||
scrollDown = () => { }
|
||||
|
||||
/**
|
||||
@@ -90,40 +92,35 @@ export class RoomService {
|
||||
this.WsChatService.updateRoomEventss(
|
||||
this.id,
|
||||
"stream-room-messages",
|
||||
(ChatMessage) => {
|
||||
|
||||
setTimeout(() => {
|
||||
ChatMessage = ChatMessage.fields.args[0]
|
||||
|
||||
(_ChatMessage) => {
|
||||
console.log('recivemessage', _ChatMessage)
|
||||
alert('receive')
|
||||
|
||||
setTimeout(()=>{
|
||||
let ChatMessage = _ChatMessage.fields.args[0]
|
||||
ChatMessage = this.fix_updatedAt(ChatMessage)
|
||||
// console.log('recivemessage', ChatMessage)
|
||||
|
||||
const message = this.prepareMessage(ChatMessage)
|
||||
this.lastMessage = message
|
||||
this.calDateDuration(ChatMessage._updatedAt)
|
||||
|
||||
const messageIsFound = this.messages.find((message) => {
|
||||
return message._id == ChatMessage._id
|
||||
})
|
||||
if (message.t == 'r') {
|
||||
this.name = message.msg
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if(this.isSenderIsNotMe(ChatMessage)) {
|
||||
this.NativeNotificationService.sendNotificationChat({
|
||||
message: message.msg,
|
||||
title: this.name
|
||||
});
|
||||
|
||||
|
||||
if(this.isSenderIsNotMe(ChatMessage)) {
|
||||
//this.addMessageDB(ChatMessage)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.addMessageDB(ChatMessage)
|
||||
|
||||
setTimeout(()=>{
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
}, 150)
|
||||
|
||||
}
|
||||
@@ -136,6 +133,7 @@ export class RoomService {
|
||||
if(message.fields.eventName == this.id+'/'+'typing') {
|
||||
|
||||
this.userThatIsTyping = this.usernameToDisplayName(message.fields.args[0])
|
||||
console.log(this.userThatIsTyping, 'this.userThatIsTyping')
|
||||
this.isTyping = message.fields.args[1]
|
||||
this.otherUserType = message.fields.args[1]
|
||||
|
||||
@@ -144,17 +142,90 @@ export class RoomService {
|
||||
})
|
||||
}
|
||||
|
||||
async addMessageDB(ChatMessage) {
|
||||
if (environment.chatOffline) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(!Array.isArray(messages)) {
|
||||
messages = []
|
||||
}
|
||||
|
||||
/* addMessageDB(ChatMessage) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(messages==null) messages = []
|
||||
|
||||
delete ChatMessage.temporaryData
|
||||
messages.push(ChatMessage)
|
||||
if(!ChatMessage._id && environment.chatOffline) {
|
||||
|
||||
this.storage.set('chatmsg' + this.id, messages)
|
||||
})
|
||||
} */
|
||||
delete ChatMessage.temporaryData
|
||||
messages.push(ChatMessage)
|
||||
this.storage.set('chatmsg' + this.id, messages)
|
||||
|
||||
} else {
|
||||
const find = messages.find((message)=> {
|
||||
return message._id == ChatMessage._id
|
||||
})
|
||||
|
||||
if(!find) {
|
||||
delete ChatMessage.temporaryData
|
||||
messages.push(ChatMessage)
|
||||
this.storage.set('chatmsg' + this.id, messages)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async updateMessageDB(ChatMessage, localReference) {
|
||||
if (environment.chatOffline) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(!Array.isArray(messages)) {
|
||||
messages = []
|
||||
}
|
||||
|
||||
let index;
|
||||
const find = messages.find((message, _index)=> {
|
||||
if(message.localReference) {
|
||||
if(message.localReference == ChatMessage.localReference) {
|
||||
index = _index
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
if(find) {
|
||||
messages[index] = ChatMessage
|
||||
this.storage.set('chatmsg' + this.id, messages)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @description delete message in the DB. get all messages, delete then corresponding message and update the store
|
||||
* @param id message ID
|
||||
*/
|
||||
private deleteMessageFromDb(id) {
|
||||
if (environment.chatOffline) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(!Array.isArray(messages)) {
|
||||
messages = []
|
||||
}
|
||||
|
||||
messages.forEach((message, index) => {
|
||||
|
||||
if(message._id == id) {
|
||||
messages.splice(index, 1)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
this.storage.set('chatmsg' + this.id, messages).then((value) => {
|
||||
console.log('MSG SAVED ON STORAGE', value)
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async receiveMessageDelete() {
|
||||
|
||||
@@ -193,58 +264,47 @@ export class RoomService {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @description delete message in the DB. get all messages, delete then corresponding message and update the store
|
||||
* @param id message ID
|
||||
*/
|
||||
private deleteMessageFromDb(id) {
|
||||
this.storage.get('chatmsg' + this.id).then((messages: any = []) => {
|
||||
if(messages==null) messages = []
|
||||
|
||||
messages.forEach((message, index) => {
|
||||
|
||||
if(message._id == id) {
|
||||
messages.splice(index, 1)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
this.storage.set('chatmsg' + this.id, messages).then((value) => {
|
||||
console.log('MSG SAVED ON STORAGE', value)
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @description sen text message
|
||||
*/
|
||||
async send({file = null, attachments = null, temporaryData = {}}) {
|
||||
|
||||
const localReference = uuidv4() + 'peter'
|
||||
|
||||
let offlineChatMessage = {
|
||||
rid: this.id,
|
||||
msg: this.message,
|
||||
attachments,
|
||||
file,
|
||||
temporaryData
|
||||
temporaryData,
|
||||
localReference
|
||||
}
|
||||
|
||||
console.log('offlineChatMessage', offlineChatMessage)
|
||||
const message: MessageService = this.prepareMessage(offlineChatMessage, environment.chatOffline)
|
||||
|
||||
//this.addMessageDB(offlineChatMessage)
|
||||
const message: MessageService = this.prepareMessage(offlineChatMessage)
|
||||
|
||||
setTimeout(() => {
|
||||
this.scrollDown()
|
||||
}, 150)
|
||||
|
||||
this.lastMessage = message
|
||||
message.send().then((ChatMessage) => {
|
||||
this.updateMessageDB(ChatMessage, localReference)
|
||||
})
|
||||
|
||||
message.send()
|
||||
|
||||
this.calDateDuration(message._updatedAt)
|
||||
this.sortRoomList()
|
||||
|
||||
if (environment.chatOffline) {
|
||||
this.messagesLocalReference.push(localReference)
|
||||
this.addMessageDB(offlineChatMessage)
|
||||
|
||||
setTimeout(() => {
|
||||
this.scrollDown()
|
||||
}, 150)
|
||||
|
||||
this.lastMessage = message
|
||||
this.calDateDuration(message._updatedAt)
|
||||
this.sortRoomList()
|
||||
}
|
||||
|
||||
this.message= ''
|
||||
}
|
||||
|
||||
@@ -262,7 +322,7 @@ export class RoomService {
|
||||
}
|
||||
|
||||
|
||||
typing(text:string = this.message) {
|
||||
sendTyping(text:string = this.message) {
|
||||
|
||||
if(this.lastMessageTxt == text) { return false }
|
||||
this.lastTimeType = new Date().getTime()
|
||||
@@ -302,7 +362,7 @@ export class RoomService {
|
||||
}
|
||||
|
||||
private setTypingOff() {
|
||||
this.typing('')
|
||||
this.sendTyping('')
|
||||
}
|
||||
|
||||
roomLeave() {
|
||||
@@ -329,48 +389,59 @@ export class RoomService {
|
||||
|
||||
|
||||
async restoreMessageFromDB() {
|
||||
await this.storage.get('chatmsg' + this.id).then( async (messages = []) => {
|
||||
if(messages==null) 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)
|
||||
|
||||
setTimeout(()=> {
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
|
||||
})
|
||||
if(wewMessage.offline == false) {
|
||||
this.prepareMessage(ChatMessage)
|
||||
} else {
|
||||
if(environment.chatOffline) alert('create offline')
|
||||
const offlineMessage = this.prepareMessage(ChatMessage)
|
||||
offlineMessage.send().then(()=>{
|
||||
this.updateMessageDB(ChatMessage, ChatMessage.localReference)
|
||||
})
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
setTimeout(()=> {
|
||||
this.scrollDown()
|
||||
}, 50)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// runs onces only
|
||||
async loadHistory(limit = 100) {
|
||||
async loadHistory({limit = 50, forceUpdate = false }) {
|
||||
|
||||
if (this.hasLoadHistory) { return false }
|
||||
|
||||
this.restoreMessageFromDB()
|
||||
if(forceUpdate == false) {
|
||||
if (this.hasLoadHistory) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
await 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)
|
||||
this.messages = this.sortService.sortDate(this.messages, '_updatedAt')
|
||||
|
||||
const wewMessage = this.prepareMessage(message, false)
|
||||
})
|
||||
|
||||
if(wewMessage.offline == false) {
|
||||
this.prepareMessage(message)
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
this.storage.set('chatmsg' + this.id, chatHistory)
|
||||
this.storage.set('chatmsg' + this.id, chatHistory.result.messages)
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
@@ -386,22 +457,32 @@ 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 (save) {
|
||||
if(!message._id && environment.chatOffline) {
|
||||
if(this.hasLoadHistory && environment.chatOffline) alert('create offline')
|
||||
|
||||
this.messages.push(wewMessage)
|
||||
return wewMessage
|
||||
}
|
||||
|
||||
const found = this.messages.find((MessageService) => {
|
||||
if (MessageService._id == message._id) {
|
||||
if(this.hasLoadHistory) console.log(`${MessageService._id} == ${message._id}`)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
if (save && !found) {
|
||||
if(this.hasLoadHistory && environment.chatOffline) alert('not 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
|
||||
@@ -410,11 +491,9 @@ export class RoomService {
|
||||
|
||||
private fix_updatedAt(message) {
|
||||
if (message.result) {
|
||||
//console.log('FIX UPDATE ', message.result)
|
||||
message.result._updatedAt = message.result._updatedAt['$date']
|
||||
} else if(message._updatedAt) {
|
||||
if(message._updatedAt.hasOwnProperty('$date')) {
|
||||
// console.log('FIX UPDATE 11', message)
|
||||
message._updatedAt = message._updatedAt['$date']
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,15 +13,14 @@ import { NativeNotificationService } from 'src/app/services/native-notification.
|
||||
import { SortService } from '../functions/sort.service';
|
||||
import { chatUser } from 'src/app/models/chatMethod';
|
||||
import { NfService } from 'src/app/services/chat/nf.service'
|
||||
|
||||
import { ChangeProfileService } from '../change-profile.service';
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
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(
|
||||
@@ -44,31 +42,41 @@ export class WsChatMethodsService {
|
||||
private sortService: SortService,
|
||||
private ChatService: ChatService,
|
||||
private NfService: NfService,
|
||||
private changeProfileService: ChangeProfileService,
|
||||
) {
|
||||
(async()=>{
|
||||
await this.restoreRooms()
|
||||
await this.getAllRooms();
|
||||
this.subscribeToRoom()
|
||||
|
||||
this.loadChat()
|
||||
|
||||
|
||||
//
|
||||
await this.getUser()
|
||||
this.getUserStatus()
|
||||
|
||||
})()
|
||||
|
||||
|
||||
/**
|
||||
* @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.currentRoom) {
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
this.changeProfileService.registerCallback(()=>{
|
||||
this.clearChat()
|
||||
this.ReLoadChat()
|
||||
this.storage.remove('Rooms');
|
||||
})
|
||||
|
||||
|
||||
// this.WsChatService.registerCallback({
|
||||
// type:'Onmessage',
|
||||
@@ -104,33 +112,58 @@ export class WsChatMethodsService {
|
||||
|
||||
}
|
||||
|
||||
private loadChat() {
|
||||
this.ReLoadChat()
|
||||
}
|
||||
|
||||
async ReLoadChat() {
|
||||
await this.restoreRooms()
|
||||
await this.getAllRooms();
|
||||
this.subscribeToRoom()
|
||||
|
||||
|
||||
//
|
||||
await this.getUser()
|
||||
this.getUserStatus()
|
||||
}
|
||||
|
||||
clearChat() {
|
||||
this.dm = {}
|
||||
this.group = {}
|
||||
this._dm = []
|
||||
this._group = []
|
||||
|
||||
this.loadingWholeList = false
|
||||
|
||||
this.dmCount = 0;
|
||||
this.groupCount = 0;
|
||||
|
||||
this.currentRoom = null
|
||||
this.users = []
|
||||
}
|
||||
|
||||
getRoomFromDb() {
|
||||
this.storage.get('Rooms').then((rooms) => {
|
||||
rooms.result.update.forEach((roomData: room) => {
|
||||
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()
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -210,7 +243,7 @@ export class WsChatMethodsService {
|
||||
|
||||
this.prepareRoom(roomData);
|
||||
|
||||
this.getGroupRoom(id).loadHistory();
|
||||
this.getGroupRoom(id).loadHistory({});
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ export class WsChatService {
|
||||
|
||||
this.ws.registerCallback({
|
||||
type:'Onmessage',
|
||||
key:'ping-pong',
|
||||
funx:(message: any) => {
|
||||
if(message.msg == "ping") {
|
||||
this.ws.send({message:{msg:"pong"}, loginRequired: false})
|
||||
@@ -79,7 +80,9 @@ export class WsChatService {
|
||||
this.isLogin = true
|
||||
this.loginResponse = message
|
||||
|
||||
this.ws.wsMsgQueue()
|
||||
setTimeout(()=>{
|
||||
this.ws.wsMsgQueue()
|
||||
}, 10)
|
||||
|
||||
resolve(message)
|
||||
}
|
||||
@@ -155,9 +158,7 @@ export class WsChatService {
|
||||
}
|
||||
|
||||
// send message to room
|
||||
send({roomId, msg, attachments = null, file = null}) {
|
||||
|
||||
const requestId = uuidv4()
|
||||
send({roomId, msg, attachments = null, file = null, requestId = uuidv4()}) {
|
||||
|
||||
var message = {
|
||||
msg: "method",
|
||||
@@ -176,7 +177,7 @@ export class WsChatService {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
|
||||
if(message.id == requestId ) { // same request send
|
||||
resolve(message)
|
||||
resolve({message, requestId})
|
||||
return true
|
||||
}
|
||||
}})
|
||||
@@ -273,7 +274,7 @@ export class WsChatService {
|
||||
|
||||
return new Promise ((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', funx:(message) =>{
|
||||
if(message.id == requestId || deepFind(message, 'result') == requestId){
|
||||
if(message.id == requestId ){
|
||||
resolve(message)
|
||||
return true
|
||||
}
|
||||
@@ -629,7 +630,11 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
|
||||
|
||||
console.log('================== welcome to socket server =====================')
|
||||
|
||||
this.ws.wsMsgQueue()
|
||||
|
||||
setTimeout(()=>{
|
||||
this.ws.wsMsgQueue()
|
||||
}, 10)
|
||||
|
||||
if(this.wsReconnect >= 1) {
|
||||
for (const [key, value] of Object.entries(this.wsCallbacks)) {
|
||||
if(value.type== 'reConnect') {
|
||||
@@ -671,6 +676,8 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
|
||||
this.wsMsgQueue[requestId] = {message, requestId, loginRequired}
|
||||
} else {
|
||||
let messageStr = JSON.stringify(message)
|
||||
// console.log('messageStr', messageStr)
|
||||
|
||||
this.socket.send(messageStr)
|
||||
}
|
||||
return requestId
|
||||
@@ -679,6 +686,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)
|
||||
|
||||
Reference in New Issue
Block a user