const url = new URL(window.location.href);
export const baseUrl = `${url.protocol}//${url.host}`;
const api = baseUrl + '/api/alpha/site';
let jwt = null;
let session_jwt = sessionStorage.getItem('jwt');
if (session_jwt != null) {
jwt = session_jwt;
} else {
let local_jwt = localStorage.getItem('jwt');
if (local_jwt != null) {
jwt = local_jwt;
}
}
export { jwt };
const navbar = document.getElementById('navbar_items');
if (jwt != null) {
var request = {method: "GET", headers: {Authorization: `Bearer ${jwt}`}};
} else {
var request = {method: "GET"};
navbar.innerHTML = '
Log in (via API)' +
'' +
'' +
'Communities' +
'' +
'' +
'' +
'User settings' +
'Donate';
}
fetch(api, request)
.then(response => response.json())
.then(data => {
// head
document.querySelector('#head_title').textContent = data.site.name;
document.querySelector('#icon_152').href = data.site.icon_152;
document.querySelector('#icon_32').href = data.site.icon_32;
document.querySelector('#icon_16').href = data.site.icon_16;
document.querySelector('#icon_shortcut').href = data.site.icon_32;
document.querySelector('#favicon').href = baseUrl + '/static/images/favicon.ico';
// navbar
document.querySelector('#navbar_title').innerHTML = '' + ' ' + data.site.name;
if (jwt != null) {
const all_communities_item = document.createElement('li');
all_communities_item.innerHTML = 'All communities'
const communities_menu = document.createElement('ul');
communities_menu.className = 'dropdown-menu'
communities_menu.appendChild(all_communities_item)
if (data.my_user.moderates.length > 0) {
const dropdown_divider = document.createElement('li');
dropdown_divider.innerHTML = '
'
communities_menu.appendChild(dropdown_divider)
const dropdown_header = document.createElement('li');
dropdown_header.innerHTML = ''
communities_menu.appendChild(dropdown_header)
for (let mods of data.my_user.moderates) {
let moderated_community_item = document.createElement('li');
if (mods.community.local) {
moderated_community_item.innerHTML = '' +
mods.community.title + '' + ' (' + mods.community.ap_domain + ')' +
''
} else {
moderated_community_item.innerHTML = '' +
mods.community.title + '' + ' (' + mods.community.ap_domain + ')' +
''
}
communities_menu.appendChild(moderated_community_item)
}
}
if (data.my_user.follows.length > 0) {
const dropdown_divider = document.createElement('li');
dropdown_divider.innerHTML = '
'
communities_menu.appendChild(dropdown_divider)
const dropdown_header = document.createElement('li');
dropdown_header.innerHTML = ''
communities_menu.appendChild(dropdown_header)
for (let follows of data.my_user.follows) {
let followed_community_item = document.createElement('li');
if (follows.community.local) {
followed_community_item.innerHTML = '' +
follows.community.title + '' + ' (' + follows.community.ap_domain + ')' +
''
} else {
followed_community_item.innerHTML = '' +
follows.community.title + '' + ' (' + follows.community.ap_domain + ')' +
''
}
communities_menu.appendChild(followed_community_item)
}
}
const communities_item = document.createElement('li')
communities_item.className = 'nav-item dropdown'
communities_item.innerHTML = 'Communities'
communities_item.appendChild(communities_menu)
navbar.appendChild(communities_item)
/*const login_item = document.createElement('li')
login_item.className = 'nav-item'
login_item.innerHTML = 'Log in (via API)'
ul.appendChild(login_item)
const communities_dropdown = document.createElement('li')
communities_dropdown.className = 'nav-item dropdown'
communities_dropdown.innerHTML = '' +
'Communities' +
''
const communities_dropdown_ul = document.createElement('ul')
communities_dropdown_ul.className = 'dropdown-menu'
const communities_dropdown_ul_item = document.createElement('li')
communities_dropdown_ul_item.className = 'dropdown-item'
communities_dropdown_ul_item.href = '/api/alpha/communities'
communities_dropdown_ul.appendChild(communities_dropdown_ul_item)
communities_dropdown.appendChild(communities_dropdown_ul)
ul.appendChild(communities_dropdown)*/
}
// site info
document.querySelector('#site_json').textContent = JSON.stringify(data, null, 2);
})