diff --git a/js/interface_common.js b/js/interface_common.js index 329ebe9..36179dd 100644 --- a/js/interface_common.js +++ b/js/interface_common.js @@ -17,6 +17,7 @@ var twister = { root: $('
') // templates should be detached from DOM and attached here; use extractTemplate() }, modal: {}, + res: {}, // reses for various reqs are cached here var: { localAccounts: [], updatesCheckClient: {} @@ -591,37 +592,20 @@ function openHashtagModalFromSearchHandler(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) { - var req = { - postboard: postboard, - query: query, - resource: resource, - id: query + '@' + resource - }; - - postboard.attr('data-request-id', req.id); +function handleClickDisplayPendingTwists(event) { + if (!event || !event.data || !event.data.req) + return; - requestQuery(req); + $(event.target).hide(); - // 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]; + queryPendingDraw(event.data.req); - req.interval = setInterval(updateQueryModal, 5000, req); -} - -function updateQueryModal(req) { - if (!isModalWithElemExists(req.postboard)) { - clearInterval(req.interval); - clearQueryProcessed(req.id); - return; - } - - requestQuery(req); + if (typeof event.data.cbFunc === 'function') + event.data.cbFunc(event.data.cbReq); } function openFavsModal(event) { @@ -654,7 +638,8 @@ function openFavsModalHandler(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) { @@ -686,18 +671,16 @@ function openMentionsModalHandler(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) { - // obtain already cached mention posts from twister_newmsgs.js - processQuery({ - postboard: modal.content.find('.postboard-posts'), - query: defaultScreenName, - resource: 'mention', - posts: getMentionsData() - }); + if (peerAlias === defaultScreenName) resetMentionsCount(); - } } function openFollowersModal(peerAlias) { @@ -2752,15 +2735,6 @@ function initInterfaceCommon() { $('.userMenu-favs a').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', {feeder: '.latest-activity'}, handleClickOpenConversation); diff --git a/js/tmobile.js b/js/tmobile.js index 602dc7c..6248d27 100644 --- a/js/tmobile.js +++ b/js/tmobile.js @@ -528,27 +528,22 @@ function encode_utf8(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") ); $.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.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); + tmobileQueryReq = req; } // 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') requestDmConversation($('#dmchat .direct-messages-thread'), dmChatUser); } diff --git a/js/twister_actions.js b/js/twister_actions.js index a8ee43c..0431b64 100644 --- a/js/twister_actions.js +++ b/js/twister_actions.js @@ -12,9 +12,6 @@ var postsPerRefresh = 10; var maxExpandPost = 8; var maxExpandPostTop = 4; -var _queryProcessedMap = {}; -var _queryPendingPosts = {}; -var autoUpdateQuery = false; // ---------------- @@ -439,55 +436,120 @@ function updateProfilePosts(postsView, username, useGetposts) { }); } -function clearQueryProcessed(id) { - if (!id) return; +function queryStart(board, query, resource) { + 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] = {}; - _queryPendingPosts[id] = []; + return req; } -function requestQuery(req) { - req.postboard.closest('div').find('.postboard-loading').show(); - if (req.resource === 'fav'){ - twisterRpc("getfavs", [req.query, 1000], function(req, posts){ - req.posts = posts; - processQuery(req) - }, req); - } - else { - dhtget(req.query, req.resource, 'm', - function (req, posts) { - req.posts = posts; - processQuery(req); - }, - req, - req.timeoutArgs - ); +function queryTick(req) { + 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 processQuery(req) { - if (!isModalWithElemExists(req.postboard) || !req.posts || !req.posts.length) +function queryClear(req) { + if (!req || !twister.res[req] || !twister.res[req].twists) return; - if (!req.id) - req.id = req.query + '@' + req.resource; - if (typeof _queryProcessedMap[req.id] !== 'object') - _queryProcessedMap[req.id] = {}; - if (typeof _queryPendingPosts[req.id] !== 'object') - _queryPendingPosts[req.id] = []; + twister.res[req].twists.pending = []; +} - for (var i = req.posts.length - 1; i >= 0; i--) { - var userpost = req.posts[i].userpost; +function queryRequest(req) { + twister.res[req].board.closest('div').find('.postboard-loading').show(); - if (userpost.fav) - userpost = userpost.fav; + 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}) + }); - 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]) { - _queryProcessedMap[req.id][key] = true; + if (userpost.fav) + userpost = userpost.fav; + if (typeof twister.res[req].twists.cached[j] === 'undefined') { if ((typeof userpost.msg !== 'string' || userpost.msg === '') && (typeof userpost.rt !== 'object' || typeof userpost.rt.msg !== 'string' || userpost.rt.msg === '')) @@ -500,53 +562,27 @@ function processQuery(req) { langFilterData = filterLang(userpost.rt.msg); if ($.Options.filterLangSimulate.val) { - req.posts[i].langFilter = langFilterData; + twists[i].langFilter = langFilterData; } else { if (!langFilterData.pass) continue; } } - _queryPendingPosts[req.id].push(req.posts[i]); - } - } - - 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(); + twister.res[req].twists.cached[j] = twists[i]; + twister.res[req].twists.pending.push(j); } } } -function displayQueryPending(postboard) { - var reqId = postboard.attr('data-request-id'); +function queryPendingDraw(req) { + 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); - _queryPendingPosts[reqId] = []; + queryClear(req); $.MAL.postboardLoaded(); } diff --git a/js/twister_newmsgs.js b/js/twister_newmsgs.js index 535c3ed..3691a47 100644 --- a/js/twister_newmsgs.js +++ b/js/twister_newmsgs.js @@ -30,10 +30,12 @@ function processMention(user, mentionTime, data) { _lastMentionTime = Math.max(mentionTime, _lastMentionTime); data.isNew = true; - var reqId = defaultScreenName + '@mention'; - if (typeof _queryPendingPosts[reqId] !== 'object') - _queryPendingPosts[reqId] = []; - _queryPendingPosts[reqId].push(data); + var req = defaultScreenName + '@mention'; + var j = data.userpost.n + '/' + data.userpost.time; + if (typeof twister.res[req].twists.cached[j] === 'undefined') { + twister.res[req].twists.cached[j] = data; + twister.res[req].twists.pending.push(j); + } } _knownMentions[key] = {mentionTime: mentionTime, data: data}; purgeOldMentions(); @@ -114,15 +116,13 @@ function requestMentionsCount() { tag: 'twister_notification_new_mentions', timeout: $.Options.showDesktopNotifMentionsTimer.val, funcClick: function () { - var postboardSelector = - '.postboard-posts[data-request-id="' + defaultScreenName + '@mention"]'; - if (!focusModalWithElement(postboardSelector, + var req = defaultScreenName + '@mention'; + if (!focusModalWithElement(twister.res[req].board, function (req) { - var postboard = $(req.postboardSelector); - postboard.closest('.postboard').find('.postboard-news').hide(); - displayQueryPending(postboard); - resetMentionsCount(); - }, {postboardSelector: postboardSelector} + twister.res[req].board.closest('.postboard') + .find('.postboard-news').click(); + }, + req )) $.MAL.showMentions(defaultScreenName); } @@ -175,8 +175,18 @@ function resetMentionsCount() { } function initMentionsCount() { - // polling mentions is a temporary solution + var req = defaultScreenName + '@mention'; + twister.res[req] = { + query: defaultScreenName, + resource: 'mention', + twists: { + cached: {}, + pending: [] + } + }; loadMentionsFromStorage(); + queryPendingPush(req, getMentionsData()); + $.MAL.updateNewMentionsUI(_newMentions); requestMentionsCount(); setInterval(requestMentionsCount, 10000);