From 16d7016e29a77562316d0f484e13a49f475680d0 Mon Sep 17 00:00:00 2001 From: jeena Date: Wed, 17 Jul 2013 00:44:47 +0200 Subject: [PATCH] fixing problems with new post --- Qt/Bungloo.py | 9 +- Qt/Windows.py | 15 +- WebKit/scripts/controller/NewPost.js | 133 ++- WebKit/scripts/controller/Oauth.js | 3 +- WebKit/scripts/controller/Profile.js | 1235 +++++++++++++------------ WebKit/scripts/controller/Timeline.js | 16 +- WebKit/scripts/helper/APICalls.js | 39 +- WebKit/scripts/helper/Core.js | 199 +--- WebKit/scripts/helper/HostApp.js | 7 +- WebKit/scripts/main.js | 2 +- 10 files changed, 851 insertions(+), 807 deletions(-) diff --git a/Qt/Bungloo.py b/Qt/Bungloo.py index e5eaf3e..a8ca0be 100755 --- a/Qt/Bungloo.py +++ b/Qt/Bungloo.py @@ -171,13 +171,12 @@ class Controller(QtCore.QObject): pass @QtCore.pyqtSlot(str) - def openNewMessageWidow(self, is_private=False, string=""): - string = str(string) - self.openNewMessageWindowInReplyTostatusIdwithStringIsPrivate(None, None, string, is_private) + def openNewMessageWidow(self): + self.openNewMessageWindowInReplyTostatus(None) @QtCore.pyqtSlot(str, str, str, bool) - def openNewMessageWindowInReplyTostatusIdwithStringIsPrivate(self, entity, status_id, string, is_private): - new_message_window = Windows.NewPost(self.app, string, "[]", is_private) + def openNewMessageWindowInReplyTostatus(self, status_string): + new_message_window = Windows.NewPost(self.app) new_message_window.show() new_message_window.setAttribute(QtCore.Qt.WA_DeleteOnClose) self.app.new_message_windows.append(new_message_window) diff --git a/Qt/Windows.py b/Qt/Windows.py index 7317f56..fb7d332 100644 --- a/Qt/Windows.py +++ b/Qt/Windows.py @@ -329,11 +329,12 @@ class FindEntity(QtGui.QDialog): class NewPost(Helper.RestorableWindow): - def __init__(self, app, string, mentions, is_private): + def __init__(self, app, string=None, mentions="[]", is_private=False, post_id=None): self.app = app self.string = string self.mentions = mentions self.is_private = is_private + self.post_id = post_id Helper.RestorableWindow.__init__(self, "newpost", self.app) self.activateWindow() @@ -414,7 +415,11 @@ class NewPost(Helper.RestorableWindow): if self.is_private: is_private = "true" - callback = "function() { bungloo.newpost.setString('%s'); bungloo.newpost.setIsPrivate(%s); bungloo.newpost.setMentions(%s);}" % (self.string, is_private, self.mentions) + post_id = "" + if self.post_id: + post_id = self.post_id + + callback = "function() { bungloo.newpost.setString('%s'); bungloo.newpost.setIsPrivate(%s); bungloo.newpost.setMentions(%s); bungloo.newPostAction.setPostId(%s); }" % (self.string, is_private, self.mentions, post_id) script = "function HostAppGo() { start('newpost', " + callback + "); }" self.webView.page().mainFrame().evaluateJavaScript(script) @@ -425,6 +430,11 @@ class NewPost(Helper.RestorableWindow): self.webView.page().mainFrame().evaluateJavaScript(script) def sendMessage(self): + script = "bungloo.newpost.send()" + self.webView.page().mainFrame().evaluateJavaScript(script) + self.close() + + """ count = len(self.textInput.toPlainText()) if count > 0 and count <= 256: message = Helper.PostModel() @@ -438,6 +448,7 @@ class NewPost(Helper.RestorableWindow): self.close() else: QtGui.qApp.beep() + """ def openFileDialog(self): fileNamePath = QtGui.QFileDialog.getOpenFileName(self, "Choose a image", "", "Images (*.png *.gif *.jpg *.jpeg)") diff --git a/WebKit/scripts/controller/NewPost.js b/WebKit/scripts/controller/NewPost.js index 113c58c..e37fcce 100644 --- a/WebKit/scripts/controller/NewPost.js +++ b/WebKit/scripts/controller/NewPost.js @@ -29,7 +29,7 @@ function() { this.counter = $("256"); var buttons = $( "

" + - "" + + //"" + "" + "" + "

"); @@ -50,6 +50,12 @@ function() { this.textarea.focus() } + NewPost.prototype.setStatus = function(status_string) { + this.status = JSON.parse(status_string); + debug(this.status) + // FIXME set string, private, mentions, etc. + }; + NewPost.prototype.setString = function(string) { this.textarea.val(string); } @@ -190,8 +196,133 @@ function() { NewPost.prototype.send = function() { debug("Send not implemented yet"); $("textarea").focus(); + var count = 256 - this.textarea.val().length + (this.mentions.length * 6); + if(count >= 0) { + this.sentNewMessage(); + } else { + debug("BEEP"); + } } + NewPost.prototype.sendNewMessage = function() { + + var content = this.textarea.val(); + + var url = URI(HostApp.serverUrl("new_post")); + + var type = in_reply_to_status_id.length == 0 ? "https://tent.io/types/status/v0#" : "https://tent.io/types/status/v0#reply"; + + var data = { + "type": type, + "published_at": parseInt(new Date().getTime(), 10), + "permissions": { + "public": !is_private + }, + "content": { + "text": content, + }, + }; + + if (location) { + //data["content"]["location"] = { "type": "Point", "coordinates": location } + } + + var mentions = this.parseMentions(content, in_reply_to_status_id, in_reply_to_entity); + + if (mentions.length > 0) { + data["mentions"] = mentions; + if (is_private) { + var entities = {}; + for (var i = 0; i < mentions.length; i++) { + var entity = mentions[i]["entity"] + entities[entity] = true; + }; + + data["permissions"]["entities"] = entities; + } + } + + // APICalls.http_call(url.toString(), http_method, callback, JSON.stringify(data)); + APICalls.post(url.toString(), JSON.stringify(data), { + content_type: data.type, + callback: callback + }); + } +/* + NewPost.prototype.sendNewMessageWithImage = function(content, in_reply_to_status_id, in_reply_to_entity, location, image_data_uri, is_private, callback) { + + var url = URI(APICalls.mkApiRootPath("/posts")); + + var data = { + "type": "https://tent.io/types/post/photo/v0.1.0", + "published_at": parseInt(new Date().getTime() / 1000, 10), + "permissions": { + "public": !is_private + }, + "content": { + "caption": content, + }, + }; + + if (location) { + data["content"]["location"] = { "type": "Point", "coordinates": location } + } + + var mentions = this.parseMentions(content, in_reply_to_status_id, in_reply_to_entity); + if (mentions.length > 0) { + data["mentions"] = mentions; + if (is_private) { + var entities = {}; + for (var i = 0; i < mentions.length; i++) { + var entity = mentions[i]["entity"] + entities[entity] = true; + }; + + data["permissions"]["entities"] = entities; + } + } + + var data_string = JSON.stringify(data); + + var boundary = "TentAttachment----------TentAttachment"; + var post = "--" + boundary + "\r\n"; + + post += 'Content-Disposition: form-data; name="post"; filename="post.json"\r\n'; + post += 'Content-Length: ' + data_string.length + '\r\n'; + post += 'Content-Type: application/vnd.tent.v0+json\r\n'; + post += 'Content-Transfer-Encoding: binary\r\n\r\n'; + post += data_string; + + post += "\r\n--" + boundary + "\r\n"; + + var blob_string = image_data_uri.split(',')[1]; + var mime_type = image_data_uri.split(',')[0].split(':')[1].split(';')[0]; + var ext = "png"; + if (mime_type == "image/jpeg") { + ext = "jpeg"; + } else if (mime_type == "image/gif") { + ext = "gif"; + } + + + post += 'Content-Disposition: form-data; name="photos[0]"; filename="photo.' + ext + '"\r\n'; + post += 'Content-Length: ' + blob_string.length + "\r\n"; + post += 'Content-Type: ' + mime_type + "\r\n"; + post += 'Content-Transfer-Encoding: base64\r\n\r\n'; + post += blob_string; + post += "\r\n--" + boundary + "--\r\n"; + + var newCallback = function(resp) { + if (resp.status == 403) { + var err = JSON.parse(resp.responseText); + HostApp.alertTitleWithMessage(resp.statusText, err.error); + } + callback(resp); + } + + APICalls.postMultipart(url.toString(), newCallback, post, boundary); + } +*/ return NewPost; }) \ No newline at end of file diff --git a/WebKit/scripts/controller/Oauth.js b/WebKit/scripts/controller/Oauth.js index f556117..69e1b41 100644 --- a/WebKit/scripts/controller/Oauth.js +++ b/WebKit/scripts/controller/Oauth.js @@ -96,7 +96,6 @@ function(HostApp, APICalls, Hmac) { those.entity = those.profile.content.entity; HostApp.setStringForKey(those.entity, "entity") HostApp.setServerUrls(those.profile.content.servers[0].urls); - APICalls.post(HostApp.serverUrl("new_post"), JSON.stringify(those.app_info), { content_type: "https://tent.io/types/app/v0#", no_auth: true, @@ -112,7 +111,7 @@ function(HostApp, APICalls, Hmac) { callback: function(resp) { var data = JSON.parse(resp.responseText); those.authRequest(data.post, app_id); - } + } }); }}); diff --git a/WebKit/scripts/controller/Profile.js b/WebKit/scripts/controller/Profile.js index d57b605..febc78c 100644 --- a/WebKit/scripts/controller/Profile.js +++ b/WebKit/scripts/controller/Profile.js @@ -1,664 +1,703 @@ define([ - "helper/HostApp", - "helper/Core", - "helper/APICalls", - "lib/URI" + "helper/HostApp", + "helper/Core", + "helper/APICalls", + "lib/URI" ], function(HostApp, Core, APICalls, URI) { - function Profile() { - - Core.call(this); - - this.action = "profile"; - - this.posts_limit = 25; - - this.container = document.createElement("div"); - this.container.className = this.action; - document.getElementById("content").appendChild(this.container); - - this.initProfileTemplate(); - this.hide(); + function Profile() { + + Core.call(this); + + this.action = "profile"; + + this.posts_limit = 25; + + this.container = document.createElement("div"); + this.container.className = this.action; + document.getElementById("content").appendChild(this.container); + + this.initProfileTemplate(); + this.hide(); - var _this = this; - setTimeout(function() { _this.showProfileForEntity() }, 500); // Load users profile on start - } + var _this = this; + setTimeout(function() { _this.showProfileForEntity() }, 500); // Load users profile on start + } - Profile.prototype = Object.create(Core.prototype); - - - Profile.prototype.show = function() { - Core.prototype.show.call(this, this.container); - } - - Profile.prototype.hide = function() { - Core.prototype.hide.call(this, this.container); - } - - Profile.prototype.logout = function() { - this.container = ""; - } - - Profile.prototype.showList = function(list) { - $(this.body).hide(); - $(this.followingsBody).hide(); - $(this.followersBody).hide(); - $(list).show(); - }; - - Profile.prototype.showProfileForEntity = function(entity) { - - if (!entity) { - entity = HostApp.stringForKey("entity"); - }; - - this.clear(); - this.entity = entity; - this.following = null; - this.following_id = null; - this.profile_template.entity.innerHTML = this.entity; - this.profile_template.entity.href = this.entity; - - this.getProfile(); - this.getFollowing(); - } - - Profile.prototype.initProfileTemplate = function() { - - var _this = this; - - var header = document.createElement("header"); - header.className = "profile"; - - this.container.appendChild(header); - - this.profile_template = { - avatar: document.createElement("img"), - name: document.createElement("h1"), - entity: document.createElement("a"), - bio: document.createElement("p"), - relationships: document.createElement("td"), - posts: document.createElement("a"), - following: document.createElement("a"), - followed: document.createElement("a"), - birthdate: document.createElement("td"), - location: document.createElement("td"), - gender: document.createElement("td"), - url: document.createElement("a"), - following_button: document.createElement("button"), - mention_button: document.createElement("button") - }; + Profile.prototype = Object.create(Core.prototype); + + + Profile.prototype.show = function() { + Core.prototype.show.call(this, this.container); + } + + Profile.prototype.hide = function() { + Core.prototype.hide.call(this, this.container); + } + + Profile.prototype.logout = function() { + this.container = ""; + } + + Profile.prototype.showList = function(list) { + $(this.body).hide(); + $(this.followingsBody).hide(); + $(this.followersBody).hide(); + $(list).show(); + }; + + Profile.prototype.showProfileForEntity = function(entity) { + + if (!entity) { + entity = HostApp.stringForKey("entity"); + }; + + this.clear(); + this.entity = entity; + this.following = null; + this.following_id = null; + this.profile_template.entity.innerHTML = this.entity; + this.profile_template.entity.href = this.entity; + + this.getProfile(); + this.getFollowing(); + } + + Profile.prototype.initProfileTemplate = function() { + + var _this = this; + + var header = document.createElement("header"); + header.className = "profile"; + + this.container.appendChild(header); + + this.profile_template = { + avatar: document.createElement("img"), + name: document.createElement("h1"), + entity: document.createElement("a"), + bio: document.createElement("p"), + relationships: document.createElement("td"), + posts: document.createElement("a"), + following: document.createElement("a"), + followed: document.createElement("a"), + birthdate: document.createElement("td"), + location: document.createElement("td"), + gender: document.createElement("td"), + url: document.createElement("a"), + following_button: document.createElement("button"), + mention_button: document.createElement("button") + }; - header.appendChild(this.profile_template.avatar); - this.profile_template.avatar.src = "img/default-avatar.png"; + header.appendChild(this.profile_template.avatar); + this.profile_template.avatar.src = "img/default-avatar.png"; - var div = document.createElement("div"); - header.appendChild(div); - - this.profile_template.following_button.onclick = function(e) { - _this.toggleFollow() - } - div.appendChild(this.profile_template.following_button); - - this.profile_template.mention_button.onclick = function() { - var e = _this.entity; - if (e.startsWith("https://")) { - e = e.substr(8, e.length); - } - HostApp.openNewMessageWidow(null, null, "^" + e + " ", false); - } - div.appendChild(this.profile_template.mention_button); - this.profile_template.mention_button.innerHTML = "Mention"; - - div.appendChild(this.profile_template.name); - - var p = document.createElement("p"); - p.appendChild(this.profile_template.entity); - div.appendChild(p); - - div.appendChild(this.profile_template.bio); - - var table = document.createElement("table"); - div.appendChild(table); - - function mkLi(name, template) { - var tr = document.createElement("tr"); - var th = document.createElement("th"); - tr.style.display = "none"; - th.innerText = name + ": "; - tr.appendChild(th); - tr.appendChild(template); - table.appendChild(tr); - } - - mkLi("Birth date", this.profile_template.birthdate); - mkLi("Location", this.profile_template.location); - mkLi("Gender", this.profile_template.gender); - - var td = document.createElement("td"); - td.appendChild(this.profile_template.url); - mkLi("Homepage", td); - - mkLi("Relationships", this.profile_template.relationships); - - td = document.createElement("td"); - td.appendChild(this.profile_template.posts); - this.profile_template.posts.href = "#"; - this.profile_template.posts.onclick = function() { _this.showPosts(); return false; }; - mkLi("Posts", td); - - td = document.createElement("td"); - td.appendChild(this.profile_template.following); - this.profile_template.following.href = "#"; - this.profile_template.following.onclick = function() { _this.showFollowings(); return false; }; - mkLi("Following", td); - - td = document.createElement("td"); - td.appendChild(this.profile_template.followed); - this.profile_template.followed.href = "#"; - this.profile_template.followed.onclick = function() { _this.showFollowers(); return false; }; - mkLi("Followed by", td); - - - this.body = document.createElement("ol"); - this.body.className = this.action; - this.container.appendChild(this.body); - - this.followingsBody = document.createElement("ol"); - this.followingsBody.className = this.action + " followings"; - this.container.appendChild(this.followingsBody); - - this.followersBody = document.createElement("ol"); - this.followersBody.className = this.action + " folloewds"; - this.container.appendChild(this.followersBody); - - } - - Profile.prototype.clear = function() { - - this.server = null; - this.before = {id: null, entity: null, loading: false}; - - - this.profile_template.avatar.src = "img/default-avatar.png"; - - this.relationships = { - following_you: false, - followed_by_you: false, - it_is_you: false - } - - this.profile_template.name.innerText = ""; - this.profile_template.entity.innerText = ""; - this.profile_template.bio.innerText = ""; - this.profile_template.relationships.innerText = ""; - this.profile_template.posts.innerText = ""; - this.profile_template.following.innerText = ""; - this.profile_template.followed.innerText = ""; - this.profile_template.birthdate.innerText = ""; - this.profile_template.location.innerText = ""; - this.profile_template.gender.innerText = ""; - this.profile_template.url.innerText = ""; - this.profile_template.url.href = ""; - - this.profile_template.posts.parentNode.parentNode.style.display = "none"; - this.profile_template.following.parentNode.parentNode.style.display = "none"; - this.profile_template.followed.parentNode.parentNode.style.display = "none"; - this.profile_template.birthdate.parentNode.style.display = "none"; - this.profile_template.location.parentNode.style.display = "none"; - this.profile_template.gender.parentNode.style.display = "none"; - this.profile_template.url.parentNode.parentNode.style.display = "none"; - - this.profile_template.following_button.style.display = ""; - this.setFollowingButton(false); - - this.body.innerHTML = ""; - this.followingsBody.innerHTML = ""; - this.followersBody.innerHTML = ""; - - this.showList(this.body); - }; - - Profile.prototype.getProfile = function() { - - var _this = this; - - if (HostApp.stringForKey("entity") == this.entity) { - this.relationships.it_is_you = true; - this.profile_template.following_button.style.display = "none"; - } - - var profile = this.cache.profiles.getItem(this.entity); - - if (profile && profile != "null") { - - this.showProfile(profile); - this.profile = profile; - - } else { - var url = HostApp.serverUrl("posts_feed") + "?types=" + encodeURIComponent("https://tent.io/types/meta/v0") + "&entities=" + encodeURIComponent(this.entity) - - APICalls.get(url, { - callback: function(resp) { - profile = JSON.parse(resp.responseText); - _this.showProfile(profile); - _this.profile = profile; - }}); - } - } - - Profile.prototype.getFollowing = function() { - if(this.entity != HostApp.stringForKey("entity")) { - var url = APICalls.mkApiRootPath("/followings") + "/" + encodeURIComponent(this.entity); - var _this = this; - APICalls.http_call(url, "GET", function(resp) { - if (resp.status >= 200 && resp.status < 400) { - var following = JSON.parse(resp.responseText); - _this.following_id = following.id - _this.setFollowingButton(true); - } else { - _this.setFollowingButton(false); - _this.following_id = null; - } - }) - } else { - this.setFollowingButton(false); - this.following_id = null; - } - } - - Profile.prototype.showProfile = function(profile) { - - //debug(profile) - return - - var basic = profile["https://tent.io/types/info/basic/v0.1.0"]; - - if (profile && basic) { - - if(basic.avatar_url) { - this.profile_template.avatar.onerror = function() { this.profile_template.avatar.src = 'img/default-avatar.png' }; - this.profile_template.avatar.src = basic.avatar_url; - } - - this.populate(this.profile_template.name, basic.name); - this.populate(this.profile_template.birthdate, basic.birthdate); - this.populate(this.profile_template.location, basic.location); - this.populate(this.profile_template.gender, basic.gender); - this.populate(this.profile_template.bio, basic.bio); - - if(basic.website_url) { - - var url = basic.website_url; - this.profile_template.url.innerText = url; - this.profile_template.url.parentNode.parentNode.style.display = ""; - - if (!url.startsWith("http")) { - url = "http://" + url; - } - - this.profile_template.url.href = url; - } - } - - if (profile) { - this.server = profile["https://tent.io/types/info/core/v0.1.0"]["servers"][0]; - this.getMeta(this.server); - this.getStatuses(this.server); - } - } - - Profile.prototype.populate = function(t, v) { - if (v) { - t.innerText = v; - t.parentNode.style.display = ""; - t.parentNode.parentNode.style.display = ""; - } - } + var div = document.createElement("div"); + header.appendChild(div); + + this.profile_template.following_button.onclick = function(e) { + _this.toggleFollow() + } + div.appendChild(this.profile_template.following_button); + + this.profile_template.mention_button.onclick = function() { + var e = _this.entity; + if (e.startsWith("https://")) { + e = e.substr(8, e.length); + } + HostApp.openNewMessageWidow(null, null, "^" + e + " ", false); + } + div.appendChild(this.profile_template.mention_button); + this.profile_template.mention_button.innerHTML = "Mention"; + + div.appendChild(this.profile_template.name); + + var p = document.createElement("p"); + p.appendChild(this.profile_template.entity); + div.appendChild(p); + + div.appendChild(this.profile_template.bio); + + var table = document.createElement("table"); + div.appendChild(table); + + function mkLi(name, template) { + var tr = document.createElement("tr"); + var th = document.createElement("th"); + tr.style.display = "none"; + th.innerText = name + ": "; + tr.appendChild(th); + tr.appendChild(template); + table.appendChild(tr); + } + + mkLi("Birth date", this.profile_template.birthdate); + mkLi("Location", this.profile_template.location); + mkLi("Gender", this.profile_template.gender); + + var td = document.createElement("td"); + td.appendChild(this.profile_template.url); + mkLi("Homepage", td); + + mkLi("Relationships", this.profile_template.relationships); + + td = document.createElement("td"); + td.appendChild(this.profile_template.posts); + this.profile_template.posts.href = "#"; + this.profile_template.posts.onclick = function() { _this.showPosts(); return false; }; + mkLi("Posts", td); + + td = document.createElement("td"); + td.appendChild(this.profile_template.following); + this.profile_template.following.href = "#"; + this.profile_template.following.onclick = function() { _this.showFollowings(); return false; }; + mkLi("Following", td); + + td = document.createElement("td"); + td.appendChild(this.profile_template.followed); + this.profile_template.followed.href = "#"; + this.profile_template.followed.onclick = function() { _this.showFollowers(); return false; }; + mkLi("Followed by", td); + + + this.body = document.createElement("ol"); + this.body.className = this.action; + this.container.appendChild(this.body); + + this.followingsBody = document.createElement("ol"); + this.followingsBody.className = this.action + " followings"; + this.container.appendChild(this.followingsBody); + + this.followersBody = document.createElement("ol"); + this.followersBody.className = this.action + " folloewds"; + this.container.appendChild(this.followersBody); + + } + + Profile.prototype.clear = function() { + + this.server = null; + this.before = {id: null, entity: null, loading: false}; + + + this.profile_template.avatar.src = "img/default-avatar.png"; + + this.relationships = { + following_you: false, + followed_by_you: false, + it_is_you: false + } + + this.profile_template.name.innerText = ""; + this.profile_template.entity.innerText = ""; + this.profile_template.bio.innerText = ""; + this.profile_template.relationships.innerText = ""; + this.profile_template.posts.innerText = ""; + this.profile_template.following.innerText = ""; + this.profile_template.followed.innerText = ""; + this.profile_template.birthdate.innerText = ""; + this.profile_template.location.innerText = ""; + this.profile_template.gender.innerText = ""; + this.profile_template.url.innerText = ""; + this.profile_template.url.href = ""; + + this.profile_template.posts.parentNode.parentNode.style.display = "none"; + this.profile_template.following.parentNode.parentNode.style.display = "none"; + this.profile_template.followed.parentNode.parentNode.style.display = "none"; + this.profile_template.birthdate.parentNode.style.display = "none"; + this.profile_template.location.parentNode.style.display = "none"; + this.profile_template.gender.parentNode.style.display = "none"; + this.profile_template.url.parentNode.parentNode.style.display = "none"; + + this.profile_template.following_button.style.display = ""; + this.setFollowingButton(false); + + this.body.innerHTML = ""; + this.followingsBody.innerHTML = ""; + this.followersBody.innerHTML = ""; + + this.showList(this.body); + }; + + Profile.prototype.getProfile = function() { + + var _this = this; + + if (HostApp.stringForKey("entity") == this.entity) { + this.relationships.it_is_you = true; + this.profile_template.following_button.style.display = "none"; + } + + var url = HostApp.serverUrl("posts_feed") + "?types=" + encodeURIComponent("https://tent.io/types/meta/v0") + "&entities=" + encodeURIComponent(this.entity) + //var url = HostApp.serverUrl("discover").replace(/{entity}/, encodeURIComponent(this.entity)); + APICalls.get(url, { + callback: function(resp) { + var profile = JSON.parse(resp.responseText); + _this.showProfile(profile); + _this.profile = profile; + }}); + } + + Profile.prototype.getFollowing = function() { + if(this.entity != HostApp.stringForKey("entity")) { + + var url = HostApp.serverUrl("posts_feed") + "?mentions=" + encodeURIComponent(this.entity) + "&types=" + encodeURIComponent("https://tent.io/types/subscription/v0#https://tent.io/types/status/v0"); + var _this = this; + + APICalls.head(url, {callback: function(resp) { + + var count = APICalls.getCount(resp); + + if (count > 0) { + _this.setFollowingButton(true); + } else { + _this.setFollowingButton(false); + } - Profile.prototype.getMeta = function(root_url) { + }}); - var _this = this; - APICalls.http_call(URI(root_url + "/followings/count").toString(), "GET", function(resp) { + } else { - _this.populate(_this.profile_template.following, resp.responseText); - }, null, false); + this.setFollowingButton(false); + this.following_id = null; + } + } - APICalls.http_call(URI(root_url + "/followers/count").toString(), "GET", function(resp) { + Profile.prototype.showProfile = function(profiles) { - _this.populate(_this.profile_template.followed, resp.responseText); - }, null, false); + if(profiles.posts.length < 1) return; + var profile = profiles.posts[0]; + var basic = profile.content.profile; - if (this.entity != HostApp.stringForKey("entity")) { - APICalls.http_call(URI(root_url + "/followers/" + encodeURIComponent(HostApp.stringForKey("entity"))).toString(), "GET", function(resp) { - if (resp.status == 200) { - _this.relationships.following_you = true; - } - _this.setRelationships(); + if (profile && basic) { - }, null, false); + // Find and apply avatar + if(profile.attachments.length > 0) { - APICalls.http_call(URI(APICalls.mkApiRootPath("/followings/" + encodeURIComponent(this.entity))), "GET", function(resp) { - if (resp.status == 200) { - _this.relationships.followed_by_you = true; - } - _this.setRelationships(); - }); + var digest = null; + for (var i = 0; i < profile.attachments.length; i++) { + var attachment = profile.attachments[i]; + if(attachment.category == "avatar") { + digest = attachment.digest; + break; + } + } + + if(digest) { + var _this = this; + this.profile_template.avatar.onerror = function() { _this.profile_template.avatar.src = 'img/default-avatar.png' }; + var avatar_url = profile.content.servers[0].urls.attachment.replace(/\{entity\}/, encodeURIComponent(profile.entity)); + this.profile_template.avatar.src = avatar_url.replace(/\{digest\}/, digest); + } + } + + this.populate(this.profile_template.name, basic.name); + this.populate(this.profile_template.birthdate, basic.birthdate); + this.populate(this.profile_template.location, basic.location); + this.populate(this.profile_template.gender, basic.gender); + this.populate(this.profile_template.bio, basic.bio); + + if(basic.website) { + + var url = basic.website; + this.profile_template.url.innerText = url; + this.profile_template.url.parentNode.parentNode.style.display = ""; + + if (!url.startsWith("http")) { + url = "http://" + url; + } + + this.profile_template.url.href = url; + } + } - } else { - this.setRelationships(); - } + if (profile) { + this.profile = profile; + this.getMeta(this.profile); + //this.getStatuses(this.server); + } + } + + Profile.prototype.populate = function(t, v) { + if (v) { + t.innerText = v; + t.parentNode.style.display = ""; + t.parentNode.parentNode.style.display = ""; + } + } + + Profile.prototype.getMeta = function(profile) { - var url = URI(root_url + "/posts/count"); - 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/photo/v0.1.0" - ]; - url.addSearch("post_types", post_types.join(",")); + var _this = this; - APICalls.http_call(url.toString(), "GET", function(resp) { + var url = HostApp.serverUrl("posts_feed") + "?entities=" + encodeURIComponent(this.entity) + "&types=" + encodeURIComponent("https://tent.io/types/relationship/v0#follower"); + APICalls.head(url, { + callback: function(resp) { + debug(APICalls.getCount(resp)) - _this.populate(_this.profile_template.posts, resp.responseText); - }, null, false); - } + _this.populate(_this.profile_template.followed, APICalls.getCount(resp)+" "); + } + }); - Profile.prototype.setRelationships = function() { - var relation = "none"; - if (this.relationships.it_is_you) { - relation = "it's you"; - } else { - if (this.relationships.following_you && !this.relationships.followed_by_you) { - relation = "is following you"; - } else if (this.relationships.following_you && this.relationships.followed_by_you) { - relation = "you both follow each other"; - } else if (!this.relationships.following_you && this.relationships.followed_by_you) { - relation = "being followed by you"; - } - } - this.populate(this.profile_template.relationships, relation); - } + var url = HostApp.serverUrl("posts_feed") + "?entities=" + encodeURIComponent(this.entity) + "&types=" + encodeURIComponent("https://tent.io/types/relationship/v0#following"); + APICalls.head(url, { + callback: function(resp) { + _this.populate(_this.profile_template.following, APICalls.getCount(resp) + " "); + } + }); + return; - Profile.prototype.getStatuses = function(root_url, add_search, append) { - var _this = this; - add_search = add_search || {}; - var url = URI(root_url + "/posts"); - url.addSearch("limit", this.posts_limit); - 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/photo/v0.1.0" - ]; - url.addSearch("post_types", post_types.join(",")); - for(var key in add_search) { - url.addSearch(key, add_search[key]); - } - APICalls.http_call(url.toString(), "GET", function(resp) { + if (this.entity != HostApp.stringForKey("entity")) { + APICalls.http_call(URI(root_url + "/followers/" + encodeURIComponent(HostApp.stringForKey("entity"))).toString(), "GET", function(resp) { + if (resp.status == 200) { + _this.relationships.following_you = true; + } + _this.setRelationships(); - var statuses = JSON.parse(resp.responseText); + }, null, false); - _this.newStatus(statuses, append); + APICalls.http_call(URI(APICalls.mkApiRootPath("/followings/" + encodeURIComponent(this.entity))), "GET", function(resp) { + if (resp.status == 200) { + _this.relationships.followed_by_you = true; + } + _this.setRelationships(); + }); - }, null, false); - } + } else { + this.setRelationships(); + } - Profile.prototype.newStatus = function(statuses, append) { + var url = URI(root_url + "/posts/count"); + 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/photo/v0.1.0" + ]; + url.addSearch("post_types", post_types.join(",")); - if(statuses != null && statuses.length > 0) { + APICalls.http_call(url.toString(), "GET", function(resp) { - this.before.loading = false; + _this.populate(_this.profile_template.posts, resp.responseText); + }, null, false); + } - if (append) statuses = statuses.reverse(); + Profile.prototype.setRelationships = function() { + var relation = "none"; + if (HostApp.stringForKey("entity") == this.entity) { + relation = "it's you"; + } else { + if (this.relationships.following_you && !this.relationships.followed_by_you) { + relation = "is following you"; + } else if (this.relationships.following_you && this.relationships.followed_by_you) { + relation = "you both follow each other"; + } else if (!this.relationships.following_you && this.relationships.followed_by_you) { + relation = "being followed by you"; + } + } + this.populate(this.profile_template.relationships, relation); + } - for(var i = statuses.length-1, c=0; i>=c; --i) { - var status = statuses[i]; + Profile.prototype.getStatuses = function(root_url, add_search, append) { + var _this = this; - if (status.type == "https://tent.io/types/post/status/v0.1.0" || status.type == "https://tent.io/types/post/photo/v0.1.0") { + add_search = add_search || {}; - var new_node = this.getStatusDOMElement(status); + var url = URI(root_url + "/posts"); + url.addSearch("limit", this.posts_limit); - if(!append && this.body.childNodes.length > 0) { + 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/photo/v0.1.0" + ]; + url.addSearch("post_types", post_types.join(",")); - if(this.body.childNodes.length > this.max_length) { + for(var key in add_search) { + url.addSearch(key, add_search[key]); + } - this.body.removeChild(this.body.lastChild); - } + APICalls.http_call(url.toString(), "GET", function(resp) { - this.body.insertBefore(new_node, this.body.firstChild); + var statuses = JSON.parse(resp.responseText); - } else { + _this.newStatus(statuses, append); - this.body.appendChild(new_node); - } + }, null, false); + } - } else if (status.type == "https://tent.io/types/post/delete/v0.1.0") { - var li = document.getElementById("post-" + status.content.id + "-" + this.action); - if (li) { - this.body.removeChild(li); - } - } else if (status.type == "https://tent.io/types/post/repost/v0.1.0") { + Profile.prototype.newStatus = function(statuses, append) { - this.getRepost(status, this.body.firstChild); - } + if(statuses != null && statuses.length > 0) { - } - } - } + this.before.loading = false; - Profile.prototype.getMoreStatusPosts = function() { - if (!this.before.loading) { - this.before.loading = true; - var add_search = { - "before_id": this.body.lastChild.status.id, - "before_id_entity": this.body.lastChild.status.entity - } - this.getStatuses(this.server, add_search, true); - } - } + if (append) statuses = statuses.reverse(); - Profile.prototype.mention = function() { + for(var i = statuses.length-1, c=0; i>=c; --i) { - } + var status = statuses[i]; - Profile.prototype.setFollowingButton = function(following) { + if (status.type == "https://tent.io/types/post/status/v0.1.0" || status.type == "https://tent.io/types/post/photo/v0.1.0") { - this.following = following; + var new_node = this.getStatusDOMElement(status); - if (following) { - this.profile_template.following_button.className = "following"; - this.profile_template.following_button.innerText = "Unfollow"; - } else { - this.profile_template.following_button.className = ""; - this.profile_template.following_button.innerText = "Follow"; - } - } + if(!append && this.body.childNodes.length > 0) { - Profile.prototype.toggleFollow = function() { + if(this.body.childNodes.length > this.max_length) { - var _this = this; + this.body.removeChild(this.body.lastChild); + } - if (this.following_id) { + this.body.insertBefore(new_node, this.body.firstChild); - this.setFollowingButton(false); - var url = APICalls.mkApiRootPath("/followings/") + this.following_id; - APICalls.http_call(url, "DELETE", function(resp) { - if (resp.status >= 200 && resp.status < 300) { - _this.setFollowingButton(false); - _this.following_id = null; - } else { - _this.setFollowingButton(true); - } - }); + } else { - } else { + this.body.appendChild(new_node); + } - this.setFollowingButton(true); - var url = URI(APICalls.mkApiRootPath("/followings")); - var data = JSON.stringify({"entity": this.entity }); - - APICalls.http_call(url.toString(), "POST", function(resp) { - if (resp.status >= 200 && resp.status < 300) { - _this.following_id = JSON.parse(resp.responseText).id - _this.setFollowingButton(true); - } else { - _this.setFollowingButton(false); - } - }, data); - } - } - - Profile.prototype.showPosts = function() { - this.showList(this.body); - } - - Profile.prototype.showFollowings = function() { - - this.showList(this.followingsBody); - this.followingsBody.innerHTML = ""; - - var _this = this; - var callback = function(resp) { - var followings = JSON.parse(resp.responseText); - for (var i = 0; i < followings.length; i++) { - var li = _this.getDOMSmallProfile(followings[i]); - _this.followingsBody.appendChild(li); - } - } - - var url = URI(this.server + "/followings"); - url.addSearch("limit", 200); - APICalls.http_call(url.toString(), "GET", callback, null, false); - } - - Profile.prototype.showFollowers = function() { - - this.showList(this.followersBody); - this.followersBody.innerHTML = ""; - - var _this = this; - var callback = function(resp) { - var followers = JSON.parse(resp.responseText); - for (var i = 0; i < followers.length; i++) { - var li = _this.getDOMSmallProfile(followers[i]); - _this.followersBody.appendChild(li); - } - } - - var url = URI(this.server + "/followers"); - url.addSearch("limit", 200); - APICalls.http_call(url.toString(), "GET", callback, null, false); - } - - Profile.prototype.getDOMSmallProfile = function(profile) { - - var li = document.createElement("li"); - - var image = document.createElement("img"); - image.title = profile.entity; - image.className = "image"; - image.src = 'img/default-avatar.png'; - li.appendChild(image); - image.onclick = function(e) { - HostApp.showProfileForEntity(e.target.title); - return false; - } - - var div = document.createElement("div"); - div.className = "data" - - var h1 = document.createElement("h1"); - var username = document.createElement("a"); - username.title = profile.entity; - username.className = "name"; - username.href = profile.entity; - username.onclick = function(e) { - HostApp.showProfileForEntity(profile.entity); - return false; - } - - h1.appendChild(username) - div.appendChild(h1); - li.appendChild(div); - - var p = document.createElement("p"); - p.className = "message"; - - var entity_tag = document.createElement("a"); - entity_tag.innerText = profile.entity; - entity_tag.href = profile.entity; - entity_tag.title = profile.entity; - - var new_line = document.createElement("br"); - var follows_since = document.createTextNode("follows since "); - var follows_since_time = document.createElement("span"); - follows_since_time.innerText = this.ISODateString(new Date(profile.created_at * 1000)); - follows_since_time.title = follows_since_time.innerText; - follows_since_time.className = "timeago"; - jQuery(follows_since_time).timeago(); - - p.appendChild(entity_tag); - p.appendChild(new_line); - p.appendChild(follows_since); - p.appendChild(follows_since_time); - div.appendChild(p); - - var profile_callback = function(p) { - - var basic = p["https://tent.io/types/info/basic/v0.1.0"]; - - if (p && basic) { - if(basic.name) { - username.title = username.innerText; - username.innerText = basic.name; - } - if(basic.avatar_url) { - image.onerror = function() { image.src = 'img/default-avatar.png'; }; - image.src = basic.avatar_url; - } - } - - } - - var p = this.cache.profiles.getItem(profile.entity); - - if (p && p != "null") { - - profile_callback(p); - - } else { - - var _this = this; - APICalls.findProfileURL(profile.entity, function(profile_url) { - - if (profile_url) { - APICalls.http_call(profile_url, "GET", function(resp) { - var p = JSON.parse(resp.responseText); - if (p && p != "null") { - _this.cache.profiles.setItem(profile.entity, p); - profile_callback(p); - } - - }, null, false); // do not send auth-headers - } - }); - } - - return li; - } - - - return Profile; + } else if (status.type == "https://tent.io/types/post/delete/v0.1.0") { + + var li = document.getElementById("post-" + status.content.id + "-" + this.action); + if (li) { + this.body.removeChild(li); + } + } else if (status.type == "https://tent.io/types/post/repost/v0.1.0") { + + this.getRepost(status, this.body.firstChild); + } + + } + } + } + + Profile.prototype.getMoreStatusPosts = function() { + if (!this.before.loading) { + this.before.loading = true; + var add_search = { + "before_id": this.body.lastChild.status.id, + "before_id_entity": this.body.lastChild.status.entity + } + this.getStatuses(this.server, add_search, true); + } + } + + Profile.prototype.mention = function() { + + } + + Profile.prototype.setFollowingButton = function(following) { + + this.following = following; + + if (following) { + this.profile_template.following_button.className = "following"; + this.profile_template.following_button.innerText = "Unfollow"; + } else { + this.profile_template.following_button.className = ""; + this.profile_template.following_button.innerText = "Follow"; + } + } + + Profile.prototype.toggleFollow = function() { + + var _this = this; + + if (this.following_id) { + + this.setFollowingButton(false); + + /* + var url = APICalls.mkApiRootPath("/followings/") + this.following_id; + APICalls.http_call(url, "DELETE", function(resp) { + if (resp.status >= 200 && resp.status < 300) { + _this.setFollowingButton(false); + _this.following_id = null; + } else { + _this.setFollowingButton(true); + } + });*/ + + } else { + + this.setFollowingButton(true); + + var url = HostApp.serverUrl("new_post"); + + var data = JSON.stringify({ + type: "https://tent.io/types/subscription/v0#https://tent.io/types/status/v0", + mentions: [{ + entity: this.entity + }] + }); + + APICalls.post(url, data, { + content_type: "https://tent.io/types/subscription/v0", + callback: function(resp) { + debug(resp.status) + if (resp.status >= 200 && resp.status < 300) { + _this.setFollowingButton(true); + } else { + _this.setFollowingButton(false); + } + } + }); + } + } + + Profile.prototype.showPosts = function() { + this.showList(this.body); + } + + Profile.prototype.showFollowings = function() { + + this.showList(this.followingsBody); + this.followingsBody.innerHTML = ""; + + var _this = this; + var callback = function(resp) { + var followings = JSON.parse(resp.responseText); + for (var i = 0; i < followings.length; i++) { + var li = _this.getDOMSmallProfile(followings[i]); + _this.followingsBody.appendChild(li); + } + } + + var url = URI(this.server + "/followings"); + url.addSearch("limit", 200); + APICalls.http_call(url.toString(), "GET", callback, null, false); + } + + Profile.prototype.showFollowers = function() { + + this.showList(this.followersBody); + this.followersBody.innerHTML = ""; + + var _this = this; + var callback = function(resp) { + var followers = JSON.parse(resp.responseText); + for (var i = 0; i < followers.length; i++) { + var li = _this.getDOMSmallProfile(followers[i]); + _this.followersBody.appendChild(li); + } + } + + var url = URI(this.server + "/followers"); + url.addSearch("limit", 200); + APICalls.http_call(url.toString(), "GET", callback, null, false); + } + + Profile.prototype.getDOMSmallProfile = function(profile) { + + var li = document.createElement("li"); + + var image = document.createElement("img"); + image.title = profile.entity; + image.className = "image"; + image.src = 'img/default-avatar.png'; + li.appendChild(image); + image.onclick = function(e) { + HostApp.showProfileForEntity(e.target.title); + return false; + } + + var div = document.createElement("div"); + div.className = "data" + + var h1 = document.createElement("h1"); + var username = document.createElement("a"); + username.title = profile.entity; + username.className = "name"; + username.href = profile.entity; + username.onclick = function(e) { + HostApp.showProfileForEntity(profile.entity); + return false; + } + + h1.appendChild(username) + div.appendChild(h1); + li.appendChild(div); + + var p = document.createElement("p"); + p.className = "message"; + + var entity_tag = document.createElement("a"); + entity_tag.innerText = profile.entity; + entity_tag.href = profile.entity; + entity_tag.title = profile.entity; + + var new_line = document.createElement("br"); + var follows_since = document.createTextNode("follows since "); + var follows_since_time = document.createElement("span"); + follows_since_time.innerText = this.ISODateString(new Date(profile.created_at * 1000)); + follows_since_time.title = follows_since_time.innerText; + follows_since_time.className = "timeago"; + jQuery(follows_since_time).timeago(); + + p.appendChild(entity_tag); + p.appendChild(new_line); + p.appendChild(follows_since); + p.appendChild(follows_since_time); + div.appendChild(p); + + var profile_callback = function(p) { + + var basic = p["https://tent.io/types/info/basic/v0.1.0"]; + + if (p && basic) { + if(basic.name) { + username.title = username.innerText; + username.innerText = basic.name; + } + if(basic.avatar_url) { + image.onerror = function() { image.src = 'img/default-avatar.png'; }; + image.src = basic.avatar_url; + } + } + + } + + var p = this.cache.profiles.getItem(profile.entity); + + if (p && p != "null") { + + profile_callback(p); + + } else { + + var _this = this; + APICalls.findProfileURL(profile.entity, function(profile_url) { + + if (profile_url) { + APICalls.http_call(profile_url, "GET", function(resp) { + var p = JSON.parse(resp.responseText); + if (p && p != "null") { + _this.cache.profiles.setItem(profile.entity, p); + profile_callback(p); + } + + }, null, false); // do not send auth-headers + } + }); + } + + return li; + } + + + + + return Profile; }); diff --git a/WebKit/scripts/controller/Timeline.js b/WebKit/scripts/controller/Timeline.js index 19030eb..c01f418 100644 --- a/WebKit/scripts/controller/Timeline.js +++ b/WebKit/scripts/controller/Timeline.js @@ -46,9 +46,13 @@ function(Core, APICalls, HostApp, URI) { } - Timeline.prototype.newStatus = function(statuses, append) { + Timeline.prototype.newStatus = function(_statuses, append) { - statuses = statuses.data; + for (var entity in _statuses.profiles) { + bungloo.cache.profiles[entity] = _statuses.profiles[entity]; + } + + statuses = _statuses.posts; if(statuses != null && statuses.length > 0) { this.before.loading = false; @@ -63,7 +67,7 @@ function(Core, APICalls, HostApp, URI) { this.since_id_entity = status.entity; } - if (status.type == "https://tent.io/types/status/v0#" || status.type == "https://tent.io/types/post/photo/v0.1.0") { + if (status.type == "https://tent.io/types/status/v0#") { var new_node = this.getStatusDOMElement(status); @@ -110,9 +114,11 @@ function(Core, APICalls, HostApp, URI) { "https://tent.io/types/delete/v0#", //"https://tent.io/types/post/photo/v0.1.0" ]; - //url.addSearch("types", post_types.join(",")); + url.addSearch("types", post_types.join(",")); //url.addSearch("sort_by", "published_at"); url.addSearch("limit", this.posts_limit); + url.addSearch("max_refs", 20); + url.addSearch("profiles", "entity"); if(this.since_id && !append) { url.addSearch("since_id", this.since_id); @@ -144,7 +150,7 @@ 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.get(url.toString(), { callback: callback }); } } diff --git a/WebKit/scripts/helper/APICalls.js b/WebKit/scripts/helper/APICalls.js index 64341b1..60a971b 100644 --- a/WebKit/scripts/helper/APICalls.js +++ b/WebKit/scripts/helper/APICalls.js @@ -36,10 +36,10 @@ function(jQuery, HostApp, Hmac, Cache) { console.error("No content type for " + options.url); return; } else { - if(options.content_type != "application/json") { - content_type = "application/vnd.tent.post.v0+json; type=\"" + options.content_type + "\""; + if(options.content_type == "application/json") { + content_type = "application/json"; } else { - content_type = options.content_type; + content_type = "application/vnd.tent.post.v0+json; type=\"" + options.content_type + "\""; } } @@ -64,6 +64,7 @@ function(jQuery, HostApp, Hmac, Cache) { } else if(!options.no_auth) { console.error("No user_access_token yet - " + options.url); } + xhr.setRequestHeader("Cache-Control", "no-cache"); }, url: options.url, contentType: content_type, @@ -72,13 +73,26 @@ function(jQuery, HostApp, Hmac, Cache) { data: options.data, processData: false, error: function(xhr, ajaxOptions, thrownError) { - console.error("HTTP CALL (" + xhr.status + ")" + xhr.statusText + " " + options.http_method + " (" + options.url + "): '" + xhr.responseText + "'"); + console.error("HTTP CALL (" + xhr.status + ") " + xhr.statusText + " " + options.http_method + " URL(" + options.url + "): '" + xhr.responseText + "'"); } }; jQuery.ajax(settings); } + APICalls.head = function(url, options) { + var settings = { + url: url, + http_method: "HEAD", + }; + + for (var key in options) { + settings[key] = options[key]; + } + + APICalls.http_call(settings); + } + APICalls.get = function(url, options) { var settings = { url: url, @@ -225,6 +239,23 @@ function(jQuery, HostApp, Hmac, Cache) { return APICalls.parseHeaderForLink(header_string, regexp); } + APICalls.parseHeader = function(header_string) { + var header_strings = header_string.split(/\n/); + var headers = {}; + for (var i = 0; i < header_strings.length; i++) { + var hs = header_strings[i].split(/:(.+)?/); + headers[hs[0]] = hs[1]; + } + return headers; + } + + APICalls.getCount = function(resp) { + var count = 0; + var headers = APICalls.parseHeader(resp.getAllResponseHeaders()); + if(headers["Count"]) count = parseInt(headers["Count"], 10); + return count; + } + APICalls.parseHeaderForLink = function(header_string, match) { var headers = header_string.split(/\n/); var links = []; diff --git a/WebKit/scripts/helper/Core.js b/WebKit/scripts/helper/Core.js index 77ea169..b4bc318 100644 --- a/WebKit/scripts/helper/Core.js +++ b/WebKit/scripts/helper/Core.js @@ -69,6 +69,7 @@ function(jQuery, APICalls, URI, HostApp, Cache) { image.className = "image"; image.src = "img/default-avatar.png"; image.onmousedown = function(e) { e.preventDefault(); }; + image.onerror = function() { this.src = 'img/default-avatar.png' }; item.appendChild(image); var image_username = a.cloneNode(); @@ -224,7 +225,7 @@ function(jQuery, APICalls, URI, HostApp, Cache) { mentions.push(mention); } - _this.replyTo(status.entity, status.id, mentions, (status && status.permissions && !status.permissions.public)); + _this.replyTo(status); return false; } @@ -234,7 +235,7 @@ function(jQuery, APICalls, URI, HostApp, Cache) { return false; } - template.username.innerText = status.entity; + if(bungloo.cache.profiles[status.entity].name) template.username.innerText = bungloo.cache.profiles[status.entity].name; template.username.href = status.entity; template.username.title = status.entity; template.username.onclick = function() { @@ -242,47 +243,12 @@ function(jQuery, APICalls, URI, HostApp, Cache) { return false; } + if(bungloo.cache.profiles[status.entity].avatar_digest) { + template.image.src = HostApp.serverUrl("attachment").replace(/\{entity\}/, encodeURIComponent(status.entity)).replace(/\{digest\}/, bungloo.cache.profiles[status.entity].avatar_digest); + } + template.image.onclick = template.username.onclick; - var profile_callback = function(p) { - - var basic = p["https://tent.io/types/info/basic/v0.1.0"]; - - if (p && basic) { - if(basic.name) { - template.username.title = template.username.innerText; - template.username.innerText = basic.name; - } - if(basic.avatar_url) { - template.image.onerror = function() { template.image.src = 'img/default-avatar.png' }; - template.image.src = basic.avatar_url; - } - } - - } - - var p = this.cache.profiles.getItem(status.entity); - - if (p && p != "null") { - - profile_callback(p); - - } else { - - APICalls.findProfileURL(status.entity, function(profile_url) { - - if (profile_url) { - APICalls.http_call(profile_url, "GET", function(resp) { - var p = JSON.parse(resp.responseText); - if (p && p != "null") { - _this.cache.profiles.setItem(status.entity, p); - profile_callback(p); - } - - }, null, false); // do not send auth-headers - } - }); - } if (status && status.permissions && !status.permissions.public) { template.is_private.style.display = ''; @@ -389,9 +355,11 @@ function(jQuery, APICalls, URI, HostApp, Cache) { template.source.innerHTML = status.__repost.app.name; template.source.title = status.__repost.app.url; } else { - template.source.href = status.app.url; - template.source.innerHTML = status.app.name; - template.source.title = status.app.url; + if(status.app) { + template.source.href = status.app.url; + template.source.innerHTML = status.app.name; + template.source.title = status.app.url; + } } return template.item; @@ -498,60 +466,6 @@ function(jQuery, APICalls, URI, HostApp, Cache) { } } - Core.prototype.sendNewMessage = function(content, in_reply_to_status_id, in_reply_to_entity, location, image_data_uri, is_private, callback) { - - if (image_data_uri) { - - this.sendNewMessageWithImage(content, in_reply_to_status_id, in_reply_to_entity, location, image_data_uri, is_private, callback); - - } else { - - var url = URI(HostApp.serverUrl("new_post")); - - var type = in_reply_to_status_id.length == 0 ? "https://tent.io/types/status/v0#" : "https://tent.io/types/status/v0#reply"; - debug(typeof in_reply_to_status_id) - debug(in_reply_to_status_id.length) - debug(type) - - var data = { - "type": type, - "published_at": parseInt(new Date().getTime(), 10), - "permissions": { - "public": !is_private - }, - "content": { - "text": content, - }, - }; - - if (location) { - //data["content"]["location"] = { "type": "Point", "coordinates": location } - } - - var mentions = this.parseMentions(content, in_reply_to_status_id, in_reply_to_entity); - - if (mentions.length > 0) { - data["mentions"] = mentions; - if (is_private) { - var entities = {}; - for (var i = 0; i < mentions.length; i++) { - var entity = mentions[i]["entity"] - entities[entity] = true; - }; - - data["permissions"]["entities"] = entities; - } - } - - // APICalls.http_call(url.toString(), http_method, callback, JSON.stringify(data)); - APICalls.post(url.toString(), JSON.stringify(data), { - content_type: data.type, - callback: callback - }); - } - } - - Core.prototype.repost = function(id, entity, callback) { var url = URI(APICalls.mkApiRootPath("/posts")); @@ -582,80 +496,6 @@ function(jQuery, APICalls, URI, HostApp, Cache) { APICalls.http_call(url.toString(), "POST", new_callback, JSON.stringify(data)); } - Core.prototype.sendNewMessageWithImage = function(content, in_reply_to_status_id, in_reply_to_entity, location, image_data_uri, is_private, callback) { - - var url = URI(APICalls.mkApiRootPath("/posts")); - - var data = { - "type": "https://tent.io/types/post/photo/v0.1.0", - "published_at": parseInt(new Date().getTime() / 1000, 10), - "permissions": { - "public": !is_private - }, - "content": { - "caption": content, - }, - }; - - if (location) { - data["content"]["location"] = { "type": "Point", "coordinates": location } - } - - var mentions = this.parseMentions(content, in_reply_to_status_id, in_reply_to_entity); - if (mentions.length > 0) { - data["mentions"] = mentions; - if (is_private) { - var entities = {}; - for (var i = 0; i < mentions.length; i++) { - var entity = mentions[i]["entity"] - entities[entity] = true; - }; - - data["permissions"]["entities"] = entities; - } - } - - var data_string = JSON.stringify(data); - - var boundary = "TentAttachment----------TentAttachment"; - var post = "--" + boundary + "\r\n"; - - post += 'Content-Disposition: form-data; name="post"; filename="post.json"\r\n'; - post += 'Content-Length: ' + data_string.length + '\r\n'; - post += 'Content-Type: application/vnd.tent.v0+json\r\n'; - post += 'Content-Transfer-Encoding: binary\r\n\r\n'; - post += data_string; - - post += "\r\n--" + boundary + "\r\n"; - - var blob_string = image_data_uri.split(',')[1]; - var mime_type = image_data_uri.split(',')[0].split(':')[1].split(';')[0]; - var ext = "png"; - if (mime_type == "image/jpeg") { - ext = "jpeg"; - } else if (mime_type == "image/gif") { - ext = "gif"; - } - - - post += 'Content-Disposition: form-data; name="photos[0]"; filename="photo.' + ext + '"\r\n'; - post += 'Content-Length: ' + blob_string.length + "\r\n"; - post += 'Content-Type: ' + mime_type + "\r\n"; - post += 'Content-Transfer-Encoding: base64\r\n\r\n'; - post += blob_string; - post += "\r\n--" + boundary + "--\r\n"; - - var newCallback = function(resp) { - if (resp.status == 403) { - var err = JSON.parse(resp.responseText); - HostApp.alertTitleWithMessage(resp.statusText, err.error); - } - callback(resp); - } - - APICalls.postMultipart(url.toString(), newCallback, post, boundary); - } - Core.prototype.remove = function(id, callback, type) { type = type || "post"; if (confirm("Really delete this " + type + "?")) { @@ -920,19 +760,8 @@ function(jQuery, APICalls, URI, HostApp, Cache) { } } - Core.prototype.replyTo = function(entity, status_id, mentions, is_private) { - - var string = "^" + entity.replace("https://", "") + " "; - - var ms = ""; - for (var i = 0; i < mentions.length; i++) { - var e = mentions[i].entity.replace("https://", ""); - if(string.indexOf(e) == -1) ms += " ^" + e; - } - - if(ms.length > 0) string += "\n\n/cc" + ms; - - HostApp.openNewMessageWidow(entity, status_id, string, is_private); + Core.prototype.replyTo = function(status) { + HostApp.openNewMessageWidow(status); } Core.prototype.postDeleted = function(post_id, entity) { diff --git a/WebKit/scripts/helper/HostApp.js b/WebKit/scripts/helper/HostApp.js index ced766a..13992ad 100644 --- a/WebKit/scripts/helper/HostApp.js +++ b/WebKit/scripts/helper/HostApp.js @@ -85,13 +85,12 @@ define(function() { } } - HostApp.openNewMessageWidow = function(entity, status_id, string, is_private) { + HostApp.openNewMessageWidow = function(status) { if (OS_TYPE == "mac") { - controller.openNewMessageWindowInReplyTo_statusId_withString_isPrivate_(entity, status_id, string, is_private); + controller.openNewMessageWindowInReplyToStatus(JSON.stringify(status)); } else { - is_private = is_private == true - controller.openNewMessageWindowInReplyTostatusIdwithStringIsPrivate(entity, status_id, string, is_private); + controller.openNewMessageWindowInReplyTostatus(JSON.stringify(status)); } } diff --git a/WebKit/scripts/main.js b/WebKit/scripts/main.js index f823148..69511ef 100644 --- a/WebKit/scripts/main.js +++ b/WebKit/scripts/main.js @@ -7,7 +7,7 @@ var bungloo = { entityProfile: null, conversation: null, search: null, - cache: {}, + cache: { profiles: {}}, newpost: null };