diff --git a/app/main.py b/app/main.py index 5570900..d995417 100644 --- a/app/main.py +++ b/app/main.py @@ -560,19 +560,21 @@ PAGE_HTML = """

Matrix chat

-

You're invited to the co-op's Matrix space. Your registered Matrix ID:

+

We use the Matrix chat protocol to collaborate. You can access our space with an inference.coop account or an account hosted elsewhere.

+

Current Matrix ID:

-
Registered Matrix ID
checking… -
- +
+ + +
+ +
- -
- - -
-
@@ -645,6 +647,11 @@ async function load() { } } +let mxidLocal = ''; // @username:inference.coop, resolved by the portal +let mxidCurrent = ''; // what is registered right now (external or local) + +function mxidValid(v) { return /^@[^\\s:]+:[^\\s]+\\.[^\\s]+$/.test(v); } + async function loadMxid() { try { const r = await fetch('/api/matrix-mxid'); @@ -652,38 +659,62 @@ async function loadMxid() { const d = await r.json(); if (d.error) { $('mxid-registered-value').textContent = 'unavailable'; - $('mxid-status').textContent = 'Could not load your Matrix registration: ' + d.error; + $('mxid-status').textContent = 'Could not load: ' + d.error; return; } - const custom = (d.matrix_mxid || '').trim(); - const local = d.local_mxid || ''; - if (custom) { - $('mxid-registered-value').textContent = custom; - $('mxid-registered-note').textContent = 'External account — Matrix invites go here; your co-op account will not receive them.'; - $('mxid-input').value = custom; - $('mxid-clear').style.display = ''; - $('mxid-custom-hint').style.display = ''; - } else if (local) { - $('mxid-registered-value').textContent = local; - $('mxid-registered-note').textContent = 'Your co-op account — invites go here.'; + mxidLocal = (d.local_mxid || '').trim(); + mxidCurrent = (d.matrix_mxid || '').trim() || mxidLocal; + $('mxid-registered-value').textContent = mxidCurrent || 'pending — will use the inference.coop account once it exists'; + const useLocalBtn = $('mxid-use-local'); + if (mxidCurrent && mxidLocal && mxidCurrent !== mxidLocal) { + useLocalBtn.textContent = 'Use ' + mxidLocal; + useLocalBtn.style.display = ''; } else { - $('mxid-registered-value').textContent = 'not set yet — invites go to your co-op account once it exists'; + useLocalBtn.style.display = 'none'; } } catch (e) { $('mxid-registered-value').textContent = 'unavailable'; - $('mxid-status').textContent = 'Could not load your Matrix registration: ' + e.message; + $('mxid-status').textContent = 'Could not load: ' + e.message; } } -async function clearMxid() { - await saveMxid(''); +function showExternalField() { + $('mxid-actions').style.display = 'none'; + $('mxid-field-row').style.display = ''; + $('mxid-input').value = (mxidCurrent && mxidCurrent !== mxidLocal) ? mxidCurrent : ''; + $('mxid-status').textContent = ''; + $('mxid-input').focus(); } -async function saveMxid(explicit) { - const mxid = (explicit !== undefined ? explicit : $('mxid-input').value).trim(); +function hideExternalField() { + $('mxid-field-row').style.display = 'none'; + $('mxid-actions').style.display = ''; + $('mxid-status').textContent = ''; +} + +async function useLocalMxid() { + const btn = $('mxid-use-local'); + btn.disabled = true; + try { + const r = await fetch('/api/matrix-mxid', { + method: 'POST', headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({ matrix_mxid: '' }) + }); + const d = await r.json(); + if (d.error) { $('mxid-status').textContent = d.error; } + else { $('mxid-status').textContent = ''; await loadMxid(); } + } catch (e) { $('mxid-status').textContent = 'Could not switch: ' + e.message; } + btn.disabled = false; +} + +async function saveMxid() { + const mxid = $('mxid-input').value.trim(); + if (!mxidValid(mxid)) { + $('mxid-status').textContent = 'Enter a Matrix ID like @username:server.org'; + return; + } const btn = $('mxid-btn'); btn.disabled = true; - $('mxid-status').textContent = ''; try { const r = await fetch('/api/matrix-mxid', { method: 'POST', headers: {'Content-Type': 'application/json'}, @@ -691,20 +722,13 @@ async function saveMxid(explicit) { }); const d = await r.json(); if (d.error) { $('mxid-status').textContent = d.error; } - else if (mxid) { - const joined = d.invite_sent === false; - $('mxid-status').innerHTML = 'Saved. An invite to the co-op Matrix space is on its way to ' + escapeHtml(mxid) + '' + - (joined ? ' — that account could not be reached right now; the nightly sync will retry automatically.' : - ' — accept it from that account to join. Your co-op account will no longer receive invites.'); - $('mxid-status').className = 'hint ok'; - } else { - $('mxid-status').textContent = 'Cleared — your co-op account will be used for Matrix.'; - $('mxid-status').className = 'hint ok'; - $('mxid-input').value = ''; - $('mxid-clear').style.display = 'none'; + else { + hideExternalField(); + await loadMxid(); + $('mxid-status').textContent = d.invite_sent === false + ? 'Saved — that account could not be reached right now; the nightly sync will retry automatically.' + : 'Saved — accept the invite from that account to join the space.'; } - btn.disabled = false; - await loadMxid(); } catch (e) { $('mxid-status').textContent = 'Could not save: ' + e.message; } btn.disabled = false; }