Browse Source

add session caching of results of mentions' and hashtags' searchings

readme-update
Simon Grim 8 years ago
parent
commit
f28d4f4153
  1. 66
      js/interface_common.js
  2. 31
      js/tmobile.js
  3. 186
      js/twister_actions.js
  4. 36
      js/twister_newmsgs.js

66
js/interface_common.js

@ -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, return;
query: query,
resource: resource,
id: query + '@' + resource
};
postboard.attr('data-request-id', req.id);
requestQuery(req); $(event.target).hide();
// use extended timeout parameters on modal refresh (requires twister_core >= 0.9.14). queryPendingDraw(event.data.req);
// 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); if (typeof event.data.cbFunc === 'function')
} event.data.cbFunc(event.data.cbReq);
function updateQueryModal(req) {
if (!isModalWithElemExists(req.postboard)) {
clearInterval(req.interval);
clearQueryProcessed(req.id);
return;
}
requestQuery(req);
} }
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);

31
js/tmobile.js

@ -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);
} }

186
js/twister_actions.js

@ -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;
if (typeof twister.res[req] !== 'object')
twister.res[req] = {
board: board,
query: query,
resource: resource,
twists: {
cached: {},
pending: []
}
};
else {
twister.res[req].board = board;
for (var i in twister.res[req].twists.cached)
if (twister.res[req].twists.pending.indexOf(i) === -1)
twister.res[req].twists.pending.push(i);
queryPendingDraw(req)
}
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);
_queryProcessedMap[id] = {}; return req;
_queryPendingPosts[id] = [];
} }
function requestQuery(req) { function queryTick(req) {
req.postboard.closest('div').find('.postboard-loading').show(); if (typeof twister.res[req].skidoo === 'function' ? twister.res[req].skidoo(req)
if (req.resource === 'fav'){ : !isModalWithElemExists(twister.res[req].board)) {
twisterRpc("getfavs", [req.query, 1000], function(req, posts){ clearInterval(twister.res[req].interval);
req.posts = posts; queryClear(req);
processQuery(req) return;
}, req);
}
else {
dhtget(req.query, req.resource, 'm',
function (req, posts) {
req.posts = posts;
processQuery(req);
},
req,
req.timeoutArgs
);
} }
queryRequest(req);
} }
function processQuery(req) { function queryClear(req) {
if (!isModalWithElemExists(req.postboard) || !req.posts || !req.posts.length) 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 (userpost.fav) if (twister.res[req].resource === 'fav')
userpost = userpost.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})
});
var key = userpost.n + ';' + userpost.time; 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 (!_queryProcessedMap[req.id][key]) { if (userpost.fav)
_queryProcessedMap[req.id][key] = true; userpost = userpost.fav;
if (typeof twister.res[req].twists.cached[j] === 'undefined') {
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(twister.res[req].board, twists, false);
attachPostsToStream(postboard, _queryPendingPosts[reqId], false); queryClear(req);
_queryPendingPosts[reqId] = [];
$.MAL.postboardLoaded(); $.MAL.postboardLoaded();
} }

36
js/twister_newmsgs.js

@ -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…
Cancel
Save