fixed mentions badget and cursor

This commit is contained in:
jeena 2013-09-09 03:33:43 +02:00
parent 8a1e17d41c
commit e8392eff7a
3 changed files with 161 additions and 105 deletions

View file

@ -1,146 +1,203 @@
define([
"helper/HostApp",
"controller/Timeline",
"lib/URI",
"helper/APICalls",
"helper/Core"
"helper/HostApp",
"controller/Timeline",
"lib/URI",
"helper/APICalls",
"helper/Core"
],
function(HostApp, Timeline, URI, APICalls, Core) {
function Mentions() {
function Mentions() {
this.is_not_init = false;
this.unread_mentions = 0;
this.is_not_init = false;
this.unread_mentions = 0;
Timeline.call(this);
Timeline.call(this);
this.action = "mentions";
this.container.className = this.action;
this.hide();
}
this.action = "mentions";
this.container.className = this.action;
this.hide();
}
Mentions.prototype = Object.create(Timeline.prototype);
Mentions.prototype = Object.create(Timeline.prototype);
Mentions.prototype.show = function() {
Core.prototype.show.call(this, this.container);
}
Mentions.prototype.show = function() {
Core.prototype.show.call(this, this.container);
}
Mentions.prototype.hide = function() {
Core.prototype.hide.call(this, this.container);
}
Mentions.prototype.hide = function() {
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) {
for (var i = 0; i < statuses.posts.length; i++) {
var status = statuses.posts[i];
var name = bungloo.cache.profiles[status.entity] ? bungloo.cache.profiles[status.entity].name : status.entity
if(this.is_not_init) {
for (var i = 0; i < statuses.posts.length; i++) {
var status = statuses.posts[i];
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"]) {
add_to_search["mentions"] = HostApp.stringForKey("entity");
}
if (!add_to_search["mentions"]) {
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) {
if (this.unread_mentions > 0) {
this.unread_mentions--;
HostApp.unreadMentions(this.unread_mentions);
}
}
Mentions.prototype.mentionRead = function(id, entity) {
if (this.unread_mentions > 0) {
this.unread_mentions--;
HostApp.unreadMentions(this.unread_mentions);
}
}
Mentions.prototype.setAllMentionsRead = function() {
this.unread_mentions = 0;
HostApp.unreadMentions(this.unread_mentions);
//this.updateLatestMentionRead();
}
Mentions.prototype.setAllMentionsRead = function() {
this.unread_mentions = 0;
HostApp.unreadMentions(this.unread_mentions);
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) {
if (status && status.type == "https://tent.io/types/post/status/v0.1.0") {
var status = this.body.childNodes[i].status;
var url = URI(APICalls.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0")));
var body = {
"mentions": {
"https://tent.io/types/post/status/v0.1.0": {
"post": status.id,
"entity": status.entity
}
}
}
if (!status.__repost) {
if (status && status.type.startsWith("https://tent.io/types/status/v0#")) {
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;
for (var i = 0; i < posts.length; i++) {
var post = posts[i];
APICalls.http_call(url.toString(), "PUT", callback, JSON.stringify(body));
}
if(post.type == "https://tent.io/types/cursor/v0#https://tent.io/rels/status-mentions") {
break;
}
}
}
mentions_post = post;
}
};
// Now prepare the cursor
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
}
]
};
// update version if the post exists
if(mentions_post) {
data.version = {
parents: [
{
version: mentions_post.version.id
}
]
}
}
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) {
}};
// either update or create the cursor
if(mentions_post) {
var url = HostApp.serverUrl("post")
.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;
}
}
}
Mentions.prototype.getLatestMentionRead = function() {
Mentions.prototype.getLatestMentionRead = function() {
var cursor_url = URI(APICalls.mkApiRootPath("/profile/" + encodeURIComponent("https://tent.io/types/info/cursor/v0.1.0")));
var cursor_url = HostApp.serverUrl("posts_feed") + "?types=" + "https://tent.io/types/cursor/v0";
APICalls.http_call(cursor_url.toString(), "GET", function(resp) {
APICalls.get(cursor_url, { callback: function(resp) {
var url = URI(APICalls.mkApiRootPath("/posts/count"));
var post_types = [
"https://tent.io/types/post/status/v0.1.0",
];
url.addSearch("post_types", post_types.join(","));
url.addSearch("mentioned_entity", HostApp.stringForKey("entity"));
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") {
try { // don't crash when there is no cursor yet
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 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 callback = function(resp) {
this.unread_mentions = parseInt(resp.responseText, 10);
HostApp.unreadMentions(this.unread_mentions);
}
APICalls.http_call(url.toString(), "GET", callback); // FIXME: error callback
});
}
*/
var uri = URI(HostApp.serverUrl("posts_feed"));
uri.addSearch("types", post_types.join(","));
uri.addSearch("since", since);
uri.addSearch("mentions", HostApp.stringForKey("entity"));
return Mentions;
APICalls.head(uri.toString(), { callback: function(resp) {
this.unread_mentions = APICalls.getCount(resp);
HostApp.unreadMentions(this.unread_mentions);
}});
}
};
}});
}
return Mentions;
});