src/providers/chat-presence-handler.ts
Properties |
|
Methods |
constructor(events: Events, chatManager: ChatManager)
|
||||||||||||
|
Defined in src/providers/chat-presence-handler.ts:29
|
||||||||||||
|
Parameters :
|
| getTimeLastConnection | ||||||||
getTimeLastConnection(timestamp: string)
|
||||||||
|
Defined in src/providers/chat-presence-handler.ts:83
|
||||||||
|
calcolo tempo trascorso tra ora e timestamp passato
Parameters :
Returns :
any
|
| goOffline |
goOffline()
|
|
Defined in src/providers/chat-presence-handler.ts:155
|
|
rimuovo la references su lastOnline rimuovo la references su connection
Returns :
void
|
| lastOnlineForUser | ||||||||
lastOnlineForUser(userid: )
|
||||||||
|
Defined in src/providers/chat-presence-handler.ts:67
|
||||||||
|
mi sottoscrivo al nodo presence/uid/lastOnline e recupero la data dell'ultimo stato online pubblico lastConnectionDate
Parameters :
Returns :
void
|
| lastOnlineRefForUser | ||||||||
lastOnlineRefForUser(userid: )
|
||||||||
|
Defined in src/providers/chat-presence-handler.ts:94
|
||||||||
|
recupero la reference di lastOnline del currentUser usata in setupMyPresence
Parameters :
Returns :
any
|
| onlineRefForUser | ||||||||
onlineRefForUser(userid: )
|
||||||||
|
Defined in src/providers/chat-presence-handler.ts:105
|
||||||||
|
recupero la reference di connections (online/offline) del currentUser usata in setupMyPresence
Parameters :
Returns :
any
|
| removeConnectionReference |
removeConnectionReference()
|
|
Defined in src/providers/chat-presence-handler.ts:161
|
|
Returns :
void
|
| removeLastOnlineReference |
removeLastOnlineReference()
|
|
Defined in src/providers/chat-presence-handler.ts:171
|
|
Returns :
void
|
| setupMyPresence | ||||||||
setupMyPresence(userid: )
|
||||||||
|
Defined in src/providers/chat-presence-handler.ts:122
|
||||||||
|
1 - imposto reference online/offline 2 - imposto reference lastConnection 3 - mi sincronizzo con /.info/connected 4 - se il valore esiste l'utente è online 5 - aggiungo nodo a connection (true) 6 - aggiungo job su onDisconnect di deviceConnectionRef che rimuove nodo connection 7 - aggiungo job su onDisconnect di lastOnlineRef che imposta timestamp 8 - salvo reference connected nel singlelton !!!!! DA FARE
Parameters :
Returns :
void
|
| userIsOnline | ||||||||
userIsOnline(userid: )
|
||||||||
|
Defined in src/providers/chat-presence-handler.ts:47
|
||||||||
|
controlla se esiste una connessione per l'utente analizzato, verificando se esiste un nodo in presence/uid/connections mi sottosrivo al nodo se non esiste pubblico utente offline se esiste pubblico utente online
Parameters :
Returns :
void
|
| Public chatManager |
chatManager:
|
Type : ChatManager
|
|
Defined in src/providers/chat-presence-handler.ts:33
|
| Public events |
events:
|
Type : Events
|
|
Defined in src/providers/chat-presence-handler.ts:32
|
| Public lastOnlineRef |
lastOnlineRef:
|
|
Defined in src/providers/chat-presence-handler.ts:29
|
| Public myConnectionsRef |
myConnectionsRef:
|
|
Defined in src/providers/chat-presence-handler.ts:28
|
| Public tenant |
tenant:
|
Type : string
|
|
Defined in src/providers/chat-presence-handler.ts:25
|
| Public urlNodeFirebase |
urlNodeFirebase:
|
Type : string
|
|
Defined in src/providers/chat-presence-handler.ts:26
|
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Events } from 'ionic-angular';
// firebase
import * as firebase from 'firebase/app';
// services
import { ChatManager } from './chat-manager/chat-manager';
// utils
import { setLastDate } from '../utils/utils';
//import { ApplicationContext } from './application-context/application-context';
/*
Generated class for the AuthService provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class ChatPresenceHandler {
public tenant: string;
public urlNodeFirebase: string;
//public deviceConnectionRef;
public myConnectionsRef; //: firebase.database.Reference;
public lastOnlineRef; //: firebase.database.Reference;
constructor(
public events: Events,
public chatManager: ChatManager
) {
this.tenant = chatManager.getTenant();
this.urlNodeFirebase = '/apps/'+this.tenant+'/';
}
/**
* controlla se esiste una connessione per l'utente analizzato,
* verificando se esiste un nodo in presence/uid/connections
* mi sottosrivo al nodo
* se non esiste pubblico utente offline
* se esiste pubblico utente online
* @param userid
*/
userIsOnline(userid){
//this.lastOnlineForUser(userid);
const that = this;
let myConnectionsRefURL = this.urlNodeFirebase+"/presence/"+userid+"/connections";
const connectionsRef = firebase.database().ref().child(myConnectionsRefURL);
connectionsRef.on("value", (child) => {
if(child.val()){
that.events.publish('statusUser:online-'+userid, userid,'online');
}
else {
that.events.publish('statusUser:offline-'+userid, userid,'offline');
}
})
}
/**
* mi sottoscrivo al nodo presence/uid/lastOnline
* e recupero la data dell'ultimo stato online
* pubblico lastConnectionDate
* @param userid
*/
lastOnlineForUser(userid){
const that = this;
let lastOnlineRefURL = this.urlNodeFirebase+"/presence/"+userid+"/lastOnline";
const lastOnlineRef = firebase.database().ref().child(lastOnlineRefURL);
lastOnlineRef.on("value", (child) => {
if(child.val()){
const lastConnectionDate = that.getTimeLastConnection(child.val());
that.events.publish('lastConnectionDate-'+userid, userid,lastConnectionDate);
}
});
}
/**
* calcolo tempo trascorso tra ora e timestamp passato
* @param timestamp
*/
getTimeLastConnection(timestamp:string){
//let timestampNumber = parseInt(timestamp)/1000;
let time = setLastDate(timestamp);
return time;
}
/**
* recupero la reference di lastOnline del currentUser
* usata in setupMyPresence
* @param userid
*/
lastOnlineRefForUser(userid){
let lastOnlineRefURL = this.urlNodeFirebase+"/presence/"+userid+"/lastOnline";
const lastOnlineRef = firebase.database().ref().child(lastOnlineRefURL);
return lastOnlineRef;
}
/**
* recupero la reference di connections (online/offline) del currentUser
* usata in setupMyPresence
* @param userid
*/
onlineRefForUser(userid){
let myConnectionsRefURL = this.urlNodeFirebase+"/presence/"+userid+"/connections";
const connectionsRef = firebase.database().ref().child(myConnectionsRefURL);
return connectionsRef;
}
/**
* 1 - imposto reference online/offline
* 2 - imposto reference lastConnection
* 3 - mi sincronizzo con /.info/connected
* 4 - se il valore esiste l'utente è online
* 5 - aggiungo nodo a connection (true)
* 6 - aggiungo job su onDisconnect di deviceConnectionRef che rimuove nodo connection
* 7 - aggiungo job su onDisconnect di lastOnlineRef che imposta timestamp
* 8 - salvo reference connected nel singlelton !!!!! DA FARE
* @param userid
*/
setupMyPresence(userid){
let that = this;
this.myConnectionsRef = this.onlineRefForUser(userid);
this.lastOnlineRef = this.lastOnlineRefForUser(userid);
let connectedRefURL = "/.info/connected";
let conn = firebase.database().ref(connectedRefURL);
conn.on('value', function(dataSnapshot) {
//console.log("KEY: ",dataSnapshot,that.deviceConnectionRef);
if(dataSnapshot.val()){
console.log("self.deviceConnectionRef: ", that.myConnectionsRef);
//if (!that.myConnectionsRef || that.myConnectionsRef==='undefined') {
if (that.myConnectionsRef) {
//this.deviceConnectionRef = myConnectionsRef.set(true);
let conection = true;
//that.deviceConnectionRef =
that.myConnectionsRef.push(conection);
//!!! quando faccio logout devo disconnettermi
that.myConnectionsRef.onDisconnect().remove();
// when I disconnect, update the last time I was seen online
let now: Date = new Date();
let timestamp = now.valueOf();
that.lastOnlineRef.onDisconnect().set(timestamp);
} else {
console.log("This is an error. self.deviceConnectionRef already set. Cannot be set again.");
}
}
});
}
/**
* rimuovo la references su lastOnline
* rimuovo la references su connection
*/
goOffline() {
console.log("goOffline.", this.myConnectionsRef)
this.removeConnectionReference();
this.removeLastOnlineReference();
}
removeConnectionReference(){
if(this.myConnectionsRef){
this.myConnectionsRef.off();
console.log("goOffline 1", this.myConnectionsRef)
this.myConnectionsRef.remove();
console.log("goOffline 2", this.myConnectionsRef)
}
this.myConnectionsRef = null;
}
removeLastOnlineReference(){
if(this.lastOnlineRef){
this.lastOnlineRef.off();
this.lastOnlineRef.remove();
}
this.lastOnlineRef = null;
}
}