mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-15 21:57:22 +01:00
sync groups' members with firebase
This commit is contained in:
@@ -18,6 +18,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');
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------
|
||||
@@ -190,7 +191,10 @@ api.create = function(req, res, next) {
|
||||
async.waterfall([
|
||||
function(cb){user.save(cb)},
|
||||
function(saved,ct,cb){group.save(cb)},
|
||||
function(saved,ct,cb){saved.populate('members',nameFields,cb)}
|
||||
function(saved,ct,cb){
|
||||
firebase.addUserToGroup(saved._id, user._id);
|
||||
saved.populate('members', nameFields, cb);
|
||||
}
|
||||
],function(err,saved){
|
||||
if (err) return next(err);
|
||||
res.json(saved);
|
||||
@@ -207,6 +211,7 @@ api.create = function(req, res, next) {
|
||||
group.save(cb);
|
||||
},
|
||||
function(saved, count, cb){
|
||||
firebase.addUserToGroup(saved._id, user._id);
|
||||
saved.populate('members', nameFields, cb);
|
||||
}
|
||||
], function(err, populated){
|
||||
@@ -479,6 +484,8 @@ api.join = function(req, res, next) {
|
||||
group.save(cb);
|
||||
},
|
||||
function(cb){
|
||||
firebase.addUserToGroup(group._id, user._id);
|
||||
// TODO why query group once again?
|
||||
populateQuery(group.type, Group.findById(group._id)).exec(cb);
|
||||
}
|
||||
], function(err, results){
|
||||
|
||||
@@ -8,9 +8,37 @@ var firebaseConfig = nconf.get('FIREBASE');
|
||||
if(isProd){
|
||||
firebaseRef = new Firebase('https://' + firebaseConfig.APP + '.firebaseio.com');
|
||||
|
||||
firebaseRef.on('value', function(snapshot){
|
||||
console.log(snapshot.val());
|
||||
firebaseRef.authWithCustomToken(firebaseConfig.SECRET, function(err, authData){
|
||||
// TODO it's ok to kill the server here? what if FB is offline?
|
||||
if(err) throw new Error('Impossible to authenticate Firebase');
|
||||
});
|
||||
}
|
||||
|
||||
var api = module.exports = {};
|
||||
var api = module.exports = {};
|
||||
|
||||
api.addUserToGroup = function(groupId, userId){
|
||||
if(!isProd) return;
|
||||
|
||||
// TODO is throw ok? we don't have callbacks
|
||||
if(!userId || !groupId) throw new Error('groupId, userId are required.');
|
||||
|
||||
firebaseRef.child('members/' + groupId + '/' + userId)
|
||||
.set(true);
|
||||
|
||||
firebaseRef.child('users/' + userId + '/' + groupId)
|
||||
.set(true);
|
||||
};
|
||||
|
||||
api.removeUserFromGroup = function(groupId, userId){
|
||||
if(!isProd) return;
|
||||
|
||||
if(!userId || !groupId) throw new Error('groupId, userId are required.');
|
||||
|
||||
firebaseRef.child('members/' + groupId + '/' + userId)
|
||||
.set(false);
|
||||
|
||||
firebaseRef.child('users/' + userId + '/' + groupId)
|
||||
.set(false);
|
||||
};
|
||||
|
||||
api.userLeaves
|
||||
@@ -5,6 +5,7 @@ var _ = require('lodash');
|
||||
var async = require('async');
|
||||
var logging = require('../logging');
|
||||
var Challenge = require('./../models/challenge').model;
|
||||
var firebase = require('../libs/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
|
||||
@@ -444,7 +445,12 @@ GroupSchema.methods.leave = function(user, keep, mainCb){
|
||||
Group.update({_id:group._id}, update, cb);
|
||||
}
|
||||
}
|
||||
], mainCb);
|
||||
], function(err){
|
||||
if(err) return mainCb(err);
|
||||
|
||||
firebase.removeUserFromGroup(group._id, user._id);
|
||||
return mainCb();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user