# CLAUDE.md - indiekit-endpoint-microsub ## Package Overview `@rmdes/indiekit-endpoint-microsub` is a comprehensive Microsub social reader plugin for Indiekit. It implements the Microsub protocol for subscribing to feeds, organizing them into channels, and reading posts in a unified timeline interface. The plugin provides both a Microsub API endpoint (for compatible clients) and a built-in web-based reader UI. **Package Name:** `@rmdes/indiekit-endpoint-microsub` **Version:** 1.0.30 **Type:** ESM module **Entry Point:** `index.js` ## Core Features - **Microsub Protocol Implementation**: Full Microsub API (channels, timeline, follow/unfollow, mute/block, search, preview) - **Web Reader UI**: Built-in Nunjucks-based reader interface with channel navigation, timeline view, and composition - **Multi-Format Feed Support**: RSS, Atom, JSON Feed, h-feed (microformats), with fallback feed discovery - **Real-Time Updates**: WebSub (PubSubHubbub) support for instant notifications - **Adaptive Polling**: Tiered polling system (2 minutes to 17+ hours) based on feed update frequency - **Read State Management**: Per-user read tracking with automatic cleanup (keeps last 30 read items per channel) - **Feed Discovery**: Automatic discovery of feeds from websites (RSS/Atom link tags, JSON Feed, h-feed) - **Webmention Receiving**: Accepts webmentions for posts in the timeline - **Media Proxy**: Proxies external images through local endpoint for privacy and caching - **Blogroll Integration**: Optionally syncs feed subscriptions with `@rmdes/indiekit-endpoint-blogroll` - **Compose UI**: Post replies, likes, reposts, and bookmarks via Micropub ## Architecture ### Data Flow ``` ┌──────────────────────────────────────────────────────────────┐ │ FEED INGESTION │ ├──────────────────────────────────────────────────────────────┤ │ Scheduler (60s interval) │ │ ↓ │ │ getFeedsToFetch() → processFeedBatch() │ │ ↓ │ │ fetchFeed() → parseFeed() → normalizeItems() │ │ ↓ │ │ addItem() → MongoDB (dedup by uid) │ └──────────────────────────────────────────────────────────────┘ ┌──────────────────────────────────────────────────────────────┐ │ READER UI │ ├──────────────────────────────────────────────────────────────┤ │ /microsub/reader/channels → List channels │ │ /microsub/reader/channels/:uid → Channel timeline │ │ /microsub/reader/channels/:uid/feeds → Manage subscriptions │ │ /microsub/reader/compose → Post via Micropub │ └──────────────────────────────────────────────────────────────┘ ┌──────────────────────────────────────────────────────────────┐ │ MICROSUB API │ ├──────────────────────────────────────────────────────────────┤ │ GET/POST /microsub?action=channels → Channel list │ │ GET/POST /microsub?action=timeline → Timeline items │ │ POST /microsub?action=follow → Subscribe to feed │ │ POST /microsub?action=unfollow → Unsubscribe │ │ POST /microsub?action=mute/block → Filter content │ └──────────────────────────────────────────────────────────────┘ ┌──────────────────────────────────────────────────────────────┐ │ REAL-TIME UPDATES │ ├──────────────────────────────────────────────────────────────┤ │ WebSub Hub → POST /microsub/websub/:id → processWebsubUpdate│ │ Webmention → POST /microsub/webmention → addNotification │ └──────────────────────────────────────────────────────────────┘ ``` ## MongoDB Collections ### `microsub_channels` Stores user channels for organizing feeds. ```javascript { _id: ObjectId, uid: "unique-short-id", // Generated 8-char alphanumeric name: "Technology", userId: "user-id", // For multi-user support order: 0, // Display order settings: { excludeTypes: ["repost"], // Filter by post type excludeRegex: "/spam|ads/i" // Filter by regex }, createdAt: "2026-02-13T...", updatedAt: "2026-02-13T..." } ``` **Special Channel**: `uid: "notifications"` (order: -1, always first) receives webmentions and mentions. **Indexes:** - `{ uid: 1 }` - Unique channel lookup - `{ userId: 1, order: 1 }` - Sorted channel list per user ### `microsub_feeds` Stores feed subscriptions and polling metadata. ```javascript { _id: ObjectId, channelId: ObjectId, // References microsub_channels url: "https://example.com/feed", title: "Example Blog", photo: "https://example.com/icon.png", tier: 1, // Polling tier (0-10) unmodified: 0, // Consecutive unchanged fetches nextFetchAt: Date, // When to poll next (kept as Date for query) lastFetchedAt: "2026-02-13T...", // ISO string status: "active" | "error", lastError: "HTTP 404", lastErrorAt: "2026-02-13T...", consecutiveErrors: 0, itemCount: 42, websub: { hub: "https://hub.example/", topic: "https://example.com/feed", secret: "random-secret", leaseSeconds: 432000, expiresAt: Date }, createdAt: "2026-02-13T...", updatedAt: "2026-02-13T..." } ``` **Polling Tiers:** - Tier 0: 1 minute - Tier 1: 2 minutes - Tier 2: 4 minutes - Tier 3: 8 minutes - ... - Tier 10: 1024 minutes (~17 hours) **Tier Adjustment:** - Content changed: tier - 1 (faster polling) - Unchanged 2x: tier + 1 (slower polling) **Indexes:** - `{ channelId: 1, url: 1 }` - Prevent duplicate subscriptions - `{ nextFetchAt: 1 }` - Scheduler query ### `microsub_items` Stores timeline items (posts/entries). ```javascript { _id: ObjectId, channelId: ObjectId, feedId: ObjectId, uid: "https://example.com/post/123", // Canonical URL or GUID type: "entry" | "event" | "review", url: "https://example.com/post/123", name: "Post Title", content: { text: "Plain text...", html: "
HTML content...
" }, summary: "Short description", published: Date, // Kept as Date for sorting updated: Date, author: { name: "Author Name", url: "https://author.example/", photo: "https://author.example/photo.jpg" }, category: ["tag1", "tag2"], photo: ["https://example.com/img.jpg"], video: ["https://example.com/vid.mp4"], audio: ["https://example.com/aud.mp3"], likeOf: ["https://liked-post.example/"], repostOf: ["https://repost.example/"], bookmarkOf: ["https://bookmark.example/"], inReplyTo: ["https://reply-to.example/"], source: { // Metadata about feed source title: "Example Blog", url: "https://example.com" }, readBy: ["user-id"], // Array of user IDs who read this createdAt: "2026-02-13T..." } ``` **Read State:** Items are marked read by adding userId to `readBy` array. Old read items are auto-deleted (keeps last 30 per channel). **Indexes:** - `{ channelId: 1, uid: 1 }` - Unique (prevents duplicates) - `{ channelId: 1, published: -1 }` - Timeline queries - `{ feedId: 1 }` - Feed-specific queries - `{ channelId: 1, url: 1 }` - URL-based mark_read operations - Text index on `name`, `content.text`, `content.html`, `summary`, `author.name` ### `microsub_notifications` Special items collection for notifications channel (webmentions, mentions). **Same schema as `microsub_items`**, stored in the notifications channel. ### `microsub_muted` Muted URLs (hide posts from specific URLs). ```javascript { _id: ObjectId, userId: "user-id", url: "https://muted-site.example/", createdAt: "2026-02-13T..." } ``` ### `microsub_blocked` Blocked authors (delete all posts from author URL). ```javascript { _id: ObjectId, userId: "user-id", authorUrl: "https://blocked-author.example/", createdAt: "2026-02-13T..." } ``` ## Key Files and Modules ### Core Entry Point **`index.js`** - Exports `MicrosubEndpoint` class - Defines routes, navigation items, mount path - Initializes MongoDB collections, scheduler, indexes, cleanup - Registers public routes (WebSub, webmention, media proxy) ### Controllers **`lib/controllers/microsub.js`** - Main Microsub API dispatcher - Routes GET/POST requests by `action` parameter - Calls specialized controllers (channels, timeline, follow, mute, block, search, preview, events) **`lib/controllers/reader.js`** - Web UI controller for reader interface - Channel management (list, create, delete, settings) - Feed management (add, remove, edit, rediscover, refresh) - Timeline rendering (pagination, read/unread filtering) - Compose form (reply, like, repost, bookmark via Micropub) - Search and discovery UI **`lib/controllers/channels.js`** - Microsub API: `action=channels` - List, create, update, delete, reorder channels **`lib/controllers/timeline.js`** - Microsub API: `action=timeline` - Get timeline items (paginated) - Mark read/unread, remove items **`lib/controllers/follow.js`** - Microsub API: `action=follow`, `action=unfollow` - Subscribe to feeds, unsubscribe - Notifies blogroll plugin via `blogroll-notify.js` **`lib/controllers/mute.js` / `block.js`** - Microsub API: `action=mute`, `action=unmute`, `action=block`, `action=unblock` - Mute URLs, block authors **`lib/controllers/search.js`** - Microsub API: `action=search` - Feed discovery from URL **`lib/controllers/preview.js`** - Microsub API: `action=preview` - Preview feed before subscribing **`lib/controllers/events.js`** - Microsub API: `action=events` - Server-Sent Events (SSE) stream for real-time updates **`lib/controllers/opml.js`** - Export subscriptions as OPML ### Storage Layer **`lib/storage/channels.js`** - `createChannel()`, `getChannels()`, `getChannel()`, `updateChannel()`, `deleteChannel()` - `reorderChannels()`, `updateChannelSettings()` - `ensureNotificationsChannel()` - Auto-creates notifications channel **`lib/storage/feeds.js`** - `createFeed()`, `getFeedsForChannel()`, `getFeedById()`, `updateFeed()`, `deleteFeed()` - `getFeedsToFetch()` - Returns feeds where `nextFetchAt <= now` - `updateFeedAfterFetch()` - Adjusts tier based on content changes - `updateFeedWebsub()` - Stores WebSub subscription data - `updateFeedStatus()` - Tracks errors and health - `getFeedsWithErrors()` - Admin diagnostics **`lib/storage/items.js`** - `addItem()` - Inserts item (dedup by `channelId + uid`) - `getTimelineItems()` - Paginated timeline with before/after cursors - `getItemById()`, `getItemsByUids()` - `markItemsRead()`, `markItemsUnread()` - Per-user read state - `removeItems()` - Delete items by ID/UID/URL - `cleanupAllReadItems()` - Startup cleanup, keeps last 30 read per channel - `createIndexes()` - Creates MongoDB indexes **`lib/storage/filters.js`** - `getMutedUrls()`, `addMutedUrl()`, `removeMutedUrl()` - `getBlockedAuthors()`, `addBlockedAuthor()`, `removeBlockedAuthor()` **`lib/storage/read-state.js`** - `getReadState()`, `markRead()`, `markUnread()` - Wraps `items.js` read operations ### Feed Processing **`lib/feeds/parser.js`** - `detectFeedType()` - Sniffs RSS/Atom/JSON Feed/h-feed from content - `parseFeed()` - Dispatcher to format-specific parsers **`lib/feeds/rss.js`** - `parseRss()` - Parses RSS 2.0 and RSS 1.0 (RDF) using `feedparser` **`lib/feeds/atom.js`** - `parseAtom()` - Parses Atom feeds using `feedparser` **`lib/feeds/jsonfeed.js`** - `parseJsonFeed()` - Parses JSON Feed 1.x **`lib/feeds/hfeed.js`** - `parseHfeed()` - Parses h-feed microformats using `microformats-parser` **`lib/feeds/normalizer.js`** - `normalizeItem()` - Converts parsed items to jf2 format **`lib/feeds/fetcher.js`** - `fetchFeed()` - HTTP fetch with User-Agent, timeout, redirect handling **`lib/feeds/discovery.js`** - `discoverFeeds()` - Parses HTML `` tags for RSS/Atom/JSON Feed - `discoverAndValidateFeeds()` - Discovery + validation - `getBestFeed()` - Prefers Atom > RSS > JSON Feed > h-feed **`lib/feeds/validator.js`** - `validateFeedUrl()` - Fetches and parses feed to ensure it's valid - Detects comments feeds (WordPress/Mastodon post replies) ### Polling System **`lib/polling/scheduler.js`** - `startScheduler()` - Runs every 60 seconds, calls `runSchedulerCycle()` - `stopScheduler()` - Cleanup on shutdown - `refreshFeedNow()` - Manual feed refresh **`lib/polling/processor.js`** - `processFeed()` - Fetch, parse, add items for one feed - `processFeedBatch()` - Concurrent processing (default 5 feeds at once) **`lib/polling/tier.js`** - `getTierInterval()` - Maps tier (0-10) to polling interval - `adjustTier()` - Increases/decreases tier based on update frequency ### Real-Time Updates **`lib/websub/discovery.js`** - `discoverWebsubHub()` - Parses feed for `` or `