src/providers/chat-contacts-synchronizer.ts
Properties |
|
Methods |
constructor(config: Config, databaseprovider: DatabaseProvider)
|
||||||||||||
|
Parameters :
|
| addContact | ||||||||
addContact(child: )
|
||||||||
|
completo profilo user con fullname salvo data ultimo aggiornamento nel DB aggiungo oppure aggiorno (sovrascrivo) un utente al DB
Parameters :
Returns :
void
|
| dispose |
dispose()
|
|
dispose reference di contacts synchronizer
Returns :
void
|
| initWithTenant | ||||||||||||
initWithTenant(tenant: , user: )
|
||||||||||||
|
inizializzo contacts synchronizer
Parameters :
Returns :
this
|
| loadFirebaseContactsData | ||||||||
loadFirebaseContactsData(lastUpdate: )
|
||||||||
|
creo una reference al nodo contacts filtro per data successiva a ultimo aggiornamento mi sottoscrivo a change, removed, added ref: https://firebase.google.com/docs/reference/js/firebase.database.Query https://stackoverflow.com/questions/41721134/firebase-angularfire2-listening-on-queried-list-child-added https://firebase.google.com/docs/database/web/lists-of-data
Parameters :
Returns :
void
|
| removeContact | ||||||||
removeContact(child: )
|
||||||||
|
salvo data ultimo aggiornamento nel DB elimino utente dal DB
Parameters :
Returns :
void
|
| startSynchro |
startSynchro()
|
|
recupero dal db locale la data dell'ultimo aggiornamento sincronizzo i contatti dal nodo firebase 'contacts'
Returns :
void
|
| Private loggeduser |
loggeduser:
|
Type : UserModel
|
| Private ref |
ref:
|
Type : firebase.database.Query
|
| Private tenant |
tenant:
|
Type : string
|
import { Injectable } from '@angular/core';
import { Config } from 'ionic-angular';
// providers
import { DatabaseProvider } from './database/database';
// models
import { UserModel } from '../models/user';
// firebase
import * as firebase from 'firebase/app';
// utils
import { contactsRef } from '../utils/utils';
@Injectable()
export class ChatContactsSynchronizer {
private tenant: string;
private loggeduser: UserModel;
private ref: firebase.database.Query;
constructor(
private config: Config,
private databaseprovider: DatabaseProvider
) {
}
/**
* inizializzo contacts synchronizer
* @param tenant
* @param user
*/
initWithTenant(tenant, user) {
this.tenant = tenant;
this.loggeduser = user;
return this;
}
/**
* recupero dal db locale la data dell'ultimo aggiornamento
* sincronizzo i contatti dal nodo firebase 'contacts'
*/
startSynchro(){
let that = this;
this.databaseprovider.getTimestamp()
.then(function(lastUpdate) {
that.loadFirebaseContactsData(lastUpdate);
});
}
/**
* creo una reference al nodo contacts
* filtro per data successiva a ultimo aggiornamento
* mi sottoscrivo a change, removed, added
* ref: https://firebase.google.com/docs/reference/js/firebase.database.Query
* https://stackoverflow.com/questions/41721134/firebase-angularfire2-listening-on-queried-list-child-added
* https://firebase.google.com/docs/database/web/lists-of-data
*/
loadFirebaseContactsData(lastUpdate){
let that = this;
console.log("lastUpdate:"+lastUpdate, that.tenant);
const urlNodeConcacts = contactsRef(that.tenant);
this.ref = firebase.database().ref(urlNodeConcacts);
this.ref.orderByChild("timestamp").startAt(lastUpdate)
this.ref.on("child_changed", function(childSnapshot) {
var childData = childSnapshot.val();
that.addContact(childData);
});
this.ref.on("child_removed", function(childSnapshot) {
var childData = childSnapshot.val();
that.removeContact(childData);
});
this.ref.on("child_added", function(childSnapshot) {
var childData = childSnapshot.val();
that.addContact(childData);
})
}
/**
* completo profilo user con fullname
* salvo data ultimo aggiornamento nel DB
* aggiungo oppure aggiorno (sovrascrivo) un utente al DB
* @param child
*/
addContact(child) {
// console.log("child_added", child);
let user:UserModel = child;
let fullname = '';
if(user['firstname'] && user['firstname'] != undefined){
fullname += user['firstname']+' '
}
if(user['lastname'] && user['lastname'] != undefined){
fullname += user['lastname']
}
user.fullname = fullname;
// console.log("fullname:",fullname);
this.databaseprovider.setTimestamp();
this.databaseprovider.addContact(user.uid, user.email, user.firstname, user.lastname, user.fullname, user.imageurl);
}
/**
* salvo data ultimo aggiornamento nel DB
* elimino utente dal DB
* @param child
*/
removeContact(child) {
this.databaseprovider.setTimestamp();
let user = child;
this.databaseprovider.removeContact(user.uid);
}
/**
* dispose reference di contacts synchronizer
*/
dispose() {
this.ref.off();
}
}