Something went wrong. Try again.
Central platform for European atproto.<cc> country community websites atproto.eu
community atproto
Something went wrong. Try again.
3.0 kB · 83 lines
Astro
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384---// Feed subscribe button. Signed in: toggle the feed in the user's saved-feeds preferences// (agent.addSavedFeeds / removeSavedFeeds, which safely read-modify-write the whole prefs// blob on the PDS). Signed out — or if the write fails for any reason (missing scope, old// session, unsupported PDS) — fall back to opening the feed on mu.social. Never errors.---
<script> import { ensureWriteScope } from '../lib/auth-step-up';
const SELECTOR = '.feed-subscribe-btn';
function buttons(): HTMLElement[] { return Array.from(document.querySelectorAll(SELECTOR)); }
function setState(b: HTMLElement, subscribed: boolean) { b.textContent = subscribed ? b.dataset.labelDone ?? 'Subscribed' : b.dataset.labelSub ?? 'Subscribe'; b.classList.toggle('subscribed', subscribed); b.setAttribute('aria-pressed', String(subscribed)); }
function openFeed(b: HTMLElement) { if (b.dataset.url) window.open(b.dataset.url, '_blank', 'noopener'); }
// On sign-in, reflect whether the viewer already saved this feed (so the button toggles). async function refresh() { const atn = (window as any).atn; const btns = buttons(); if (!btns.length || !atn?.agent) return; try { const prefs = await atn.agent.getPreferences(); for (const b of btns) { const saved = prefs.savedFeeds?.find((f: any) => f.value === b.dataset.feed); if (saved) b.dataset.savedId = saved.id; else delete b.dataset.savedId; setState(b, Boolean(saved)); } } catch { /* leave buttons in the default "subscribe" state; click falls back to the deeplink */ } }
document.addEventListener('click', async (e) => { const btn = (e.target as HTMLElement)?.closest(SELECTOR) as HTMLElement | null; if (!btn) return; const atn = (window as any).atn; if (!atn?.agent) { openFeed(btn); return; } // Subscribing writes saved-feeds prefs, which needs the activity scope. Ask once; if the // member declines, fall back to the deeplink (a confirmed grant redirects away before here). if (!(await ensureWriteScope('subscribe to this feed'))) { openFeed(btn); return; } const agent = atn.agent; const feed = btn.dataset.feed as string; btn.setAttribute('disabled', ''); try { if (btn.dataset.savedId) { await agent.removeSavedFeeds([btn.dataset.savedId]); delete btn.dataset.savedId; setState(btn, false); } else { const [created] = await agent.addSavedFeeds([{ type: 'feed', value: feed, pinned: true }]); if (created?.id) btn.dataset.savedId = created.id; setState(btn, true); } } catch { // Any failure (scope not granted, old session, PDS quirk): fall back to the deeplink. openFeed(btn); } finally { btn.removeAttribute('disabled'); } });
document.addEventListener('atn:signedin', refresh); document.addEventListener('atn:signedout', refresh); if ((window as any).atn) refresh();</script>