26 lines
5.5 KiB
JavaScript
26 lines
5.5 KiB
JavaScript
const dashboard=document.getElementById('dashboard'),nav=dashboard?.querySelector('nav');
|
|
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
|
|
async function api(path,body){const response=await fetch('/api/luckperms/'+path,{cache:'no-store',...(body?{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)}:{})});const data=await response.json();if(!response.ok)throw Error(data.message||'LuckPerms is unavailable.');return data;}
|
|
if(dashboard&&nav){
|
|
const section=el('section');section.id='luckperms';section.hidden=true;
|
|
const tab=el('a','LuckPerms');tab.href='#luckperms';tab.hidden=true;nav.append(tab);
|
|
section.append(el('h2','LuckPerms'),el('p','Live Minecraft groups shared by Arrakis and the lobby. Website account ranks are managed separately in Website ranks.'));
|
|
const status=el('p');status.setAttribute('role','status');section.append(status);
|
|
const overview=el('details'),overviewTitle=el('summary','Live group overview');overview.append(overviewTitle);section.append(overview);const refresh=el('button','Refresh all groups');refresh.type='button';overview.append(refresh);
|
|
const groupList=el('div');groupList.className='lp-groups';overview.append(groupList);
|
|
const tools=el('div');tools.hidden=true;
|
|
tools.append(el('h3','Official LuckPerms web editor'),el('p','Owners can edit all game groups, inheritance, prefixes and suffixes here. A fresh session is private: do not share its address. Creating a session sends the groups and permission data to the official LuckPerms editor service.'));
|
|
const start=el('button','Create private editor session');start.type='button';tools.append(start);
|
|
const link=el('a','Open editor in a separate tab');link.target='_blank';link.rel='noopener noreferrer';link.hidden=true;tools.append(link);
|
|
const frame=el('iframe');frame.title='Official LuckPerms groups editor';frame.referrerPolicy='no-referrer';frame.className='lp-editor';frame.hidden=true;tools.append(frame);
|
|
const form=el('form'),label=el('label','Apply or trust command supplied by the editor'),input=el('input');input.name='command';input.required=true;input.maxLength=220;input.autocomplete='off';input.spellcheck=false;input.placeholder='lp applyedits …';label.append(input);const apply=el('button','Run editor command');apply.type='submit';form.append(label,apply);tools.append(form,el('p','If the editor asks you to trust this browser, paste its lp trusteditor command here. Save changes in the editor, then paste its lp applyedits command if requested. Arbitrary console commands are rejected.'));
|
|
section.append(tools);dashboard.append(section);
|
|
let generation=0,currentUser=null;
|
|
async function load(){const token=generation;refresh.disabled=true;status.textContent='Loading live groups…';try{const data=await api('groups');if(token!==generation)return;groupList.replaceChildren();status.textContent=data.groups.length+' live LuckPerms groups';overviewTitle.textContent='Live group overview ('+data.groups.length+')';for(const group of data.groups){const card=el('article');card.append(el('code',group.name),el('span',group.nodes+' nodes'));groupList.append(card);}if(!data.groups.length)groupList.append(el('p','LuckPerms has no stored groups.'));}catch(error){if(token===generation){groupList.replaceChildren();status.textContent=error.message;}}finally{refresh.disabled=false;}}
|
|
refresh.onclick=load;
|
|
start.onclick=async()=>{const token=generation;start.disabled=true;status.textContent='Creating a private groups editor session…';frame.hidden=true;frame.removeAttribute('src');link.hidden=true;try{const data=await api('editor',{});if(token!==generation||!['owners','official_account','web_net_admin'].includes(currentUser?.rank))return;if(!/^https:\/\/luckperms\.net\/editor\/[A-Za-z0-9_-]{3,200}$/.test(data.url))throw Error('Invalid editor address.');const nativeUrl="/luckperms-editor/editor/"+data.url.split("/").pop();frame.src=nativeUrl;link.href=nativeUrl;link.hidden=false;frame.hidden=false;status.textContent='Private editor session ready. Changes affect game permissions; website account ranks remain separate.';}catch(error){if(token===generation)status.textContent=error.message;}finally{start.disabled=false;}};
|
|
form.onsubmit=async event=>{event.preventDefault();const command=input.value.trim();if(!/^\/?lp (applyedits|trusteditor) [A-Za-z0-9_-]{3,200}$/.test(command)){status.textContent='Paste the exact lp applyedits or lp trusteditor command from the editor.';return;}if(!confirm('Run this LuckPerms editor command? It may change game permissions across the network.'))return;const token=generation;apply.disabled=true;try{const data=await api('apply',{command});if(token!==generation)return;input.value='';await load();status.textContent=data.message;}catch(error){if(token===generation)status.textContent=error.message;}finally{apply.disabled=false;}};
|
|
async function session(user){generation++;currentUser=user;const allowed=user?.permissions?.includes('web.menu.luckperms');section.hidden=!allowed;tab.hidden=!allowed;tools.hidden=!(allowed&&['owners','official_account','web_net_admin'].includes(user.rank)&&user.permissions.includes('web.luckperms.edit'));frame.hidden=true;frame.removeAttribute('src');link.hidden=true;link.removeAttribute('href');input.value='';groupList.replaceChildren();if(allowed&&user.permissions.includes('web.luckperms.view'))await load();}
|
|
window.addEventListener('dune:session',event=>session(event.detail));
|
|
const response=await fetch('/api/auth/session',{cache:'no-store'});if(response.ok)await session((await response.json()).user);
|
|
}
|

Staff