');
content.append(exinport);
space.append(content);
}
// All the gui setup to add/remove blocked user
function setup_block_gui(){
// TODO: make this look nicer
var block_gui = $(
'
' + // TODO: use lables here instead....
'Block posts' +
'Block comments' +
'Block replies' +
''+
'
'
).insertBefore('.panel-header');
$('.panel-header').css('clear', 'right');
block_gui.find('span').css('color', 'gray').find('input').prop('disabled', true);
// Get user id via imgur api form username (TODO: find a way without the api. It need to be there somewhere on the page already)
var CLIENT_ID = 'cd0695f1226536b';
var username = window.location.pathname.split('/', 4) [2];
var endpoint = 'https://api.imgur.com/3/account/' + username;
$.ajax({
url: endpoint,
method: 'GET',
headers: {
Authorization: 'Client-ID ' + CLIENT_ID,
Accept: 'application/json'
},
success: function(response){
// TODO: handle failed requests
if(response.success){
// post, comment, reply, last seen username
var checkbox_post = block_gui.find('#block_posts');
var checkbox_comment = block_gui.find('#block_comments');
var checkbox_reply = block_gui.find('#block_replies');
if(blocked.hasOwnProperty(response.data.id)){
var user = blocked[response.data.id];
// update name (if necessarry)
if(user[3] != username){
user[3] = username;
localStorage.setItem("blocked_user", JSON.stringify(blocked));
}
checkbox_post.prop('checked', blocked[response.data.id][0] == 1);
checkbox_comment.prop('checked', blocked[response.data.id][1] == 1);
checkbox_reply.prop('checked', blocked[response.data.id][2] == 1);
}else{
checkbox_post.prop('checked', false);
checkbox_comment.prop('checked', false);
checkbox_reply.prop('checked', false);
}
block_gui.find('img').hide();
block_gui.find('span').css('color', 'white').find('input').prop('disabled', false);
function _set_flags(userid, index, state){
// post, commment, reply, last seen, username
blocked = $.parseJSON(localStorage.getItem('blocked_user')) || {};
var user = blocked[userid] || [0, 0, 0, username];
user[index] = state;
if(user[0] + user[1] + user[2] == 0){
delete blocked[userid];
} else {
blocked[userid] = user;
}
localStorage.setItem("blocked_user", JSON.stringify(blocked));
}
// Block posts ?
checkbox_post.change(function(){
_set_flags(response.data.id, 0, checkbox_post.prop('checked') ? 1 : 0);
});
// Block comments ?
checkbox_comment.change(function(){
_set_flags(response.data.id, 1, checkbox_comment.prop('checked') ? 1 : 0);
});
// Block replies ?
checkbox_reply.change(function(){
_set_flags(response.data.id, 2, checkbox_reply.prop('checked') ? 1 : 0);
});
}
},
error: undefined
});
}
/*
* Yadda yadda
*/
function handle_blocked_comments(blocked_cb) {
var insideNav = Imgur.InsideNav._instance;
captioninstance = insideNav._.captionInstance;
captions = captioninstance._.captions[insideNav._.hash];
var blocked_captions = captioninstance.getCaptionIdsByFilter(insideNav._.hash, function (caption) {
return blocked.hasOwnProperty(caption.author_id) && blocked[caption.author_id][1] == 1;
});
$.grep(blocked_captions, function (v) {
blocked_cb(captions.set[v]);
});
}
/*
* Traverse the comment replies and test if there are any non blocked comments
*/
function has_nonblocked_childs(caption) {
if (captions.children[caption.id] == null)
return false
for (var i = 0; i < captions.children[caption.id].length; ++i) {
var child = captions.set[captions.children[caption.id][i]];
if (!blocked.hasOwnProperty(child.author_id))
return true;
if (has_nonblocked_childs(child))
return true;
}
return false;
}
/*
* captioninstance.deleteCaption sometimes doesn't work (why ever), So lets just rewrite that
*/
function deleteCaption(caption_id) {
captions.children[captions.parent[caption_id]] = $.grep(captions.children[captions.parent[caption_id]], function (child_id) {
return child_id != caption_id;
});
delete captions.parent[caption_id];
delete captions.set[caption_id];
//delete captions.children[caption_id];
}
function hookit_pre(parent, funcname, callback){
var _ori = parent[funcname];
parent[funcname] = function(){
arguments = callback.apply(this, arguments);
return _ori.apply(this, arguments);
}
}
function hookit_post(parent, funcname, callback){
var _ori = parent[funcname];
parent[funcname] = function(){
var _ret = _ori.apply(this, arguments);
_ret = callback.apply(this, [arguments, _ret]);
return _ret;
};
}
// Are we on the settings page ?
if (window.location.pathname.indexOf('/account/settings') == 0) {
setup_settings_gui();
}
// Show hide and show buttons on user profile
else if (window.location.pathname.indexOf('/user/') == 0 && window.location.pathname.indexOf('/favorites/') == - 1) {
// Why would you block yourself ?
if(imgur._.auth.url != window.location.pathname.split('/', 4) [2]){
setup_block_gui();
}
}
// Filter posts and comments
else {
// TODO: navigate away when first load is blocked ?
// Filter posts:
/* TODO: try this for the initial load
hookit_pre(Imgur.InsideNav._instance._.sideGallery, '_loadPage', function(){
console.log('asd load', arguments);
return arguments;
});
*/
// This works sometimes for the initial load, but always for every load after that
hookit_pre(Imgur.InsideNav._instance._.sideGallery, '_ajaxSuccess', function(args){
arguments[0]['data'] = $.grep(arguments[0]['data'], function(v){
return !(blocked.hasOwnProperty(v.account_id) && blocked[v.account_id][0] == 1);
});
return arguments;
});
imgur._.emitter.callbacks['sidegalleryPageLoad'].push({f:function(){
// pretty dirty but the values are overwritten soemwehere after the event
// TODO: ...
window.setTimeout(function(){
Imgur.InsideNav._instance._.sideGallery.state.items = $.grep(Imgur.InsideNav._instance._.sideGallery.state.items, function(v){
//if((blocked.hasOwnProperty(v.account_id) && blocked[v.account_id][0] == 1))
// console.log('asd block', v);
return !(blocked.hasOwnProperty(v.account_id) && blocked[v.account_id][0] == 1);
});
// recreate the box (TODO: Read that code)
Imgur.InsideNav._instance._.sideGallery._onScroll();
}, 1000);
}, this_arg:{}});
// Filter comments
// I didn't found a fitting event to filter the comments, so lets just hook the process comments function
// TODO: try to run earlier or run once at site load for already loaded comments
var _pcaptions = Imgur.InsideNav._instance.processCaptions;
function process_captions_hook(ori, ori_this) {
handle_blocked_comments(function (comment) {
//if(blocked[comment.author_id][1] == 0)
//return;
//console.log('asd removed before render:', comment);
if (blocked[comment.author_id][2] == 1 || !has_nonblocked_childs(comment)) {
deleteCaption(comment.id);
} else {
comment.caption = 'Blocked';
}
});
ori.call(Imgur.InsideNav._instance);
}
Imgur.InsideNav._instance.processCaptions = process_captions_hook.bind(this, _pcaptions, Imgur.InsideNav._instance);
/*
* Remove all already rendered comments. Used for the initial load.
*/
function remove_rendered_comments() {
handle_blocked_comments(function (comment) {
if (blocked[comment.author_id][2] == 1 || !has_nonblocked_childs(comment)) {
captioninstance.deleteCaption(comment.id);
$('.caption').filter('[data-id="' + comment.id + '"]').hide();
} else {
comment.caption = 'Blocked';
$('.caption').filter('[data-id="' + comment.id + '"]').find('.usertext p span').text('Blocked');
}
});
}
remove_rendered_comments();
}