mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 05:16:07 +00:00
Improve filter callback
This commit is contained in:
@@ -95,6 +95,7 @@ export class HomePage implements OnInit {
|
||||
private RouteService: RouteService,
|
||||
private RocketChatClientService: RocketChatClientService) {
|
||||
|
||||
|
||||
this.RocketChatClientService.getRooms().then((rooms: any)=>{
|
||||
console.log('rooms', rooms)
|
||||
|
||||
@@ -106,6 +107,7 @@ export class HomePage implements OnInit {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
this.RocketChatClientService.receiveLiveMessageFromRoom(
|
||||
'fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM',
|
||||
this.constructor.name,
|
||||
@@ -114,6 +116,10 @@ export class HomePage implements OnInit {
|
||||
}
|
||||
)
|
||||
|
||||
this.RocketChatClientService.loadHistory('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 50).then((message)=>{
|
||||
console.log('loadHistory', message)
|
||||
})
|
||||
|
||||
window['jj'] = ()=>{
|
||||
//send message // roomId // Message
|
||||
this.RocketChatClientService.send('fsMwcNdufWvdnChj7ya9nF9cX2HizxxWAM', 'Mensagem enviada programaticamente.'+ new Date().toISOString())
|
||||
|
||||
@@ -8,5 +8,6 @@ export interface wsCallbacksParams {
|
||||
|
||||
export interface msgQueue {
|
||||
message: object,
|
||||
requestId: string
|
||||
requestId: string,
|
||||
loginRequired: boolean
|
||||
}
|
||||
@@ -57,6 +57,7 @@ export class UserSession {
|
||||
}[]
|
||||
UserName: string
|
||||
Password: string
|
||||
RochetChatUser: string
|
||||
Profile: any;
|
||||
LoginPreference: 'None' | 'Password' | 'Pin' | null;
|
||||
PIN: string
|
||||
|
||||
@@ -30,23 +30,23 @@ export class AuthService {
|
||||
public alertController: AlertController,
|
||||
private aesencrypt: AESEncrypt,
|
||||
private cookieService: CookieService,
|
||||
private RocketChatClientService: RocketChatClientService
|
||||
) {
|
||||
private RocketChatClientService: RocketChatClientService) {
|
||||
|
||||
this.headers = new HttpHeaders();
|
||||
|
||||
if (SessionStore.exist) {
|
||||
this.ValidatedUser = SessionStore.user
|
||||
|
||||
console.log('login', SessionStore.user.RochetChatUser, SessionStore.user.Password)
|
||||
this.RocketChatClientService.connect(()=>{
|
||||
this.RocketChatClientService.login({
|
||||
username: SessionStore.user.UserName,
|
||||
username: SessionStore.user.RochetChatUser,
|
||||
password: SessionStore.user.Password
|
||||
}).then((message) => {
|
||||
console.log('rocket chat login successfully', message)
|
||||
}).catch((message)=>{
|
||||
console.log('rocket chat login failed', message)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
@@ -91,6 +91,7 @@ export class AuthService {
|
||||
}
|
||||
|
||||
session.Password = user.password
|
||||
session.RochetChatUser = user.username.split('@')[0]
|
||||
|
||||
session.BasicAuthKey = user.BasicAuthKey
|
||||
|
||||
@@ -117,9 +118,10 @@ export class AuthService {
|
||||
this.RocketChatClientService.connect(()=>{
|
||||
|
||||
this.RocketChatClientService.login({
|
||||
username: user.username,
|
||||
username: SessionStore.user.RochetChatUser,
|
||||
password: user.password
|
||||
}).then((message) => {
|
||||
alert('sucess')
|
||||
console.log('rocket chat login successfully', message)
|
||||
}).catch((message)=>{
|
||||
console.log('rocket chat login failed', message)
|
||||
|
||||
@@ -1,53 +1,21 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service'
|
||||
|
||||
import { deepFind } from 'src/plugin/deep'
|
||||
/**
|
||||
* Use this class to interact with rocketChat realtime API
|
||||
*/
|
||||
@Injectable({
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class RocketChatClientService {
|
||||
export class RocketChatClientService {
|
||||
|
||||
private hasPing = false
|
||||
private firstPingFunx: Function
|
||||
isLogin = false;
|
||||
requestQueue = []
|
||||
|
||||
constructor() {}
|
||||
|
||||
private returns() {}
|
||||
|
||||
private attemptRequestQueue(arg, options) {
|
||||
|
||||
const conditionToSend = this.ws.connected == false || this.isLogin == false
|
||||
|
||||
if(!conditionToSend) {
|
||||
console.log('requestQueue')
|
||||
this.requestQueue.push({
|
||||
arg: arg,
|
||||
methodName: options.methodName,
|
||||
status: {
|
||||
isLogin: this.isLogin,
|
||||
isConnected: this.ws.connected
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return conditionToSend
|
||||
}
|
||||
|
||||
private runRequestQueue(){
|
||||
this.requestQueue.forEach((request,index, object)=>{
|
||||
console.log('runRequestQueue '+index)
|
||||
const method = request.methodName
|
||||
const arg = request.arg
|
||||
this[method](...arg)
|
||||
object.splice(index, 1);
|
||||
})
|
||||
}
|
||||
|
||||
connect(firstPingFunx: Function) {
|
||||
this.hasPing = false
|
||||
this.firstPingFunx = firstPingFunx
|
||||
@@ -60,12 +28,12 @@ export class RocketChatClientService {
|
||||
support: ["1"]
|
||||
}
|
||||
|
||||
this.ws.send(connectMessage)
|
||||
this.ws.send(connectMessage, 'connect', false)
|
||||
|
||||
this.ws.registerCallback({type:'Onmessage',funx:(message: any) => {
|
||||
this.ws.registerCallback({type:'Onmessage', funx:(message: any) => {
|
||||
|
||||
if(message.msg == "ping") {
|
||||
this.ws.send({msg:"pong"})
|
||||
this.ws.send({msg:"pong"},'recoonect', false)
|
||||
if(this.hasPing == false) {
|
||||
// first ping
|
||||
firstPingFunx()
|
||||
@@ -93,39 +61,58 @@ export class RocketChatClientService {
|
||||
}
|
||||
]
|
||||
}
|
||||
this.ws.send(loginRequest, requestId)
|
||||
this.ws.send(loginRequest, requestId, false)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
// console.log(message.result.id, requestId)
|
||||
if(message.id == requestId || message.result.id == requestId) { // same request send
|
||||
if(message.result) {
|
||||
if(message.result.token) {
|
||||
this.isLogin = true
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
|
||||
if(message.id == requestId || deepFind(message, 'result.id') == requestId) { // same request send
|
||||
|
||||
if(message.result.token) {
|
||||
this.isLogin = true
|
||||
setTimeout(()=>{
|
||||
this.ws.wsMsgQueue()
|
||||
this.runRequestQueue()
|
||||
resolve(message)
|
||||
} else {
|
||||
this.isLogin = false
|
||||
reject(message)
|
||||
}
|
||||
},0)
|
||||
|
||||
resolve(message)
|
||||
} else {
|
||||
this.isLogin = false
|
||||
reject(message)
|
||||
}
|
||||
|
||||
reject(message)
|
||||
|
||||
return true
|
||||
}
|
||||
}})
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
getRooms(requestId = uuidv4()) {
|
||||
|
||||
const request = {
|
||||
"msg": "method",
|
||||
"method": "rooms/get",
|
||||
"id": requestId,
|
||||
"params": [ { "$date": 1480377601 } ]
|
||||
}
|
||||
|
||||
this.ws.send(request, requestId, true)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
|
||||
if(message.id == requestId) { // same request send
|
||||
resolve(message)
|
||||
return true
|
||||
}
|
||||
}})
|
||||
});
|
||||
}
|
||||
|
||||
logout() {}
|
||||
|
||||
send(roomId, message) {
|
||||
|
||||
const conditions = this.attemptRequestQueue({...roomId, message}, {methodName:'send'})
|
||||
if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); }
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
var request = {
|
||||
@@ -139,12 +126,13 @@ export class RocketChatClientService {
|
||||
}]
|
||||
}
|
||||
|
||||
this.ws.send(request, requestId);
|
||||
this.ws.send(request, requestId, true);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
if(message.id == requestId || message.result.id == requestId) { // same request send
|
||||
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
|
||||
resolve(message)
|
||||
return true
|
||||
}
|
||||
}})
|
||||
});
|
||||
@@ -154,32 +142,9 @@ export class RocketChatClientService {
|
||||
deleteMessage() {}
|
||||
createRoom() {}
|
||||
|
||||
getRooms() {
|
||||
const conditions = this.attemptRequestQueue({},{methodName: 'getRooms'})
|
||||
if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); }
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
const request = {
|
||||
"msg": "method",
|
||||
"method": "rooms/get",
|
||||
"id": requestId,
|
||||
"params": [ { "$date": 1480377601 } ]
|
||||
}
|
||||
this.ws.send(request, requestId)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
if(message.id == requestId || message.result.id == requestId) { // same request send
|
||||
resolve(message)
|
||||
}
|
||||
}})
|
||||
});
|
||||
}
|
||||
|
||||
loadHistory(roomId, limit: number = 50) {
|
||||
const conditions = this.attemptRequestQueue({...roomId, limit}, {methodName:'loadHistory'})
|
||||
if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); }
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
@@ -197,23 +162,20 @@ export class RocketChatClientService {
|
||||
]
|
||||
}
|
||||
|
||||
this.ws.send(request, requestId)
|
||||
this.ws.send(request, requestId, true)
|
||||
|
||||
|
||||
// return new Promise((resolve, reject) => {
|
||||
// this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
// if(message.id == requestId || message.result.id == requestId) { // same request send
|
||||
// resolve(message)
|
||||
// }
|
||||
// }})
|
||||
// });
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
|
||||
resolve(message)
|
||||
return true
|
||||
}
|
||||
}})
|
||||
});
|
||||
}
|
||||
|
||||
subscribe(roomId : string) {
|
||||
|
||||
const conditions = this.attemptRequestQueue({roomId},{methodName: 'subscribe'})
|
||||
if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); }
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
var subscribeRequest = {
|
||||
@@ -226,12 +188,13 @@ export class RocketChatClientService {
|
||||
]
|
||||
}
|
||||
|
||||
this.ws.send(subscribeRequest);
|
||||
this.ws.send(subscribeRequest, requestId, true);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
if(message.msg == 'ready' || message.subs[0] == requestId) { // same request send
|
||||
if(message.msg == 'ready' || deepFind(message, 'subs.0') == requestId) { // same request send
|
||||
resolve(message)
|
||||
return true
|
||||
}
|
||||
}})
|
||||
});
|
||||
@@ -263,9 +226,6 @@ export class RocketChatClientService {
|
||||
|
||||
streamRoomMessages(roomId : string) {
|
||||
|
||||
const conditions = this.attemptRequestQueue({roomId},{methodName: 'subscribe'})
|
||||
if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); }
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
const request = {
|
||||
@@ -278,20 +238,19 @@ export class RocketChatClientService {
|
||||
]
|
||||
}
|
||||
|
||||
this.ws.send(request);
|
||||
this.ws.send(request, requestId, true);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
if(message.msg == 'ready' || message.subs[0] == requestId) { // same request send
|
||||
if(message.msg == 'ready' || deepFind(message, 'subs.0') == requestId) { // same request send
|
||||
resolve(message)
|
||||
return true
|
||||
}
|
||||
}})
|
||||
});
|
||||
}
|
||||
|
||||
streamNotifyRoom(roomId : string) {
|
||||
const conditions = this.attemptRequestQueue({roomId},{methodName: 'subscribe'})
|
||||
if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); }
|
||||
|
||||
const requestId = uuidv4()
|
||||
|
||||
@@ -306,12 +265,13 @@ export class RocketChatClientService {
|
||||
]
|
||||
};
|
||||
|
||||
this.ws.send(streamNotifyObj, requestId)
|
||||
this.ws.send(streamNotifyObj, requestId, true)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws.registerCallback({type:'Onmessage', requestId, runOnces: true, funx:(message)=>{
|
||||
if(message.id == requestId || message.result.id == requestId) { // same request send
|
||||
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
|
||||
resolve('')
|
||||
return true
|
||||
}
|
||||
}})
|
||||
});
|
||||
@@ -334,12 +294,14 @@ export class RocketChatClientService {
|
||||
|
||||
this.wsCallbacks[params.requestId] = {
|
||||
type: params.type,
|
||||
funx: params.funx,
|
||||
runOnces: params.runOnces
|
||||
funx: params.funx
|
||||
}
|
||||
|
||||
},
|
||||
deleteCallback(){},
|
||||
deleteCallback(requestId: string){
|
||||
console.log('delete', this.wsCallbacks[requestId])
|
||||
delete this.wsCallbacks[requestId]
|
||||
},
|
||||
connect:(url)=> {
|
||||
this.ws.connected = false
|
||||
this.wsUrl = url
|
||||
@@ -357,17 +319,23 @@ export class RocketChatClientService {
|
||||
},
|
||||
wsMsgQueue:()=> {
|
||||
this.wsMsgQueue.forEach((item, index, object) => {
|
||||
this.ws.send(item.message, item.requestId);
|
||||
object.splice(index, 1);
|
||||
if(item.loginRequired && this.isLogin) {
|
||||
this.ws.send(item.message, item.requestId, item.loginRequired);
|
||||
object.splice(index, 1);
|
||||
} else if(item.loginRequired == false) {
|
||||
this.ws.send(item.message, item.requestId, item.loginRequired);
|
||||
object.splice(index, 1);
|
||||
}
|
||||
|
||||
})
|
||||
},
|
||||
send: (message: object, requestId = uuidv4()) => {
|
||||
send: (message: object, requestId = uuidv4(), loginRequired) => {
|
||||
|
||||
if (this.ws.connected == false) { // save data to send when back online
|
||||
if (this.ws.connected == false || loginRequired == true && this.isLogin == false) { // save data to send when back online
|
||||
console.log('save msgQueue')
|
||||
this.wsMsgQueue.push({message, requestId})
|
||||
this.wsMsgQueue.push({message, requestId, loginRequired})
|
||||
} else {
|
||||
console.log('send to rocket chat', message)
|
||||
// console.log('send to rocket chat', message)
|
||||
let messageStr = JSON.stringify(message)
|
||||
this.socket.send(messageStr)
|
||||
}
|
||||
@@ -376,16 +344,17 @@ export class RocketChatClientService {
|
||||
|
||||
onmessage:(event: any)=> {
|
||||
const data = JSON.parse(event.data)
|
||||
console.log('event.data', data)
|
||||
// console.log('event.data', data)
|
||||
|
||||
for (const [key, value] of Object.entries(this.wsCallbacks)) {
|
||||
if(value.type== 'Onmessage') {
|
||||
value.funx(data)
|
||||
if(value.runOnces) {
|
||||
delete this.wsCallbacks[value.requestId || key]
|
||||
}
|
||||
}
|
||||
const dontRepeat = value.funx(data)
|
||||
|
||||
if(dontRepeat) {
|
||||
delete this.wsCallbacks[key]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
function deepFind(obj, path) {
|
||||
var paths = path.split('.')
|
||||
, current = obj
|
||||
, i;
|
||||
|
||||
for (i = 0; i < paths.length; ++i) {
|
||||
if (current[paths[i]] == undefined) {
|
||||
return undefined;
|
||||
} else {
|
||||
current = current[paths[i]];
|
||||
}
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
deepFind: deepFind,
|
||||
};
|
||||
Reference in New Issue
Block a user