mirror of
https://github.com/twisterarmy/twister-html.git
synced 2025-03-13 05:51:03 +00:00
add session caching of results of mentions' and hashtags' searchings
This commit is contained in:
parent
ff8b99f939
commit
f28d4f4153
@ -17,6 +17,7 @@ var twister = {
|
|||||||
root: $('<div>') // templates should be detached from DOM and attached here; use extractTemplate()
|
root: $('<div>') // templates should be detached from DOM and attached here; use extractTemplate()
|
||||||
},
|
},
|
||||||
modal: {},
|
modal: {},
|
||||||
|
res: {}, // reses for various reqs are cached here
|
||||||
var: {
|
var: {
|
||||||
localAccounts: [],
|
localAccounts: [],
|
||||||
updatesCheckClient: {}
|
updatesCheckClient: {}
|
||||||
@ -591,37 +592,20 @@ function openHashtagModalFromSearchHandler(hashtag) {
|
|||||||
title: '#' + hashtag
|
title: '#' + hashtag
|
||||||
});
|
});
|
||||||
|
|
||||||
setupQueryModalUpdating(modal.content.find('.postboard-posts'), hashtag, 'hashtag');
|
var req = queryStart(modal.content.find('.postboard-posts'), hashtag, 'hashtag');
|
||||||
|
modal.content.find('.postboard-news').on('click', {req: req}, handleClickDisplayPendingTwists);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupQueryModalUpdating(postboard, query, resource) {
|
function handleClickDisplayPendingTwists(event) {
|
||||||
var req = {
|
if (!event || !event.data || !event.data.req)
|
||||||
postboard: postboard,
|
|
||||||
query: query,
|
|
||||||
resource: resource,
|
|
||||||
id: query + '@' + resource
|
|
||||||
};
|
|
||||||
|
|
||||||
postboard.attr('data-request-id', req.id);
|
|
||||||
|
|
||||||
requestQuery(req);
|
|
||||||
|
|
||||||
// use extended timeout parameters on modal refresh (requires twister_core >= 0.9.14).
|
|
||||||
// our first query above should be faster (with default timeoutArgs of twisterd),
|
|
||||||
// then we may possibly collect more posts on our second try by waiting more.
|
|
||||||
req.timeoutArgs = [10000, 2000, 3];
|
|
||||||
|
|
||||||
req.interval = setInterval(updateQueryModal, 5000, req);
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateQueryModal(req) {
|
|
||||||
if (!isModalWithElemExists(req.postboard)) {
|
|
||||||
clearInterval(req.interval);
|
|
||||||
clearQueryProcessed(req.id);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
requestQuery(req);
|
$(event.target).hide();
|
||||||
|
|
||||||
|
queryPendingDraw(event.data.req);
|
||||||
|
|
||||||
|
if (typeof event.data.cbFunc === 'function')
|
||||||
|
event.data.cbFunc(event.data.cbReq);
|
||||||
}
|
}
|
||||||
|
|
||||||
function openFavsModal(event) {
|
function openFavsModal(event) {
|
||||||
@ -654,7 +638,8 @@ function openFavsModalHandler(peerAlias) {
|
|||||||
title: polyglot.t('users_favs', {username: peerAlias})
|
title: polyglot.t('users_favs', {username: peerAlias})
|
||||||
});
|
});
|
||||||
|
|
||||||
setupQueryModalUpdating(modal.content.find('.postboard-posts'), peerAlias, 'fav');
|
var req = queryStart(modal.content.find('.postboard-posts'), peerAlias, 'fav');
|
||||||
|
modal.content.find('.postboard-news').on('click', {req: req}, handleClickDisplayPendingTwists);
|
||||||
}
|
}
|
||||||
|
|
||||||
function openMentionsModal(event) {
|
function openMentionsModal(event) {
|
||||||
@ -686,18 +671,16 @@ function openMentionsModalHandler(peerAlias) {
|
|||||||
title: polyglot.t('users_mentions', {username: peerAlias})
|
title: polyglot.t('users_mentions', {username: peerAlias})
|
||||||
});
|
});
|
||||||
|
|
||||||
setupQueryModalUpdating(modal.content.find('.postboard-posts'), peerAlias, 'mention');
|
var req = queryStart(modal.content.find('.postboard-posts'), peerAlias, 'mention');
|
||||||
|
modal.content.find('.postboard-news')
|
||||||
|
.on('click',
|
||||||
|
{req: req, cbFunc: (peerAlias === defaultScreenName) ? resetMentionsCount : ''},
|
||||||
|
handleClickDisplayPendingTwists
|
||||||
|
)
|
||||||
|
;
|
||||||
|
|
||||||
if (peerAlias === defaultScreenName) {
|
if (peerAlias === defaultScreenName)
|
||||||
// obtain already cached mention posts from twister_newmsgs.js
|
|
||||||
processQuery({
|
|
||||||
postboard: modal.content.find('.postboard-posts'),
|
|
||||||
query: defaultScreenName,
|
|
||||||
resource: 'mention',
|
|
||||||
posts: getMentionsData()
|
|
||||||
});
|
|
||||||
resetMentionsCount();
|
resetMentionsCount();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function openFollowersModal(peerAlias) {
|
function openFollowersModal(peerAlias) {
|
||||||
@ -2752,15 +2735,6 @@ function initInterfaceCommon() {
|
|||||||
$('.userMenu-favs a').on('click', openFavsModal);
|
$('.userMenu-favs a').on('click', openFavsModal);
|
||||||
$('.favs-from-user').on('click', openFavsModal);
|
$('.favs-from-user').on('click', openFavsModal);
|
||||||
|
|
||||||
$('#hashtag-modal-template .postboard-news').on('click', function (event) {
|
|
||||||
var elem = $(event.target).hide().closest('.postboard').find('.postboard-posts');
|
|
||||||
|
|
||||||
displayQueryPending(elem);
|
|
||||||
|
|
||||||
if (elem.attr('data-request-id') === defaultScreenName + '@mention')
|
|
||||||
resetMentionsCount();
|
|
||||||
});
|
|
||||||
|
|
||||||
getElem('.latest-activity', true).on('mouseup',
|
getElem('.latest-activity', true).on('mouseup',
|
||||||
{feeder: '.latest-activity'}, handleClickOpenConversation);
|
{feeder: '.latest-activity'}, handleClickOpenConversation);
|
||||||
|
|
||||||
|
@ -528,27 +528,22 @@ function encode_utf8(s) {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmobileQueryReq = {}; // FIXME need to rework all that searching
|
var tmobileQueryReq;
|
||||||
|
|
||||||
function setupHashtagOrMention(postboard, tag, res) {
|
function setupHashtagOrMention(board, query, resource) {
|
||||||
$.MAL.setPostTemplate( $("#post-template-home") );
|
$.MAL.setPostTemplate( $("#post-template-home") );
|
||||||
$.mobile.showPageLoadingMsg();
|
$.mobile.showPageLoadingMsg();
|
||||||
|
board.empty();
|
||||||
|
|
||||||
var reqId = tag + '@' + res;
|
var req = queryStart(board, query, resource);
|
||||||
|
|
||||||
clearQueryProcessed(reqId);
|
twister.res[req].boardAutoAppend = true;
|
||||||
|
twister.res[req].skidoo = function (req) {
|
||||||
|
var curPage = $.mobile.activePage.attr('id');
|
||||||
|
return (curPage !== 'mentions' && curPage !== 'hashtag') || req !== tmobileQueryReq;
|
||||||
|
};
|
||||||
|
|
||||||
tmobileQueryReq.postboard = postboard.text('').attr('data-request-id', reqId);
|
tmobileQueryReq = req;
|
||||||
tmobileQueryReq.query = tag;
|
|
||||||
tmobileQueryReq.resource = res;
|
|
||||||
|
|
||||||
if (tag === defaultScreenName && res === 'mention') {
|
|
||||||
// obtain already cached mention posts from twister_newmsgs.js
|
|
||||||
tmobileQueryReq.posts = getMentionsData();
|
|
||||||
processQuery(tmobileQueryReq);
|
|
||||||
}
|
|
||||||
|
|
||||||
requestQuery(tmobileQueryReq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// every 2 seconds do something page specific.
|
// every 2 seconds do something page specific.
|
||||||
@ -570,12 +565,6 @@ function tmobileTick() {
|
|||||||
}
|
}
|
||||||
}, {} );
|
}, {} );
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (curPage === 'mentions' || curPage === 'hashtag') {
|
|
||||||
autoUpdateQuery = true;
|
|
||||||
requestQuery(tmobileQueryReq);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (curPage === 'dmchat')
|
if (curPage === 'dmchat')
|
||||||
requestDmConversation($('#dmchat .direct-messages-thread'), dmChatUser);
|
requestDmConversation($('#dmchat .direct-messages-thread'), dmChatUser);
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,6 @@
|
|||||||
var postsPerRefresh = 10;
|
var postsPerRefresh = 10;
|
||||||
var maxExpandPost = 8;
|
var maxExpandPost = 8;
|
||||||
var maxExpandPostTop = 4;
|
var maxExpandPostTop = 4;
|
||||||
var _queryProcessedMap = {};
|
|
||||||
var _queryPendingPosts = {};
|
|
||||||
var autoUpdateQuery = false;
|
|
||||||
|
|
||||||
// ----------------
|
// ----------------
|
||||||
|
|
||||||
@ -439,55 +436,120 @@ function updateProfilePosts(postsView, username, useGetposts) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearQueryProcessed(id) {
|
function queryStart(board, query, resource) {
|
||||||
if (!id) return;
|
var req = query + '@' + resource;
|
||||||
|
|
||||||
_queryProcessedMap[id] = {};
|
if (typeof twister.res[req] !== 'object')
|
||||||
_queryPendingPosts[id] = [];
|
twister.res[req] = {
|
||||||
}
|
board: board,
|
||||||
|
query: query,
|
||||||
function requestQuery(req) {
|
resource: resource,
|
||||||
req.postboard.closest('div').find('.postboard-loading').show();
|
twists: {
|
||||||
if (req.resource === 'fav'){
|
cached: {},
|
||||||
twisterRpc("getfavs", [req.query, 1000], function(req, posts){
|
pending: []
|
||||||
req.posts = posts;
|
}
|
||||||
processQuery(req)
|
};
|
||||||
}, req);
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
dhtget(req.query, req.resource, 'm',
|
twister.res[req].board = board;
|
||||||
function (req, posts) {
|
for (var i in twister.res[req].twists.cached)
|
||||||
req.posts = posts;
|
if (twister.res[req].twists.pending.indexOf(i) === -1)
|
||||||
processQuery(req);
|
twister.res[req].twists.pending.push(i);
|
||||||
},
|
|
||||||
req,
|
queryPendingDraw(req)
|
||||||
req.timeoutArgs
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
queryRequest(req);
|
||||||
|
|
||||||
|
// use extended timeout parameters on modal refresh (requires twister_core >= 0.9.14).
|
||||||
|
// our first query above should be faster (with default timeoutArgs of twisterd),
|
||||||
|
// then we may possibly collect more posts on our second try by waiting more.
|
||||||
|
twister.res[req].timeoutArgs = [10000, 2000, 3];
|
||||||
|
|
||||||
|
twister.res[req].interval = setInterval(queryTick, 5000, req);
|
||||||
|
|
||||||
|
return req;
|
||||||
}
|
}
|
||||||
|
|
||||||
function processQuery(req) {
|
function queryTick(req) {
|
||||||
if (!isModalWithElemExists(req.postboard) || !req.posts || !req.posts.length)
|
if (typeof twister.res[req].skidoo === 'function' ? twister.res[req].skidoo(req)
|
||||||
|
: !isModalWithElemExists(twister.res[req].board)) {
|
||||||
|
clearInterval(twister.res[req].interval);
|
||||||
|
queryClear(req);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
queryRequest(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
function queryClear(req) {
|
||||||
|
if (!req || !twister.res[req] || !twister.res[req].twists)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!req.id)
|
twister.res[req].twists.pending = [];
|
||||||
req.id = req.query + '@' + req.resource;
|
}
|
||||||
if (typeof _queryProcessedMap[req.id] !== 'object')
|
|
||||||
_queryProcessedMap[req.id] = {};
|
|
||||||
if (typeof _queryPendingPosts[req.id] !== 'object')
|
|
||||||
_queryPendingPosts[req.id] = [];
|
|
||||||
|
|
||||||
for (var i = req.posts.length - 1; i >= 0; i--) {
|
function queryRequest(req) {
|
||||||
var userpost = req.posts[i].userpost;
|
twister.res[req].board.closest('div').find('.postboard-loading').show();
|
||||||
|
|
||||||
|
if (twister.res[req].resource === 'fav')
|
||||||
|
twisterRpc('getfavs', [twister.res[req].query, 1000],
|
||||||
|
queryProcess, req);
|
||||||
|
else
|
||||||
|
dhtget(twister.res[req].query, twister.res[req].resource, 'm',
|
||||||
|
queryProcess, req, twister.res[req].timeoutArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
function queryProcess(req, twists) {
|
||||||
|
queryPendingPush(req, twists);
|
||||||
|
|
||||||
|
if (typeof twister.res[req].skidoo === 'function' ? twister.res[req].skidoo(req)
|
||||||
|
: !isModalWithElemExists(twister.res[req].board))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (twister.res[req].twists.pending.length) {
|
||||||
|
if (twister.res[req].board.children().length && !$.mobile
|
||||||
|
&& $.Options.showDesktopNotifPostsModal.val === 'enable'
|
||||||
|
&& (twister.res[req].resource !== 'mention' || twister.res[req].query !== defaultScreenName))
|
||||||
|
$.MAL.showDesktopNotification({
|
||||||
|
body: polyglot.t('You got') + ' ' + polyglot.t('new_posts', twister.res[req].twists.pending.length) + ' '
|
||||||
|
+ polyglot.t('in search result') + '.',
|
||||||
|
tag: 'twister_notification_new_posts_modal',
|
||||||
|
timeout: $.Options.showDesktopNotifPostsModalTimer.val,
|
||||||
|
funcClick: (function() {
|
||||||
|
focusModalWithElement(twister.res[this.req].board,
|
||||||
|
function (req) {
|
||||||
|
twister.res[req].board.closest('.postboard')
|
||||||
|
.find('.postboard-news').click();
|
||||||
|
},
|
||||||
|
this.req
|
||||||
|
);
|
||||||
|
}).bind({req: req})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!twister.res[req].board.children().length || twister.res[req].boardAutoAppend)
|
||||||
|
queryPendingDraw(req);
|
||||||
|
else {
|
||||||
|
twister.res[req].board.closest('div').find('.postboard-news') // FIXME we'd replace 'div' with '.postboard' but need to dig through tmobile first
|
||||||
|
.text(polyglot.t('new_posts', twister.res[req].twists.pending.length))
|
||||||
|
.fadeIn('slow')
|
||||||
|
;
|
||||||
|
twister.res[req].board.closest('div').find('.postboard-loading').hide();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function queryPendingPush(req, twists) {
|
||||||
|
if (!req || !twister.res[req] || !twists || !twists.length)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (var i = twists.length - 1; i >= 0; i--) {
|
||||||
|
var userpost = twists[i].userpost;
|
||||||
|
var j = userpost.n + '/' + userpost.time;
|
||||||
|
|
||||||
if (userpost.fav)
|
if (userpost.fav)
|
||||||
userpost = userpost.fav;
|
userpost = userpost.fav;
|
||||||
|
|
||||||
var key = userpost.n + ';' + userpost.time;
|
if (typeof twister.res[req].twists.cached[j] === 'undefined') {
|
||||||
|
|
||||||
if (!_queryProcessedMap[req.id][key]) {
|
|
||||||
_queryProcessedMap[req.id][key] = true;
|
|
||||||
|
|
||||||
if ((typeof userpost.msg !== 'string' || userpost.msg === '')
|
if ((typeof userpost.msg !== 'string' || userpost.msg === '')
|
||||||
&& (typeof userpost.rt !== 'object'
|
&& (typeof userpost.rt !== 'object'
|
||||||
|| typeof userpost.rt.msg !== 'string' || userpost.rt.msg === ''))
|
|| typeof userpost.rt.msg !== 'string' || userpost.rt.msg === ''))
|
||||||
@ -500,53 +562,27 @@ function processQuery(req) {
|
|||||||
langFilterData = filterLang(userpost.rt.msg);
|
langFilterData = filterLang(userpost.rt.msg);
|
||||||
|
|
||||||
if ($.Options.filterLangSimulate.val) {
|
if ($.Options.filterLangSimulate.val) {
|
||||||
req.posts[i].langFilter = langFilterData;
|
twists[i].langFilter = langFilterData;
|
||||||
} else {
|
} else {
|
||||||
if (!langFilterData.pass)
|
if (!langFilterData.pass)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_queryPendingPosts[req.id].push(req.posts[i]);
|
twister.res[req].twists.cached[j] = twists[i];
|
||||||
}
|
twister.res[req].twists.pending.push(j);
|
||||||
}
|
|
||||||
|
|
||||||
if (_queryPendingPosts[req.id].length) {
|
|
||||||
if (!$.hasOwnProperty('mobile') && $.Options.showDesktopNotifPostsModal.val === 'enable'
|
|
||||||
&& (req.resource !== 'mention' || req.query !== defaultScreenName)) {
|
|
||||||
$.MAL.showDesktopNotification({
|
|
||||||
body: polyglot.t('You got') + ' ' + polyglot.t('new_posts', _queryPendingPosts[req.id].length) + ' '
|
|
||||||
+ polyglot.t('in search result') + '.',
|
|
||||||
tag: 'twister_notification_new_posts_modal',
|
|
||||||
timeout: $.Options.showDesktopNotifPostsModalTimer.val,
|
|
||||||
funcClick: (function() {
|
|
||||||
focusModalWithElement(this.postboard,
|
|
||||||
function (req) {
|
|
||||||
req.postboard.closest('.postboard').find('.postboard-news').hide();
|
|
||||||
displayQueryPending(req.postboard);
|
|
||||||
}, {postboard: this.postboard}
|
|
||||||
);
|
|
||||||
}).bind({postboard: req.postboard})
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!req.postboard.children().length || autoUpdateQuery) {
|
|
||||||
displayQueryPending(req.postboard);
|
|
||||||
} else {
|
|
||||||
req.postboard.closest('div').find('.postboard-news') // FIXME we'd replace 'div' with '.postboard' but need to dig through tmobile first
|
|
||||||
.text(polyglot.t('new_posts', _queryPendingPosts[req.id].length))
|
|
||||||
.fadeIn('slow')
|
|
||||||
;
|
|
||||||
req.postboard.closest('div').find('.postboard-loading').hide();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayQueryPending(postboard) {
|
function queryPendingDraw(req) {
|
||||||
var reqId = postboard.attr('data-request-id');
|
var twists = [];
|
||||||
|
for (var i = 0; i < twister.res[req].twists.pending.length; i++)
|
||||||
|
twists.push(twister.res[req].twists.cached[twister.res[req].twists.pending[i]]);
|
||||||
|
|
||||||
attachPostsToStream(postboard, _queryPendingPosts[reqId], false);
|
attachPostsToStream(twister.res[req].board, twists, false);
|
||||||
_queryPendingPosts[reqId] = [];
|
|
||||||
|
queryClear(req);
|
||||||
|
|
||||||
$.MAL.postboardLoaded();
|
$.MAL.postboardLoaded();
|
||||||
}
|
}
|
||||||
|
@ -30,10 +30,12 @@ function processMention(user, mentionTime, data) {
|
|||||||
_lastMentionTime = Math.max(mentionTime, _lastMentionTime);
|
_lastMentionTime = Math.max(mentionTime, _lastMentionTime);
|
||||||
data.isNew = true;
|
data.isNew = true;
|
||||||
|
|
||||||
var reqId = defaultScreenName + '@mention';
|
var req = defaultScreenName + '@mention';
|
||||||
if (typeof _queryPendingPosts[reqId] !== 'object')
|
var j = data.userpost.n + '/' + data.userpost.time;
|
||||||
_queryPendingPosts[reqId] = [];
|
if (typeof twister.res[req].twists.cached[j] === 'undefined') {
|
||||||
_queryPendingPosts[reqId].push(data);
|
twister.res[req].twists.cached[j] = data;
|
||||||
|
twister.res[req].twists.pending.push(j);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_knownMentions[key] = {mentionTime: mentionTime, data: data};
|
_knownMentions[key] = {mentionTime: mentionTime, data: data};
|
||||||
purgeOldMentions();
|
purgeOldMentions();
|
||||||
@ -114,15 +116,13 @@ function requestMentionsCount() {
|
|||||||
tag: 'twister_notification_new_mentions',
|
tag: 'twister_notification_new_mentions',
|
||||||
timeout: $.Options.showDesktopNotifMentionsTimer.val,
|
timeout: $.Options.showDesktopNotifMentionsTimer.val,
|
||||||
funcClick: function () {
|
funcClick: function () {
|
||||||
var postboardSelector =
|
var req = defaultScreenName + '@mention';
|
||||||
'.postboard-posts[data-request-id="' + defaultScreenName + '@mention"]';
|
if (!focusModalWithElement(twister.res[req].board,
|
||||||
if (!focusModalWithElement(postboardSelector,
|
|
||||||
function (req) {
|
function (req) {
|
||||||
var postboard = $(req.postboardSelector);
|
twister.res[req].board.closest('.postboard')
|
||||||
postboard.closest('.postboard').find('.postboard-news').hide();
|
.find('.postboard-news').click();
|
||||||
displayQueryPending(postboard);
|
},
|
||||||
resetMentionsCount();
|
req
|
||||||
}, {postboardSelector: postboardSelector}
|
|
||||||
))
|
))
|
||||||
$.MAL.showMentions(defaultScreenName);
|
$.MAL.showMentions(defaultScreenName);
|
||||||
}
|
}
|
||||||
@ -175,8 +175,18 @@ function resetMentionsCount() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function initMentionsCount() {
|
function initMentionsCount() {
|
||||||
// polling mentions is a temporary solution
|
var req = defaultScreenName + '@mention';
|
||||||
|
twister.res[req] = {
|
||||||
|
query: defaultScreenName,
|
||||||
|
resource: 'mention',
|
||||||
|
twists: {
|
||||||
|
cached: {},
|
||||||
|
pending: []
|
||||||
|
}
|
||||||
|
};
|
||||||
loadMentionsFromStorage();
|
loadMentionsFromStorage();
|
||||||
|
queryPendingPush(req, getMentionsData());
|
||||||
|
|
||||||
$.MAL.updateNewMentionsUI(_newMentions);
|
$.MAL.updateNewMentionsUI(_newMentions);
|
||||||
requestMentionsCount();
|
requestMentionsCount();
|
||||||
setInterval(requestMentionsCount, 10000);
|
setInterval(requestMentionsCount, 10000);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user