cattle_grid.extensions.examples.simple_storage
This extension is an example of storing activities and then serving them through a HTTP API.
I will possibly extend it to also store objects
(and provide some Create
activity creation), but
not much more.
A real storage mechanism should have several features this simple API has not, e.g.
- Serving a HTML page through content negotiation
- Allowing one to update the content of the database
- Collecting and adding metadata, e.g. a replies collection for objects
Usage:
[[extensions]]
module = "cattle_grid.extensions.examples.simple_storage"
api_prefix = "/simple_storage"
config = { prefix = "/simple_storage/" }
extension = Extension(name='simple storage', module=__name__, lifespan=lifespan, config_class=SimpleStorageConfiguration)
module-attribute
The simple storage extension
get_activity(uuid, headers, session)
async
Returns the activity or object
Source code in cattle_grid/extensions/examples/simple_storage/__init__.py
main()
async
Basic endpoint that just returns a string, so requesting with an uuid doesn’t return an error.
simple_storage_publish_activity(msg, config, session, broker=Context())
async
Method to subscribe to the publish_activity
routing_key.
An activity send to this endpoint will be stored in the
database, and then published through the send_message
mechanism.
The stored activity can then be retrieved through the HTTP API.
Source code in cattle_grid/extensions/examples/simple_storage/__init__.py
simple_storage_publish_object(msg, config, session, broker=Context())
async
Publishes an object, subscribed to the routing key
publish_object
.
We note that this routine creates a Create
activity for the object.
Source code in cattle_grid/extensions/examples/simple_storage/__init__.py
config
SimpleStorageConfiguration
Bases: BaseModel
Configuration of the simple storage extension
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefix
|
str
|
|
'/simple_storage/'
|
Source code in cattle_grid/extensions/examples/simple_storage/config.py
prefix = '/simple_storage/'
class-attribute
instance-attribute
Path to use before the generated uuid. The protocol and domain will be extracted from the actor id. See cattle_grid.extensions.examples.simple_storage.config.determine_url_start.
determine_url_start(actor_id, prefix)
Used to determine the url of a stored object
>>> determine_url_start("http://abel.example/actor/alice",
... "/simple/storage/")
'http://abel.example/simple/storage/'
Source code in cattle_grid/extensions/examples/simple_storage/config.py
database
CommittingSession = Annotated[AsyncSession, Depends(with_session_commit)]
module-attribute
Session that commits the transaction
FastApiSession = Annotated[AsyncSession, FADepends(with_fast_api_session)]
module-attribute
Session annotation to be used with FastAPI
lifespan(engine)
async
The lifespan ensure that the necessary database table is created.
Source code in cattle_grid/extensions/examples/simple_storage/database.py
message_types
PublishActivity
Bases: BaseModel
Used when publishing an activity
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actor
|
str
|
|
required |
data
|
dict
|
|
required |
Source code in cattle_grid/extensions/examples/simple_storage/message_types.py
actor = Field(examples=['http://alice.example'])
class-attribute
instance-attribute
The actor performing the activity
data = Field(examples=[{'@context': 'https://www.w3.org/ns/activitystreams', 'type': 'AnimalSound', 'actor': 'http://alice.example', 'to': ['http://bob.example'], 'content': 'moo'}])
class-attribute
instance-attribute
Activity to publish
PublishObject
Bases: BaseModel
Used when publishing an object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actor
|
str
|
|
required |
data
|
dict
|
|
required |
Source code in cattle_grid/extensions/examples/simple_storage/message_types.py
actor = Field(examples=['http://alice.example'])
class-attribute
instance-attribute
The actor performing the activity
data = Field(examples=[{'@context': 'https://www.w3.org/ns/activitystreams', 'type': 'Note', 'attributedTo': 'http://alice.example', 'to': ['http://bob.example'], 'content': 'moo'}])
class-attribute
instance-attribute
Object to publish
models
StoredActivity
Bases: Base
Stored activity in the database
Source code in cattle_grid/extensions/examples/simple_storage/models.py
__tablename__ = 'simple_storage_stored_activity'
class-attribute
instance-attribute
name of the table
actor = mapped_column()
class-attribute
instance-attribute
The actor that created the activity
create_date = mapped_column(server_default=func.now())
class-attribute
instance-attribute
The create timestamp
data = mapped_column(JSON)
class-attribute
instance-attribute
The activity as JSON
id = mapped_column(UUIDType(binary=True), primary_key=True)
class-attribute
instance-attribute
The id (uuid as bytes)
StoredObject
Bases: Base
Stored object in the database
Source code in cattle_grid/extensions/examples/simple_storage/models.py
__tablename__ = 'simple_storage_stored_object'
class-attribute
instance-attribute
name of the table
actor = mapped_column()
class-attribute
instance-attribute
The actor that created the object
create_date = mapped_column(server_default=func.now())
class-attribute
instance-attribute
The create timestamp
data = mapped_column(JSON)
class-attribute
instance-attribute
The object as JSON
id = mapped_column(UUIDType(binary=True), primary_key=True)
class-attribute
instance-attribute
The id (uuid as bytes)
test_api
create_actor(base_url, preferred_username=None, identifiers={}, profile={})
async
Creates a new actor in the database
Source code in cattle_grid/activity_pub/actor.py
database()
async
new_auth_config(actor_id, username=None)
Creates a new authorization configuration
Source code in cattle_grid/config/auth.py
save_auth_config(filename, config)
Saves the authorization configuration to a file
test_actor()
async
with_database(db_uri='sqlite://:memory:', generate_schemas=False)
async
Opens the connection to the database using tortoise
Source code in cattle_grid/database.py
test_handlers
create_actor(base_url, preferred_username=None, identifiers={}, profile={})
async
Creates a new actor in the database
Source code in cattle_grid/activity_pub/actor.py
database()
async
new_auth_config(actor_id, username=None)
Creates a new authorization configuration
Source code in cattle_grid/config/auth.py
save_auth_config(filename, config)
Saves the authorization configuration to a file
test_actor()
async
with_database(db_uri='sqlite://:memory:', generate_schemas=False)
async
Opens the connection to the database using tortoise