Browse Source

caching of pending post, tuning of timeline updating

master
Simon Grim 10 years ago
parent
commit
42f0e54861
  1. 4
      js/interface_home.js
  2. 2
      js/mobile_abstract.js
  3. 8
      js/twister_actions.js
  4. 107
      js/twister_timeline.js

4
js/interface_home.js

@ -17,7 +17,7 @@ var InterfaceFunctions = function() @@ -17,7 +17,7 @@ var InterfaceFunctions = function()
var newPosts = parseInt($(".userMenu .menu-news").text());
if (!newPosts)
newPosts = postsPerRefresh;
requestTimelineUpdate("latest",newPosts,followingUsers,promotedPostsOnly);
requestTimelineUpdate('pending',newPosts,followingUsers,promotedPostsOnly);
});
// Add refresh posts for home link in menu
@ -25,7 +25,7 @@ var InterfaceFunctions = function() @@ -25,7 +25,7 @@ var InterfaceFunctions = function()
var newPosts = parseInt($(".userMenu .menu-news").text());
if (!newPosts)
newPosts = postsPerRefresh;
requestTimelineUpdate("latest",newPosts,followingUsers,promotedPostsOnly);
requestTimelineUpdate('pending',newPosts,followingUsers,promotedPostsOnly);
});
$( ".promoted-posts-only").click(function() {

2
js/mobile_abstract.js

@ -105,7 +105,7 @@ var MAL = function() @@ -105,7 +105,7 @@ var MAL = function()
if ($.Options.getShowDesktopNotifPostsOpt() === 'enable') {
this.showDesktopNotif(false, polyglot.t('You got')+' '+polyglot.t('new_posts', newPosts)+' '+polyglot.t('in postboard')+'.', false,'twister_notification_new_posts', $.Options.getShowDesktopNotifPostsTimerOpt(), (function() {
requestTimelineUpdate('latest',this,followingUsers,promotedPostsOnly);
requestTimelineUpdate('pending',this,followingUsers,promotedPostsOnly);
}).bind(newPosts), false)
}
} else {

8
js/twister_actions.js

@ -226,6 +226,9 @@ function requestPostRecursively(containerToAppend,username,resource,count,useGet @@ -226,6 +226,9 @@ function requestPostRecursively(containerToAppend,username,resource,count,useGet
function newPostMsg(msg, $postOrig) {
if( lastPostId != undefined ) {
if ( typeof _sendedPostIDs !== 'undefined' )
_sendedPostIDs.push(lastPostId + 1);
var params = [defaultScreenName, lastPostId + 1, msg]
if( $postOrig.length ) {
params.push($postOrig.attr('data-screen-name'));
@ -235,7 +238,6 @@ function newPostMsg(msg, $postOrig) { @@ -235,7 +238,6 @@ function newPostMsg(msg, $postOrig) {
function(arg, ret) { incLastPostId(); }, null,
function(arg, ret) { var msg = ("message" in ret) ? ret.message : ret;
alert(polyglot.t("ajax_error", { error: msg })); }, null);
setTimeout('requestTimelineUpdate("latest",1,["'+defaultScreenName+'"],promotedPostsOnly)', 1000);
} else {
alert(polyglot.t("Internal error: lastPostId unknown (following yourself may fix!)"));
}
@ -250,13 +252,15 @@ function newRtMsg($postOrig) { @@ -250,13 +252,15 @@ function newRtMsg($postOrig) {
var rtObj = { sig_userpost :sig_userpost, userpost : userpost };
if( lastPostId != undefined ) {
if ( typeof _sendedPostIDs !== 'undefined' )
_sendedPostIDs.push(lastPostId + 1);
var params = [defaultScreenName, lastPostId+1, rtObj]
twisterRpc("newrtmsg", params,
function(arg, ret) { incLastPostId(); }, null,
function(arg, ret) { var msg = ("message" in ret) ? ret.message : ret;
alert(polyglot.t("ajax_error", { error: msg })); }, null);
setTimeout('requestTimelineUpdate("latest",1,["'+defaultScreenName+'"],promotedPostsOnly)', 1000);
} else {
alert(polyglot.t("Internal error: lastPostId unknown (following yourself may fix!)"));
}

107
js/twister_timeline.js

@ -12,7 +12,8 @@ var _idTrackerMap = {}; @@ -12,7 +12,8 @@ var _idTrackerMap = {};
var _idTrackerSpam = new idTrackerObj();
var _lastHaveMap = {};
var _refreshInProgress = false;
var _newPostsPending = 0;
var _newPostsPending = [];
var _sendedPostIDs = [];
var timelineLoaded = false;
/* object to keep tracking of post ids for a given user, that is, which
@ -79,7 +80,6 @@ function requestObj(users, mode, count, getspam) @@ -79,7 +80,6 @@ function requestObj(users, mode, count, getspam)
this.mode = mode; // 'latest', 'latestFirstTime' or 'older'
this.count = count;
this.getspam = getspam;
this.updateReportNewPosts = (users.toString() === defaultScreenName || mode === 'older') ? false : true;
// getRequest method returns the list parameter expected by getposts rpc
this.getRequest = function() {
@ -149,13 +149,41 @@ function requestGetposts(req) @@ -149,13 +149,41 @@ function requestGetposts(req)
function processReceivedPosts(req, posts)
{
//hiding posts can cause empty postboard, so we have to track the count...
var p2a = posts.length;
for( var i = 0; i < posts.length; i++ ) {
var post = posts[i];
if (willBeHidden(post)) {
p2a--;
continue;
if (willBeHidden(posts[i])) {
posts.splice(i, 1);
i--;
}
}
showPosts(req, posts);
req.doneReportProcessing(posts.length);
//if the count of recieved posts less or equals to requested then...
if (req.mode === 'done') {
timelineLoaded = true;
$.MAL.postboardLoaded();
_refreshInProgress = false;
} else {
//we will request more older post...
req.count -= posts.length;
if (req.count > 0) {
//console.log('we are requesting '+req.count+' more posts...');
requestGetposts(req);
} else {
timelineLoaded = true;
$.MAL.postboardLoaded();
_refreshInProgress = false;
}
}
}
function showPosts(req, posts)
{
var streamItemsParent = $.MAL.getStreamPostsParent();
for( var i = 0; i < posts.length; i++ ) {
if ( req.users.indexOf(posts[i]['userpost']['n']) > -1 ) {
var post = posts[i];
var streamPost = postToElem(post, "original", req.getspam);
var timePost = post["userpost"]["time"];
@ -166,7 +194,6 @@ function processReceivedPosts(req, posts) @@ -166,7 +194,6 @@ function processReceivedPosts(req, posts)
// insert the post in timeline ordered by (you guessed) time
// FIXME: lame! searching everything everytime. please optimize!
var streamItemsParent = $.MAL.getStreamPostsParent();
var streamItems = streamItemsParent.children();
if( streamItems.length == 0) {
// timeline is empty
@ -205,29 +232,6 @@ function processReceivedPosts(req, posts) @@ -205,29 +232,6 @@ function processReceivedPosts(req, posts)
}
req.reportProcessedPost(post["userpost"]["n"],post["userpost"]["k"], streamPostAppended);
}
req.doneReportProcessing(p2a);
if (req.updateReportNewPosts) {
_newPostsPending = 0; // FIXME maybe we need updating here instead this zeroing
$.MAL.reportNewPosts(_newPostsPending);
}
//if the count of recieved posts less then or equals to requested...
if (req.mode === 'done') {
timelineLoaded = true;
$.MAL.postboardLoaded();
_refreshInProgress = false;
} else {
//we will request more older post...
req.count -= p2a;
if (req.count > 0) {
//console.log('we are requesting '+req.count+' more posts...');
requestGetposts(req);
} else {
timelineLoaded = true;
$.MAL.postboardLoaded();
_refreshInProgress = false;
}
}
}
@ -241,7 +245,16 @@ function requestTimelineUpdate(mode, count, timelineUsers, getspam) @@ -241,7 +245,16 @@ function requestTimelineUpdate(mode, count, timelineUsers, getspam)
_refreshInProgress = true;
if( timelineUsers.length ) {
var req = new requestObj(timelineUsers, mode, count, getspam);
if (mode === 'pending') {
req.mode = 'latest';
showPosts(req, _newPostsPending);
_newPostsPending = [];
$.MAL.reportNewPosts(_newPostsPending.length);
$.MAL.postboardLoaded();
_refreshInProgress = false;
} else {
requestGetposts(req);
}
} else {
console.log("requestTimelineUpdate: not following any users");
}
@ -293,24 +306,35 @@ function processLastHave(userHaves) @@ -293,24 +306,35 @@ function processLastHave(userHaves)
// callback for getposts to update the number of new pending posts not shown in timeline
function processNewPostsConfirmation(expected, posts)
{
//we don't want to produce alert for the posts that won't be displayed
var pnp = 0;
//console.log('we got '+posts.length+' posts from expected '+expected+' for confirmation');
//console.log(posts);
// we want to report about new posts that would be displayed
var rnp = 0;
// we want to display sended posts immediately
var sendedPostsPending = [];
for( var i = posts.length-1; i >= 0; i-- ) {
if (willBeHidden(posts[i]) || posts[i]['userpost']['n'] === defaultScreenName) {
pnp++;
if ( !willBeHidden(posts[i]) ) {
if ( _sendedPostIDs.indexOf(posts[i]['userpost']['k']) > -1 ) {
sendedPostsPending.push(posts[i]);
} else {
_newPostsPending.push(posts[i]);
rnp++;
}
}
}
_newPostsPending += posts.length - pnp;
if( _newPostsPending ) {
$.MAL.reportNewPosts(_newPostsPending);
if ( rnp > 0 ) {
$.MAL.reportNewPosts(_newPostsPending.length);
}
if ( sendedPostsPending.length > 0 ) {
var req = new requestObj([defaultScreenName],'latest',sendedPostsPending.length,promotedPostsOnly);
showPosts(req, sendedPostsPending);
}
if( posts.length < expected ) {
// new DMs have probably been produced by users we follow.
// check with getdirectmsgs
requestDMsCount();
}
// TODO: possibly cache this response
}
function timelineChangedUser()
@ -319,7 +343,8 @@ function timelineChangedUser() @@ -319,7 +343,8 @@ function timelineChangedUser()
_idTrackerSpam = new idTrackerObj();
_lastHaveMap = {};
_refreshInProgress = false;
_newPostsPending = 0;
_newPostsPending = [];
_sendedPostIDs = [];
timelineLoaded = false;
}

Loading…
Cancel
Save