Merge pull request #6237 from HabitRPG/api-v3-firebase

port firebase lib to api v3
This commit is contained in:
Matteo Pagliazzi
2015-11-14 13:14:50 +01:00
6 changed files with 72 additions and 4 deletions

View File

@@ -19,7 +19,7 @@ var isProd = nconf.get('NODE_ENV') === 'production';
var api = module.exports; var api = module.exports;
var pushNotify = require('./../pushNotifications'); var pushNotify = require('./../pushNotifications');
var analytics = utils.analytics; var analytics = utils.analytics;
var firebase = require('../../libs/firebase'); var firebase = require('../../libs/api-v2/firebase');
/* /*
------------------------------------------------------------------------ ------------------------------------------------------------------------

View File

@@ -13,7 +13,7 @@ var moment = require('moment');
var logging = require('./../../libs/api-v2/logging'); var logging = require('./../../libs/api-v2/logging');
var acceptablePUTPaths; var acceptablePUTPaths;
var api = module.exports; var api = module.exports;
var firebase = require('../../libs/firebase'); var firebase = require('../../libs/api-v2/firebase');
var webhook = require('../../libs/api-v2/webhook'); var webhook = require('../../libs/api-v2/webhook');
// api.purchase // Shared.ops // api.purchase // Shared.ops

View File

@@ -0,0 +1,68 @@
import Firebase from 'firebase';
import nconf from 'nconf';
const IS_PROD = nconf.get('IS_PROD');
const FIREBASE_CONFIG = nconf.get('FIREBASE');
const FIREBASE_ENABLED = IS_PROD && FIREBASE_CONFIG.ENABLED === 'true';
let firebaseRef;
if (FIREBASE_ENABLED) {
firebaseRef = new Firebase(`https://${FIREBASE_CONFIG.APP}.firebaseio.com`);
// TODO what happens if an op is sent before client is authenticated?
firebaseRef.authWithCustomToken(FIREBASE_CONFIG.SECRET, (err) => {
// TODO it's ok to kill the server here? what if FB is offline?
if (err) throw new Error('Impossible to authenticate Firebase');
});
}
export function updateGroupData (group) {
if (!FIREBASE_ENABLED) return;
// TODO is throw ok? we don't have callbacks
if (!group) throw new Error('group obj is required.');
// Return in case of tavern (comparison working because we use string for _id)
if (group._id === 'habitrpg') return;
firebaseRef.child(`rooms/${group._id}`)
.set({
name: group.name,
});
}
export function addUserToGroup (groupId, userId) {
if (!FIREBASE_ENABLED) return;
if (!userId || !groupId) throw new Error('groupId, userId are required.');
if (groupId === 'habitrpg') return;
firebaseRef.child(`members/${groupId}/${userId}`).set(true);
firebaseRef.child(`users/${userId}/rooms/${groupId}`).set(true);
}
export function removeUserFromGroup (groupId, userId) {
if (!FIREBASE_ENABLED) return;
if (!userId || !groupId) throw new Error('groupId, userId are required.');
if (groupId === 'habitrpg') return;
firebaseRef.child(`members/${groupId}/${userId}`).remove();
firebaseRef.child(`users/${userId}/rooms/${groupId}`).remove();
}
export function deleteGroup (groupId) {
if (!FIREBASE_ENABLED) return;
if (!groupId) throw new Error('groupId is required.');
if (groupId === 'habitrpg') return;
firebaseRef.child(`members/${groupId}`).remove();
// FIXME not really necessary as long as we only store room data,
// as empty objects are automatically deleted (/members/... in future...)
firebaseRef.child(`rooms/${groupId}`).remove();
}
// FIXME not really necessary as long as we only store room data,
// as empty objects are automatically deleted
export function deleteUser (userId) {
if (!FIREBASE_ENABLED) return;
if (!userId) throw new Error('userId is required.');
firebaseRef.child(`users/${userId}`).remove();
}

View File

@@ -6,7 +6,7 @@ var _ = require('lodash');
var async = require('async'); var async = require('async');
var logging = require('../libs/api-v2/logging'); var logging = require('../libs/api-v2/logging');
var Challenge = require('./../models/challenge').model; var Challenge = require('./../models/challenge').model;
var firebase = require('../libs/firebase'); var firebase = require('../libs/api-v2/firebase');
// NOTE any change to groups' members in MongoDB will have to be run through the API // NOTE any change to groups' members in MongoDB will have to be run through the API
// changes made directly to the db will cause Firebase to get out of sync // changes made directly to the db will cause Firebase to get out of sync

View File

@@ -42,7 +42,7 @@ let db = mongoose.connect(nconf.get('NODE_DB_URI'), mongooseOptions, (err) => {
autoinc.init(db); autoinc.init(db);
import './libs/firebase'; import './libs/api-v3/firebase';
// load schemas & models // load schemas & models
import './models/challenge'; import './models/challenge';