diff --git a/website/src/controllers/api-v2/groups.js b/website/src/controllers/api-v2/groups.js index 0fc969c3af..62ac3804b5 100644 --- a/website/src/controllers/api-v2/groups.js +++ b/website/src/controllers/api-v2/groups.js @@ -19,7 +19,7 @@ var isProd = nconf.get('NODE_ENV') === 'production'; var api = module.exports; var pushNotify = require('./../pushNotifications'); var analytics = utils.analytics; -var firebase = require('../../libs/firebase'); +var firebase = require('../../libs/api-v2/firebase'); /* ------------------------------------------------------------------------ diff --git a/website/src/controllers/api-v2/user.js b/website/src/controllers/api-v2/user.js index cd747e7d5c..5c6d256044 100644 --- a/website/src/controllers/api-v2/user.js +++ b/website/src/controllers/api-v2/user.js @@ -13,7 +13,7 @@ var moment = require('moment'); var logging = require('./../../libs/api-v2/logging'); var acceptablePUTPaths; var api = module.exports; -var firebase = require('../../libs/firebase'); +var firebase = require('../../libs/api-v2/firebase'); var webhook = require('../../libs/api-v2/webhook'); // api.purchase // Shared.ops diff --git a/website/src/libs/firebase.js b/website/src/libs/api-v2/firebase.js similarity index 100% rename from website/src/libs/firebase.js rename to website/src/libs/api-v2/firebase.js diff --git a/website/src/libs/api-v3/firebase.js b/website/src/libs/api-v3/firebase.js new file mode 100644 index 0000000000..ce20f3c7fc --- /dev/null +++ b/website/src/libs/api-v3/firebase.js @@ -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(); +} \ No newline at end of file diff --git a/website/src/models/group.js b/website/src/models/group.js index b2da0011d7..0d3011cc38 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -6,7 +6,7 @@ var _ = require('lodash'); var async = require('async'); var logging = require('../libs/api-v2/logging'); 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 // changes made directly to the db will cause Firebase to get out of sync diff --git a/website/src/server.js b/website/src/server.js index 2562c9bb87..d8b98b10db 100644 --- a/website/src/server.js +++ b/website/src/server.js @@ -42,7 +42,7 @@ let db = mongoose.connect(nconf.get('NODE_DB_URI'), mongooseOptions, (err) => { autoinc.init(db); -import './libs/firebase'; +import './libs/api-v3/firebase'; // load schemas & models import './models/challenge';