Skip to content

Commit

Permalink
Mentions works
Browse files Browse the repository at this point in the history
  • Loading branch information
armandocat committed Oct 10, 2017
1 parent 08c541a commit 55f2777
Showing 1 changed file with 149 additions and 33 deletions.
182 changes: 149 additions & 33 deletions src/mentions_tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<div class="switch-field" style="margin-bottom: -4px; margin-left: 20px;">\
<input type="radio" id="mentions-type-posts" name="mentions-type" class="mentions-type" value="0" checked/>\
<label for="mentions-type-posts">Posts</label>\
<input type="radio" id="mentions-type-comments" name="mentions-type"- class="mentions-type" value="1" />\
<input type="radio" id="mentions-type-comments" name="mentions-type" class="mentions-type" value="1" />\
<label for="mentions-type-comments">Comments</label>\
<input type="radio" id="mentions-type-both" name="mentions-type" class="mentions-type" value="2" />\
<label for="mentions-type-both">Both</label>\
Expand All @@ -68,46 +68,162 @@
</div>');

mentionsTab.find('.MentionsTabLoadMore button').on('click', function(){
var loadMore = $(this).parent();
loadMore.hide();
mentionsTab.find('.MentionsTabLoading').show();
var from = parseInt(loadMore.data('from'), 10);
getPosts(mentionsTab, window.SteemMoreInfo.Utils.getPageAccountName(), from);
getPostsAndComments(mentionsTab, window.SteemMoreInfo.Utils.getPageAccountName());
});
mentionsTab.find('.mentions-type').on('change', function() {
getPostsAndComments(mentionsTab, window.SteemMoreInfo.Utils.getPageAccountName(), true);
});

getPosts(mentionsTab, window.SteemMoreInfo.Utils.getPageAccountName());
getPostsAndComments(mentionsTab, window.SteemMoreInfo.Utils.getPageAccountName());
};



var getPosts = function(mentionsTab, name, fromOrNull) {
fromOrNull = fromOrNull || 0;
window.SMI_AJAX({
// url: 'https://webapi.steemdata.com/Comments?where={"$text":{"$search":"\\"@' + name + '\\""}}&sort=-created&page=' + (fromOrNull+1),
url: 'https://webapi.steemdata.com/Posts?where={"$text":{"$search":"\\"@' + name + '\\""}}&sort=-created&page=' + (fromOrNull+1),
type: 'GET',
error: function(err){
console.log(err);
//TODO: error
},
success: function(data) {
var posts = data._items;
var postsList = mentionsTab.find('.PostsList__summaries');
if(postsList.length){
posts.forEach(function(post) {
var el = window.SteemMoreInfo.Utils.createPostSummary(post);
postsList.append(el);
var _getPostsAndComments = function(whats, name, info, cb) {
info = info || {
buffer: {},
from: {},
index: {},
posts: [],
hasMore: true
};
info.postsFrom = info.posts.length;

var merge = function() {
var keys = Object.keys(info.buffer);
var checkIndexes = function() {
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if((info.index[key] || 0) < info.buffer[key].length){
return true;
}
}
return false;
};
var checkFrom = function() {
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if(info.from[key] !== -1){
return true;
}
}
return false;
};
var added = 0;
while(added < 50 && checkIndexes()) {
var max = null;
var maxKey;
keys.forEach(function(key) {
var index = (info.index[key] || 0);
var post = info.buffer[key][index] || null;
if(post){
if(!max){
max = post;
maxKey = key;
}else{
var d1 = moment(max.created);
var d2 = moment(post.created);
if(d1 < d2){
max = post;
maxKey = key;
}
}
}
});
info.index[maxKey] = (info.index[maxKey] || 0) + 1;
info.posts.push(max);
added++;
}
info.hasMore = (checkIndexes() || checkFrom());
};

var done = 0;
var successCb = function(what, data){
done++;
if(data){
var buffer = info.buffer[what] || [];
buffer = buffer.concat(data._items);
info.buffer[what] = buffer;

if(data._links.next){
info.from[what] = (info.from[what] || 0) + 1;
}else{
info.from[what] = -1;
}
}
if(done === whats.length){
merge();
cb(info);
}
};

whats.forEach(function(what) {
var from = info.from[what] || 0;
if(from === -1){
successCb(what, null);
}else{
var index = info.index[what] || 0;
var buffer = info.buffer[what] || [];

if(buffer.length >= index + 50){
successCb(what, null);
}else{
window.SMI_AJAX({
url: 'https://webapi.steemdata.com/' + what + '?where={"$text":{"$search":"\\"@' + name + '\\""}}&sort=-created&page=' + (from+1),
type: 'GET',
error: function(err){
console.log(err);
//TODO: error
},
success: function(data) {
successCb(what, data);
}
});
}
}
});
};

mentionsTab.find('.MentionsTabLoading').hide();
mentionsTab.find('.MentionsTab').show();
if(data._links.next){
var from = fromOrNull + 1;
var loadMore = mentionsTab.find('.MentionsTabLoadMore');
loadMore.data('from', from);
loadMore.show();
}

var getPostsAndComments = function(mentionsTab, name, reset) {
var v = mentionsTab.find('.mentions-type:checked').val();
var whats;
if(v == 0){
whats = ['Posts'];
} else if(v == 1) {
whats = ['Comments'];
} else {
whats = ['Posts', 'Comments'];
}

var loadMore = mentionsTab.find('.MentionsTabLoadMore');
var info1;
if(reset){
loadMore.data('posts-download-info', null);
mentionsTab.find('.PostsList__summaries').html('');
}else{
info1 = loadMore.data('posts-download-info');
}
loadMore.hide();
mentionsTab.find('.MentionsTabLoading').show();

_getPostsAndComments(whats, name, info1, function(info2){
loadMore.data('posts-download-info', info2);
var posts = info2.posts;
var postsFrom = info2.postsFrom;
var hasMore = info2.hasMore;

var postsList = mentionsTab.find('.PostsList__summaries');
if(postsList.length){
for (var i = postsFrom; i < posts.length; i++) {
var post = posts[i]
var el = window.SteemMoreInfo.Utils.createPostSummary(post);
postsList.append(el);
}

mentionsTab.find('.MentionsTabLoading').hide();
mentionsTab.find('.MentionsTab').show();
if(hasMore){
loadMore.show();
}
}
});
Expand Down

0 comments on commit 55f2777

Please sign in to comment.