Skip to content

Commit

Permalink
feat(facebook): Remove calls to Facebook Opengraph (#407)
Browse files Browse the repository at this point in the history
Intent: clean and simplify the source code.
  • Loading branch information
adrienjoly committed Nov 28, 2020
1 parent 14177a7 commit 156593d
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 252 deletions.
50 changes: 1 addition & 49 deletions app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var ObjectId = mongodb.ObjectId; //ObjectID.createFromHexString;
var emailModel = require('../models/email.js');
var postModel = require('../models/post.js');
var searchModel = require('../models/search.js');
var fbModel = require('../models/facebook.js');
var snip = require('../snip.js');

var crypto = require('crypto');
Expand Down Expand Up @@ -58,11 +57,6 @@ var USER_FIELDS = {
var defaultPref = (exports.DEFAULT_PREF = {
// ui preferences
hideBkAd: false, // hide the "add bookmaklet" side box on home page
// facebook opengraph prefs:
ogLik: true, // like track
ogAdd: true, // add track
ogPla: false, // play track
ogCPl: true, // create playlist

// email notification freq (7=weekly, 1=daily, 0=instantly, -1=never)
emLik: -1, // "like" notif (default: never)
Expand Down Expand Up @@ -114,7 +108,6 @@ exports.EM_LABEL = {
var TESTING_DIGEST = config.digestImmediate;

(function parseHandlesFromRouteFile(routeFile) {
var nb = 0;
// console.log('Parsing reserved usernames from', routeFile, '...');
snip.forEachFileLine(routeFile, function (line) {
if (typeof line != 'string') return; // console.log('=> Parsed', nb, 'handles from:', routeFile);
Expand All @@ -124,20 +117,17 @@ var TESTING_DIGEST = config.digestImmediate;
var handle = line.substr(0, end);
if (handle.indexOf('{') == -1) {
USERNAME_RESERVED[handle] = true;
++nb;
}
}
});
})('config/app.route');

(function parseHandlesFromTextFile(fileName) {
var nb = 0;
// console.log('Parsing reserved usernames from', fileName, '...');
snip.forEachFileLine(fileName, function (line) {
if (typeof line != 'string') return; // console.log('=> Parsed', nb, 'handles from:', fileName);
if (!line.length || line[0] == '#') return;
USERNAME_RESERVED[line] = true;
++nb;
});
})('config/reservedwords.txt');

Expand Down Expand Up @@ -547,45 +537,7 @@ exports.createPlaylist = function (uId, name, handler) {
exports.save(user, function () {
console.log('created playlist:', pl.name, pl.id);
searchModel.indexPlaylist(uId, pl.id, pl.name);
//handler(pl);

if (user.fbTok && user.pref && user.pref.ogCPl)
try {
console.log('publishing to fb opengraph...');
var params = {
method: 'POST',
playlist: config.urlPrefix + '/u/' + uId + '/playlist/' + pl.id,
};
fbModel.graphApiRequest(
user.fbTok,
'/me/music.playlists',
params,
function (result) {
//console.log("opengraph playlist =>", result);
if (result && result.id)
exports.setPlaylist(
uId,
pl.id,
{ fbId: result.id },
function () {
console.log('=> done updating playlist', pl);
handler(pl);
}
);
else {
exports.setPlaylist(uId, pl.id, {}, function () {
console.log('user.createPlaylist failed opengraph request');
console.log('=> done updating playlist', pl);
handler(pl);
});
}
}
);
} catch (e) {
console.log('createplaylist error: ', e);
handler(pl);
}
else handler(pl);
handler(pl);
});
});
};
Expand Down
26 changes: 3 additions & 23 deletions app/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,9 @@ <h2 class="head-name-tab">Settings</h2>
<div id="fbConn"></div>
{{#pref}}
<div id="pref">
<label>
<input
type="checkbox"
name="ogAdd"
value="1"
data-checked="{{ogAdd}}"
/>
Publish added tracks to Facebook
</label>
<label>
<input
type="checkbox"
name="ogLik"
value="1"
data-checked="{{ogLik}}"
/>
Publish liked tracks to Facebook
</label>
<!--<label>
<input type="checkbox" name="ogPla" value="1" data-checked="{{ogPla}}">
Publish played tracks to Facebook
</label>-->
<p class="msg"></p>
<p class="msg" style="margin-top: 15px">
You can login to your Openwhyd account using Facebook.
</p>
</div>
{{/pref}}
</fieldset>
Expand Down
137 changes: 40 additions & 97 deletions public/js/facebook.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,54 @@
/* global $, FB, user, showMessage */
/* global $ */

const globals = window;

var href = window.location.href + '/';

var namespace, fbId;
var fbId;
if (href.indexOf('openwhyd.org/') > 0) {
namespace = 'whydapp';
// namespace = 'whydapp';
fbId = '169250156435902';
} else if (href.indexOf('whyd.fr/') > 0) {
} /*else if (href.indexOf('whyd.fr/') > 0) {
// pre-production
namespace = 'whyd-test';
fbId = '1059973490696893';
} else {
namespace = 'whyd-dev';
} */ else {
// namespace = 'whyd-dev';
fbId = '118010211606360';
}

var facebookPerms = 'public_profile,email'; // 'user_friends,user_interests,user_likes,email,publish_stream';
var FB_ACTION_URI_PREFIX = href.substr(0, href.indexOf('/', 10)); //"http://openwhyd.org";
var facebookPerms = 'public_profile,email';

var whenFbReadyQueue = [];

function whenFbReady(fct) {
globals.whenFbReady = function (fct) {
whenFbReadyQueue.push(fct);
}
};

function fbSafeCall(fct, failFct) {
if (!window.FB) {
showMessage('Unable to connect to Facebook. Did you block it?', true);
const fbSafeCall = function (fct, failFct) {
if (!globals.FB) {
globals.showMessage(
'Unable to connect to Facebook. Did you block it?',
true
);
if (failFct) failFct();
} else fct();
}
};

function fbIsLogged(cb) {
if (window.FB)
FB.getLoginStatus(function (response) {
globals.fbIsLogged = function (cb) {
if (globals.FB)
globals.FB.getLoginStatus(function (response) {
cb(
response.status === 'connected' &&
window.user.fbId == response.authResponse.userID
globals.user.fbId == response.authResponse.userID
);
}, true);
else if (cb) cb(false);
}

function fbPost(url, callback) {
console.log('fbPost', url);
if (window.FB)
FB.api(url, 'post', function (response) {
console.log('fbPost response:', response);
if (callback) callback(response);
});
else if (callback) callback();
}

var verbToPrefName = {
add: 'ogAdd',
repost: 'ogAdd',
like: 'ogLik',
listen: 'ogPla',
};

function fbIsActionPermitted(verb) {
return (
verb &&
verbToPrefName[verb] &&
user &&
user.pref &&
user.pref[verbToPrefName[verb]]
);
}

function fbAction(verb, uri, type, callback) {
//console.log("fbAction", verb, uri, fbIsActionPermitted(verb));
fbIsLogged(function (loggedIn) {
if (loggedIn && fbIsActionPermitted(verb)) {
var url =
verb == 'listen'
? '/me/music.listens?song=' +
FB_ACTION_URI_PREFIX +
uri.replace('/c/', '/post/')
: '/me/' +
namespace +
':' +
verb +
'?' +
(type || 'website') +
'=' +
FB_ACTION_URI_PREFIX +
uri;
fbPost(url, callback);
} else if (callback) callback();
});
}

function fbLike(uri, callback) {
fbIsLogged(function (loggedIn) {
if (loggedIn && fbIsActionPermitted('like'))
fbPost(
'/me/og.likes' + '?object=' + FB_ACTION_URI_PREFIX + uri,
callback
);
else if (callback) callback();
});
}

function fbAuth(perms, cb, dontLink) {
globals.fbAuth = function (perms, cb, dontLink) {
fbSafeCall(function () {
FB.login(
globals.FB.login(
function (response) {
console.log('fb response', response);
response = response || {};
Expand All @@ -126,10 +69,10 @@ function fbAuth(perms, cb, dontLink) {
{ scope: facebookPerms + (perms ? ',' + perms : '') }
);
}, cb);
}
};

function fbLogin(perms, cb) {
fbAuth(
globals.fbLogin = function (perms, cb) {
globals.fbAuth(
perms,
function (fbId, fbAuthResponse) {
if (!fbId) {
Expand Down Expand Up @@ -170,10 +113,10 @@ function fbLogin(perms, cb) {
},
true
);
}
};

function fbRegister(perms, cb) {
fbAuth(
globals.fbRegister = function (perms, cb) {
globals.fbAuth(
perms,
function (fbId, fbAuthResponse) {
if (!fbId) {
Expand All @@ -186,7 +129,7 @@ function fbRegister(perms, cb) {
access_token: fbAuthResponse.authResponse.accessToken,
};

FB.api('/me', options, (response) => {
globals.FB.api('/me', options, (response) => {
cb({
fbAuthResponse: fbAuthResponse.authResponse, // fbTok
fbUser: {
Expand All @@ -199,32 +142,32 @@ function fbRegister(perms, cb) {
},
true
);
}
};

window.fbAsyncInit = function () {
FB.init({
globals.fbAsyncInit = function () {
globals.FB.init({
appId: fbId,
version: 'v2.3',
status: true,
cookie: true,
oauth: true,
xfbml: true,
});
window.whenFbReady = function (fct) {
globals.whenFbReady = function (fct) {
fct();
};
while (whenFbReadyQueue.length) whenFbReadyQueue.shift()();
};

whenFbReady(function () {
globals.whenFbReady(function () {
console.log('watching fb events');
FB.Event.subscribe('edge.create', function (targetUrl) {
window.Whyd.tracking.logSocial('facebook', 'like', targetUrl);
globals.FB.Event.subscribe('edge.create', function (targetUrl) {
globals.Whyd.tracking.logSocial('facebook', 'like', targetUrl);
});
FB.Event.subscribe('message.send', function (sharedUrl) {
globals.FB.Event.subscribe('message.send', function (sharedUrl) {
//var pId = sharedUrl.split("/").pop();
//console.log("facebook invitation was sent", pId);
window.Whyd.tracking.logSocial('facebook', 'message', sharedUrl);
globals.Whyd.tracking.logSocial('facebook', 'message', sharedUrl);
});
});

Expand Down
Loading

0 comments on commit 156593d

Please sign in to comment.