Document Processors Reference
Document processors are classes that intercept and modify ActivityPub notifications during sending and receiving. They enable custom processing logic for federation notifications.
Base Class
activitypub.processors.DocumentProcessor
Built-in Processors
Actor Deletion Processor
activitypub.processors.ActorDeletionDocumentProcessor
Bases: DocumentProcessor
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.processors.CompactJsonLdDocumentProcessor
Bases: DocumentProcessor
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
Document processors are configured in Django settings under
FEDERATION['DOCUMENT_PROCESSORS']. They are applied in order for
both incoming and outgoing notifications.
FEDERATION = {
'DOCUMENT_PROCESSORS': [
'activitypub.processors.ActorDeletionDocumentProcessor',
'activitypub.processors.CompactJsonLdDocumentProcessor',
'myapp.processors.CustomProcessor',
]
}
Creating Custom Processors
Create a custom document processor by subclassing DocumentProcessor:
from activitypub.processors import DocumentProcessor
class CustomProcessor(DocumentProcessor):
def process_incoming(self, document):
# Modify incoming documents
if document.get('type') == 'Create':
# Custom logic for Create activities
pass
return document
def process_outgoing(self, document):
# Modify outgoing documents
if document.get('type') == 'Like':
# Add custom metadata
pass
return document
Register your processor in Django settings to enable it.