dynamically update post counts

This commit is contained in:
codl 2017-08-12 01:52:33 +02:00
parent d8d31d9b0a
commit 8236c4526d
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
3 changed files with 30 additions and 4 deletions

View File

@ -41,14 +41,14 @@
clearTimeout(status_timeout);
status_timeout = setTimeout(show_saving, 70);
send_settings(get_all_inputs())
promise = send_settings(get_all_inputs())
.then(data => {
show_success();
clearTimeout(status_timeout);
status_timeout = setTimeout(hide_status, 3000);
backoff_level = 0;
})
.catch(e => {
});
promise.catch(e => {
console.error('Fetch rejected:', e);
show_error();
clearTimeout(status_timeout);
@ -56,6 +56,7 @@
backoff_level += 1;
backoff_level = Math.min(backoff_level, 5);
});
promise.then(fetch_counters).then(update_counters);
// remove server-rendered banner
let settings_section = document.querySelector('#settings-section');
@ -108,4 +109,19 @@
// silently send_settings in case the user changed settings while the page was loading
send_settings(get_all_inputs());
function fetch_counters(){
return fetch('/api/viewer', {
credentials: 'same-origin',
})
.then(resp => { if(!resp.ok){ return Promise.reject(resp) }; return resp; })
.then(resp => resp.json());
}
function update_counters(counters){
document.querySelector('#post-count').textContent = counters.post_count;
document.querySelector('#eligible-estimate').textContent = counters.eligible_for_delete_estimate;
}
setInterval(() => fetch_counters().then(update_counters), 1000 * 20)
})();

View File

@ -180,3 +180,13 @@ def api_settings_put():
updated[key] = data[key]
db.session.commit()
return jsonify(status='success', updated=updated)
@app.route('/api/viewer')
@require_auth_api
def api_viewer_post_counts():
viewer = get_viewer()
return jsonify(
post_count=viewer.post_count(),
eligible_for_delete_estimate=viewer.estimate_eligible_for_delete(),
# more? maybe later
)

View File

@ -24,7 +24,7 @@
{% endif %}
</div>
{% set post_count = g.viewer.account.post_count() %}
<p>Currently keeping track of {{ post_count }} of your posts, roughly {{ g.viewer.account.estimate_eligible_for_delete() }} of which currently match your expiration rules.</p>
<p>Currently keeping track of <span id="post-count">{{ post_count }}</span> of your posts, roughly <span id="eligible-estimate">{{ g.viewer.account.estimate_eligible_for_delete() }}</span> of which currently match your expiration rules.</p>
{% if g.viewer.account.service == 'twitter'
and g.viewer.account.reported_post_count
and post_count < g.viewer.account.reported_post_count * 3/4