Posting Status

Currently minimal_activitypub only implements standard status posting (polls and direct messages / DMs are not currently implemented).

Posting text only status

The sample code below shows a very simple text only status being posted.

1from minimal_activitypub.client_2_server import ActivityPub
2from minimal_activitypub import Status
3
4async def post_status(instance_api: ActivityPub, status: str) -> Status:
5
6   posted_status = await instance_api.post_status(status=status)

Posting status that includes picture

The sample code below shows how to post a status with images attached. Basically the images/media needs to be posted first and the media id(s) need to be included when posting the status itself.

 1import aiofiles
 2import magic
 3from minimal_activitypub.client_2_server import ActivityPub
 4from minimal_activitypub import Status
 5
 6async def post_status(instance_api: ActivityPub, status: str, image_path: str) -> Dict[str, Any]:
 7
 8   # First determine mime-type
 9   mime_type = magic.from_file(image_path, mime=True)
10
11   # Post media
12   async with aiofiles.open(image_path, "rb") as image_file:
13      posted_media = await instance_api.post_media(file=image_file, mime_type=mime_type)
14
15   # Determine media id to include when posting status
16   media_id = posted_media["id"]
17
18   # Post actual status and include image
19   posted_status = await instance_api.post_status(status=status, media_ids=[media_id])

Method Signatures

Following are the method signatures used in the above examples.

class minimal_activitypub.client_2_server.ActivityPub

Simplifies interacting with an ActivityPub server / instance.

This is a minimal implementation only implementing methods needed for the function of MastodonAmnesia

__init__(instance, client, access_token=None)

Initialise ActivityPub instance with reasonable default values.

Parameters:
  • instance (str) – domain name or url to instance to connect to

  • client (AsyncClient) – httpx AsyncClient to use for communicating with instance

  • access_token (str | None) – authentication token

  • self (ActivityPubClass)

Return type:

None

__new__(**kwargs)
async post_media(file, mime_type, description=None, focus=None)

Post a media file (image or video).

Parameters:
  • file (BinaryIO) – The file to be uploaded

  • mime_type (str) – Mime type

  • description (str | None) – A plain-text description of the media, for accessibility purposes

  • focus (Tuple[float, float] | None) – Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0 (see “Focal points <https://docs.joinmastodon.org/methods/statuses/media/#focal-points>”_)

  • self (ActivityPubClass)

Returns:

Dict containing details for this media on server, such a id, url etc

Return type:

Any

async post_status(status, visibility=Visibility.PUBLIC, media_ids=None, sensitive=False, spoiler_text=None)

Post a status to the fediverse.

Parameters:
  • status (str) – The text to be posted on the timeline.

  • visibility (Visibility) – Visibility of the posted status. Enumerable one of public, unlisted, private, or direct. Defaults to public

  • media_ids (List[str] | None) – List of ids for media (pictures, videos, etc) to be attached to this post. Can be None if no media is to be attached. Defaults to None

  • sensitive (bool) – Set to true the post is of a sensitive nature and should be marked as such. For example overly political or explicit material is often marked as sensitive. Applies particularly to attached media. Defaults to False

  • spoiler_text (str | None) – Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field. Defaults to None

  • self (ActivityPubClass)

Returns:

Dict of the status just posted.

Return type:

Dict[str, Any]