add misskey support to known instances

This commit is contained in:
codl 2021-12-09 16:27:14 +01:00
parent e7744a1964
commit e5971e3848
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
2 changed files with 57 additions and 21 deletions

View File

@ -10,8 +10,8 @@ import {SLOTS, normalize_known, known_load, known_save} from './known_instances.
document.querySelector('#mastodon_another_instance_button_template').innerHTML + '`;');
const mastodon_top_instances =
Function('return JSON.parse(`' + document.querySelector('#mastodon_top_instances').innerHTML + '`);')();
const misskey_container = document.querySelector('#misskey_instance_buttons');
const misskey_container = document.querySelector('#misskey_instance_buttons');
const misskey_button_template = Function('first', 'instance',
'return `' + document.querySelector('#misskey_instance_button_template').innerHTML + '`;');
const misskey_another_button_template = Function(
@ -28,10 +28,16 @@ import {SLOTS, normalize_known, known_load, known_save} from './known_instances.
known = await resp.json();
}
else {
known = [{
"instance": "mastodon.social",
"hits": 0
}];
known = {
mastodon:[{
"instance": "mastodon.social",
"hits": 0
}],
misskey:[
"instance": "misskey.io",
"hits": 0
],
};
}
known_save(known)
fetch('/api/known_instances', {method: 'DELETE'})
@ -40,17 +46,12 @@ import {SLOTS, normalize_known, known_load, known_save} from './known_instances.
return known;
}
async function replace_buttons(){
let known = await get_known();
known = normalize_known(known);
known_save(known);
function replace_buttons(top_instances, known_instances, container,
template, template_another_instance){
let filtered_top_instances = []
for(let instance of mastodon_top_instances){
for(let instance of top_instances){
let found = false;
for(let k of known){
for(let k of known_instances){
if(k['instance'] == instance['instance']){
found = true;
break;
@ -61,20 +62,35 @@ import {SLOTS, normalize_known, known_load, known_save} from './known_instances.
}
}
let instances = known.concat(filtered_top_instances).slice(0, SLOTS);
let instances = known_instances.concat(filtered_top_instances).slice(0, SLOTS);
let html = '';
let first = true;
for(let instance of instances){
html += mastodon_button_template(first, instance['instance'])
html += template(first, instance['instance'])
first = false;
}
html += mastodon_another_button_template();
html += template_another_instance();
mastodon_container.innerHTML = html;
container.innerHTML = html;
}
replace_buttons();
async function init_buttons(){
let known = await get_known();
known.mastodon = normalize_known(known.mastodon);
known.misskey = normalize_known(known.misskey);
known_save(known);
replace_buttons(mastodon_top_instances, known.mastodon,
mastodon_container, mastodon_button_template,
mastodon_another_button_template);
replace_buttons(misskey_top_instances, known.misskey,
misskey_container, misskey_button_template,
misskey_another_button_template);
}
init_buttons();
})();

View File

@ -1,6 +1,24 @@
const STORAGE_KEY = 'forget_known_instances';
const STORAGE_KEY = 'forget_known_instances@2021-12-09';
export const SLOTS = 5;
function load_and_migrate_old(){
const OLD_KEY = "forget_known_instances";
let olddata = localStorage.getItem(OLD_KEY);
if(olddata != null){
olddata = JSON.parse(olddata)
let newdata = {
mastodon: olddata,
misskey: [
"instance": "misskey.io",
"hits": 0
]
};
known_save(newdata);
localStorage.removeItem(OLD_KEY);
return newdata;
}
}
export function known_save(known){
localStorage.setItem(STORAGE_KEY, JSON.stringify(known));
}
@ -9,6 +27,8 @@ export function known_load(){
let known = localStorage.getItem(STORAGE_KEY);
if(known){
known = JSON.parse(known);
} else {
known = load_and_migrate_old();
}
return known;
}