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-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-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-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() {
|
2013-10-06 14:34:40 +00:00
|
|
|
token = localStorage.wordpressToken;
|
2013-05-29 19:55:23 +00:00
|
|
|
if(token !== undefined) {
|
|
|
|
task.chain();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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;
|
2013-10-06 14:34:40 +00:00
|
|
|
function oauthRedirect() {
|
|
|
|
core.oauthRedirect('WordPress', function() {
|
|
|
|
task.chain(getCode);
|
|
|
|
});
|
|
|
|
}
|
2013-05-29 19:55:23 +00:00
|
|
|
function getCode() {
|
2013-10-06 14:34:40 +00:00
|
|
|
eventMgr.onMessage("Please make sure the Wordpress authorization popup is not blocked by your browser.");
|
2013-05-29 19:55:23 +00:00
|
|
|
localStorage.removeItem("wordpressCode");
|
2013-10-08 00:34:15 +00:00
|
|
|
authWindow = utils.popupWindow('html/wordpress-oauth-client.html?client_id=' + WORDPRESS_CLIENT_ID, 'stackedit-wordpress-oauth', 960, 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
|
|
|
code = localStorage.wordpressCode;
|
2013-05-29 19:55:23 +00:00
|
|
|
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;
|
2013-10-06 14:34:40 +00:00
|
|
|
localStorage.wordpressToken = token;
|
2013-05-29 19:55:23 +00:00
|
|
|
task.chain();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
task.error(new Error(errorMsg));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-10-06 14:34:40 +00:00
|
|
|
task.chain(oauthRedirect);
|
2013-05-29 19:55:23 +00:00
|
|
|
});
|
|
|
|
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) {
|
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 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);
|
|
|
|
});
|
2013-06-16 10:47:35 +00:00
|
|
|
task.enqueue();
|
2013-05-29 19:55:23 +00:00
|
|
|
};
|
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
|
|
|
});
|