Stackedit/js/helpers/wordpressHelper.js

166 lines
5.7 KiB
JavaScript
Raw Normal View History

2013-05-27 19:45:33 +00:00
define([
"jquery",
"core",
"utils",
2013-06-10 21:22:32 +00:00
"extensionMgr",
"asyncRunner"
2013-05-27 19:45:33 +00:00
], function($, core, utils, extensionMgr, asyncRunner) {
2013-05-16 00:19:58 +00:00
2013-05-29 19:55:23 +00:00
var token = undefined;
2013-05-16 00:19:58 +00:00
2013-05-29 19:55:23 +00:00
var wordpressHelper = {};
2013-05-16 00:19:58 +00:00
2013-05-29 19:55:23 +00:00
// Only used to check the offline status
function connect(task) {
task.onRun(function() {
if(core.isOffline === true) {
task.error(new Error("Operation not available in offline mode.|stopPublish"));
return;
}
task.chain();
});
}
2013-05-16 00:19:58 +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() {
token = localStorage["wordpressToken"];
if(token !== undefined) {
task.chain();
return;
}
extensionMgr.onMessage("Please make sure the Wordpress authorization popup is not blocked by your browser.");
var errorMsg = "Failed to retrieve a token from Wordpress.";
// We add time for user to enter his credentials
task.timeout = ASYNC_TASK_LONG_TIMEOUT;
var code = undefined;
function getCode() {
localStorage.removeItem("wordpressCode");
authWindow = utils.popupWindow('wordpress-oauth-client.html?client_id=' + WORDPRESS_CLIENT_ID, 'stackedit-wordpress-oauth', 960, 600);
authWindow.focus();
intervalId = setInterval(function() {
if(authWindow.closed === true) {
clearInterval(intervalId);
authWindow = undefined;
intervalId = undefined;
code = localStorage["wordpressCode"];
if(code === undefined) {
task.error(new Error(errorMsg));
return;
}
localStorage.removeItem("wordpressCode");
task.chain(getToken);
}
}, 500);
}
function getToken() {
$.getJSON(WORDPRESS_PROXY_URL + "authenticate/" + code, function(data) {
if(data.token !== undefined) {
token = data.token;
localStorage["wordpressToken"] = token;
task.chain();
}
else {
task.error(new Error(errorMsg));
}
});
}
task.chain(getCode);
});
task.onError(function() {
if(intervalId !== undefined) {
clearInterval(intervalId);
}
if(authWindow !== undefined) {
authWindow.close();
}
});
}
2013-05-16 00:19:58 +00:00
2013-05-29 19:55:23 +00:00
wordpressHelper.upload = function(site, postId, tags, title, content, callback) {
var task = asyncRunner.createTask();
connect(task);
authenticate(task);
task.onRun(function() {
var url = WORDPRESS_PROXY_URL + "post";
var data = {
token: token,
site: site,
postId: postId,
tags: tags,
title: title,
content: content
};
$.ajax({
url: url,
data: data,
type: "POST",
dataType: "json",
timeout: AJAX_TIMEOUT
}).done(function(response, textStatus, jqXHR) {
if(response.body.ID) {
postId = response.body.ID;
task.chain();
return;
}
var error = {
code: response.code,
message: response.body.error
};
// Handle error
if(error.code === 404) {
if(error.message == "unknown_blog") {
error = 'Site "' + site + '" not found on WordPress.|removePublish';
}
else if(error.message == "unknown_post") {
error = 'Post ' + postId + ' not found on WordPress.|removePublish';
}
}
handleError(error, task);
}).fail(function(jqXHR) {
var error = {
code: jqXHR.status,
message: jqXHR.statusText
};
handleError(error, task);
});
});
task.onSuccess(function() {
callback(undefined, postId);
});
task.onError(function(error) {
callback(error);
});
asyncRunner.addTask(task);
};
2013-05-16 00:19:58 +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 WordPress.";
if((error.code === 400 && error.message == "invalid_token") || error.code === 401 || error.code === 403) {
localStorage.removeItem("wordpressToken");
errorMsg = "Access to WordPress 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 wordpressHelper;
2013-05-16 00:19:58 +00:00
});