Skip to content

Conversation

@sij411
Copy link
Contributor

@sij411 sij411 commented Nov 22, 2025

Summary

This PR refactors the @fedify/relay package to introduce a factory pattern that supports multiple relay implementations, specifically Mastodon and LitePub relay protocols. The changes improve code organization, maintainability, and extensibility for future relay implementations.

Key Changes

  • Factory Pattern: Introduced createRelay() factory function that instantiates relay implementations based on type ("mastodon" or "litepub")
  • Separate Relay Implementations:
    • MastodonRelay: Implements Mastodon's relay protocol with immediate follower acceptance
    • LitePubRelay: Implements LitePub's relay protocol with reciprocal follow pattern (pending → accepted states)
  • Refactored Core Module: Extracted common relay infrastructure into relay.ts with shared:
    • Actor dispatcher for relay server actor
    • Key pair management (RSA + Ed25519)
    • Followers/Following collection dispatchers
  • Comprehensive Test Coverage: Added dedicated test suites for both implementations:
    • mastodon.test.ts: 896 lines of tests covering Mastodon relay behavior
    • litepub.test.ts: 896 lines of tests covering LitePub relay behavior with reciprocal follows
    • Removed old monolithic relay.test.ts

Protocol Differences

Mastodon Relay:

  • Immediately accepts Follow activities and adds followers to the active list
  • Simpler subscription flow without reciprocal follow requirement
  • Suitable for permissive relay scenarios

LitePub Relay:

  • Implements reciprocal follow pattern (relay follows back the follower)
  • Two-state follower lifecycle: pendingaccepted
  • Follower is only activated after accepting the relay's Follow activity
  • More secure and controlled subscription process

Architecture Improvements

  • Modular Design: Each relay type is self-contained with its own file and tests
  • Extensibility: Easy to add new relay implementations (e.g., Akkoma, Pleroma variants)
  • Shared Infrastructure: Common functionality centralized in relay.ts and relayBuilder
  • Type Safety: Strong TypeScript typing with proper interfaces (RelayOptions, LitePubFollower)

Migration Guide

Before:

import { MastodonRelay, LitePubRelay } from "@fedify/relay";
const mastodonRelay = new MastodonRelay(options);
const litepubRelay = new LitePubRelay(options);

After:

import { createRelay } from "@fedify/relay";

// For Mastodon-compatible relay
const mastodonRelay = createRelay("mastodon", options);

// For LitePub-compatible relay
const litepubRelay = createRelay("litepub", options);

Files Changed

  • packages/relay/src/relay.ts: Refactored to shared infrastructure (771 → 161 lines)
  • packages/relay/src/mastodon.ts: New Mastodon relay implementation (191 lines)
  • packages/relay/src/litepub.ts: New LitePub relay implementation (297 lines)
  • packages/relay/src/mastodon.test.ts: New comprehensive test suite (784 lines)
  • packages/relay/src/litepub.test.ts: New comprehensive test suite (896 lines)
  • packages/relay/src/relay.test.ts: Removed old monolithic tests (1128 lines deleted)
  • packages/relay/src/mod.ts: Updated exports to include new relay classes

Test Plan

  • All Mastodon relay tests pass (784 lines of test coverage)
  • All LitePub relay tests pass (896 lines of test coverage)
  • Factory function correctly instantiates both relay types
  • Follower management works correctly for both protocols
  • Activity forwarding (Create, Update, Delete, Move, Announce) tested for both
  • Subscription approval/rejection flows tested
  • Reciprocal follow pattern tested for LitePub
  • HTTP signature verification works across both implementations

Breaking Changes

This is a breaking change that requires updates to existing relay usage code:

  • The Relay class constructor is replaced with createRelay() factory function
  • Users must specify relay type: "mastodon" or "litepub"
  • Default behavior changes based on selected relay type

Related Commits

  • 36b6574: Add relay factory pattern with federation builder
  • 03ea8aa: Add create factory function for mastodon and litepub
  • 1e7c16f: Add mastodon relay and its test
  • 0e17411: Add litepub relay and its test
  • 7902d02: Delete old relay test

🤖 Generated with Claude Code

@issues-auto-labeler issues-auto-labeler bot added activitypub/interop Interoperability issues activitypub/mastodon Mastodon compatibility breaking change Breaking change component/federation Federation object related component/relay labels Nov 22, 2025
@sij411 sij411 marked this pull request as ready for review November 23, 2025 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

activitypub/interop Interoperability issues activitypub/mastodon Mastodon compatibility breaking change Breaking change component/federation Federation object related component/relay

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant