diff --git a/js/tmobile.js b/js/tmobile.js index 6248d27..bf69bbe 100644 --- a/js/tmobile.js +++ b/js/tmobile.js @@ -535,13 +535,13 @@ function setupHashtagOrMention(board, query, resource) { $.mobile.showPageLoadingMsg(); board.empty(); - var req = queryStart(board, query, resource); - - twister.res[req].boardAutoAppend = true; - twister.res[req].skidoo = function (req) { - var curPage = $.mobile.activePage.attr('id'); - return (curPage !== 'mentions' && curPage !== 'hashtag') || req !== tmobileQueryReq; - }; + var req = queryStart(board, query, resource, undefined, undefined, { + boardAutoAppend: true, + skidoo: function (req) { + var curPage = $.mobile.activePage.attr('id'); + return (curPage !== 'mentions' && curPage !== 'hashtag') || req !== tmobileQueryReq; + } + }); tmobileQueryReq = req; } diff --git a/js/twister_actions.js b/js/twister_actions.js index 37b9692..bec3efe 100644 --- a/js/twister_actions.js +++ b/js/twister_actions.js @@ -436,10 +436,10 @@ function updateProfilePosts(postsView, username, useGetposts) { }); } -function queryStart(board, query, resource) { +function queryStart(board, query, resource, timeoutArgs, intervalTimeout, extra) { var req = query + '@' + resource; - if (typeof twister.res[req] !== 'object') + if (typeof twister.res[req] !== 'object') { twister.res[req] = { board: board, query: query, @@ -449,16 +449,18 @@ function queryStart(board, query, resource) { pending: [] } }; - else { + for (i in extra) + twister.res[req][i] = extra[i]; + } 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) + queryPendingDraw(req); } - if (req === defaultScreenName + '@mention') + if (twister.res[req].interval) return req; queryRequest(req); @@ -466,9 +468,9 @@ function queryStart(board, query, resource) { // 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].timeoutArgs = timeoutArgs ? timeoutArgs : [10000, 2000, 3]; - twister.res[req].interval = setInterval(queryTick, 5000, req); + twister.res[req].interval = setInterval(queryTick, intervalTimeout ? intervalTimeout : 5000, req); return req; } @@ -477,24 +479,30 @@ 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); + twister.res[req].interval = 0; + queryPendingClear(req); return; } queryRequest(req); } -function queryClear(req) { - if (!req || !twister.res[req] || !twister.res[req].twists) - return; - +function queryPendingClear(req) { twister.res[req].twists.pending = []; } function queryRequest(req) { - twister.res[req].board.closest('div').find('.postboard-loading').show(); + if (twister.res[req].board && isModalWithElemExists(twister.res[req].board)) + twister.res[req].board.closest('div').find('.postboard-loading').show(); - if (twister.res[req].resource === 'fav') + if (twister.res[req].resource === 'mention' && twister.res[req].query === defaultScreenName) { + twisterRpc('getmentions', [twister.res[req].query, 100, {since_id: twister.res[req].lastTorrentId}], + queryProcess, req, + function () {console.warn('getmentions API requires twister-core > 0.9.27');} + ); + dhtget(twister.res[req].query, twister.res[req].resource, 'm', + queryProcess, req, twister.res[req].timeoutArgs); + } else if (twister.res[req].resource === 'fav') twisterRpc('getfavs', [twister.res[req].query, 1000], queryProcess, req); else @@ -503,16 +511,44 @@ function queryRequest(req) { } function queryProcess(req, twists) { - queryPendingPush(req, twists); + if (!req || !twister.res[req] || !twists || !twists.length) + return; - if (typeof twister.res[req].skidoo === 'function' ? twister.res[req].skidoo(req) - : !isModalWithElemExists(twister.res[req].board)) + var lengthNew = 0; + var lengthPending = twister.res[req].twists.pending.length; + + if (twister.res[req].resource === 'mention' && twister.res[req].query === defaultScreenName) + lengthNew = queryPendingPushMentions(req, twists); + else + lengthNew = queryPendingPush(req, twists); + + if (typeof twister.res[req].skidoo === 'function' && twister.res[req].skidoo(req)) 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)) + if (lengthNew) + if (twister.res[req].resource === 'mention' && twister.res[req].query === defaultScreenName) { + $.MAL.updateNewMentionsUI(twister.res[req].lengthNew); + $.MAL.soundNotifyMentions(); + if (!$.mobile && $.Options.showDesktopNotifMentions.val === 'enable') + $.MAL.showDesktopNotification({ + body: polyglot.t('You got') + ' ' + polyglot.t('new_mentions', twister.res[req].lengthNew) + '.', + tag: 'twister_notification_new_mentions', + timeout: $.Options.showDesktopNotifMentionsTimer.val, + funcClick: (function () { + if (!twister.res[this.req].board || !focusModalWithElement(twister.res[this.req].board, + function (req) { + twister.res[req].board.closest('.postboard') + .find('.postboard-news').click(); + }, + this.req + )) + $.MAL.showMentions(defaultScreenName); + }).bind({req: req}) + }); + } else if (!$.mobile && $.Options.showDesktopNotifPostsModal.val === 'enable' + && (twister.res[req].resource !== 'mention' || twister.res[req].query !== defaultScreenName) + && twister.res[req].board && isModalWithElemExists(twister.res[req].board) + && twister.res[req].board.children().length) $.MAL.showDesktopNotification({ body: polyglot.t('You got') + ' ' + polyglot.t('new_posts', twister.res[req].twists.pending.length) + ' ' + polyglot.t('in search result') + '.', @@ -529,6 +565,10 @@ function queryProcess(req, twists) { }).bind({req: req}) }); + if (twister.res[req].twists.pending.length > lengthPending) { // there is some twists may be which are not considered new so lengthNew equals zero (mentions thing) + if (!twister.res[req].board || (!$.mobile && !isModalWithElemExists(twister.res[req].board))) + return; + if (!twister.res[req].board.children().length || twister.res[req].boardAutoAppend) queryPendingDraw(req); else { @@ -542,23 +582,23 @@ function queryProcess(req, twists) { } function queryPendingPush(req, twists) { - if (!req || !twister.res[req] || !twists || !twists.length) - return; + var lengthNew = 0; + var needForFilter = $.Options.filterLang.val !== 'disable' && $.Options.filterLangForSearching.val; for (var i = twists.length - 1; i >= 0; i--) { var userpost = twists[i].userpost; var j = userpost.n + '/' + userpost.time; - if (userpost.fav) - userpost = userpost.fav; - if (typeof twister.res[req].twists.cached[j] === 'undefined') { + if (userpost.fav) + userpost = userpost.fav; + if ((typeof userpost.msg !== 'string' || userpost.msg === '') && (typeof userpost.rt !== 'object' || typeof userpost.rt.msg !== 'string' || userpost.rt.msg === '')) continue; - if ($.Options.filterLang.val !== 'disable' && $.Options.filterLangForSearching.val) { + if (needForFilter) { if (typeof userpost.msg === 'string' && userpost.msg !== '') langFilterData = filterLang(userpost.msg); else @@ -572,10 +612,13 @@ function queryPendingPush(req, twists) { } } + lengthNew++; twister.res[req].twists.cached[j] = twists[i]; twister.res[req].twists.pending.push(j); } } + + return lengthNew; } function queryPendingDraw(req) { @@ -585,7 +628,7 @@ function queryPendingDraw(req) { attachPostsToStream(twister.res[req].board, twists, false); - queryClear(req); + queryPendingClear(req); $.MAL.postboardLoaded(); } diff --git a/js/twister_newmsgs.js b/js/twister_newmsgs.js index dc82ee7..92b3b1a 100644 --- a/js/twister_newmsgs.js +++ b/js/twister_newmsgs.js @@ -78,27 +78,11 @@ function loadMentionsFromStorage() { storage.remove('newMentions'); } -function requestMentionsCount() { - // first: getmentions from torrents we follow - twisterRpc('getmentions', [defaultScreenName, 100, {since_id: twister.mentions.lastTorrentId}], - processNewMentions, undefined, - function(req, ret) {console.warn('getmentions API requires twister-core > 0.9.27');}, null - ); - // second: get mentions from dht (not-following) - dhtget(defaultScreenName, 'mention', 'm', - processNewMentions, undefined, - twister.res[defaultScreenName + '@mention'].timeoutArgs - ); -} - -function processNewMentions(req, res) { - if (!res || !res.length) - return; - +function queryPendingPushMentions(req, res) { var lengthNew = 0; - var lengthPending = twister.mentions.twists.pending.length; + var lengthPending = twister.res[req].twists.pending.length; var timeCurrent = new Date().getTime() / 1000 + 7200; // 60 * 60 * 2 - var timeLastMention = twister.mentions.lastTime; + var timeLastMention = twister.res[req].lastTime; for (var i = 0; i < res.length; i++) { if (res[i].userpost.time > timeCurrent) { @@ -108,64 +92,31 @@ function processNewMentions(req, res) { } if (res[i].id) { - twister.mentions.lastTorrentId = Math.max(twister.mentions.lastTorrentId, res[i].id); + twister.res[req].lastTorrentId = Math.max(twister.res[req].lastTorrentId, res[i].id); delete res[i].id; } var j = res[i].userpost.n + '/' + res[i].userpost.time; - if (typeof twister.mentions.twists.cached[j] === 'undefined') { - twister.mentions.twists.cached[j] = res[i]; - twister.mentions.twists.pending.push(j); + if (typeof twister.res[req].twists.cached[j] === 'undefined') { + twister.res[req].twists.cached[j] = res[i]; + twister.res[req].twists.pending.push(j); // mention must be somewhat recent compared to last known one to be considered new if (res[i].userpost.time + 259200 > timeLastMention) { // 3600 * 24 * 3 lengthNew++; - twister.mentions.lastTime = Math.max(res[i].userpost.time, twister.mentions.lastTime); - twister.mentions.twists.cached[j].isNew = true; + twister.res[req].lastTime = Math.max(res[i].userpost.time, twister.res[req].lastTime); + twister.res[req].twists.cached[j].isNew = true; } } } - if (lengthNew) { - twister.mentions.lengthNew += lengthNew; - $.MAL.updateNewMentionsUI(twister.mentions.lengthNew); - $.MAL.soundNotifyMentions(); - if (!$.mobile && $.Options.showDesktopNotifMentions.val === 'enable') - $.MAL.showDesktopNotification({ - body: polyglot.t('You got') + ' ' + polyglot.t('new_mentions', twister.mentions.lengthNew) + '.', - tag: 'twister_notification_new_mentions', - timeout: $.Options.showDesktopNotifMentionsTimer.val, - funcClick: function () { - var req = defaultScreenName + '@mention'; - if (!twister.res[req].board || !focusModalWithElement(twister.res[req].board, - function (req) { - twister.res[req].board.closest('.postboard') - .find('.postboard-news').click(); - }, - req - )) - $.MAL.showMentions(defaultScreenName); - } - }); - } + if (lengthNew) + twister.res[req].lengthNew += lengthNew; - if (twister.mentions.twists.pending.length > lengthPending) { + if (twister.res[req].twists.pending.length > lengthPending) saveMentionsToStorage(); - var req = defaultScreenName + '@mention'; - if (!twister.res[req].board || !isModalWithElemExists(twister.res[req].board)) - return; - - 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.mentions.twists.pending.length)) - .fadeIn('slow') - ; - twister.res[req].board.closest('div').find('.postboard-loading').hide(); - } - } + return lengthNew; } function resetMentionsCount() { @@ -180,25 +131,17 @@ function resetMentionsCount() { } function initMentionsCount() { - var req = defaultScreenName + '@mention'; - twister.res[req] = { - query: defaultScreenName, - resource: 'mention', - timeoutArgs: [10000, 2000, 3], - twists: { - cached: {}, - pending: [], - }, + var req = queryStart('', defaultScreenName, 'mention', [10000, 2000, 3], 10000, { lastTime: 0, lastTorrentId: -1, - lengthNew: 0 - }; + lengthNew: 0, + skidoo: function () {return false;} + }); twister.mentions = twister.res[req]; + loadMentionsFromStorage(); $.MAL.updateNewMentionsUI(twister.mentions.lengthNew); - requestMentionsCount(); - twister.mentions.interval = setInterval(requestMentionsCount, 10000); } // --- direct messages --- @@ -252,6 +195,7 @@ function requestDMsCount() { if (newDMs) { $.MAL.updateNewDMsUI(newDMs); $.MAL.soundNotifyDM(); + if (!$.mobile && $.Options.showDesktopNotifDMs.val === 'enable') { $.MAL.showDesktopNotification({ body: polyglot.t('You got') + ' ' + polyglot.t('new_direct_messages', newDMs) + '.', @@ -265,6 +209,7 @@ function requestDMsCount() { if (newDMs) { $.MAL.updateNewGroupDMsUI(newDMs); $.MAL.soundNotifyDM(); + if (!$.mobile && $.Options.showDesktopNotifDMs.val === 'enable') { $.MAL.showDesktopNotification({ body: polyglot.t('You got') + ' ' + polyglot.t('new_group_messages', newDMs) + '.',