Files

103 lines
3.1 KiB
JavaScript

/* Profile page — sovereign://profile
*
* Fetches profile data (identity, kind 0 metadata, kind 3 contacts,
* kind 10002 relay list, npub, QR code URL) from sovereign://profile/data
* (JSON) and renders it.
*/
function sovereignGet(url) {
return new Promise(function(resolve, reject) {
var x = new XMLHttpRequest();
x.open('GET', url, true);
x.onreadystatechange = function() {
if (x.readyState !== 4) return;
try { resolve(JSON.parse(x.responseText)); }
catch (e) { reject(e); }
};
x.onerror = function() { reject(new Error('XHR error')); };
x.send();
});
}
function esc(s) {
var d = document.createElement('div');
d.textContent = s == null ? '' : String(s);
return d.innerHTML;
}
function infoRow(label, value) {
return '<div class="info-row"><span>' + esc(label) + '</span>' +
'<span class="info">' + (value == null || value === '' ?
'<span class="missing">(not found)</span>' : esc(value)) +
'</span></div>';
}
function renderProfile(d) {
var c = document.getElementById('profile-body');
if (!d || !d.pubkey) {
c.innerHTML = '<p class="note">No identity loaded. Log in to see your profile.</p>';
return;
}
var html = '';
/* Profile picture (kind 0). */
if (d.picture) {
html += '<img class="profile-picture" src="' + esc(d.picture) +
'" alt="Profile picture">';
}
/* QR code for the npub URI. */
if (d.qr_url) {
html += '<div class="qr"><img src="' + esc(d.qr_url) +
'" alt="npub QR code"></div>';
}
/* Identity section. */
html += '<h2>Identity</h2>';
html += infoRow('Pubkey', d.pubkey_short);
html += infoRow('npub URI', d.npub);
html += infoRow('Method', d.method);
html += infoRow('Signing', d.signing);
/* Profile (kind 0). */
html += '<h2>Profile (kind 0)</h2>';
if (!d.kind0_present) {
html += '<p class="missing">(no kind 0 event in database)</p>';
}
html += infoRow('Name', d.name);
html += infoRow('Display Name', d.display_name);
if (d.about) {
html += '<div class="info-row"><span>About</span>' +
'<span class="info" style="max-width:400px;">' + esc(d.about) +
'</span></div>';
}
html += infoRow('Website', d.website);
html += infoRow('Lightning (lud16)', d.lud16);
html += infoRow('Last updated', d.kind0_created);
html += infoRow('Events in DB', d.kind0_count);
/* Contacts (kind 3). */
html += '<h2>Contacts (kind 3)</h2>';
if (!d.kind3_present) {
html += '<p class="missing">(no kind 3 event in database)</p>';
}
html += infoRow('Following', (d.contact_count != null ?
d.contact_count + ' pubkey(s)' : ''));
html += infoRow('Last updated', d.kind3_created);
html += infoRow('Events in DB', d.kind3_count);
/* Relay list (kind 10002). */
html += '<h2>Relay List (kind 10002)</h2>';
html += infoRow('Events in DB', d.kind10002_count);
c.innerHTML = html;
}
sovereignGet('sovereign://profile/data?_=' + Date.now())
.then(function(d) { renderProfile(d); })
.catch(function(e) {
document.getElementById('profile-body').innerHTML =
'<p class="note">Failed to load profile: ' + esc(e.message) + '</p>';
});