From 650f107d21a40bdbfdb99d5f519635bb33fa871f Mon Sep 17 00:00:00 2001 From: jeena Date: Mon, 27 May 2013 09:44:43 +0200 Subject: [PATCH] more work on posting and showing status posts --- WebKit/scripts/controller/Mentions.js | 1 + WebKit/scripts/controller/Oauth.js | 42 ++++++++----- WebKit/scripts/controller/Sidebar.js | 2 +- WebKit/scripts/controller/Timeline.js | 22 ++++--- WebKit/scripts/helper/APICalls.js | 91 +++++++++------------------ WebKit/scripts/helper/Core.js | 42 ++++++++++--- WebKit/scripts/helper/Hmac.js | 43 ++----------- 7 files changed, 108 insertions(+), 135 deletions(-) diff --git a/WebKit/scripts/controller/Mentions.js b/WebKit/scripts/controller/Mentions.js index 7d20d5b..68fc7ec 100644 --- a/WebKit/scripts/controller/Mentions.js +++ b/WebKit/scripts/controller/Mentions.js @@ -10,6 +10,7 @@ function(HostApp, Timeline, URI, APICalls, Core) { function Mentions() { + return // FIXME this.is_not_init = false; this.unread_mentions = 0; diff --git a/WebKit/scripts/controller/Oauth.js b/WebKit/scripts/controller/Oauth.js index a60a286..f020b5e 100644 --- a/WebKit/scripts/controller/Oauth.js +++ b/WebKit/scripts/controller/Oauth.js @@ -87,31 +87,40 @@ function(HostApp, APICalls, Hmac) { Oauth.prototype.register = function (url) { var those = this; - APICalls.get(url, { callback: function(resp) { + debug(url) + APICalls.get(url, { + no_auth: true, + callback: function(resp) { those.profile = JSON.parse(resp.responseText); those.entity = those.profile.content.entity; HostApp.setStringForKey(those.entity, "entity") HostApp.setServerUrls(those.profile.content.servers[0].urls); - var callback = function(resp) { + APICalls.post(HostApp.serverUrl("new_post"), JSON.stringify(those.app_info), { + content_type: "https://tent.io/types/app/v0#", + no_auth: true, + callback: function(resp) { + var app_id = JSON.parse(resp.responseText).id; var header_string = resp.getAllResponseHeaders(); var regexp = /https:\/\/tent.io\/rels\/credentials/i var url = APICalls.parseHeaderForLink(header_string, regexp); - APICalls.http_call(url, "GET", function(resp) { - var data = JSON.parse(resp.responseText); - those.authRequest(data, app_id); - }, null, false) - } - APICalls.post(HostApp.serverUrl("new_post"), JSON.stringify(those.app_info), {callback: callback}); + APICalls.get(url, { + content_type: "https://tent.io/types/app/v0#", + no_auth: true, + callback: function(resp) { + var data = JSON.parse(resp.responseText); + those.authRequest(data, app_id); + }}); + }}); }}); } Oauth.prototype.authRequest = function(credentials, app_id) { - + HostApp.setStringForKey(app_id, "app_id"); HostApp.setStringForKey(credentials.id, "app_hawk_id"); HostApp.setStringForKey(credentials.content.hawk_key, "app_hawk_key"); @@ -137,20 +146,20 @@ function(HostApp, APICalls, Hmac) { }); var those = this; - var http_method = "POST"; - var callback = function(resp) { - those.requestAccessTokenTicketFinished(resp.responseText); - }; - var auth_header = Hmac.makeHawkAuthHeader( url, - http_method, + "POST", HostApp.stringForKey("app_hawk_id"), HostApp.stringForKey("app_hawk_key"), requestBody ); - APICalls.http_call(url, http_method, callback, requestBody, auth_header); + APICalls.post(url, requestBody, { + content_type: "https://tent.io/types/app/v0#", + auth_header: auth_header, + callback: function(resp) { + those.requestAccessTokenTicketFinished(resp.responseText); + }}); } else { console.error("State is not the same: {" + this.state + "} vs {" + urlVars["state"] + "}") @@ -162,6 +171,7 @@ function(HostApp, APICalls, Hmac) { Oauth.prototype.requestAccessTokenTicketFinished = function(responseBody) { var access = JSON.parse(responseBody); + debug(access) HostApp.setStringForKey(access["access_token"], "user_access_token"); HostApp.setSecret(access["hawk_key"]); diff --git a/WebKit/scripts/controller/Sidebar.js b/WebKit/scripts/controller/Sidebar.js index 69b8831..e715f94 100644 --- a/WebKit/scripts/controller/Sidebar.js +++ b/WebKit/scripts/controller/Sidebar.js @@ -51,7 +51,7 @@ function(HostApp, APICalls, Cache) { document.body.className = "body-timeline"; document.body.id = "with-sidebar"; - this.setEntityAvatar(); + //this.setEntityAvatar(); FIXME this.setOnScroll(); } diff --git a/WebKit/scripts/controller/Timeline.js b/WebKit/scripts/controller/Timeline.js index 307a651..19030eb 100644 --- a/WebKit/scripts/controller/Timeline.js +++ b/WebKit/scripts/controller/Timeline.js @@ -30,7 +30,7 @@ function(Core, APICalls, HostApp, URI) { document.getElementById("content").appendChild(this.container); var _this = this; - this.reloadIntervall = setInterval(function() { _this.getNewData() }, this.timeout); + //this.reloadIntervall = setInterval(function() { _this.getNewData() }, this.timeout); //FIXME back this.getNewData(); } @@ -48,6 +48,7 @@ function(Core, APICalls, HostApp, URI) { Timeline.prototype.newStatus = function(statuses, append) { + statuses = statuses.data; if(statuses != null && statuses.length > 0) { this.before.loading = false; @@ -62,7 +63,7 @@ function(Core, APICalls, HostApp, URI) { this.since_id_entity = status.entity; } - if (status.type == "https://tent.io/types/post/status/v0.1.0" || status.type == "https://tent.io/types/post/photo/v0.1.0") { + if (status.type == "https://tent.io/types/status/v0#" || status.type == "https://tent.io/types/post/photo/v0.1.0") { var new_node = this.getStatusDOMElement(status); @@ -100,15 +101,16 @@ function(Core, APICalls, HostApp, URI) { add_to_search = add_to_search || {}; var those = this; - var url = URI(APICalls.mkApiRootPath("/posts")); + var url = URI(HostApp.serverUrl("posts_feed")); var post_types = [ - "https://tent.io/types/post/repost/v0.1.0", - "https://tent.io/types/post/status/v0.1.0", - "https://tent.io/types/post/delete/v0.1.0", - "https://tent.io/types/post/photo/v0.1.0" + "https://tent.io/types/status/v0#", + "https://tent.io/types/status/v0#reply", + "https://tent.io/types/repost/v0#", + "https://tent.io/types/delete/v0#", + //"https://tent.io/types/post/photo/v0.1.0" ]; - url.addSearch("post_types", post_types.join(",")); + //url.addSearch("types", post_types.join(",")); //url.addSearch("sort_by", "published_at"); url.addSearch("limit", this.posts_limit); @@ -127,7 +129,6 @@ function(Core, APICalls, HostApp, URI) { those.reload_blocked = false; try { - var json = JSON.parse(resp.responseText); those.newStatus(json, append); @@ -143,7 +144,8 @@ function(Core, APICalls, HostApp, URI) { if (!this.reload_blocked) { this.reload_blocked = true; - APICalls.http_call(url.toString(), http_method, callback, data); // FIXME: error callback + // APICalls.http_call(url.toString(), http_method, callback, data); // FIXME: error callback + APICalls.get(url.toString(), { callback: callback }); } } } diff --git a/WebKit/scripts/helper/APICalls.js b/WebKit/scripts/helper/APICalls.js index 3176a13..dd942de 100644 --- a/WebKit/scripts/helper/APICalls.js +++ b/WebKit/scripts/helper/APICalls.js @@ -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); } diff --git a/WebKit/scripts/helper/Core.js b/WebKit/scripts/helper/Core.js index 68e1e41..802ca7a 100644 --- a/WebKit/scripts/helper/Core.js +++ b/WebKit/scripts/helper/Core.js @@ -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 + }); } } diff --git a/WebKit/scripts/helper/Hmac.js b/WebKit/scripts/helper/Hmac.js index 0e2b1fc..be4db38 100644 --- a/WebKit/scripts/helper/Hmac.js +++ b/WebKit/scripts/helper/Hmac.js @@ -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');