Browse Source

tune twists' requests processing

readme-update
Simon Grim 8 years ago
parent
commit
727a4a7fd1
  1. 10
      js/tmobile.js
  2. 93
      js/twister_actions.js
  3. 95
      js/twister_newmsgs.js

10
js/tmobile.js

@ -535,13 +535,13 @@ function setupHashtagOrMention(board, query, resource) {
$.mobile.showPageLoadingMsg(); $.mobile.showPageLoadingMsg();
board.empty(); board.empty();
var req = queryStart(board, query, resource); var req = queryStart(board, query, resource, undefined, undefined, {
boardAutoAppend: true,
twister.res[req].boardAutoAppend = true; skidoo: function (req) {
twister.res[req].skidoo = function (req) {
var curPage = $.mobile.activePage.attr('id'); var curPage = $.mobile.activePage.attr('id');
return (curPage !== 'mentions' && curPage !== 'hashtag') || req !== tmobileQueryReq; return (curPage !== 'mentions' && curPage !== 'hashtag') || req !== tmobileQueryReq;
}; }
});
tmobileQueryReq = req; tmobileQueryReq = req;
} }

93
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; var req = query + '@' + resource;
if (typeof twister.res[req] !== 'object') if (typeof twister.res[req] !== 'object') {
twister.res[req] = { twister.res[req] = {
board: board, board: board,
query: query, query: query,
@ -449,16 +449,18 @@ function queryStart(board, query, resource) {
pending: [] pending: []
} }
}; };
else { for (i in extra)
twister.res[req][i] = extra[i];
} else {
twister.res[req].board = board; twister.res[req].board = board;
for (var i in twister.res[req].twists.cached) for (var i in twister.res[req].twists.cached)
if (twister.res[req].twists.pending.indexOf(i) === -1) if (twister.res[req].twists.pending.indexOf(i) === -1)
twister.res[req].twists.pending.push(i); twister.res[req].twists.pending.push(i);
queryPendingDraw(req) queryPendingDraw(req);
} }
if (req === defaultScreenName + '@mention') if (twister.res[req].interval)
return req; return req;
queryRequest(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). // 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), // 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. // 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; return req;
} }
@ -477,24 +479,30 @@ function queryTick(req) {
if (typeof twister.res[req].skidoo === 'function' ? twister.res[req].skidoo(req) if (typeof twister.res[req].skidoo === 'function' ? twister.res[req].skidoo(req)
: !isModalWithElemExists(twister.res[req].board)) { : !isModalWithElemExists(twister.res[req].board)) {
clearInterval(twister.res[req].interval); clearInterval(twister.res[req].interval);
queryClear(req); twister.res[req].interval = 0;
queryPendingClear(req);
return; return;
} }
queryRequest(req); queryRequest(req);
} }
function queryClear(req) { function queryPendingClear(req) {
if (!req || !twister.res[req] || !twister.res[req].twists)
return;
twister.res[req].twists.pending = []; twister.res[req].twists.pending = [];
} }
function queryRequest(req) { function queryRequest(req) {
if (twister.res[req].board && isModalWithElemExists(twister.res[req].board))
twister.res[req].board.closest('div').find('.postboard-loading').show(); 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], twisterRpc('getfavs', [twister.res[req].query, 1000],
queryProcess, req); queryProcess, req);
else else
@ -503,16 +511,44 @@ function queryRequest(req) {
} }
function queryProcess(req, twists) { 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) var lengthNew = 0;
: !isModalWithElemExists(twister.res[req].board)) 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; return;
if (twister.res[req].twists.pending.length) { if (lengthNew)
if (twister.res[req].board.children().length && !$.mobile if (twister.res[req].resource === 'mention' && twister.res[req].query === defaultScreenName) {
&& $.Options.showDesktopNotifPostsModal.val === 'enable' $.MAL.updateNewMentionsUI(twister.res[req].lengthNew);
&& (twister.res[req].resource !== 'mention' || twister.res[req].query !== defaultScreenName)) $.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({ $.MAL.showDesktopNotification({
body: polyglot.t('You got') + ' ' + polyglot.t('new_posts', twister.res[req].twists.pending.length) + ' ' body: polyglot.t('You got') + ' ' + polyglot.t('new_posts', twister.res[req].twists.pending.length) + ' '
+ polyglot.t('in search result') + '.', + polyglot.t('in search result') + '.',
@ -529,6 +565,10 @@ function queryProcess(req, twists) {
}).bind({req: req}) }).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) if (!twister.res[req].board.children().length || twister.res[req].boardAutoAppend)
queryPendingDraw(req); queryPendingDraw(req);
else { else {
@ -542,23 +582,23 @@ function queryProcess(req, twists) {
} }
function queryPendingPush(req, twists) { function queryPendingPush(req, twists) {
if (!req || !twister.res[req] || !twists || !twists.length) var lengthNew = 0;
return; var needForFilter = $.Options.filterLang.val !== 'disable' && $.Options.filterLangForSearching.val;
for (var i = twists.length - 1; i >= 0; i--) { for (var i = twists.length - 1; i >= 0; i--) {
var userpost = twists[i].userpost; var userpost = twists[i].userpost;
var j = userpost.n + '/' + userpost.time; var j = userpost.n + '/' + userpost.time;
if (typeof twister.res[req].twists.cached[j] === 'undefined') {
if (userpost.fav) if (userpost.fav)
userpost = userpost.fav; 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 === ''))
continue; continue;
if ($.Options.filterLang.val !== 'disable' && $.Options.filterLangForSearching.val) { if (needForFilter) {
if (typeof userpost.msg === 'string' && userpost.msg !== '') if (typeof userpost.msg === 'string' && userpost.msg !== '')
langFilterData = filterLang(userpost.msg); langFilterData = filterLang(userpost.msg);
else else
@ -572,10 +612,13 @@ function queryPendingPush(req, twists) {
} }
} }
lengthNew++;
twister.res[req].twists.cached[j] = twists[i]; twister.res[req].twists.cached[j] = twists[i];
twister.res[req].twists.pending.push(j); twister.res[req].twists.pending.push(j);
} }
} }
return lengthNew;
} }
function queryPendingDraw(req) { function queryPendingDraw(req) {
@ -585,7 +628,7 @@ function queryPendingDraw(req) {
attachPostsToStream(twister.res[req].board, twists, false); attachPostsToStream(twister.res[req].board, twists, false);
queryClear(req); queryPendingClear(req);
$.MAL.postboardLoaded(); $.MAL.postboardLoaded();
} }

95
js/twister_newmsgs.js

@ -78,27 +78,11 @@ function loadMentionsFromStorage() {
storage.remove('newMentions'); storage.remove('newMentions');
} }
function requestMentionsCount() { function queryPendingPushMentions(req, res) {
// 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;
var lengthNew = 0; 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 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++) { for (var i = 0; i < res.length; i++) {
if (res[i].userpost.time > timeCurrent) { if (res[i].userpost.time > timeCurrent) {
@ -108,64 +92,31 @@ function processNewMentions(req, res) {
} }
if (res[i].id) { 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; delete res[i].id;
} }
var j = res[i].userpost.n + '/' + res[i].userpost.time; var j = res[i].userpost.n + '/' + res[i].userpost.time;
if (typeof twister.mentions.twists.cached[j] === 'undefined') { if (typeof twister.res[req].twists.cached[j] === 'undefined') {
twister.mentions.twists.cached[j] = res[i]; twister.res[req].twists.cached[j] = res[i];
twister.mentions.twists.pending.push(j); twister.res[req].twists.pending.push(j);
// mention must be somewhat recent compared to last known one to be considered new // mention must be somewhat recent compared to last known one to be considered new
if (res[i].userpost.time + 259200 > timeLastMention) { // 3600 * 24 * 3 if (res[i].userpost.time + 259200 > timeLastMention) { // 3600 * 24 * 3
lengthNew++; lengthNew++;
twister.mentions.lastTime = Math.max(res[i].userpost.time, twister.mentions.lastTime); twister.res[req].lastTime = Math.max(res[i].userpost.time, twister.res[req].lastTime);
twister.mentions.twists.cached[j].isNew = true; twister.res[req].twists.cached[j].isNew = true;
} }
} }
} }
if (lengthNew) { if (lengthNew)
twister.mentions.lengthNew += lengthNew; twister.res[req].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 (twister.mentions.twists.pending.length > lengthPending) { if (twister.res[req].twists.pending.length > lengthPending)
saveMentionsToStorage(); saveMentionsToStorage();
var req = defaultScreenName + '@mention'; return lengthNew;
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();
}
}
} }
function resetMentionsCount() { function resetMentionsCount() {
@ -180,25 +131,17 @@ function resetMentionsCount() {
} }
function initMentionsCount() { function initMentionsCount() {
var req = defaultScreenName + '@mention'; var req = queryStart('', defaultScreenName, 'mention', [10000, 2000, 3], 10000, {
twister.res[req] = {
query: defaultScreenName,
resource: 'mention',
timeoutArgs: [10000, 2000, 3],
twists: {
cached: {},
pending: [],
},
lastTime: 0, lastTime: 0,
lastTorrentId: -1, lastTorrentId: -1,
lengthNew: 0 lengthNew: 0,
}; skidoo: function () {return false;}
});
twister.mentions = twister.res[req]; twister.mentions = twister.res[req];
loadMentionsFromStorage(); loadMentionsFromStorage();
$.MAL.updateNewMentionsUI(twister.mentions.lengthNew); $.MAL.updateNewMentionsUI(twister.mentions.lengthNew);
requestMentionsCount();
twister.mentions.interval = setInterval(requestMentionsCount, 10000);
} }
// --- direct messages --- // --- direct messages ---
@ -252,6 +195,7 @@ function requestDMsCount() {
if (newDMs) { if (newDMs) {
$.MAL.updateNewDMsUI(newDMs); $.MAL.updateNewDMsUI(newDMs);
$.MAL.soundNotifyDM(); $.MAL.soundNotifyDM();
if (!$.mobile && $.Options.showDesktopNotifDMs.val === 'enable') { if (!$.mobile && $.Options.showDesktopNotifDMs.val === 'enable') {
$.MAL.showDesktopNotification({ $.MAL.showDesktopNotification({
body: polyglot.t('You got') + ' ' + polyglot.t('new_direct_messages', newDMs) + '.', body: polyglot.t('You got') + ' ' + polyglot.t('new_direct_messages', newDMs) + '.',
@ -265,6 +209,7 @@ function requestDMsCount() {
if (newDMs) { if (newDMs) {
$.MAL.updateNewGroupDMsUI(newDMs); $.MAL.updateNewGroupDMsUI(newDMs);
$.MAL.soundNotifyDM(); $.MAL.soundNotifyDM();
if (!$.mobile && $.Options.showDesktopNotifDMs.val === 'enable') { if (!$.mobile && $.Options.showDesktopNotifDMs.val === 'enable') {
$.MAL.showDesktopNotification({ $.MAL.showDesktopNotification({
body: polyglot.t('You got') + ' ' + polyglot.t('new_group_messages', newDMs) + '.', body: polyglot.t('You got') + ' ' + polyglot.t('new_group_messages', newDMs) + '.',

Loading…
Cancel
Save