Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial Commit

  • Loading branch information...
commit 7cd0000450c942df51692fe825cb7b4e3e471b25 0 parents
@lynxluna lynxluna authored
104 backend.js
@@ -0,0 +1,104 @@
+var scaling = new (require('blaast/scaling').Scaling)();
+
+// Require and initialize Facebook API. For appId and appSecret,
+// insert your Facebook Application's information.
+//
+// On setting up your Facebook application:
+// http://devsupport.blaast.com/kb/sdk/using-the-facebook-api-part-1-setup
+//
+// Initializing the Facebook API will add an object 'facebook' to the client object.
+
+// --- When done, remove these lines
+log.warn('You need to insert your Facebook application\'s appId and appSecret to backend.js.');
+log.warn('Please follow the instructions in http://devsupport.blaast.com/kb/sdk/using-the-facebook-api-part-1-setup');
+// --- When done, remove these lines
+
+require('facebook').init({
+ appId: 'put your appId here',
+ appSecret: 'put your appSecret here',
+ scope: ['read_stream', 'read_requests', 'publish_stream', 'user_photos', 'friends_photos']
+});
+
+var clients = {};
+app.realtime(function(client, event) {
+ if (event === 'CONNECTED') {
+ clients[client.user.id] = client;
+ // To require another connection attempt, use client.facebook.logout()
+ // Needed if you want to change the scope of the authorization.
+ // client.facebook.logout();
+ } else if (event === 'DISCONNECTED') {
+ delete clients[client.user.id];
+ }
+});
+
+app.message(function(client, action, data) {
+ if (action === 'sync') {
+ console.log('Client asking for sync, fetching from FB.');
+
+ // Retrieve current user's profile with client.facebook.profile()
+ client.facebook.profile(function(err, data) {
+ if (err) {
+ log.warn('Failed to fetch profile from Facebook.');
+ return;
+ }
+
+ client.msg('profile-name', { name: data.name });
+ });
+
+ // Retrieve current user's friends list with client.facebook.friends()
+ client.facebook.friends(function(err, data) {
+ if (err) {
+ log.warn('Failed to fetch friends from Facebook.');
+ return;
+ }
+
+ client.msg('friends', data);
+ });
+ } else
+ if (action === 'post') {
+ // Post to wall with client.facebook.post()
+ client.facebook.post(data.message, function(err, result) {
+ log.info('Posted to Facebook with err: ' + JSON.stringify(err));
+ log.info('Posted to Facebook with result: ' + JSON.stringify(result));
+ });
+ }
+});
+
+// Serve Facebook profile pictures:
+// - request.id is in form 'pic/facebook-id'.
+// - client.facebook.picture(id) will fetch a profile photo URL with a facebook id.
+
+app.setResourceHandler(function(request, response) {
+ function fail(msg) {
+ console.log(msg);
+ response.failed(msg);
+ }
+
+ if (request.id.substring(0, 4) === 'pic/') {
+ var id = request.id.substring(4);
+
+ var client = clients[request.owner];
+ if (client) {
+ // Fetch the profile photo URL by facebook id
+ client.facebook.picture(id, function(err, url) {
+ if (!err) {
+ // Fetch the actual photo and scale it to 40x40
+ scaling.scale(url, 40, 40, 'image/jpeg', function(err, data) {
+ if (!err) {
+ // Reply back with the photo data
+ response.reply('image/jpeg', data);
+ } else {
+ fail('Could not fetch photo with url='+ url);
+ }
+ });
+ } else {
+ fail('Could not fetch photo for id=' + id);
+ }
+ });
+ } else {
+ fail('Request from unknown client');
+ }
+ } else {
+ fail('Unknown request.');
+ }
+});
63 controller/main.js
@@ -0,0 +1,63 @@
+var ImageView = require('ui').ImageView;
+
+var app = this;
+
+exports[':keypress'] = function(key) {
+ this.get('input').emit('keypress', key);
+};
+
+exports[':load'] = function() {
+ var input = this.get('input');
+ input.on('submit', function() {
+ if (input !== '') {
+ app.msg('post', {
+ message: input.value()
+ });
+ }
+ input.value('');
+ });
+};
+
+exports[':resized'] = function(width, height) {
+ this.get('grid').setColumnCount(width/40);
+};
+
+exports.setName = function(name) {
+ this.get('name').label(name);
+};
+
+exports.handleFriends = function(arr) {
+ var grid = this.get('grid');
+
+ grid.setEntrySize(40, 40);
+ grid.setColumnCount(6);
+
+ for (var n=0; n < arr.length && n < 36; n++) {
+ var friend = arr[n];
+ console.log('adding friend: ' + friend.id);
+
+ var image = new ImageView({
+ style: {
+ width: 40,
+ height: 40
+ }
+ });
+ image.resource('pic/' + friend.id);
+
+ grid.add(image);
+ }
+};
+
+app.on('connected', function() {
+ console.log('Connected to backend.');
+ app.msg('sync');
+});
+
+app.on('message', function(action, data) {
+ if (action === 'profile-name') {
+ app.view('main').setName(data.name + '\'s friendgrid');
+ }
+ if (action === 'friends') {
+ app.view('main').handleFriends(data);
+ }
+});
4 metadata.json
@@ -0,0 +1,4 @@
+{
+ "name": "example, FacebookFriends",
+ "default-view": "main"
+}
BIN  res/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 views/main.json
@@ -0,0 +1,35 @@
+{
+ "style": {
+ "background-color": "white",
+ "border": 10
+ },
+
+ "contents": [
+ {
+ "name": "name",
+ "control": "TextView",
+ "label": "Loading profile...",
+ "style": {
+ "width": "fill-parent",
+ "align": "center"
+ }
+ },
+ {
+ "name": "grid",
+ "control": "CellLayout",
+ "style": {
+ "width": "fill-parent",
+ "height": "fill-parent"
+ }
+ },
+ {
+ "name": "input",
+ "control": "InputBox",
+ "style": {
+ "background-color": "#8888ee",
+ "width": "fill-parent",
+ "height": 20
+ }
+ }
+ ]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.