2020-10-17 16:02:50 -05:00
|
|
|
async function render(path, s){
|
2020-10-14 07:44:19 -05:00
|
|
|
var context = await getContext();
|
2020-10-17 16:02:50 -05:00
|
|
|
if(!s)
|
|
|
|
history.pushState({}, context.sitename, location.protocol+'//'+location.host+path);
|
2020-10-17 00:11:24 -05:00
|
|
|
switch(path){
|
2020-10-14 07:44:19 -05:00
|
|
|
//nothing but context
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/about\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
document.body.innerHTML = nunjucks.render('about.njk', context);
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/login\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
document.body.innerHTML = nunjucks.render('login.njk', context);
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/register\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
if(!context.registration) window.location = '/';
|
|
|
|
document.body.innerHTML = nunjucks.render('registration.njk', context);
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/changepwd\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
document.body.innerHTML = nunjucks.render('changepwd.njk', context);
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/chat\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
document.body.innerHTML = nunjucks.render('chat.html', context);
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/help\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
document.body.innerHTML = nunjucks.render('help.njk', context);
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
|
|
|
//need to hit the API
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/users\/live\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
var list = JSON.parse(await makeRequest("POST", "/api/users/live", JSON.stringify({num: 50})));
|
|
|
|
document.body.innerHTML = nunjucks.render('live.njk', Object.assign({list: list.users}, context));
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/users\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
var list = JSON.parse(await makeRequest("POST", "/api/users/all", JSON.stringify({num: 50})));
|
|
|
|
document.body.innerHTML = nunjucks.render('list.njk', Object.assign({list: list.users}, context));
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/profile\/chat\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
if(!context.auth.name) window.location = '/login';
|
|
|
|
var config = JSON.parse(await makeRequest("GET", '/api/'+context.auth.name+'/config'));
|
|
|
|
config = {
|
|
|
|
integ: {
|
|
|
|
twitch: config.twitch,
|
|
|
|
xmpp: config.xmpp,
|
|
|
|
irc: config.irc,
|
|
|
|
discord: config.discord
|
|
|
|
}
|
|
|
|
};
|
|
|
|
document.body.innerHTML = nunjucks.render('chat_integ.njk', Object.assign(config, context));
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/profile\/?$/) || {}).input:
|
2020-10-14 07:44:19 -05:00
|
|
|
if(!context.auth.name) window.location = '/login';
|
|
|
|
var config = JSON.parse(await makeRequest("GET", '/api/'+context.auth.name+'/config'));
|
|
|
|
config = {
|
|
|
|
meta: {
|
|
|
|
title: config.title,
|
|
|
|
about: config.about
|
|
|
|
},
|
|
|
|
rflag: {record_flag: config.record_flag},
|
|
|
|
twitch: config.twitch_mirror
|
|
|
|
};
|
|
|
|
document.body.innerHTML = nunjucks.render('profile.njk', Object.assign(config, context));
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
|
|
|
//parsing slugs
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/invite\//) || {}).input: // /invite/:code
|
|
|
|
document.body.innerHTML = nunjucks.render('invite.njk', Object.assign({icode: path.substring(8)}, context));
|
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
|
|
|
//slugs and API
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/users\/.+\/?$/) || {}).input: // /users/:user
|
|
|
|
if(path.substring(path.length - 1).indexOf('/') !== -1)
|
|
|
|
var usr = path.substring(7, path.length - 1);
|
|
|
|
else var usr = path.substring(7);
|
2020-10-14 07:44:19 -05:00
|
|
|
var config = JSON.parse(await makeRequest("GET", '/api/'+usr+'/config'));
|
|
|
|
if(!config.title){document.body.innerHTML = nunjucks.render('404.njk', context); break;}
|
|
|
|
document.body.innerHTML = nunjucks.render('user.njk', Object.assign({about: config.about, title: config.title, username: config.username}, context));
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-18 09:23:28 -05:00
|
|
|
initPlayer(usr);
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/vods\/.+\/manage\/?$/) || {}).input: // /vods/:user/manage
|
|
|
|
var usr = path.substring(6, (path.length - 7));
|
2020-10-14 07:44:19 -05:00
|
|
|
if(context.auth.name !== usr) window.location = '/vods/'+usr;
|
|
|
|
var vods = JSON.parse(await makeRequest("GET", '/api/'+usr+'/vods'));
|
|
|
|
document.body.innerHTML = nunjucks.render('managevods.njk', Object.assign({user: usr, list: vods.vods.filter(fn => fn.name.endsWith('.mp4'))}, context));
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 00:11:24 -05:00
|
|
|
case (path.match(/^\/vods\/.+\/?$/) || {}).input: // /vods/:user
|
|
|
|
if(path.substring(path.length - 1).indexOf('/') !== -1)
|
|
|
|
var usr = path.substring(6, path.length - 1);
|
|
|
|
else var usr = path.substring(6);
|
2020-10-14 07:44:19 -05:00
|
|
|
var vods = JSON.parse(await makeRequest("GET", '/api/'+usr+'/vods'));
|
|
|
|
document.body.innerHTML = nunjucks.render('vods.njk', Object.assign({user: usr, list: vods.vods.filter(fn => fn.name.endsWith('.mp4'))}, context));
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
|
|
|
//root
|
|
|
|
case "/":
|
2020-10-17 00:11:24 -05:00
|
|
|
render('/users/live');
|
2021-01-08 17:25:44 -06:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
|
|
|
case "":
|
2020-10-17 00:11:24 -05:00
|
|
|
render('/users/live');
|
2021-01-08 17:25:44 -06:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
break;
|
2020-10-17 15:37:25 -05:00
|
|
|
case "/index.html":
|
|
|
|
render('/users/live');
|
2021-01-08 17:25:44 -06:00
|
|
|
modifyLinks();
|
2020-10-17 15:37:25 -05:00
|
|
|
break;
|
2020-10-14 07:44:19 -05:00
|
|
|
//404
|
|
|
|
default:
|
|
|
|
document.body.innerHTML = nunjucks.render('404.njk', context);
|
2020-10-17 00:11:24 -05:00
|
|
|
modifyLinks();
|
2020-10-14 07:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 16:02:50 -05:00
|
|
|
window.addEventListener('popstate', (event) => {
|
|
|
|
render(document.location.pathname, true);
|
|
|
|
});
|
|
|
|
|
2020-10-14 07:44:19 -05:00
|
|
|
async function getContext(){
|
|
|
|
var info = JSON.parse(await makeRequest('GET', '/api/instance/info'));
|
|
|
|
info.sitename = info.name;
|
|
|
|
info.name = null;
|
|
|
|
info.auth = {
|
|
|
|
is: document.cookie.match(/^(.*;)?\s*X-Auth-As\s*=\s*[^;]+(.*)?$/) !== null,
|
|
|
|
name: parseCookie(document.cookie)['X-Auth-As']
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeRequest(method, url, payload) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
xhr.open(method, url);
|
|
|
|
xhr.onload = function () {
|
|
|
|
if (this.status >= 200 && this.status < 300) {
|
|
|
|
resolve(xhr.response);
|
|
|
|
} else {
|
|
|
|
reject({
|
|
|
|
status: this.status,
|
|
|
|
statusText: xhr.statusText
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.onerror = function () {
|
|
|
|
reject({
|
|
|
|
status: this.status,
|
|
|
|
statusText: xhr.statusText
|
|
|
|
});
|
|
|
|
};
|
|
|
|
!payload ? xhr.send() : xhr.send(payload);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseCookie(c){
|
|
|
|
if(typeof(c) !== 'string' || !c.includes('=')) return {};
|
|
|
|
return Object.assign({[c.split('=')[0].trim()]:c.split('=')[1].split(';')[0].trim()}, parseCookie(c.split(/;(.+)/)[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleLoad() {
|
|
|
|
var r = JSON.parse(document.getElementById('responseFrame').contentDocument.documentElement.textContent).success
|
|
|
|
if (typeof(r) !== 'undefined') window.location.href = '/profile'
|
2020-10-17 00:11:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function modifyLinks() {
|
|
|
|
for (var ls = document.links, numLinks = ls.length, i=0; i<numLinks; i++){
|
2021-01-15 09:43:16 -06:00
|
|
|
if(ls[i].href.indexOf(location.protocol+'//'+location.host) !== -1 && ls[i].href.match(new RegExp(/\/[A-Za-z0-9_\-~]+\.[A-Za-z0-9]+$/)) === null) {
|
2020-10-17 00:11:24 -05:00
|
|
|
//should be a regular link
|
|
|
|
ls[i].setAttribute('onclick', 'return internalLink(\"'+ls[i].href.substring((location.protocol+'//'+location.host).length)+'\")');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function internalLink(path){
|
|
|
|
this.render(path);
|
|
|
|
return false;
|
2020-10-17 05:26:31 -05:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:23:28 -05:00
|
|
|
function sleep(ms) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
2020-10-17 05:26:31 -05:00
|
|
|
|
2020-10-18 09:23:28 -05:00
|
|
|
var shakaPolyFilled = false;
|
|
|
|
async function initPlayer(usr) {
|
|
|
|
var manifestUri = document.location.protocol+'//'+document.location.host+'/live/'+usr+'/index.mpd';
|
|
|
|
if(!shakaPolyFilled){
|
|
|
|
shaka.polyfill.installAll();
|
|
|
|
shakaPolyFilled = true;
|
|
|
|
}
|
2021-01-08 16:13:21 -06:00
|
|
|
var live = JSON.parse(await makeRequest("GET", "/api/"+usr+"/config")).live;
|
|
|
|
if(live){
|
|
|
|
// Create a Player instance.
|
|
|
|
const video = document.getElementById('video');
|
|
|
|
const player = new shaka.Player(video);
|
|
|
|
// Listen for error events.
|
|
|
|
player.addEventListener('error', onErrorEvent);
|
2020-10-17 05:26:31 -05:00
|
|
|
|
2021-01-08 16:13:21 -06:00
|
|
|
video.addEventListener('play', () => {
|
|
|
|
document.getElementById('playbtn').style.visibility = 'hidden';
|
|
|
|
});
|
|
|
|
video.addEventListener('pause', () => {
|
|
|
|
document.getElementById('playbtn').style.visibility = 'visible';
|
|
|
|
});
|
|
|
|
// Try to load a manifest.
|
|
|
|
// This is an asynchronous process.
|
|
|
|
try {
|
|
|
|
await player.load(manifestUri);
|
|
|
|
// This runs if the asynchronous load is successful.
|
|
|
|
console.log('The video has now been loaded!');
|
|
|
|
} catch (e) {
|
|
|
|
// onError is executed if the asynchronous load fails.
|
|
|
|
onError(e);
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-08 17:25:44 -06:00
|
|
|
if(document.getElementById('video') !== null)
|
|
|
|
setTimeout(initPlayer, 5000, usr);
|
2020-10-18 09:23:28 -05:00
|
|
|
}
|
2020-10-17 05:26:31 -05:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:23:28 -05:00
|
|
|
function onErrorEvent(event) {
|
|
|
|
// Extract the shaka.util.Error object from the event.
|
|
|
|
onError(event.detail);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onError(error) {
|
|
|
|
// Log the error.
|
|
|
|
console.error('Error code', error.code, 'object', error);
|
2021-01-15 09:43:16 -06:00
|
|
|
}
|