Skip to content

Message Processors Reference

Message processors are classes that intercept and modify ActivityPub messages during sending and receiving. They enable custom processing logic for federation messages.

Base Class

activitypub.message_processors.MessageProcessor

Built-in Processors

Actor Deletion Processor

activitypub.message_processors.ActorDeletionMessageProcessor

Bases: MessageProcessor

process_incoming(document)

Mastodon is constantly sending DELETE messages for all users who move/delete their accounts to all known network, even when we never even seen that actor before.

To avoid having to process the whole message, we will simply drop the message if it's a DELETE for an actor that we have no reference in our database.

If we do have the reference, then we might be interested in cleaning up properly.

JSON-LD Compaction Processor

activitypub.message_processors.CompactJsonLdMessageProcessor

Bases: MessageProcessor

process_outgoing(document)

Many Fediverse servers do not properly treat ActivityPub data as JSON-LD and expect attribute names without prefixes (e.g., "name" instead of "as:name").

With this processor we transform the compacted JSON-LD from outgoing messages (with prefixes like "as:name") to simple attribute names ("name").

Configuration

Message processors are configured in Django settings under FEDERATION['MESSAGE_PROCESSORS']. They are applied in order for both incoming and outgoing messages.

FEDERATION = {
    'MESSAGE_PROCESSORS': [
        'activitypub.message_processors.ActorDeletionMessageProcessor',
        'activitypub.message_processors.CompactJsonLdMessageProcessor',
        'myapp.processors.CustomProcessor',
    ]
}

Creating Custom Processors

Create a custom message processor by subclassing MessageProcessor:

from activitypub.message_processors import MessageProcessor

class CustomProcessor(MessageProcessor):
    def process_incoming(self, document):
        # Modify incoming messages
        if document.get('type') == 'Create':
            # Custom logic for Create activities
            pass
        return document

    def process_outgoing(self, document):
        # Modify outgoing messages
        if document.get('type') == 'Like':
            # Add custom metadata
            pass
        return document

Register your processor in Django settings to enable it.