Skip to content

Commit

Permalink
Get most recent posts from page
Browse files Browse the repository at this point in the history
  • Loading branch information
btidwell committed Apr 10, 2015
1 parent c77727e commit 3721152
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
32 changes: 32 additions & 0 deletions controllers/feed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = function FeedControllerModule(pb){
var util = pb.util;
var PluginService = pb.PluginService;
var OauthService = pb.PluginService.getService("oauthService", "pencilblue_facebook");
var PostsService = pb.PluginService.getService("postsService", "pencilblue_facebook");
var FB = require('fb');

function FeedController(){};

util.inherits(FeedController, pb.BaseController);

FeedController.prototype.getPagePosts = function(cb){
OauthService.getAccessToken(function(accessToken){
PostsService.getPagePosts(accessToken, cb);
});
};

FeedController.getRoutes = function(cb){
var routes = [
{
method: 'get',
path: '/action/facebook/posts',
auth_required: false,
content_type: 'application/json',
handler: 'getPagePosts'
}
];
cb(null, routes);
};

return FeedController;
};
59 changes: 59 additions & 0 deletions details.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"uid": "pencilblue_facebook",
"name": "Facebook Plugin",
"description": "Facebook feed plugin for Pencilblue",
"pb_version": "0.4.x",
"version": "0.1.0",
"author": {
"name": "CareerBuilder CMS Team",
"email": "talentengagementcloud@careerbuilder.com",
"website": "http://careerbuilder.com",
"contributors": [
{
"name": "Christina Chatham",
"email": "christina.chatham@careerbuilder.com"
},
{
"name": "Ben Tidwell",
"email": "ben.tidwell@careerbuilder.com"
},
{
"name": "Corrine Olson",
"email": "corrine.olson@careerbuilder.com"
},
{
"name": "Evan Adams",
"email": "Evan.Adams@careerbuilder.com"
}
]
},
"settings": [
{
"name": "app_id",
"value": ""
},
{
"name": "app_secret",
"value": ""
},
{
"name": "facebook_page_id",
"value": ""
}
],
"permissions": {
"ACCESS_USER": [],
"ACCESS_WRITER": [],
"ACCESS_EDITOR": [],
"ACCESS_MANAGING_EDITOR": []
},
"main_module": {
"path": "facebook.js"
},
"dependencies": {
"fb": "0.7.0"
},
"theme": {
"settings": []
}
}
52 changes: 52 additions & 0 deletions facebook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module.exports = function FacebookPluginModule(pb) {
function Facebook(){}

/**
* Called when the application is being installed for the first time.
*
* @param cb A callback that must be called upon completion. cb(Error, Boolean).
* The result should be TRUE on success and FALSE on failure
*/
Facebook.onInstall = function(cb) {
pb.log.info("Facebook Plugin Installed");
cb(null, true);
};

/**
* Called when the application is uninstalling this plugin. The plugin should
* make every effort to clean up any plugin-specific DB items or any in function
* overrides it makes.
*
* @param cb A callback that must be called upon completion. cb(Error, Boolean).
* The result should be TRUE on success and FALSE on failure
*/
Facebook.onUninstall = function(cb) {
pb.log.info("Facebook Job Plugin Uninstalled");
cb(null, true);
};

/**
* Called when the application is starting up. The function is also called at
* the end of a successful install. It is guaranteed that all core PB services
* will be available including access to the core DB.
*
* @param cb A callback that must be called upon completion. cb(Error, Boolean).
* The result should be TRUE on success and FALSE on failure
*/
Facebook.onStartup = function(cb) {
cb(null, true);
};

/**
* Called when the application is gracefully shutting down. No guarantees are
* provided for how much time will be provided the plugin to shut down.
*
* @param cb A callback that must be called upon completion. cb(Error, Boolean).
* The result should be TRUE on success and FALSE on failure
*/
Facebook.onShutdown = function(cb) {
cb(null, true);
};

return Facebook;
};
38 changes: 38 additions & 0 deletions services/oauth_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = function OauthServiceModule(pb){
var PluginService = pb.PluginService;
var FB = require('fb');

function OauthService(){}

OauthService.init = function(cb){
pb.log.debug("OauthService: Initialized");
cb(null, true);
};

OauthService.getName = function(){
return "oauthService";
};

OauthService.getAccessToken = function(cb){
var pluginService = new PluginService();
pluginService.getSettingsKV('pencilblue_facebook', function(err, settings){
FB.api('oauth/access_token', {
client_id: settings.app_id,
client_secret: settings.app_secret,
grant_type: 'client_credentials'
}, function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
throw res.error;
}
var accessToken = res.access_token;
FB.setAccessToken(accessToken);
if(cb){
cb(accessToken);
}
});
});
};

return OauthService;
};
29 changes: 29 additions & 0 deletions services/posts_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = function PostsServiceModule(pb){
var PluginService = pb.PluginService;
var FB = require('fb');

function PostsService(){}

PostsService.init = function(cb){
pb.log.debug("PostsService: Initialized");
cb(null, true);
};

PostsService.getName = function(){
return "postsService";
};

PostsService.getPagePosts = function(accessToken, cb){
var pluginService = new PluginService();
pluginService.getSettingsKV('pencilblue_facebook', function(err, settings){
FB.api('/v2.3/' + settings.facebook_page_id + '/posts', {"access_token":accessToken},function(response){
cb({
status:200,
content:JSON.stringify(response)
});
});
});
};

return PostsService;
};

0 comments on commit 3721152

Please sign in to comment.