Skip to content

ActivityPub

An ActivityPub model refers to all the django model classes that map to the Activity Streams/ActivityPub core types.

There are two primary base types for ActivityPub: Object and Link. Considering that all the other types (Activity, Actor, Collection) are subtypes of Object, we have the following Django model classes:

CoreType

At the root we have the "CoreType" model. This model contains nothing but the internal identifier for the resource - i.e, not its URI. This class is defined to be able to represent relationships to objects that can be represented by either one of Object or Link

BaseActivityStreamsObject

This model is not abstract, but it is used mostly to allow us to define the subtypes of Object. You won't be using this class directly most of the time.

Object

The type attribute is one choice defined by the Object.Types enum class, where each value map to the Activity Streams types (e.g, Note, Image, Video, Question, etc...).

>>> from activitypub.models import Object

>>> my_note = Object(type=Object.Types.NOTE, content='Writing good documentation is an art that I am yet to master')
>>> my_note.save()

>>> print(my_note.type)

https://www.w3.org/ns/activitystreams#Note

Actor

Actors are the entities that carry out activities. They also are a subtype of BaseActivityStreamsObject and can be of type Person,Group, Service, Application, etc.

>>> from activitypub.models import Actor

>>> my_bot = Actor.objects.filter(type=Actor.Types.SERVICE).first()

>>> print(my_bot.name)

'My bot'

Activity

Activities are the actions executed by the actors. They are also a subtype of BaseActivityStreamsObject and its types map to all the possible types of activities (eg. Follow, Create, Delete, Accept, etc...)

>>> from activitypub.models import Activity


activity = Activity.objects.create(actor=my_bot, type=Activity.Types.CREATE, object=my_note)

Collection

Collections are containers for other Object or Link resources. They can be of type Collection or OrderedCollection. The collection has a many-to-many relationship to CollectionItem.

Ordering of the collection is defined by the ordering_method attribute, which is one of the choices from the OrderingMethods Enum class.

  • OrderingMethods.NONE (collection is not ordered, no guarantee about order)
  • OrderingMethods.CREATE_TIME (collection is ordered by creation time)
  • OrderingMethods.KEY (collection is ordered by the "order" value of the collection item)

A Link describes a qualified, indirect reference to another CoreType record.

Reference

A Reference model is used to store URIs. Any instance of BaseActivityStreamsObject may contain its reference.

Having reference records separate from its models allows us to process JSON-LD documents that have no real information about the object except its URI. A reference with a valid URI can be fetched and resolved to its underlying object.

Cryptographic Key Pair

The keypair is provided by any actor that needs to authenticate the messages that it sends. Keys are usually referenced by its "key id"

Actors that are controlled by the server application (i.e, its URI is from a local Domain can have keypairs generated by the server.