|
| 1 | +# ActivityPub Bridge for SubStream Protocol |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The ActivityPub bridge enables SubStream Protocol to interoperate with the Fediverse (Mastodon, Lemmy, PeerTube, etc.), ensuring SubStream is not a content silo. When creators post new content, announcements are automatically federated to their followers across the decentralized social web. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Automatic Content Federation**: New content announcements are sent to Fediverse followers |
| 10 | +- **Teaser Generation**: Content previews with links back to SubStream-gated content |
| 11 | +- **Creator Actor Profiles**: Each creator gets an ActivityPub actor representing them |
| 12 | +- **Follower Management**: Handles follow/unfollow requests from Fediverse instances |
| 13 | +- **Engagement Tracking**: Records likes, shares, and comments from the Fediverse |
| 14 | +- **Background Processing**: Asynchronous federation queue with retry logic |
| 15 | +- **Web3 Integration**: Links to Stellar blockchain accounts and SubStream profiles |
| 16 | + |
| 17 | +## Architecture |
| 18 | + |
| 19 | +### Core Components |
| 20 | + |
| 21 | +1. **ActivityPubService** (`services/activityPubService.js`) |
| 22 | + - Core ActivityPub protocol implementation |
| 23 | + - Actor profile generation |
| 24 | + - Content announcement creation |
| 25 | + - HTTP signature handling |
| 26 | + |
| 27 | +2. **FederationService** (`services/federationService.js`) |
| 28 | + - Queue management for federation |
| 29 | + - Creator initialization |
| 30 | + - Background job coordination |
| 31 | + |
| 32 | +3. **FederationWorker** (`src/services/federationWorker.js`) |
| 33 | + - Background processing of federation queue |
| 34 | + - Retry logic and error handling |
| 35 | + - Queue statistics and cleanup |
| 36 | + |
| 37 | +4. **ActivityPub Routes** (`routes/activityPub.js`) |
| 38 | + - WebFinger endpoint for actor discovery |
| 39 | + - Actor endpoints (inbox, outbox, followers) |
| 40 | + - NodeInfo for Fediverse discovery |
| 41 | + |
| 42 | +### Database Schema |
| 43 | + |
| 44 | +- `activitypub_actors`: Creator actor profiles and keys |
| 45 | +- `activitypub_followers`: Fediverse followers tracking |
| 46 | +- `activitypub_activities`: Sent federation activities |
| 47 | +- `activitypub_engagements`: Received interactions |
| 48 | +- `federation_queue`: Background processing queue |
| 49 | + |
| 50 | +## Configuration |
| 51 | + |
| 52 | +Add these environment variables to your `.env` file: |
| 53 | + |
| 54 | +```bash |
| 55 | +# ActivityPub Federation Configuration |
| 56 | +ACTIVITYPUB_ENABLED=true |
| 57 | +ACTIVITYPUB_BASE_URL=https://your-domain.com |
| 58 | +ACTIVITYPUB_WORKER_INTERVAL=30000 |
| 59 | +ACTIVITYPUB_MAX_RETRIES=3 |
| 60 | +ACTIVITYPUB_SIGNING_SECRET=your-activitypub-signing-secret-key |
| 61 | +``` |
| 62 | + |
| 63 | +### Environment Variables |
| 64 | + |
| 65 | +- `ACTIVITYPUB_ENABLED`: Enable/disable federation (default: true) |
| 66 | +- `ACTIVITYPUB_BASE_URL`: Base URL for ActivityPub endpoints |
| 67 | +- `ACTIVITYPUB_WORKER_INTERVAL`: Queue processing interval in milliseconds (default: 30000) |
| 68 | +- `ACTIVITYPUB_MAX_RETRIES`: Maximum retry attempts for failed federation (default: 3) |
| 69 | +- `ACTIVITYPUB_SIGNING_SECRET`: Secret for deterministic key generation |
| 70 | + |
| 71 | +## API Endpoints |
| 72 | + |
| 73 | +### Fediverse Endpoints |
| 74 | + |
| 75 | +- `GET /.well-known/webfinger` - WebFinger actor discovery |
| 76 | +- `GET /.well-known/nodeinfo` - NodeInfo discovery |
| 77 | +- `GET /nodeinfo/2.1` - NodeInfo schema |
| 78 | +- `GET /ap/actor/:address` - Actor profile |
| 79 | +- `POST /ap/actor/:address/inbox` - Actor inbox |
| 80 | +- `GET /ap/actor/:address/outbox` - Actor outbox |
| 81 | +- `GET /ap/actor/:address/followers` - Actor followers |
| 82 | + |
| 83 | +### Management Endpoints |
| 84 | + |
| 85 | +- `POST /ap/federate/:contentId` - Manual federation trigger |
| 86 | + |
| 87 | +## Content Federation Flow |
| 88 | + |
| 89 | +1. **Content Creation**: Creator posts new content via SubStream API |
| 90 | +2. **Automatic Queuing**: Content is automatically queued for federation |
| 91 | +3. **Background Processing**: Federation worker processes queue items |
| 92 | +4. **Actor Generation**: Creator's ActivityPub actor is created/updated |
| 93 | +5. **Announcement Creation**: ActivityPub announcement with teaser is generated |
| 94 | +6. **Federation**: Announcement sent to all Fediverse followers |
| 95 | +7. **Engagement Tracking**: Interactions are recorded for analytics |
| 96 | + |
| 97 | +## Teaser Generation |
| 98 | + |
| 99 | +Content announcements include: |
| 100 | + |
| 101 | +- **Teaser Text**: First 200 characters of content description |
| 102 | +- **Direct Link**: Link back to SubStream content page |
| 103 | +- **Creator Attribution**: Link to creator's SubStream profile |
| 104 | +- **Metadata**: Title, tags, thumbnail if available |
| 105 | +- **Blockchain Links**: Reference to Stellar account |
| 106 | + |
| 107 | +Example announcement format: |
| 108 | +```html |
| 109 | +<p>New video content available on SubStream Protocol...</p> |
| 110 | +<p><a href="https://substream.protocol/content/123" target="_blank">Watch full content on SubStream 🔗</a></p> |
| 111 | +<p><small>Posted by <a href="https://substream.protocol/creator/GABC..." target="_blank">@creator_name</a> on SubStream Protocol</small></p> |
| 112 | +``` |
| 113 | + |
| 114 | +## Fediverse Compatibility |
| 115 | + |
| 116 | +The implementation is compatible with: |
| 117 | + |
| 118 | +- **Mastodon**: Full support for posts, follows, likes, shares |
| 119 | +- **Lemmy**: Community posts and discussions |
| 120 | +- **PeerTube**: Video content federation |
| 121 | +- **Pleroma**: ActivityPub-compliant instances |
| 122 | +- **Misskey**: Japanese Fediverse platform |
| 123 | +- **Other ActivityPub instances**: Standard protocol compliance |
| 124 | + |
| 125 | +## Security Considerations |
| 126 | + |
| 127 | +- **HTTP Signatures**: All outgoing requests are signed with RSA keys |
| 128 | +- **Deterministic Keys**: Creator keys are generated deterministically from wallet address |
| 129 | +- **Private Key Storage**: RSA private keys stored securely in database |
| 130 | +- **Signature Verification**: Incoming requests are verified before processing |
| 131 | +- **Rate Limiting**: Federation endpoints respect existing rate limiting |
| 132 | + |
| 133 | +## Monitoring and Analytics |
| 134 | + |
| 135 | +### Federation Statistics |
| 136 | + |
| 137 | +Track federation effectiveness via: |
| 138 | + |
| 139 | +```javascript |
| 140 | +const stats = await federationService.getFederationStats(creatorAddress); |
| 141 | +// Returns: { total_activities, successful_activities, failed_activities, followers } |
| 142 | +``` |
| 143 | + |
| 144 | +### Queue Monitoring |
| 145 | + |
| 146 | +Monitor federation queue health: |
| 147 | + |
| 148 | +```javascript |
| 149 | +const queueStats = federationWorker.getQueueStats(); |
| 150 | +// Returns: { total, pending, processing, completed, failed } |
| 151 | +``` |
| 152 | + |
| 153 | +### Engagement Analytics |
| 154 | + |
| 155 | +Track Fediverse engagement: |
| 156 | + |
| 157 | +```sql |
| 158 | +-- Query engagement by type |
| 159 | +SELECT |
| 160 | + activity_type, |
| 161 | + COUNT(*) as count, |
| 162 | + activity_actor |
| 163 | +FROM activitypub_engagements |
| 164 | +WHERE creator_address = ? |
| 165 | +GROUP BY activity_type, activity_actor; |
| 166 | +``` |
| 167 | + |
| 168 | +## Troubleshooting |
| 169 | + |
| 170 | +### Common Issues |
| 171 | + |
| 172 | +1. **Federation Not Working** |
| 173 | + - Check `ACTIVITYPUB_ENABLED=true` in environment |
| 174 | + - Verify `ACTIVITYPUB_BASE_URL` is correct |
| 175 | + - Check federation worker is running |
| 176 | + |
| 177 | +2. **Missing Actor Profiles** |
| 178 | + - Run database migration: `npm run migrate` |
| 179 | + - Initialize creator: `federationService.initializeCreator(address)` |
| 180 | + |
| 181 | +3. **Failed Federation** |
| 182 | + - Check federation queue: `federationWorker.getQueueStats()` |
| 183 | + - Review error logs for specific failures |
| 184 | + - Verify target instance accessibility |
| 185 | + |
| 186 | +4. **Signature Verification Failures** |
| 187 | + - Check `ACTIVITYPUB_SIGNING_SECRET` is set |
| 188 | + - Verify deterministic key generation |
| 189 | + - Check clock synchronization |
| 190 | + |
| 191 | +### Debug Mode |
| 192 | + |
| 193 | +Enable debug logging: |
| 194 | + |
| 195 | +```bash |
| 196 | +DEBUG=activitypub:* npm start |
| 197 | +``` |
| 198 | + |
| 199 | +## Testing |
| 200 | + |
| 201 | +### Unit Tests |
| 202 | + |
| 203 | +```bash |
| 204 | +npm test -- --testPathPattern=activitypub |
| 205 | +``` |
| 206 | + |
| 207 | +### Integration Tests |
| 208 | + |
| 209 | +```bash |
| 210 | +npm run test:integration -- activitypub |
| 211 | +``` |
| 212 | + |
| 213 | +### Manual Testing |
| 214 | + |
| 215 | +1. **Test WebFinger Discovery** |
| 216 | + ```bash |
| 217 | + curl "https://your-domain.com/.well-known/webfinger?resource=acct:creator_GABC...@your-domain.com" |
| 218 | + ``` |
| 219 | + |
| 220 | +2. **Test Actor Profile** |
| 221 | + ```bash |
| 222 | + curl "https://your-domain.com/ap/actor/GABC..." |
| 223 | + ``` |
| 224 | + |
| 225 | +3. **Test Manual Federation** |
| 226 | + ```bash |
| 227 | + curl -X POST "https://your-domain.com/ap/federate/content123" |
| 228 | + ``` |
| 229 | + |
| 230 | +## Migration Guide |
| 231 | + |
| 232 | +### From Previous Version |
| 233 | + |
| 234 | +1. Run database migration: |
| 235 | + ```bash |
| 236 | + npm run migrate |
| 237 | + ``` |
| 238 | + |
| 239 | +2. Update environment variables: |
| 240 | + ```bash |
| 241 | + # Add new ActivityPub variables |
| 242 | + ACTIVITYPUB_ENABLED=true |
| 243 | + ACTIVITYPUB_BASE_URL=https://your-domain.com |
| 244 | + ``` |
| 245 | + |
| 246 | +3. Restart application: |
| 247 | + ```bash |
| 248 | + npm restart |
| 249 | + ``` |
| 250 | + |
| 251 | +### Existing Content |
| 252 | + |
| 253 | +Existing content is not automatically federated. To federate existing content: |
| 254 | + |
| 255 | +```javascript |
| 256 | +// For each content item |
| 257 | +await federationService.queueContentForFederation(content); |
| 258 | +``` |
| 259 | + |
| 260 | +## Performance Considerations |
| 261 | + |
| 262 | +- **Queue Processing**: Default 30-second intervals, adjust based on volume |
| 263 | +- **Batch Size**: Processes 10 items per queue run |
| 264 | +- **Retry Logic**: Exponential backoff with 5-minute maximum delay |
| 265 | +- **Database Indexing**: Optimized indexes on frequently queried fields |
| 266 | +- **Memory Usage**: Federation worker uses minimal memory |
| 267 | + |
| 268 | +## Future Enhancements |
| 269 | + |
| 270 | +Planned improvements: |
| 271 | + |
| 272 | +1. **Enhanced Content Types**: Support for more content formats |
| 273 | +2. **Community Federation**: Lemmy community integration |
| 274 | +3. **Threaded Comments**: Fediverse comment threading |
| 275 | +4. **Rich Media**: Audio, images, and mixed content |
| 276 | +5. **Analytics Dashboard**: Built-in federation analytics |
| 277 | +6. **OAuth2 Integration**: Third-party app access |
| 278 | +7. **Content Warnings**: Sensitive content handling |
| 279 | +8. **Custom Emojis**: Platform-specific emoji support |
| 280 | + |
| 281 | +## Contributing |
| 282 | + |
| 283 | +When contributing to the ActivityPub bridge: |
| 284 | + |
| 285 | +1. Follow ActivityPub specification (W3C Recommendation) |
| 286 | +2. Test with multiple Fediverse platforms |
| 287 | +3. Ensure backward compatibility |
| 288 | +4. Add comprehensive tests |
| 289 | +5. Update documentation |
| 290 | + |
| 291 | +## Resources |
| 292 | + |
| 293 | +- [ActivityPub W3C Specification](https://www.w3.org/TR/activitypub/) |
| 294 | +- [Fediverse Documentation](https://fediverse.party/) |
| 295 | +- [Mastodon API Documentation](https://docs.joinmastodon.org/) |
| 296 | +- [WebFinger RFC 7033](https://tools.ietf.org/html/rfc7033) |
| 297 | +- [NodeInfo Protocol](https://nodeinfo.diaspora.software/) |
| 298 | + |
| 299 | +## Support |
| 300 | + |
| 301 | +For ActivityPub bridge issues: |
| 302 | + |
| 303 | +1. Check existing GitHub issues |
| 304 | +2. Review troubleshooting section |
| 305 | +3. Enable debug logging |
| 306 | +4. Provide federation logs |
| 307 | +5. Include target instance details |
| 308 | + |
| 309 | +--- |
| 310 | + |
| 311 | +This ActivityPub bridge transforms SubStream into a truly interoperable Web3-social platform, enabling creators to reach audiences across the entire Fediverse while maintaining their monetization layer on Stellar. |
0 commit comments