Impl room leaving and fix frontend room combobox

This commit is contained in:
oxalica 2024-09-03 05:38:20 -04:00
parent cc51d53575
commit 2eb884766a
4 changed files with 159 additions and 40 deletions

View file

@ -71,6 +71,7 @@
<label for="join-new-room">join public room:</label>
<select id="join-new-room"></select>
<button id="leave-room">leave room</select>
<button id="refresh-rooms">refresh room list</select>
</div>
<div>

View file

@ -2,6 +2,7 @@ const msgFlow = document.querySelector('#msg-flow');
const userPubkeyDisplay = document.querySelector('#user-pubkey');
const serverUrlInput = document.querySelector('#server-url');
const roomsInput = document.querySelector('#rooms');
const leaveRoomBtn = document.querySelector('#leave-room');
const joinNewRoomInput = document.querySelector('#join-new-room');
const chatInput = document.querySelector('#chat');
const regenKeyBtn = document.querySelector('#regen-key');
@ -172,6 +173,7 @@ async function genAuthHeader() {
async function enterRoom(ruuid) {
log(`loading room: ${ruuid}`);
curRoom = ruuid;
roomsInput.value = ruuid;
genAuthHeader()
.then(opts => fetch(`${serverUrl}/room/${ruuid}`, opts))
@ -218,7 +220,7 @@ async function connectServer(newServerUrl) {
log('connecting server');
wsUrl.protocol = wsUrl.protocol == 'http:' ? 'ws:' : 'wss:';
wsUrl.pathname += '/ws';
wsUrl.pathname += wsUrl.pathname.endsWith('/') ? 'ws' : '/ws';
ws = new WebSocket(wsUrl);
ws.onopen = async (_) => {
const auth = await signData({ typ: 'auth' });
@ -256,8 +258,14 @@ async function loadRoomList(autoJoin) {
log('loading room list');
async function loadInto(targetEl, filter) {
const emptyEl = document.createElement('option');
emptyEl.value = '';
emptyEl.innerText = '-';
emptyEl.disabled = true;
targetEl.replaceChildren(emptyEl);
targetEl.value = '';
try {
targetEl.replaceChildren();
const resp = await fetch(`${serverUrl}/room?filter=${filter}`, await genAuthHeader())
const json = await resp.json()
if (resp.status !== 200) throw new Error(`status ${resp.status}: ${json.error.message}`);
@ -274,8 +282,11 @@ async function loadRoomList(autoJoin) {
loadInto(roomsInput, 'joined')
.then(async (_) => {
if (autoJoin && roomsInput.value !== '') {
await enterRoom(roomsInput.value);
if (autoJoin) {
const el = roomsInput.querySelector('option:nth-child(2)');
if (el !== null) {
await enterRoom(el.value);
}
}
});
@ -303,6 +314,25 @@ async function joinRoom(ruuid) {
}
}
async function leaveRoom() {
try {
leaveRoomBtn.disabled = true;
await signAndPost(`${serverUrl}/room/${curRoom}/admin`, {
room: curRoom,
typ: 'remove_member',
user: await getUserPubkey(),
});
log('left room');
await loadRoomList(true);
} catch (e) {
console.error(e);
log(`failed to leave room: ${e}`);
} finally {
leaveRoomBtn.disabled = false;
}
}
async function signAndPost(url, data) {
const signedPayload = await signData(data);
const resp = await fetch(url, {
@ -398,6 +428,9 @@ chatInput.onkeypress = async (e) => {
regenKeyBtn.onclick = async (_) => {
await generateKeypair();
};
leaveRoomBtn.onclick = async (_) => {
await leaveRoom();
};
roomsInput.onchange = async (_) => {
await enterRoom(roomsInput.value);
};