known instances: bump instance counter when logging in

This commit is contained in:
codl 2019-03-15 21:09:22 +01:00
parent 915a6029d7
commit 2bacbaa8b1
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
6 changed files with 59 additions and 17 deletions

View File

@ -1,9 +1,9 @@
const SLOTS = 5;
import {known_load, known_save} from './known_instances.js';
(function instance_buttons(){ (function instance_buttons(){
const SLOTS = 5;
const STORAGE_KEY = 'forget_known_instances';
const container = document.querySelector('#mastodon_instance_buttons'); const container = document.querySelector('#mastodon_instance_buttons');
const button_template = Function('first', 'instance', const button_template = Function('first', 'instance',
'return `' + document.querySelector('#instance_button_template').innerHTML + '`;'); 'return `' + document.querySelector('#instance_button_template').innerHTML + '`;');
@ -14,8 +14,7 @@
Function('return JSON.parse(`' + document.querySelector('#top_instances').innerHTML + '`);')(); Function('return JSON.parse(`' + document.querySelector('#top_instances').innerHTML + '`);')();
async function get_known(){ async function get_known(){
let known = JSON.parse(localStorage.getItem(STORAGE_KEY)); let known = known_load();
let has_been_fetched = false;
if(!known){ if(!known){
let resp = await fetch('/api/known_instances'); let resp = await fetch('/api/known_instances');
if(resp.ok && resp.headers.get('content-type') == 'application/json'){ if(resp.ok && resp.headers.get('content-type') == 'application/json'){
@ -27,7 +26,7 @@
"hits": 0 "hits": 0
}]; }];
} }
save(known) known_save(known)
fetch('/api/known_instances', {method: 'DELETE'}) fetch('/api/known_instances', {method: 'DELETE'})
} }
@ -64,15 +63,12 @@
return head.concat(tail) return head.concat(tail)
} }
function save(known){
localStorage.setItem(STORAGE_KEY, JSON.stringify(known));
}
async function replace_buttons(){ async function replace_buttons(){
let known = await get_known(); let known = await get_known();
known = normalize(known); known = normalize(known);
save(known); known_save(known);
let filtered_top_instances = [] let filtered_top_instances = []
for(let instance of top_instances){ for(let instance of top_instances){

13
assets/known_instances.js Normal file
View File

@ -0,0 +1,13 @@
const STORAGE_KEY = 'forget_known_instances';
export function known_save(known){
localStorage.setItem(STORAGE_KEY, JSON.stringify(known));
}
export function known_load(){
let known = localStorage.getItem(STORAGE_KEY);
if(known){
known = JSON.parse(known);
}
return known;
}

View File

@ -1,5 +1,6 @@
import Banner from '../components/Banner.html'; import Banner from '../components/Banner.html';
import ArchiveForm from '../components/ArchiveForm.html'; import ArchiveForm from '../components/ArchiveForm.html';
import {known_load, known_save} from './known_instances.js'
(function settings_init(){ (function settings_init(){
if(!('fetch' in window)){ if(!('fetch' in window)){
@ -142,10 +143,12 @@ import ArchiveForm from '../components/ArchiveForm.html';
let last_viewer = {}; let last_viewer = {};
function update_viewer(viewer){ function update_viewer(viewer){
if(last_viewer == JSON.stringify(viewer)){ let dumped = JSON.stringify(viewer);
if(last_viewer == dumped){
console.log('viewers is the same');
return; return;
} }
last_viewer = JSON.stringify(viewer); last_viewer = dumped;
document.querySelector('#post-count').textContent = viewer.post_count; document.querySelector('#post-count').textContent = viewer.post_count;
document.querySelector('#eligible-estimate').textContent = viewer.eligible_for_delete_estimate; document.querySelector('#eligible-estimate').textContent = viewer.eligible_for_delete_estimate;
@ -163,7 +166,9 @@ import ArchiveForm from '../components/ArchiveForm.html';
banner.set(viewer); banner.set(viewer);
} }
update_viewer(JSON.parse(document.querySelector('script[data-viewer]').textContent)) let viewer_from_dom = JSON.parse(document.querySelector('script[data-viewer]').textContent)
update_viewer(viewer_from_dom)
function set_viewer_timeout(){ function set_viewer_timeout(){
setTimeout(() => fetch_viewer().then(update_viewer).then(set_viewer_timeout, set_viewer_timeout), setTimeout(() => fetch_viewer().then(update_viewer).then(set_viewer_timeout, set_viewer_timeout),
@ -202,4 +207,31 @@ import ArchiveForm from '../components/ArchiveForm.html';
}, },
}) })
} }
function bump_instance(instance_name){
let known_instances = known_load();
let found = false;
for(let instance of known_instances){
if(instance['instance'] == instance_name){
instance.hits ++;
found = true;
break;
}
}
if(!found){
let instance = {"instance": instance_name, "hits": 1};
known_instances.push(instance);
}
known_save(known_instances);
}
if(viewer_from_dom['service'] == 'mastodon' && location.hash == '#bump_instance'){
console.log('bumpin')
bump_instance(viewer_from_dom['id'].split('@')[1])
let url = new URL(location.href)
url.hash = '';
history.replaceState('', '', url);
}
})(); })();

View File

@ -120,6 +120,7 @@ def task_rollup():
for filename in filenames: for filename in filenames:
src = 'assets/{}'.format(filename) src = 'assets/{}'.format(filename)
dst = 'static/{}'.format(filename) dst = 'static/{}'.format(filename)
name = filename.split('.')[0]
yield dict( yield dict(
name=filename, name=filename,
file_dep=list(chain( file_dep=list(chain(
@ -130,7 +131,7 @@ def task_rollup():
clean=True, clean=True,
actions=[ actions=[
['node_modules/.bin/rollup', '-c', ['node_modules/.bin/rollup', '-c',
'-i', src, '-o', dst, '-f', 'iife'], '-i', src, '-o', dst, '-n', name, '-f', 'iife'],
], ],
) )

View File

@ -265,7 +265,7 @@ def mastodon_login_step2(instance_url):
g.viewer = session g.viewer = session
resp = redirect(url_for('index')) resp = redirect(url_for('index', _anchor='bump_instance'))
return resp return resp

View File

@ -74,7 +74,7 @@
<script type="text/html+template" id="instance_button_template"> <script type="text/html+template" id="instance_button_template">
<a style='background-color:#282c37' class='btn primary' <a style='background-color:#282c37' class='btn primary'
href="{{ url_for('mastodon_login_step1') }}/${encodeURIComponent(instance)}"> href="{{ url_for('mastodon_login_step1') }}?instance_url=${encodeURIComponent(instance)}">
${ !first? '' : ` ${ !first? '' : `
{{picture(st, 'mastodon', (20,40,80), ('webp', 'png'))}} {{picture(st, 'mastodon', (20,40,80), ('webp', 'png'))}}
Log in with Log in with