diff --git a/lib/inbox-listeners.js b/lib/inbox-listeners.js index 54844c8..5df6ba7 100644 --- a/lib/inbox-listeners.js +++ b/lib/inbox-listeners.js @@ -332,16 +332,54 @@ async function logActivity(collections, storeRaw, record) { let _apChannelId = null; /** - * Look up the ActivityPub channel's ObjectId (cached after first call). + * Look up (or auto-create) the ActivityPub channel's ObjectId. + * Cached after first successful call. + * + * The channel is created with `userId: "default"` so it appears in the + * Microsub reader UI alongside user-created channels. + * * @param {object} collections - MongoDB collections * @returns {Promise} */ async function getApChannelId(collections) { if (_apChannelId) return _apChannelId; - const channel = await collections.microsub_channels?.findOne({ + if (!collections.microsub_channels) return null; + + let channel = await collections.microsub_channels.findOne({ uid: "activitypub", }); - _apChannelId = channel?._id || null; + + if (!channel) { + // Auto-create the channel with the same fields the Microsub plugin uses + const maxOrderDoc = await collections.microsub_channels + .find({ userId: "default" }) + .sort({ order: -1 }) + .limit(1) + .toArray(); + const order = maxOrderDoc.length > 0 ? maxOrderDoc[0].order + 1 : 0; + + const doc = { + uid: "activitypub", + name: "Fediverse", + userId: "default", + order, + settings: { excludeTypes: [], excludeRegex: null }, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + }; + await collections.microsub_channels.insertOne(doc); + channel = doc; + console.info("[ActivityPub] Auto-created Microsub channel 'Fediverse'"); + } else if (!channel.userId) { + // Fix existing channel missing userId (created by earlier version) + await collections.microsub_channels.updateOne( + { _id: channel._id }, + { $set: { userId: "default" } }, + ); + console.info("[ActivityPub] Fixed Microsub channel: set userId to 'default'"); + } + + _apChannelId = channel._id; return _apChannelId; } diff --git a/package.json b/package.json index 4ea84b5..edf9803 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rmdes/indiekit-endpoint-activitypub", - "version": "1.0.16", + "version": "1.0.17", "description": "ActivityPub federation endpoint for Indiekit via Fedify. Adds full fediverse support: actor, inbox, outbox, followers, following, syndication, and Mastodon migration.", "keywords": [ "indiekit",