Last active
August 29, 2015 13:57
-
-
Save danklynn/9570894 to your computer and use it in GitHub Desktop.
Tracking "signed-in" users with a CurrentUsers model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
Relevant snippets only. See the complete file here: | |
https://github.com/danklynn/FullContact-API-Sails-Chat/blob/2-fullcontact-api/views/main/chat.ejs | |
--> | |
<script> | |
var CurrrentUserModel = Backbone.Model.extend({ | |
urlRoot: '/currentUsers' | |
}); | |
var CurrentUserCollection = SailsCollection.extend({ | |
sailsCollection: 'currentUsers', | |
model: CurrrentUserModel | |
}); | |
var UsersView = Backbone.View.extend({ | |
el: '#usersContainer', | |
initialize: function () { | |
this.collection.on('add', this.render, this); | |
this.collection.on('remove', this.render, this); | |
this.collection.on('change', this.render, this); | |
this.render(); | |
}, | |
template: _.template("<div><img class=\"photo\" src=\"{{ photoUrl || '/images/nophoto.png' }}\" /><b>{{ username }} </b></div>"), | |
render: function () { | |
this.$el.html(""); | |
this.collection.each(function(user){ | |
this.$el.append(this.template(user.toJSON())); | |
}, this) | |
} | |
}); | |
var uView = new UsersView({collection: users}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
attributes: { | |
username: 'STRING', | |
password: 'STRING', | |
photoUrl: 'STRING', | |
twitterUsername: 'STRING' | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.fullcontact = { | |
'passageWayPrefix' : 'http://YOUR-RUNSCOPE-PASSAGEWAY-SUBDOMAIN.passageway.io', | |
'apiKey' : 'YOUR FULLCONTACT API KEY' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
webhook: function (req, res) { | |
var photoUrl = ''; | |
var photos = req.body.result.photos; | |
if ( photos ) { | |
// find the profile's primary photo | |
for (var i=0; i<photos.length; i++) { | |
if (photos[i].isPrimary) { | |
photoUrl = photos[i].url; | |
break; | |
} | |
} | |
} | |
var userId = parseInt(req.param('userId')); | |
// Update the user record. | |
Users.findOne(userId).done(function(err, user) { | |
user.photoUrl = photoUrl; | |
user.save(function(err) { | |
Users.publishUpdate(user.id, { | |
photoUrl : photoUrl | |
}); | |
}); | |
}); | |
// Also update the currently-signed-in user, if present. | |
CurrentUsers.findOne(userId).done(function(err, user) { | |
user.photoUrl = photoUrl; | |
user.save(function(err) { | |
CurrentUsers.publishUpdate(user.id, { | |
photoUrl : photoUrl | |
}); | |
}); | |
}); | |
res.send(200); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.enrich = function(username, userId) { | |
var webhookUrl = sails.config.fullcontact.passageWayPrefix + '/fullcontact/' + userId; | |
var https = require('https'), options = { | |
host : "api.fullcontact.com", | |
path : "/v2/person.json?apiKey=" + | |
sails.config.fullcontact.apiKey + | |
"&email=" + encodeURIComponent(username) + | |
"&webhookBody=json" + | |
"&webhookUrl=" + encodeURIComponent(webhookUrl) | |
}; | |
https.get(options, function(res) {}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git clone git@github.com:danklynn/FullContact-API-Sails-Chat.git | |
cd FullContact-API-Sails-Chat | |
git checkout 1-track-current-users | |
npm install -g sails-0.9.3 | |
npm install | |
sails lift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
/* ... */ | |
login: function (req, res) { | |
/* ... */ | |
FullContactService.enrich(user.username, user.id); | |
/* ... */ | |
}, | |
/* ... */ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
/* ... */ | |
login: function (req, res) { | |
/* ... */ | |
// add the current user to the signed-in users set | |
CurrentUsers.create(user.toObject()).done(function(error, u) { | |
CurrentUsers.publishCreate(user.toObject()); | |
}); | |
/* ... */ | |
}, | |
logout: function (req, res) { | |
if (req.session.user) { | |
var username = req.session.user.username; | |
req.session.user = null; | |
// try to find the current user | |
CurrentUsers.findOneByUsername(username).done(function(err, usr){ | |
if (err) { | |
res.set('error', 'DB Error'); | |
res.send(500, { error: "DB Error" }); | |
} else { | |
if (usr) { | |
// destry the current user | |
usr.destroy(function(e){ | |
/* Once the user is destroyed, notify connected clients */ | |
CurrentUsers.publishDestroy(usr.id); | |
}); | |
} | |
} | |
}); | |
} | |
res.redirect('/'); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git checkout 2-fullcontact-api |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment