Added conversation view
This commit is contained in:
parent
6c05512285
commit
fa2bda0394
13 changed files with 474 additions and 48 deletions
|
@ -0,0 +1,126 @@
|
|||
define([
|
||||
"helper/HostApp",
|
||||
"helper/Core",
|
||||
"helper/Paths",
|
||||
"lib/URI"
|
||||
],
|
||||
|
||||
function(HostApp, Core, Paths, URI, Followings) {
|
||||
|
||||
|
||||
function Conversation() {
|
||||
|
||||
Core.call(this);
|
||||
|
||||
this.action = "conversation";
|
||||
|
||||
this.body = document.createElement("ol");
|
||||
this.body.className = this.action;
|
||||
document.body.appendChild(this.body);
|
||||
}
|
||||
|
||||
Conversation.prototype = Object.create(Core.prototype);
|
||||
|
||||
|
||||
Conversation.addStatus = function(status) {
|
||||
|
||||
this.body.appendChild(this.getStatusDOMElement(status));
|
||||
}
|
||||
|
||||
|
||||
Conversation.prototype.showStatus = function(id, entity) {
|
||||
|
||||
this.body.innerHTML = "";
|
||||
this.append(id, entity);
|
||||
}
|
||||
|
||||
Conversation.prototype.append = function(id, entity, node, add_after) {
|
||||
|
||||
var _this = this;
|
||||
|
||||
var callback = function(resp) {
|
||||
|
||||
var status = JSON.parse(resp.responseText);
|
||||
|
||||
var dom_element = _this.getStatusDOMElement(status);
|
||||
|
||||
if (node) {
|
||||
|
||||
node.parentNode.insertBefore(dom_element, node);
|
||||
|
||||
} else {
|
||||
dom_element.className = "highlight";
|
||||
_this.body.appendChild(dom_element);
|
||||
|
||||
_this.appendMentioned(id, entity, dom_element);
|
||||
}
|
||||
|
||||
|
||||
if (status.mentions && status.mentions.length > 0 && status.mentions[0].post) {
|
||||
_this.append(status.mentions[0].post, status.mentions[0].entity, dom_element);
|
||||
}
|
||||
}
|
||||
|
||||
function getRemoteStatus(profile) {
|
||||
var server = profile["https://tent.io/types/info/core/v0.1.0"].servers[0];
|
||||
Paths.getURL(URI(server + "/posts/" + id).toString(), "GET", callback, null, false);
|
||||
}
|
||||
|
||||
|
||||
if (entity == HostApp.stringForKey("entity")) {
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/posts/" + id));
|
||||
Paths.getURL(url.toString(), "GET", callback, null);
|
||||
|
||||
} else if(this.followings.followings[entity]) {
|
||||
|
||||
getRemoteStatus(this.followings.followings[entity].profile);
|
||||
|
||||
} else {
|
||||
|
||||
Paths.findProfileURL(entity, function(profile_url) {
|
||||
|
||||
if (profile_url) {
|
||||
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
|
||||
getRemoteStatus(JSON.parse(resp.responseText));
|
||||
|
||||
}, null, false); // do not send auth-headers
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Conversation.prototype.appendMentioned = function(id, entity, node) {
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/posts"));
|
||||
url.addSearch("mentioned_post", id);
|
||||
url.addSearch("post_types", "https%3A%2F%2Ftent.io%2Ftypes%2Fpost%2Fstatus%2Fv0.1.0");
|
||||
|
||||
var _this = this;
|
||||
var callback = function(resp) {
|
||||
|
||||
var statuses = JSON.parse(resp.responseText);
|
||||
|
||||
for (var i = 0; i < statuses.length; i++) {
|
||||
|
||||
var status = statuses[i];
|
||||
var dom_element = _this.getStatusDOMElement(status);
|
||||
_this.body.appendChild(dom_element);
|
||||
|
||||
_this.appendMentioned(status.id, status.entity, dom_element);
|
||||
}
|
||||
}
|
||||
|
||||
Paths.getURL(url.toString(), "GET", callback);
|
||||
|
||||
}
|
||||
|
||||
// /posts?limit=10&mentioned_post=gnqqyt&post_types=https%3A%2F%2Ftent.io%2Ftypes%2Fpost%2Fstatus%2Fv0.1.0,https%3A%2F%2Ftent.io%2Ftypes%2Fpost%2Frepost%2Fv0.1.0 HTTP/1.1" 200 - 0.0582
|
||||
|
||||
|
||||
|
||||
return Conversation;
|
||||
|
||||
});
|
|
@ -18,7 +18,13 @@ function(HostApp, Paths, Hmac) {
|
|||
],
|
||||
"scopes": {
|
||||
"read_posts": "Uses posts to show them in a list",
|
||||
"write_posts": "Posts on users behalf"
|
||||
"write_posts": "Posts on users behalf",
|
||||
"read_profile": "Displays your own profile",
|
||||
"write_profile": "Updating profile and mentions pointer",
|
||||
"read_followers": "Display a list of people who follow you",
|
||||
"write_followers": "Be able to block people who follow you",
|
||||
"read_followings": "Display following list and their older posts in conversations",
|
||||
"write_followings": "Follow ne entities"
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -74,7 +80,7 @@ function(HostApp, Paths, Hmac) {
|
|||
+ "&redirect_uri=" + escape(this.app_info["redirect_uris"][0])
|
||||
+ "&scope=" + Object.keys(this.app_info["scopes"]).join(",")
|
||||
+ "&state=" + this.state
|
||||
+ "&tent_post_types=" + escape("https://tent.io/types/posts/status/v0.1.0");
|
||||
+ "&tent_post_types=all";
|
||||
|
||||
HostApp.openURL(this.apiRoot() + auth);
|
||||
}
|
||||
|
|
|
@ -3,13 +3,16 @@ define([
|
|||
"helper/Paths",
|
||||
"lib/URI",
|
||||
"helper/HostApp",
|
||||
"helper/Followings",
|
||||
"lib/vendor/jquery.plugins"
|
||||
],
|
||||
|
||||
function(jQuery, Paths, URI, HostApp) {
|
||||
function(jQuery, Paths, URI, HostApp, Followings) {
|
||||
|
||||
function Core() {
|
||||
|
||||
this.followings = new Followings();
|
||||
|
||||
}
|
||||
|
||||
Core.prototype.getTemplate = function() {
|
||||
|
@ -121,7 +124,6 @@ function(jQuery, Paths, URI, HostApp) {
|
|||
|
||||
var template = this.getTemplate();
|
||||
|
||||
|
||||
template.reply_to.onclick = function() {
|
||||
|
||||
var mentions = [];
|
||||
|
@ -140,25 +142,39 @@ function(jQuery, Paths, URI, HostApp) {
|
|||
template.username.innerText = status.entity;
|
||||
template.username.href = status.entity; // FIXME open profile
|
||||
|
||||
Paths.findProfileURL(status.entity, function(profile_url) {
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
var profile = JSON.parse(resp.responseText);
|
||||
var basic = profile["https://tent.io/types/info/basic/v0.1.0"];
|
||||
var profile = function(profile) {
|
||||
|
||||
if (profile && 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;
|
||||
}
|
||||
}
|
||||
}, null, false); // do not send auth-headers
|
||||
var basic = profile["https://tent.io/types/info/basic/v0.1.0"];
|
||||
|
||||
if (profile && 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
if (this.followings.followings[status.entity]) {
|
||||
|
||||
profile(this.followings.followings[status.entity].profile);
|
||||
|
||||
} else {
|
||||
|
||||
Paths.findProfileURL(status.entity, function(profile_url) {
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
var p = JSON.parse(resp.responseText);
|
||||
profile(p)
|
||||
}, null, false); // do not send auth-headers
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
template.in_reply.parentNode.className = "hidden";
|
||||
|
||||
|
@ -174,6 +190,12 @@ function(jQuery, Paths, URI, HostApp) {
|
|||
time.className = "timeago";
|
||||
jQuery(time).timeago();
|
||||
template.ago.appendChild(time);
|
||||
|
||||
template.ago.href = "#"
|
||||
template.ago.onclick = function() {
|
||||
HostApp.showConversation(status.id, status.entity);
|
||||
return false;
|
||||
}
|
||||
|
||||
// {"type":"Point","coordinates":[57.10803113,12.25854746]}
|
||||
if (status.content && status.content.location && status.content.location.type == "Point") {
|
||||
|
@ -272,30 +294,44 @@ function(jQuery, Paths, URI, HostApp) {
|
|||
}
|
||||
}
|
||||
|
||||
var _this = this;
|
||||
for (var i = 0; i < mentions_in_text.length; i++) {
|
||||
var mention = mentions_in_text[i];
|
||||
|
||||
(function(mention) { // need this closure
|
||||
Paths.findProfileURL(mention.entity, function(profile_url) {
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
var profile = JSON.parse(resp.responseText);
|
||||
var basic = profile["https://tent.io/types/info/basic/v0.1.0"];
|
||||
|
||||
if (profile && basic) {
|
||||
if(basic.name) {
|
||||
var new_text = node.innerHTML.replace(
|
||||
mention.text,
|
||||
"<strong class='name' title='" + mention.entity + "'" + ">"
|
||||
+ basic.name
|
||||
+ "</strong>"
|
||||
);
|
||||
node.innerHTML = new_text;
|
||||
}
|
||||
}
|
||||
}, null, false); // do not send auth-headers
|
||||
var profile = function(profile) {
|
||||
var basic = profile["https://tent.io/types/info/basic/v0.1.0"];
|
||||
|
||||
if (profile && basic) {
|
||||
if(basic.name) {
|
||||
var new_text = node.innerHTML.replace(
|
||||
mention.text,
|
||||
"<strong class='name' title='" + mention.entity + "'" + ">"
|
||||
+ basic.name
|
||||
+ "</strong>"
|
||||
);
|
||||
node.innerHTML = new_text;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (_this.followings.followings[mention.entity]) {
|
||||
|
||||
profile(_this.followings.followings[mention.entity].profile)
|
||||
|
||||
} else {
|
||||
|
||||
Paths.findProfileURL(mention.entity, function(profile_url) {
|
||||
if (profile_url) {
|
||||
Paths.getURL(profile_url, "GET", function(resp) {
|
||||
var p = JSON.parse(resp.responseText);
|
||||
profile(p)
|
||||
}, null, false); // do not send auth-headers
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
})(mention);
|
||||
}
|
||||
}
|
||||
|
|
50
WebKit/scripts/helper/Followings.js
Normal file
50
WebKit/scripts/helper/Followings.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
define([
|
||||
"helper/Paths",
|
||||
"lib/URI"
|
||||
],
|
||||
|
||||
function(Paths, URI) {
|
||||
|
||||
function Followings() {
|
||||
|
||||
this.timeout = 2 * 60 * 1000;
|
||||
this.followings = {};
|
||||
this.before_id = null;
|
||||
|
||||
var _this = this;
|
||||
this.intervall = setInterval(function() { _this.getAllFollowings(); }, this.timeout);
|
||||
|
||||
this.getAllFollowings();
|
||||
}
|
||||
|
||||
Followings.prototype.getAllFollowings = function() {
|
||||
|
||||
var _this = this;
|
||||
|
||||
var callback = function(resp) {
|
||||
|
||||
var fs = JSON.parse(resp.responseText)
|
||||
|
||||
if (fs.length < 1) return;
|
||||
|
||||
for (var i = 0; i < fs.length; i++) {
|
||||
|
||||
var following = fs[i];
|
||||
_this.before_id = following.id;
|
||||
_this.followings[following.entity] = following;
|
||||
}
|
||||
|
||||
_this.getAllFollowings();
|
||||
}
|
||||
|
||||
var url = URI(Paths.mkApiRootPath("/followings"));
|
||||
if (this.before_id) {
|
||||
url.addSearch("before_id", this.before_id);
|
||||
}
|
||||
|
||||
Paths.getURL(url, "GET", callback);
|
||||
}
|
||||
|
||||
return Followings;
|
||||
|
||||
});
|
|
@ -61,6 +61,15 @@ define(function() {
|
|||
}
|
||||
}
|
||||
|
||||
HostApp.showConversation = function(id, entity) {
|
||||
|
||||
if (OS_TYPE == "mac") {
|
||||
controller.showConversationForPostId_andEntity_(id, entity);
|
||||
} else {
|
||||
controller.showConversationForPostIdandEntity(id, entity);
|
||||
}
|
||||
}
|
||||
|
||||
return HostApp;
|
||||
|
||||
});
|
|
@ -21,21 +21,28 @@ function(jQuery, HostApp, Hmac) {
|
|||
}
|
||||
|
||||
Paths.getURL = function(url, http_method, callback, data, auth_header) {
|
||||
|
||||
jQuery.ajax({
|
||||
|
||||
beforeSend: function(xhr) {
|
||||
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 && user_access_token) {
|
||||
|
||||
auth_header = Hmac.makeAuthHeader(
|
||||
url,
|
||||
http_method,
|
||||
HostApp.stringForKey("user_mac_key"),
|
||||
user_access_token
|
||||
)
|
||||
);
|
||||
xhr.setRequestHeader("Authorization", auth_header);
|
||||
}
|
||||
}
|
||||
|
@ -54,6 +61,7 @@ function(jQuery, HostApp, Hmac) {
|
|||
}
|
||||
|
||||
Paths.findProfileURL = function(entity, callback) {
|
||||
|
||||
jQuery.ajax({
|
||||
url: entity,
|
||||
type: "HEAD",
|
||||
|
|
|
@ -39,6 +39,12 @@ function start(view) {
|
|||
|
||||
} else if (view == "conversation") {
|
||||
|
||||
require(["controller/Conversation"], function(Conversation) {
|
||||
|
||||
tentia_instance = new Conversation();
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,10 +65,12 @@ function loadPlugin(url) {
|
|||
}
|
||||
|
||||
function debug(string) {
|
||||
|
||||
if (typeof string != "string") {
|
||||
string = JSON.stringify(string);
|
||||
}
|
||||
|
||||
alert("DEBUG: " + string);
|
||||
}
|
||||
|
||||
setTimeout(HostAppGo, 1000);
|
||||
setTimeout(HostAppGo, 2000);
|
||||
|
|
Reference in a new issue