Last active
August 29, 2015 14:10
-
-
Save MhdSyrwan/301ac6d9181633b3418c to your computer and use it in GitHub Desktop.
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
/** | |
* @ngdoc function | |
* @name publishToFacebookGroups | |
* @description publishes a deal as photos to an array of fb groups | |
* @param {resource} deal The deal to share | |
* @param {array} groups An Array of groups to share on | |
* @returns {boolean} A promise to know later if the deal sharing process failed or succeeded | |
*/ | |
function publishToFacebookGroups(deal, groups) { | |
$rootScope.currentUser.balance -= groups.length; | |
var deferred = $q.defer(); | |
// The Full Sharing Payload (Image/Post) | |
var sharingPayload = { | |
hLink: window.location.toString().split('-')[0], //TODO: Try without slug | |
hPicture: deal.cover_photo_url, | |
hName: deal.title, | |
hDescription: deal.description, | |
hMessage: $compile($templateCache.get('partials/foo.html'))(deal) | |
}; | |
// Initializing Batch Processing Job Queue | |
batch_data = []; | |
// Creating Batch Processing Jobs | |
for (i=0;i<groups.length;i++) { | |
// Options for each job | |
var options = { | |
method: 'post', | |
relative_url: group[i].url + "/photos", // e.g: '/groups/:id/photos' | |
// Serializing json to query string via $.param | |
body: $.param({ | |
message: sharingPayload.hMessage, | |
url: sharingPayload.hPicture | |
}) | |
} | |
// Pusing Job Options to the Queue | |
batch_data.push(options); | |
} | |
// Doing the Actual Request with Facebook API | |
FB.api("/", 'post', { | |
batch: batch_data // The batch we prepared above | |
}, function(fbResult) { | |
var results = []; | |
// if request succeed we should recieve an Array | |
if (fbResult.length) deferred.resolve(fbResult); //TODO: discuss what to send | |
// Iterating over returned posts ids from facebook | |
for (i=0;i<fbResult.length;i++) { | |
// Parsing Facebook Resource JSON | |
var fbResource = JSON.parse(fbResult[i].body); | |
// if it's a real resource push it to the server | |
//TODO: Why we could recieve something without id !? What is it ? | |
if (fbResource['id']) { | |
results.push(fbResource['post_id']); // we just need post id to attach it to our deal Record in DB | |
} | |
} | |
// Pushing Post Ids to our server to be attached with the deal | |
$.post( SETTINGS.mainURL + '/deals/' + deal.id + '/fb_groups_shared.json',{ | |
share_data: results | |
} | |
).done(function(res) { | |
deferred.resolve(res); //TODO: discuss what to send | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment