Stackedit/public/res/helpers/tumblrHelper.js

179 lines
6.2 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
"core",
"utils",
2013-07-30 08:46:36 +00:00
"eventMgr",
2013-06-16 10:47:35 +00:00
"classes/AsyncTask"
2013-07-30 08:46:36 +00:00
], function($, core, utils, eventMgr, AsyncTask) {
2013-04-28 22:33:17 +00:00
2013-05-29 19:55:23 +00:00
var oauthParams = undefined;
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) {
var authWindow = undefined;
var intervalId = undefined;
task.onRun(function() {
if(oauthParams !== undefined) {
task.chain();
return;
}
2013-10-06 14:34:40 +00:00
var serializedOauthParams = localStorage.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
task.timeout = ASYNC_TASK_LONG_TIMEOUT;
var oauth_object = undefined;
function getOauthToken() {
$.getJSON(TUMBLR_PROXY_URL + "request_token", function(data) {
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() {
core.oauthRedirect('Tumblr', function() {
task.chain(getVerifier);
});
}
2013-05-29 19:55:23 +00:00
function getVerifier() {
localStorage.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-10-06 14:34:40 +00:00
oauth_object.oauth_verifier = localStorage.tumblrVerifier;
2013-05-29 19:55:23 +00:00
if(oauth_object.oauth_verifier === undefined) {
task.error(new Error(errorMsg));
return;
}
localStorage.removeItem("tumblrVerifier");
task.chain(getAccessToken);
}
}, 500);
}
function getAccessToken() {
$.getJSON(TUMBLR_PROXY_URL + "access_token", oauth_object, function(data) {
if(data.access_token !== undefined && data.access_token_secret !== undefined) {
2013-10-06 14:34:40 +00:00
localStorage.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
2013-05-29 19:55:23 +00:00
tumblrHelper.upload = function(blogHostname, postId, tags, format, 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,
title: title,
content: content
}, oauthParams);
$.ajax({
url: TUMBLR_PROXY_URL + "post",
data: data,
type: "POST",
dataType: "json",
timeout: AJAX_TIMEOUT
}).done(function(post, textStatus, jqXHR) {
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) {
var errorMsg = undefined;
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;
localStorage.removeItem("tumblrOauthParams");
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
});