fixed mentions badget and cursor
This commit is contained in:
parent
8a1e17d41c
commit
e8392eff7a
3 changed files with 161 additions and 105 deletions
|
@ -1,146 +1,203 @@
|
||||||
define([
|
define([
|
||||||
"helper/HostApp",
|
"helper/HostApp",
|
||||||
"controller/Timeline",
|
"controller/Timeline",
|
||||||
"lib/URI",
|
"lib/URI",
|
||||||
"helper/APICalls",
|
"helper/APICalls",
|
||||||
"helper/Core"
|
"helper/Core"
|
||||||
],
|
],
|
||||||
|
|
||||||
function(HostApp, Timeline, URI, APICalls, Core) {
|
function(HostApp, Timeline, URI, APICalls, Core) {
|
||||||
|
|
||||||
|
|
||||||
function Mentions() {
|
function Mentions() {
|
||||||
|
|
||||||
this.is_not_init = false;
|
this.is_not_init = false;
|
||||||
this.unread_mentions = 0;
|
this.unread_mentions = 0;
|
||||||
|
|
||||||
Timeline.call(this);
|
Timeline.call(this);
|
||||||
|
|
||||||
this.action = "mentions";
|
this.action = "mentions";
|
||||||
this.container.className = this.action;
|
this.container.className = this.action;
|
||||||
|
|
||||||
this.hide();
|
this.hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mentions.prototype = Object.create(Timeline.prototype);
|
Mentions.prototype = Object.create(Timeline.prototype);
|
||||||
|
|
||||||
Mentions.prototype.show = function() {
|
Mentions.prototype.show = function() {
|
||||||
Core.prototype.show.call(this, this.container);
|
Core.prototype.show.call(this, this.container);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mentions.prototype.hide = function() {
|
Mentions.prototype.hide = function() {
|
||||||
Core.prototype.hide.call(this, this.container);
|
Core.prototype.hide.call(this, this.container);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Mentions.prototype.newStatus = function(statuses, append) {
|
Mentions.prototype.newStatus = function(statuses, append) {
|
||||||
|
|
||||||
Timeline.prototype.newStatus.call(this, statuses, append);
|
Timeline.prototype.newStatus.call(this, statuses, append);
|
||||||
|
|
||||||
if(this.is_not_init) {
|
if(this.is_not_init) {
|
||||||
for (var i = 0; i < statuses.posts.length; i++) {
|
for (var i = 0; i < statuses.posts.length; i++) {
|
||||||
|
|
||||||
var status = statuses.posts[i];
|
var status = statuses.posts[i];
|
||||||
var name = bungloo.cache.profiles[status.entity] ? bungloo.cache.profiles[status.entity].name : status.entity
|
var name = bungloo.cache.profiles[status.entity] ? bungloo.cache.profiles[status.entity].name : status.entity
|
||||||
|
|
||||||
if(!append) HostApp.notificateUserAboutMention(status.content.text, name, status.id, status.entity);
|
if(!append) HostApp.notificateUserAboutMention(status.content.text, name, status.id, status.entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.is_not_init = true;
|
this.is_not_init = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mentions.prototype.getNewData = function(add_to_search, append, query) {
|
Mentions.prototype.getNewData = function(add_to_search, append, query) {
|
||||||
|
|
||||||
add_to_search = add_to_search || {};
|
add_to_search = add_to_search || {};
|
||||||
|
|
||||||
if (!add_to_search["mentions"]) {
|
if (!add_to_search["mentions"]) {
|
||||||
add_to_search["mentions"] = HostApp.stringForKey("entity");
|
add_to_search["mentions"] = HostApp.stringForKey("entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
Timeline.prototype.getNewData.call(this, add_to_search, append, query);
|
Timeline.prototype.getNewData.call(this, add_to_search, append, query);
|
||||||
|
|
||||||
//this.getLatestMentionRead();
|
this.getLatestMentionRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mentions.prototype.mentionRead = function(id, entity) {
|
Mentions.prototype.mentionRead = function(id, entity) {
|
||||||
if (this.unread_mentions > 0) {
|
if (this.unread_mentions > 0) {
|
||||||
this.unread_mentions--;
|
this.unread_mentions--;
|
||||||
HostApp.unreadMentions(this.unread_mentions);
|
HostApp.unreadMentions(this.unread_mentions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Mentions.prototype.setAllMentionsRead = function() {
|
Mentions.prototype.setAllMentionsRead = function() {
|
||||||
this.unread_mentions = 0;
|
this.unread_mentions = 0;
|
||||||
HostApp.unreadMentions(this.unread_mentions);
|
HostApp.unreadMentions(this.unread_mentions);
|
||||||
//this.updateLatestMentionRead();
|
this.updateLatestMentionRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: those two functions need to be rewritten
|
Mentions.prototype.updateLatestMentionRead = function() {
|
||||||
/*
|
|
||||||
Mentions.prototype.updateLatestMentionRead = function() {
|
|
||||||
|
|
||||||
for (var i = 0; i < this.body.childNodes.length; i++) {
|
var cursor_url = HostApp.serverUrl("posts_feed") + "?types=" + "https://tent.io/types/cursor/v0";
|
||||||
|
|
||||||
var status = this.body.childNodes[i].status;
|
// find the first real post
|
||||||
|
for (var i = 0; i < this.body.childNodes.length; i++) {
|
||||||
|
|
||||||
if (!status.__repost) {
|
var status = this.body.childNodes[i].status;
|
||||||
if (status && status.type == "https://tent.io/types/post/status/v0.1.0") {
|
|
||||||
|
|
||||||
var url = URI(APICalls.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0")));
|
if (!status.__repost) {
|
||||||
var body = {
|
if (status && status.type.startsWith("https://tent.io/types/status/v0#")) {
|
||||||
"mentions": {
|
|
||||||
"https://tent.io/types/post/status/v0.1.0": {
|
|
||||||
"post": status.id,
|
|
||||||
"entity": status.entity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var callback = function(resp) {
|
// First find out if there is such a cursor or if we need to create it first
|
||||||
|
APICalls.get(cursor_url, { callback: function(resp) {
|
||||||
|
|
||||||
}
|
var posts = JSON.parse(resp.responseText).posts;
|
||||||
|
var mentions_post = null;
|
||||||
|
|
||||||
APICalls.http_call(url.toString(), "PUT", callback, JSON.stringify(body));
|
for (var i = 0; i < posts.length; i++) {
|
||||||
}
|
var post = posts[i];
|
||||||
|
|
||||||
break;
|
if(post.type == "https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions") {
|
||||||
}
|
|
||||||
}
|
mentions_post = post;
|
||||||
}
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Now prepare the cursor
|
||||||
|
|
||||||
|
|
||||||
Mentions.prototype.getLatestMentionRead = function() {
|
var data = {
|
||||||
|
type: "https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions",
|
||||||
|
content: {},
|
||||||
|
permissions: {
|
||||||
|
public: false,
|
||||||
|
},
|
||||||
|
refs: [
|
||||||
|
{
|
||||||
|
"post": status.id,
|
||||||
|
"type": status.type,
|
||||||
|
"entity": status.entity
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
var cursor_url = URI(APICalls.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0")));
|
// update version if the post exists
|
||||||
|
if(mentions_post) {
|
||||||
|
data.version = {
|
||||||
|
parents: [
|
||||||
|
{
|
||||||
|
version: mentions_post.version.id
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
APICalls.http_call(cursor_url.toString(), "GET", function(resp) {
|
var options = {
|
||||||
|
content_type: data.type,
|
||||||
|
accept: 'application/vnd.tent.post.v0+json; type="https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions"',
|
||||||
|
callback: function(resp) {
|
||||||
|
|
||||||
var url = URI(APICalls.mkApiRootPath("/posts/count"));
|
}};
|
||||||
var post_types = [
|
|
||||||
"https://tent.io/types/post/status/v0.1.0",
|
// either update or create the cursor
|
||||||
];
|
if(mentions_post) {
|
||||||
url.addSearch("post_types", post_types.join(","));
|
var url = HostApp.serverUrl("post")
|
||||||
url.addSearch("mentioned_entity", HostApp.stringForKey("entity"));
|
.replace(/\{entity\}/, encodeURIComponent(HostApp.stringForKey("entity")))
|
||||||
|
.replace(/\{post\}/, mentions_post.id)
|
||||||
|
APICalls.put(url, JSON.stringify(data), options);
|
||||||
|
} else {
|
||||||
|
var url = HostApp.serverUrl("posts_feed");
|
||||||
|
APICalls.post(url, JSON.stringify(data), options);
|
||||||
|
}
|
||||||
|
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
try { // don't crash when there is no cursor yet
|
Mentions.prototype.getLatestMentionRead = function() {
|
||||||
var body = JSON.parse(resp.responseText);
|
|
||||||
var cursor = body["mentions"]["https://tent.io/types/post/status/v0.1.0"];
|
|
||||||
url.addSearch("since_id", cursor["post"]);
|
|
||||||
url.addSearch("since_id_entity", cursor["entity"]);
|
|
||||||
} catch(e) { }
|
|
||||||
|
|
||||||
var callback = function(resp) {
|
var cursor_url = HostApp.serverUrl("posts_feed") + "?types=" + "https://tent.io/types/cursor/v0";
|
||||||
this.unread_mentions = parseInt(resp.responseText, 10);
|
|
||||||
HostApp.unreadMentions(this.unread_mentions);
|
|
||||||
}
|
|
||||||
|
|
||||||
APICalls.http_call(url.toString(), "GET", callback); // FIXME: error callback
|
APICalls.get(cursor_url, { callback: function(resp) {
|
||||||
});
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
return Mentions;
|
var posts = JSON.parse(resp.responseText).posts;
|
||||||
|
|
||||||
|
for (var i = 0; i < posts.length; i++) {
|
||||||
|
var post = posts[i];
|
||||||
|
|
||||||
|
if(post.type == "https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions") {
|
||||||
|
|
||||||
|
var since = post.version.received_at;
|
||||||
|
var post_id = post.refs[0].post;
|
||||||
|
var post_types = [
|
||||||
|
"https://tent.io/types/status/v0#reply",
|
||||||
|
"https://tent.io/types/status/v0#"
|
||||||
|
];
|
||||||
|
|
||||||
|
var uri = URI(HostApp.serverUrl("posts_feed"));
|
||||||
|
uri.addSearch("types", post_types.join(","));
|
||||||
|
uri.addSearch("since", since);
|
||||||
|
uri.addSearch("mentions", HostApp.stringForKey("entity"));
|
||||||
|
|
||||||
|
APICalls.head(uri.toString(), { callback: function(resp) {
|
||||||
|
|
||||||
|
this.unread_mentions = APICalls.getCount(resp);
|
||||||
|
HostApp.unreadMentions(this.unread_mentions);
|
||||||
|
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Mentions;
|
||||||
|
|
||||||
});
|
});
|
|
@ -38,7 +38,7 @@ function(APICalls, HostApp) {
|
||||||
var buttons = $(
|
var buttons = $(
|
||||||
"<p>" +
|
"<p>" +
|
||||||
//"<button id='images'><img src='images/images.png'></button>" +
|
//"<button id='images'><img src='images/images.png'></button>" +
|
||||||
"<button id='private'><img src='img/public.png'></button>" +
|
"<button id='private'><img src='img/public.png'></button> " +
|
||||||
"<button id='send'><img src='img/send.png'></button>" +
|
"<button id='send'><img src='img/send.png'></button>" +
|
||||||
"</p>");
|
"</p>");
|
||||||
|
|
||||||
|
@ -267,9 +267,8 @@ function(APICalls, HostApp) {
|
||||||
|
|
||||||
var content = this.textarea.val();
|
var content = this.textarea.val();
|
||||||
|
|
||||||
var type = "https://tent.io/types/status/v0#";
|
|
||||||
var data = {
|
var data = {
|
||||||
type: type,
|
type: "https://tent.io/types/status/v0#",
|
||||||
content: {
|
content: {
|
||||||
text: content
|
text: content
|
||||||
},
|
},
|
||||||
|
|
|
@ -131,7 +131,7 @@ function(Core, APICalls, HostApp, URI) {
|
||||||
uri.addSearch("types", post_types.join(","));
|
uri.addSearch("types", post_types.join(","));
|
||||||
//uri.addSearch("sort_by", "published_at");
|
//uri.addSearch("sort_by", "published_at");
|
||||||
uri.addSearch("limit", this.posts_limit);
|
uri.addSearch("limit", this.posts_limit);
|
||||||
uri.addSearch("max_refs", 20);
|
//uri.addSearch("max_refs", 20);
|
||||||
uri.addSearch("profiles", "entity");
|
uri.addSearch("profiles", "entity");
|
||||||
|
|
||||||
if(this.since_time) {
|
if(this.since_time) {
|
||||||
|
|
Reference in a new issue