From 20c078fb386d93efc08795b6e43b12b38ba01d0b Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Thu, 25 Sep 2014 22:13:19 -0300 Subject: [PATCH] prefer using getposts for modal profile whenever possible. this makes profile much faster for users you follow. --- js/twister_actions.js | 56 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/js/twister_actions.js b/js/twister_actions.js index 0a963ec..8b5c081 100644 --- a/js/twister_actions.js +++ b/js/twister_actions.js @@ -136,27 +136,52 @@ function requestRTs(postLi) } } +function appendPostToContainer(postFromJson, containerToAppend) +{ + var newStreamPost = postToElem(postFromJson, "original"); + newStreamPost.hide(); + containerToAppend.append( newStreamPost ); + newStreamPost.slideDown("fast"); + $.MAL.postboardLoaded(); +} + var profilePostsLoading = false; -function requestPostRecursively(containerToAppend,username,resource,count) +function requestPostRecursively(containerToAppend,username,resource,count,useGetposts) { + var max_id = -1; if( !resource ) { var streamItems = containerToAppend.children(); if( streamItems.length != 0 ) { var lastItem = streamItems.eq(streamItems.length-1); resource = "post" + lastItem.find(".post-data").attr("data-lastk"); + max_id = parseInt(lastItem.find(".post-data").attr("data-id"))-1; } } profilePostsLoading = true; - dhtget( username, resource, "s", + + if( useGetposts ) { + req = {username: username} + if( max_id != -1 ) { + req.max_id = max_id; + } + + twisterRpc("getposts", [count,[req]], + function(args, posts) { + for( var i = 0; i < posts.length; i++ ) { + appendPostToContainer(posts[i],args.containerToAppend); + } + profilePostsLoading = false; + }, {containerToAppend:containerToAppend}, + function(args, ret) { + profilePostsLoading = false; + }, {}); + } else { + dhtget( username, resource, "s", function(args, postFromJson) { if( postFromJson ) { - var newStreamPost = postToElem(postFromJson, "original"); - newStreamPost.hide(); - args.containerToAppend.append( newStreamPost ); - newStreamPost.slideDown("fast"); - $.MAL.postboardLoaded(); + appendPostToContainer(postFromJson,args.containerToAppend); if( args.count > 1 ) { var userpost = postFromJson["userpost"]; @@ -174,6 +199,7 @@ function requestPostRecursively(containerToAppend,username,resource,count) profilePostsLoading = false; } }, {containerToAppend:containerToAppend, count:count} ); + } } @@ -238,13 +264,25 @@ function updateProfileData(profileModalContent, username) { profileModalContent.find(".following-count").parent().attr("href", $.MAL.followingUrl(username)); var postsView = profileModalContent.find(".postboard-posts"); - requestPostRecursively(postsView,username,"status",10); + + // try using getposts first. fallback to dht. + twisterRpc("getposts", [1,[{username: username}]], + function(args, posts) { + updateProfilePosts(postsView, username, posts.length); + }, {}, + function(args, ret) { + updateProfilePosts(postsView, username, false); + }, {}); +} + +function updateProfilePosts(postsView, username, useGetposts) { + requestPostRecursively(postsView,username,"status",postsPerRefresh, useGetposts); postsView.scroll(function(){ if (!profilePostsLoading) { var $this = $(this); if ($this.scrollTop() >= this.scrollHeight - $this.height() - 20) { - requestPostRecursively($this,username,"",10); + requestPostRecursively($this,username,"",postsPerRefresh, useGetposts); } } });