more work on posting and showing status posts

This commit is contained in:
jeena 2013-05-27 09:44:43 +02:00
parent f73ce7d196
commit 650f107d21
7 changed files with 108 additions and 135 deletions

View file

@ -22,80 +22,47 @@ function(jQuery, HostApp, Hmac, Cache) {
}
return vars;
}
/*
APICalls.http_call = function(url, http_method, callback, data, auth_header, accepts) {
if(accepts !== false) accepts = accepts || "application/vnd.tent.post.v0+json";
var options = {
beforeSend: function(xhr) {
if(accepts !== false) xhr.setRequestHeader("Accept", accepts);
if (data) xhr.setRequestHeader("Content-Length", data.length);
if (auth_header) { // if is_set? auth_header
xhr.setRequestHeader("Authorization", auth_header);
} else {
var user_access_token = HostApp.stringForKey("user_access_token");
if (auth_header !== false && typeof user_access_token != "undefined") {
auth_header = Hmac.makeAuthHeader(
url,
http_method,
HostApp.secret(),
user_access_token
);
xhr.setRequestHeader("Authorization", auth_header);
}
}
},
url: url,
contentType: 'application/vnd.tent.post.v0+json; type="https://tent.io/types/app/v0#"',
type: http_method,
complete: callback,
data: data,
processData: false,
error: function(xhr, ajaxOptions, thrownError) {
console.error("getURL (" + xhr.status + ")" + xhr.statusText + " " + http_method + " (" + url + "): '" + xhr.responseText + "'");
}
}
debug(url)
jQuery.ajax(options);
}
*/
APICalls.http_call = function(options) {
if(!options.content_type) {
if (typeof options === "string") {
console.error(options + " not implemented yet")
return;
}
var content_type = null;
if(options.http_method == "POST" && !options.content_type) {
console.error("No content type for " + options.url);
return;
} else {
content_type = "application/vnd.tent.post.v0+json; type=\"" + options.content_type + "\"";
}
var settings = {
beforeSend: function(xhr) {
if (options.data) xhr.setRequestHeader("Content-Length", data.length);
if (options.accept) xhr.setRequestHeader("Accept", "application/vnd.tent.post.v0+json");
if (options.data) xhr.setRequestHeader("Content-Length", options.data.length);
if (options.accept) xhr.setRequestHeader("Accept", options.accept);
else xhr.setRequestHeader("Accept", "application/vnd.tent.post.v0+json");
var user_access_token = HostApp.stringForKey("user_access_token");
if (!no_auth && user_access_token) {
if (!options.auth_header && !options.no_auth && user_access_token) {
var auth_header = Hmac.makeHawkAuthHeader(
options.url,
options.http_method,
HostApp.secret(),
user_access_token
user_access_token,
HostApp.secret()//,
//HostApp.stringForKey("app_id")
);
xhr.setRequestHeader("Authorization", auth_header);
} else {
} else if(options.auth_header) {
xhr.setRequestHeader("Authorization", options.auth_header);
} else if(!options.no_auth) {
console.error("No user_access_token yet - " + options.url);
}
}
},
url: options.url,
contentType: options.content_type,
type: url.http_method,
contentType: content_type,
type: options.http_method,
complete: options.callback,
data: options.data,
processData: false,
@ -111,13 +78,11 @@ function(jQuery, HostApp, Hmac, Cache) {
var settings = {
url: url,
http_method: "GET",
accept: null,
data: null,
no_auth: false
content_type: null
};
jQuery.extend(settings, options);
for (var key in options) {
settings[key] = options[key];
}
APICalls.http_call(settings);
}
@ -129,7 +94,9 @@ function(jQuery, HostApp, Hmac, Cache) {
data: data
};
jQuery.extend(settings, options);
for (var key in options) {
settings[key] = options[key];
}
APICalls.http_call(settings);
}

View file

@ -162,7 +162,28 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
}
Core.prototype.getStatusDOMElement = function(status) {
/*
{
"app": {
"id": "P8FJjaiRv0AKXfjUMd_4YQ",
"name": "Bungloo on Linux",
"url": "http:\/\/jabs.nu\/bungloo\/"
},
"content": {
"text": "jeena test"
},
"entity": "http:\/\/155969d81672.alpha.attic.is",
"id": "HlSXe8MREzU4h2fGLGSnCA",
"published_at": 1369566009,
"received_at": 1369566008799,
"type": "https:\/\/tent.io\/types\/status\/v0#",
"version": {
"id": "a2f702b4615c7d7dd0f98c73d7b55749880bf6e437a77349454ff10745d134c6",
"published_at": 1369566009,
"received_at": 1369566008799
}
}
*/
var _this = this;
var template = this.getTemplate();
@ -320,6 +341,7 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
this.findMentions(template.message, status.mentions);
/*
for (var i = 0; i < status.mentions.length; i++) {
var mention = status.mentions[i];
if (mention.entity == HostApp.stringForKey("entity")) {
@ -327,10 +349,10 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
break;
}
}
var published_at = typeof status.__repost == "undefined" ? status.published_at : status.__repost.published_at;
*/
var published_at = typeof status.__repost == "undefined" ? status.version.published_at : status.__repost.published_at;
var time = document.createElement("abbr");
time.innerText = this.ISODateString(new Date(published_at * 1000));
time.innerText = this.ISODateString(new Date(published_at));
time.title = time.innerText;
time.className = "timeago";
jQuery(time).timeago();
@ -483,13 +505,13 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
} else {
var url = URI(APICalls.mkApiRootPath("/posts"));
var url = URI(HostApp.serverUrl("new_post"));
var http_method = "POST";
var data = {
"type": "https://tent.io/types/post/status/v0.1.0",
"published_at": parseInt(new Date().getTime() / 1000, 10),
"type": in_reply_to_status_id ? "https://tent.io/types/status/v0#" : "https://tent.io/types/status/v0#reply",
"published_at": parseInt(new Date().getTime(), 10),
"permissions": {
"public": !is_private
},
@ -517,7 +539,11 @@ function(jQuery, APICalls, URI, HostApp, Cache) {
}
}
APICalls.http_call(url.toString(), http_method, callback, JSON.stringify(data));
// APICalls.http_call(url.toString(), http_method, callback, JSON.stringify(data));
APICalls.post(url.toString(), JSON.stringify(data), {
content_type: data.type,
callback: callback
});
}
}

