v0.3.4: Comment edit/delete, native notifications, forward-compatible protocol, UI fixes

Comment edit & delete:
- EditComment/DeleteComment BlobHeaderDiffOps with upstream+downstream propagation
- Trust-based: comment author can edit/delete, post author can delete
- Storage: edit_comment(), delete_comment() methods
- Frontend: inline edit (Enter/Escape), delete with confirm

Native notifications:
- tauri-plugin-notification for system notifications on all platforms
- Triggers for messages, new posts, reactions, and comments
- notif_reacts setting added, button-group toggles replace dropdowns
- _notifReady flag prevents startup spam

Protocol hardening:
- BlobHeaderDiffOp::Unknown variant with #[serde(other)] for forward compatibility
- Old nodes silently skip unknown ops instead of crashing

UI fixes:
- Self removed from Following list
- Offline follows in lightbox popup (auto-show if no one online)
- Sent DMs filtered from My Posts
- Comment threading scoped to closest .post (fixes duplicate ID issue)
- Select dropdown text legible in WebKitGTK (black on white options)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Scott Reimers 2026-03-18 00:47:53 -04:00
parent ce176a2299
commit 0abc244ee9
18 changed files with 1616 additions and 67 deletions

View file

@ -357,16 +357,30 @@ function toast(msg) {
setTimeout(() => toastEl.classList.add('hidden'), 3000);
}
// --- Notifications ---
// --- Notifications (Tauri plugin) ---
let _notifiedMessages = new Set();
let _notifiedReacts = new Set();
let _notifiedComments = new Set();
let _notifiedPosts = new Set();
let _notifReady = false;
async function maybeNotify(title, body, tag) {
if (!('Notification' in window)) return;
if (Notification.permission === 'default') {
await Notification.requestPermission();
}
if (Notification.permission === 'granted') {
new Notification(title, { body, tag, silent: false });
}
try {
if (window.__TAURI__?.notification) {
const { isPermissionGranted, requestPermission, sendNotification } = window.__TAURI__.notification;
let granted = await isPermissionGranted();
if (!granted) {
const perm = await requestPermission();
granted = perm === 'granted';
}
if (granted) {
sendNotification({ title, body, channelId: 'default' });
}
} else if ('Notification' in window) {
// Fallback for browsers
if (Notification.permission === 'default') await Notification.requestPermission();
if (Notification.permission === 'granted') new Notification(title, { body, tag, silent: false });
}
} catch (_) {}
}
// --- Popover helpers ---
@ -618,7 +632,48 @@ async function loadFeed(force) {
// Fingerprint: post IDs + reaction counts + comment counts
const fp = posts.map(p => `${p.id}:${(p.reactionCounts||[]).map(r=>r.emoji+r.count).join(',')}:${p.commentCount||0}`).join('|');
if (!force && fp === _feedFingerprint) return;
const oldFp = _feedFingerprint;
_feedFingerprint = fp;
// Notify on new posts and engagement (skip first load)
if (_notifReady && oldFp) {
try {
const notifPosts = await invoke('get_setting', { key: 'notif_posts' }).catch(() => null) || 'off';
const notifReacts = await invoke('get_setting', { key: 'notif_reacts' }).catch(() => null) || 'on';
for (const p of posts) {
// New post notifications
if (!p.isMe && notifPosts !== 'off' && !_notifiedPosts.has(p.id)) {
_notifiedPosts.add(p.id);
if (_notifiedPosts.size > posts.length) { // skip initial bulk
maybeNotify(`New post from ${p.authorName || p.author.slice(0,8)}`, (p.content || '').slice(0, 80), `post-${p.id}`);
}
}
// Reaction notifications on our posts
if (p.isMe && notifReacts !== 'off' && p.reactionCounts) {
for (const r of p.reactionCounts) {
const key = `${p.id}-${r.emoji}-${r.count}`;
if (!_notifiedReacts.has(key)) {
_notifiedReacts.add(key);
if (_notifiedReacts.size > 1) {
maybeNotify(`${r.emoji} on your post`, `${r.count} ${r.emoji} reactions`, `react-${key}`);
}
}
}
}
// Comment notifications on our posts
if (p.isMe && notifReacts !== 'off' && p.commentCount > 0) {
const key = `${p.id}-comments-${p.commentCount}`;
if (!_notifiedComments.has(key)) {
_notifiedComments.add(key);
if (_notifiedComments.size > 1) {
maybeNotify('New comment on your post', (p.content || '').slice(0, 40), `comment-${p.id}`);
}
}
}
}
} catch (_) {}
}
// Preserve expanded comment threads
const expandedComments = new Set();
feedList.querySelectorAll('.comment-thread:not(.hidden)').forEach(el => {
@ -650,7 +705,7 @@ async function loadFeed(force) {
async function loadMyPosts(force) {
try {
const posts = await invoke('get_all_posts');
const mine = posts.filter(p => p.isMe && p.visibility !== 'encrypted-for-me');
const mine = posts.filter(p => p.isMe && p.visibility !== 'encrypted-for-me' && !(p.recipients && p.recipients.length > 0));
const fp = mine.map(p => `${p.id}:${(p.reactionCounts||[]).map(r=>r.emoji+r.count).join(',')}:${p.commentCount||0}`).join('|');
if (!force && fp === _myPostsFingerprint) return;
_myPostsFingerprint = fp;
@ -993,7 +1048,9 @@ async function loadFollows() {
const approvedSet = new Set(outbound.filter(r => r.status === 'approved').map(r => r.nodeId));
const inboundApprovedSet = new Set(inbound.filter(r => r.status === 'approved').map(r => r.nodeId));
if (follows.length === 0) {
// Filter out self before rendering
const others = follows.filter(f => f.nodeId !== myNodeId);
if (others.length === 0) {
followsList.innerHTML = `<div>${renderEmptyState('Not following anyone', 'Follow suggested peers or connect manually.')}</div>`;
} else {
const now = Date.now();
@ -1037,8 +1094,14 @@ async function loadFollows() {
</div>`;
};
const online = follows.filter(f => f.isOnline || (f.lastActivityMs > 0 && (now - f.lastActivityMs) < ONLINE_THRESHOLD));
const offline = follows.filter(f => !online.includes(f));
// If isOnline field isn't available (old build), show all as online
const hasOnlineField = others.some(f => f.isOnline !== undefined);
const online = hasOnlineField
? others.filter(f => f.isOnline || (f.lastActivityMs > 0 && (now - f.lastActivityMs) < ONLINE_THRESHOLD))
: others;
const offline = hasOnlineField
? others.filter(f => !online.includes(f))
: [];
let html = '';
if (online.length > 0) {
@ -1046,11 +1109,36 @@ async function loadFollows() {
html += online.map(renderFollowCard).join('');
}
if (offline.length > 0) {
html += `<div class="follows-section-header">Following: Offline (${offline.length})</div>`;
html += offline.map(renderFollowCard).join('');
html += `<div class="follows-section-header follows-offline-header" style="cursor:pointer">Following: Offline (${offline.length})</div>`;
}
followsList.innerHTML = html;
// Open offline follows in lightbox
if (offline.length > 0) {
followsList.querySelectorAll('.follows-offline-header').forEach(hdr => {
hdr.addEventListener('click', () => {
const existing = document.querySelector('.offline-lightbox');
if (existing) existing.remove();
const overlay = document.createElement('div');
overlay.className = 'offline-lightbox';
overlay.innerHTML = `
<div class="offline-lightbox-content">
<div class="offline-lightbox-header">
<h3>Following: Offline (${offline.length})</h3>
<button class="offline-lightbox-close">x</button>
</div>
<div class="offline-lightbox-list">${offline.map(renderFollowCard).join('')}</div>
</div>`;
document.body.appendChild(overlay);
overlay.querySelector('.offline-lightbox-close').onclick = () => overlay.remove();
overlay.addEventListener('click', (e) => { if (e.target === overlay) overlay.remove(); });
// Wire up buttons inside the lightbox
attachFollowHandlers(overlay);
});
});
}
// Attach unfollow handlers
followsList.querySelectorAll('.unfollow-btn').forEach(btn => {
btn.addEventListener('click', async () => {
@ -1924,7 +2012,8 @@ document.addEventListener('click', async (e) => {
// Comment toggle → expand/collapse thread
if (e.target.classList.contains('comment-toggle-btn')) {
const postId = e.target.dataset.postId;
const threadEl = document.getElementById('comments-' + postId);
const postEl = e.target.closest('.post');
const threadEl = postEl ? postEl.querySelector('.comment-thread') : document.getElementById('comments-' + postId);
if (!threadEl) return;
if (threadEl.classList.contains('hidden')) {
threadEl.classList.remove('hidden');
@ -1945,7 +2034,8 @@ document.addEventListener('click', async (e) => {
try {
await invoke('comment_on_post', { postId, content });
input.value = '';
const threadEl = document.getElementById('comments-' + postId);
const postEl = e.target.closest('.post');
const threadEl = postEl ? postEl.querySelector('.comment-thread') : document.getElementById('comments-' + postId);
if (threadEl) await loadCommentThread(postId, threadEl);
refreshPostEngagement(postId);
} catch (err) { toast('Error: ' + err); }
@ -1953,6 +2043,56 @@ document.addEventListener('click', async (e) => {
return;
}
// Edit comment
if (e.target.classList.contains('comment-edit-btn')) {
const postId = e.target.dataset.postId;
const ts = parseInt(e.target.dataset.ts);
const bubble = e.target.closest('.comment-bubble');
const textEl = bubble.querySelector('.comment-text');
const oldContent = textEl.textContent;
textEl.innerHTML = `<input class="comment-edit-input" value="${escapeHtml(oldContent)}" style="width:100%;background:#1a1a2e;color:#e0e0e0;border:1px solid #444;border-radius:3px;padding:0.2rem;font-size:0.8rem" />`;
const input = textEl.querySelector('.comment-edit-input');
input.focus();
input.addEventListener('keydown', async (ev) => {
if (ev.key === 'Enter') {
const newContent = input.value.trim();
if (newContent && newContent !== oldContent) {
try {
await invoke('edit_comment', { postId, timestampMs: ts, newContent });
const postEl = bubble.closest('.post');
const threadEl = postEl ? postEl.querySelector('.comment-thread') : null;
if (threadEl) await loadCommentThread(postId, threadEl);
toast('Comment edited');
} catch (err) { toast('Error: ' + err); }
} else {
textEl.textContent = oldContent;
}
} else if (ev.key === 'Escape') {
textEl.textContent = oldContent;
}
});
input.addEventListener('blur', () => {
if (textEl.querySelector('.comment-edit-input')) textEl.textContent = oldContent;
});
return;
}
// Delete comment
if (e.target.classList.contains('comment-delete-btn')) {
const postId = e.target.dataset.postId;
const ts = parseInt(e.target.dataset.ts);
if (!confirm('Delete this comment?')) return;
try {
await invoke('delete_comment', { postId, timestampMs: ts });
const postEl = e.target.closest('.post');
const threadEl = postEl ? postEl.querySelector('.comment-thread') : null;
if (threadEl) await loadCommentThread(postId, threadEl);
refreshPostEngagement(postId);
toast('Comment deleted');
} catch (err) { toast('Error: ' + err); }
return;
}
// Close emoji picker on outside click
closeEmojiPicker();
});
@ -1968,10 +2108,15 @@ async function loadCommentThread(postId, container) {
for (const c of comments) {
const name = c.authorName || c.author.substring(0, 12);
const time = relativeTime(c.timestampMs);
html += `<div class="comment-bubble">
const isMyComment = c.author === myNodeId;
const editDeleteBtns = isMyComment
? `<button class="comment-edit-btn" data-post-id="${c.postId}" data-ts="${c.timestampMs}" title="Edit">edit</button>
<button class="comment-delete-btn" data-post-id="${c.postId}" data-ts="${c.timestampMs}" title="Delete">del</button>`
: '';
html += `<div class="comment-bubble" data-post-id="${c.postId}" data-ts="${c.timestampMs}">
<span class="comment-author">${escapeHtml(name)}</span>
<span class="comment-text">${escapeHtml(c.content)}</span>
<span class="comment-time">${time}</span>
<span class="comment-time">${time} ${editDeleteBtns}</span>
</div>`;
}
html += `<div class="comment-compose">
@ -2593,38 +2738,36 @@ $('#notifications-btn').addEventListener('click', async () => {
// Load current settings
const msgVal = await invoke('get_setting', { key: 'notif_messages' }).catch(() => null) || 'on';
const postVal = await invoke('get_setting', { key: 'notif_posts' }).catch(() => null) || 'off';
const reactVal = await invoke('get_setting', { key: 'notif_reacts' }).catch(() => null) || 'on';
const nearbyVal = await invoke('get_setting', { key: 'notif_nearby' }).catch(() => null) || 'on';
function btnGroup(label, key, value, options) {
const btns = options.map(([v, text]) =>
`<button class="notif-opt${v === value ? ' active' : ''}" data-key="${key}" data-value="${v}">${text}</button>`
).join('');
return `<div class="notif-row"><span class="notif-label">${label}</span><div class="notif-opts">${btns}</div></div>`;
}
const html = `
<label for="notif-messages">Messages</label>
<select id="notif-messages">
<option value="off"${msgVal === 'off' ? ' selected' : ''}>Off</option>
<option value="on"${msgVal === 'on' ? ' selected' : ''}>On (no preview)</option>
<option value="preview"${msgVal === 'preview' ? ' selected' : ''}>On (with preview)</option>
</select>
<label for="notif-posts">Posts</label>
<select id="notif-posts">
<option value="off"${postVal === 'off' ? ' selected' : ''}>Off</option>
<option value="follows"${postVal === 'follows' ? ' selected' : ''}>From follows &amp; audience</option>
<option value="recommended"${postVal === 'recommended' ? ' selected' : ''}>Recommended by follows</option>
<option value="popular"${postVal === 'popular' ? ' selected' : ''}>Popular (100+)</option>
</select>
<label for="notif-nearby">Nearby Users</label>
<select id="notif-nearby">
<option value="off"${nearbyVal === 'off' ? ' selected' : ''}>Off</option>
<option value="on"${nearbyVal === 'on' ? ' selected' : ''}>On</option>
</select>
<p class="empty-hint" style="margin-top:0.5rem">Changes are saved automatically.</p>`;
${btnGroup('Messages', 'notif_messages', msgVal, [['off','Off'],['on','On'],['preview','Preview']])}
${btnGroup('Posts', 'notif_posts', postVal, [['off','Off'],['follows','Follows'],['recommended','Recommended'],['popular','Popular']])}
${btnGroup('Reactions &amp; Comments', 'notif_reacts', reactVal, [['off','Off'],['on','On']])}
${btnGroup('Nearby Users', 'notif_nearby', nearbyVal, [['off','Off'],['on','On']])}
<p class="empty-hint" style="margin-top:0.75rem">Changes are saved automatically.</p>`;
openPopover('Notifications', html, {
onOpen() {
for (const id of ['notif-messages', 'notif-posts', 'notif-nearby']) {
$(`#${id}`).addEventListener('change', async (e) => {
const key = id.replace('-', '_');
await invoke('set_setting', { key, value: e.target.value });
document.querySelectorAll('.notif-opt').forEach(btn => {
btn.addEventListener('click', async () => {
const key = btn.dataset.key;
const value = btn.dataset.value;
// Update active state
btn.closest('.notif-opts').querySelectorAll('.notif-opt').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
await invoke('set_setting', { key, value });
toast('Setting saved');
});
}
});
}
});
});
@ -2665,6 +2808,9 @@ async function init() {
const info = await loadNodeInfo();
await loadStats();
await loadFeed();
await loadMessages();
// Now safe to fire notifications (initial data loaded, won't spam)
_notifReady = true;
// Show setup overlay if no profile exists
if (info && !info.hasProfile) {