const url = new URL(window.location.href);
export const baseUrl = `${url.protocol}//${url.host}`;
const api_site = 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_site, 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;
// 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 user_settings_item = document.createElement('li')
user_settings_item.className = 'nav-item'
user_settings_item.innerHTML = 'User settings';
navbar.appendChild(user_settings_item)
const logout_item = document.createElement('li')
logout_item.className = 'nav-item'
logout_item.innerHTML = 'Log out (via API)';
navbar.appendChild(logout_item)
}
// site info
let postlist = document.querySelector('#post_list_request')
if (jwt != null) {
document.querySelector('#site_request').innerHTML = 'GET /api/alpha/site
[LOGGED IN]'
if (postlist) {
postlist.innerHTML = 'GET /api/alpha/post/list?type_=Subscribed&sort=New&page=1
'
}
} else {
document.querySelector('#site_request').innerHTML = 'GET /api/alpha/site
[LOGGED OUT]'
if (postlist) {
postlist.innerHTML = 'GET /api/alpha/post/list?type_=Popular&sort=Hot&page=1
'
}
}
document.querySelector('#site_json').textContent = JSON.stringify(data, null, 2);
})
let postlist = document.querySelector('#post_list_request');
if (postlist) {
if (jwt != null) {
var api_postlist = baseUrl + '/api/alpha/post/list?type_=Subscribed&sort=New&page=1';
} else {
var api_postlist = baseUrl + '/api/alpha/post/list?type_=Popular&sort=Hot&page=1';
}
fetch(api_postlist, request)
.then(response => response.json())
.then(data => {
document.querySelector('#post_list_json').textContent = JSON.stringify(data, null, 2);
})
}