Stackedit/public/res/helpers/tumblrHelper.js

186 lines
6.4 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
2013-11-05 23:03:38 +00:00
"constants",
2013-05-27 19:45:33 +00:00
"core",
"utils",
2013-11-05 23:03:38 +00:00
"storage",
"logger",
2013-07-30 08:46:36 +00:00
"eventMgr",
2013-06-16 10:47:35 +00:00
"classes/AsyncTask"
2013-11-05 23:03:38 +00:00
], function($, constants, core, utils, storage, logger, eventMgr, AsyncTask) {
2013-04-28 22:33:17 +00:00
2013-11-05 23:03:38 +00:00
var oauthParams;
2013-04-28 22:33:17 +00:00
2013-05-29 19:55:23 +00:00
var tumblrHelper = {};
2013-04-28 22:33:17 +00:00
2013-08-04 00:53:46 +00:00
// Listen to offline status changes
var isOffline = false;
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
isOffline = isOfflineParam;
});
2013-05-29 19:55:23 +00:00
// Only used to check the offline status
function connect(task) {
task.onRun(function() {
2013-08-04 00:53:46 +00:00
if(isOffline === true) {
2013-05-29 19:55:23 +00:00
task.error(new Error("Operation not available in offline mode.|stopPublish"));
return;
}
task.chain();
});
}
2013-04-28 22:33:17 +00:00
2013-05-29 19:55:23 +00:00
// Try to authenticate with OAuth
function authenticate(task) {
2013-11-05 23:03:38 +00:00
var authWindow;
var intervalId;
2013-05-29 19:55:23 +00:00
task.onRun(function() {
if(oauthParams !== undefined) {
task.chain();
return;
}
2013-11-05 23:03:38 +00:00
var serializedOauthParams = storage.tumblrOauthParams;
2013-05-29 19:55:23 +00:00
if(serializedOauthParams !== undefined) {
oauthParams = JSON.parse(serializedOauthParams);
task.chain();
return;
}
var errorMsg = "Failed to retrieve a token from Tumblr.";
// We add time for user to enter his credentials
2013-11-05 23:03:38 +00:00
task.timeout = constants.ASYNC_TASK_LONG_TIMEOUT;
var oauth_object;
2013-05-29 19:55:23 +00:00
function getOauthToken() {
2013-11-05 23:03:38 +00:00
$.getJSON(constants.TUMBLR_PROXY_URL + "request_token", function(data) {
2013-05-29 19:55:23 +00:00
if(data.oauth_token !== undefined) {
oauth_object = data;
2013-10-06 14:34:40 +00:00
task.chain(oauthRedirect);
2013-05-29 19:55:23 +00:00
}
else {
task.error(new Error(errorMsg));
}
});
}
2013-10-06 14:34:40 +00:00
function oauthRedirect() {
2014-01-13 18:57:59 +00:00
utils.redirectConfirm('You are being redirected to <strong>Tumblr</strong> authorization page.', function() {
2013-10-06 14:34:40 +00:00
task.chain(getVerifier);
2013-10-13 21:13:27 +00:00
}, function() {
task.error(new Error('Operation canceled.'));
2013-10-06 14:34:40 +00:00
});
}
2013-05-29 19:55:23 +00:00
function getVerifier() {
2013-11-05 23:03:38 +00:00
storage.removeItem("tumblrVerifier");
2013-10-08 00:34:15 +00:00
authWindow = utils.popupWindow('html/tumblr-oauth-client.html?oauth_token=' + oauth_object.oauth_token, 'stackedit-tumblr-oauth', 800, 600);
2013-05-29 19:55:23 +00:00
authWindow.focus();
intervalId = setInterval(function() {
if(authWindow.closed === true) {
clearInterval(intervalId);
authWindow = undefined;
intervalId = undefined;
2013-11-05 23:03:38 +00:00
oauth_object.oauth_verifier = storage.tumblrVerifier;
2013-05-29 19:55:23 +00:00
if(oauth_object.oauth_verifier === undefined) {
task.error(new Error(errorMsg));
return;
}
2013-11-05 23:03:38 +00:00
storage.removeItem("tumblrVerifier");
2013-05-29 19:55:23 +00:00
task.chain(getAccessToken);
}
}, 500);
}
function getAccessToken() {
2013-11-05 23:03:38 +00:00
$.getJSON(constants.TUMBLR_PROXY_URL + "access_token", oauth_object, function(data) {
2013-05-29 19:55:23 +00:00
if(data.access_token !== undefined && data.access_token_secret !== undefined) {
2013-11-05 23:03:38 +00:00
storage.tumblrOauthParams = JSON.stringify(data);
2013-05-29 19:55:23 +00:00
oauthParams = data;
task.chain();
}
else {
task.error(new Error(errorMsg));
}
});
}
task.chain(getOauthToken);
});
task.onError(function() {
if(intervalId !== undefined) {
clearInterval(intervalId);
}
if(authWindow !== undefined) {
authWindow.close();
}
});
}
2013-04-28 22:33:17 +00:00
tumblrHelper.upload = function(blogHostname, postId, tags, format, state, date, title, content, callback) {
2013-06-16 10:47:35 +00:00
var task = new AsyncTask();
2013-05-29 19:55:23 +00:00
connect(task);
authenticate(task);
task.onRun(function() {
var data = $.extend({
blog_hostname: blogHostname,
post_id: postId,
tags: tags,
format: format,
state: state,
date: date,
2013-05-29 19:55:23 +00:00
title: title,
content: content
}, oauthParams);
$.ajax({
2013-11-05 23:03:38 +00:00
url: constants.TUMBLR_PROXY_URL + "post",
2013-05-29 19:55:23 +00:00
data: data,
type: "POST",
dataType: "json",
2013-11-05 23:03:38 +00:00
timeout: constants.AJAX_TIMEOUT
}).done(function(post) {
2013-05-29 19:55:23 +00:00
postId = post.id;
task.chain();
}).fail(function(jqXHR) {
var error = {
code: jqXHR.status,
message: jqXHR.statusText
};
// Handle error
if(error.code === 404 && postId !== undefined) {
error = 'Post ' + postId + ' not found on Tumblr.|removePublish';
}
handleError(error, task);
});
});
task.onSuccess(function() {
callback(undefined, postId);
});
task.onError(function(error) {
callback(error);
});
2013-06-16 10:47:35 +00:00
task.enqueue();
2013-05-29 19:55:23 +00:00
};
2013-04-28 22:33:17 +00:00
2013-05-29 19:55:23 +00:00
function handleError(error, task) {
2013-11-05 23:03:38 +00:00
var errorMsg;
2013-05-29 19:55:23 +00:00
if(error) {
logger.error(error);
// Try to analyze the error
if(typeof error === "string") {
errorMsg = error;
}
else {
errorMsg = "Could not publish on Tumblr.";
if(error.code === 401 || error.code === 403) {
oauthParams = undefined;
2013-11-05 23:03:38 +00:00
storage.removeItem("tumblrOauthParams");
2013-05-29 19:55:23 +00:00
errorMsg = "Access to Tumblr account is not authorized.";
task.retry(new Error(errorMsg), 1);
return;
}
else if(error.code <= 0) {
core.setOffline();
errorMsg = "|stopPublish";
}
}
}
task.error(new Error(errorMsg));
}
return tumblrHelper;
2013-04-28 22:33:17 +00:00
});