Skip to content

One Actor server

The one actor server is intended to provide an ActivityPub actor to run tests with (see here). For this, one runs the server via

python -mfediverse_pasture.one_actor --generate_config

Then one can use the actor from inside a python script via

from fediverse_pasture.one_actor import bovine_actor_and_actor_object

bovine_actor, actor = bovine_actor_and_actor_object("http://localhost:2917/")
await bovine_actor.init(session=session)

where session represents an aiohttp.ClientSession. Furthermore, one_actor exposes a page at / that displays objects received in the inbox. Due to the simplistic implementation, one can only open the page once, and the current content is lost on a refresh.

Command line usage

python -m fediverse_pasture.one_actor

Usage:

python -m fediverse_pasture.one_actor [OPTIONS]

Options:

  --only_generate_config
  --assets TEXT           Directory to serve assets from. Assets will be
                          served under /assets/
  --port INTEGER          port to run on
  --reload
  --help                  Show this message and exit.

The domain name the actor objects return is interfered from the host the request is made to. This means specifying the domain is only necessary, when using the actor in an external application.

Exported methods

fediverse_pasture.one_actor

bovine_actor_and_actor_object(domain)

Returns the BovineActor and the Actor object corresponding to the actor being served by one_actor

Parameters:

Name Type Description Default
domain str

The domain the actor lives on

required

Returns:

Type Description
Tuple[BovineActor, Actor]

BovineActor and Actor object

Source code in fediverse_pasture/one_actor.py
def bovine_actor_and_actor_object(domain: str) -> Tuple[BovineActor, Actor]:
    """Returns the BovineActor and the Actor object corresponding to the
    actor being served by one_actor

    :param domain: The domain the actor lives on
    :return: BovineActor and Actor object"""
    dp = DataProvider.load()

    actor_id = urljoin(domain, "/actor")

    return bovine_actor_for_actor_data(actor_id, dp.one_actor)

Serving static files

Using the --assets parameter, a directory can be specified to be served as static files. This is done using the method

fediverse_pasture.server.assets_blueprint_for_directory(directory)

Returns a Blueprint that serves the directory as static files. Files ending in .jsonap will be served with content-type application/activity+json.

Parameters:

Name Type Description Default
directory str

Directory to serve static files from

required
Source code in fediverse_pasture/server/__init__.py
def assets_blueprint_for_directory(directory: str) -> Blueprint:
    """Returns a [Blueprint][quart.blueprints.Blueprint] that serves the directory
    as static files. Files ending in `.jsonap` will be served with
    content-type `application/activity+json`.

    :param directory: Directory to serve static files from
    """

    blueprint = Blueprint("assets", __name__)

    @blueprint.get("/assets/<filename>")
    async def assets(filename):
        if filename.endswith(".jsonap"):
            return await send_from_directory(
                directory, filename, mimetype="application/activity+json"
            )
        return await send_from_directory(directory, filename)

    return blueprint