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,8 +1,8 @@
(function instance_buttons(){
const SLOTS = 5;
const STORAGE_KEY = 'forget_known_instances';
import {known_load, known_save} from './known_instances.js';
(function instance_buttons(){
const container = document.querySelector('#mastodon_instance_buttons');
const button_template = Function('first', 'instance',
@ -14,8 +14,7 @@
Function('return JSON.parse(`' + document.querySelector('#top_instances').innerHTML + '`);')();
async function get_known(){
let known = JSON.parse(localStorage.getItem(STORAGE_KEY));
let has_been_fetched = false;
let known = known_load();
if(!known){
let resp = await fetch('/api/known_instances');
if(resp.ok && resp.headers.get('content-type') == 'application/json'){
@ -27,7 +26,7 @@
"hits": 0
}];
}
save(known)
known_save(known)
fetch('/api/known_instances', {method: 'DELETE'})
}
@ -64,15 +63,12 @@
return head.concat(tail)
}
function save(known){
localStorage.setItem(STORAGE_KEY, JSON.stringify(known));
}
async function replace_buttons(){
let known = await get_known();
known = normalize(known);
save(known);
known_save(known);
let filtered_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 ArchiveForm from '../components/ArchiveForm.html';
import {known_load, known_save} from './known_instances.js'
(function settings_init(){
if(!('fetch' in window)){
@ -142,10 +143,12 @@ import ArchiveForm from '../components/ArchiveForm.html';
let last_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;
}
last_viewer = JSON.stringify(viewer);
last_viewer = dumped;
document.querySelector('#post-count').textContent = viewer.post_count;
document.querySelector('#eligible-estimate').textContent = viewer.eligible_for_delete_estimate;
@ -163,7 +166,9 @@ import ArchiveForm from '../components/ArchiveForm.html';
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(){
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:
src = 'assets/{}'.format(filename)
dst = 'static/{}'.format(filename)
name = filename.split('.')[0]
yield dict(
name=filename,
file_dep=list(chain(
@ -130,7 +131,7 @@ def task_rollup():
clean=True,
actions=[
['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
resp = redirect(url_for('index'))
resp = redirect(url_for('index', _anchor='bump_instance'))
return resp

View File

@ -74,7 +74,7 @@
<script type="text/html+template" id="instance_button_template">
<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? '' : `
{{picture(st, 'mastodon', (20,40,80), ('webp', 'png'))}}
Log in with