View file

@ -7,40 +7,7 @@ function(URI, CryptoJS) {
var Hmac = {};
Hmac.makeAuthHeader = function(url, http_method, mac_key, mac_key_id) {
debug("makeAuthHeader should not be used anymore, bug!")
url = URI(url);
var nonce = Hmac.makeid(8);
var time_stamp = parseInt((new Date).getTime() / 1000, 10);
var port = url.port();
if (!port) {
port = url.protocol() == "https" ? "443" : "80";
}
var normalizedRequestString = ""
+ time_stamp + '\n'
+ nonce + '\n'
+ http_method + '\n'
+ url.path() + url.search() + url.hash() + '\n'
+ url.hostname() + '\n'
+ port + '\n'
+ '\n' ;
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, mac_key);
hmac.update(normalizedRequestString);
var hash = hmac.finalize();
var mac = hash.toString(CryptoJS.enc.Base64);
return 'MAC id="' + mac_key_id +
'", ts="' + time_stamp +
'", nonce="' + nonce +
'", mac="' + mac + '"';
}
Hmac.makeHawkAuthHeader = function(url, http_method, hawk_id, key, payload, app_id) {
Hmac.makeHawkAuthHeader = function(url, http_method, hawk_id, key, app_id) {
url = URI(url);
var nonce = Hmac.makeid(8);
@ -58,24 +25,22 @@ function(URI, CryptoJS) {
+ url.path() + url.search() + url.hash() + '\n' // request uri
+ url.hostname().toLowerCase() + '\n' // host
+ port + '\n' // port
+ Hmac.calculatePayloadHash(payload) + '\n' // hash
+ '\n' // Hmac.calculatePayloadHash(payload) + '\n' // hash // FIXME implement payload validation
+ '\n' // ext (we don't use it)
var app = "";
if(app_id) {
app = ', app="' + app_id + "'";
normalizedRequestString += app_id + "\n" + // app
'\n'; // dlg should be empty
}
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
hmac.update(normalizedRequestString);
var hash = hmac.finalize();
var mac = hash.toString(CryptoJS.enc.Base64);
return 'Hawk id="' + hawk_id +
'", mac="' + mac +
'", ts="' + time_stamp +
@ -84,6 +49,8 @@ function(URI, CryptoJS) {
}
Hmac.calculatePayloadHash = function (payload) {
if (!payload) return "";
var hash = CryptoJS.algo.SHA256.create();
hash.update('hawk.1.payload\n');
hash.update('application/vnd.tent.post.v0+json\n');