Declaring a federated type
The decorators and contracts that plug an entity into ActivityPub and WebFinger
A federated type is declared on the class that owns the data, with two decorators. Nothing to register anywhere else: the registry scans the application at boot.
The contracts
/** What the federation needs from an entity: to write its own document. */
export interface ApActorSource {
sanitizeActivity(urls: ApUrls): Promise<APActor>;
}
export interface IActivityPubProvider {
/** The entity `id` designates, or null. `id` is the raw URL segment. */
resolveActivityPub(id: string): Promise<ApActorSource | null>;
}
export interface IWebFingerProvider extends IActivityPubProvider {
/** A specific JRD, when the one built from the actor is not enough. */
resolveWebFinger?(id: string, domain: string): Promise<WebFingerDocument | null>;
}id arrives as-is from the URL (cozy, 48, hactazia): the provider is the one that
interprets it, and decides what an invalid identifier means (null).
The decorators
@ActivityPubProvider({ segment: 'p', api: 'plugins' })
@WebFingerProvider()
@Injectable()
export class PluginsService implements IActivityPubProvider {
async resolveActivityPub(id: string): Promise<ApActorSource | null> {
return this.findBySlug(id);
}
}| Option | Role |
|---|---|
segment | Segment of /ap/<segment>/<id> and prefix of acct:<segment>:<id>@<host> |
api | REST route of the type (plugins) — it is the nox/1.0 link of the JRD |
bare | Also answers a bare identifier (acct:cozy@host) — reserved to users |
The document itself is not written in the provider: the model produces it, through
sanitizeActivity(urls). The rule is the same for every type (Asset, Instance, User): the
model converts, the caller provides the bases to publish under.
Asset kinds
@AssetController applies both decorators on its own — its key is already the segment (w, m,
a) and its endpoint the REST route — and AssetControllerBase.resolveActivityPub() provides the
lookup. A new asset type is therefore federated without a single extra line.
The registry
FederationRegistry reads the decorators of every controller and provider (DiscoveryService),
once at boot, and indexes the types by segment:
[FederationRegistry] Federating: a, m, w, u (bare), iTwo classes claiming the same segment — or two bare identifiers — fail the boot: a mute type would be worse than a broken start.
The registry goes through discovery rather than injection: the asset, instance and user features
already depend on the federation, and an injection would close a cycle Nest resolves to
undefined at boot.