Skip to content

bovine.activitystreams.utils

bovine.activitystreams.utils

uris_for_public module-attribute

uris_for_public = [
    "Public",
    "as:Public",
    "https://www.w3.org/ns/activitystreams#Public",
]

URIs used to represent the public collection. See Section 5.6 of ActivityPub.

actor_for_object

actor_for_object(data: dict) -> str

Look up for the actor id either from attributedTo or actor

Source code in bovine/bovine/activitystreams/utils/__init__.py
def actor_for_object(data: dict) -> str:
    """Look up for the actor id either from attributedTo or actor"""
    if "attributedTo" in data:
        actor = data.get("attributedTo")
    else:
        actor = data.get("actor")
    actor = id_for_object(actor)

    if actor:
        return actor

    return "__NO__ACTOR__"

as_list

as_list(value: dict | list | str) -> list

Converts a value to a list by enclosing it

>>> as_list("http://example.com")
['http://example.com']

>>> as_list(["http://example.com"])
['http://example.com']
Source code in bovine/bovine/activitystreams/utils/__init__.py
def as_list(value: dict | list | str) -> list:
    """Converts a value to a list by enclosing it

    ```pycon
    >>> as_list("http://example.com")
    ['http://example.com']

    >>> as_list(["http://example.com"])
    ['http://example.com']

    ```

    """

    if isinstance(value, list):
        return value

    return [value]

contains_public

contains_public(array: list[str]) -> bool

Checks if the list contains public

>>> contains_public(["Public"])
True

>>> contains_public(["http://alice.example"])
False
Source code in bovine/bovine/activitystreams/utils/__init__.py
def contains_public(array: list[str]) -> bool:
    """Checks if the list contains public

    ```pycon
    >>> contains_public(["Public"])
    True

    >>> contains_public(["http://alice.example"])
    False

    ```
    """

    return any(x in uris_for_public for x in array)

copy_to_and_cc

copy_to_and_cc(origin: dict, destination: dict) -> dict

Copies the audience from the origin object to the destination object

Source code in bovine/bovine/activitystreams/utils/__init__.py
def copy_to_and_cc(origin: dict, destination: dict) -> dict:
    """Copies the audience from the origin object to the destination object"""

    for key in ["to", "cc", "bto", "bcc", "audience"]:
        if key in origin:
            destination[key] = list(
                property_for_key_as_set(origin, key)
                | property_for_key_as_set(destination, key)
            )

    return destination

fediverse_handle_from_actor

fediverse_handle_from_actor(actor: dict) -> str

Given an actor object, i.e. a dict, determines the fediverse handle

Source code in bovine/bovine/activitystreams/utils/__init__.py
def fediverse_handle_from_actor(actor: dict) -> str:
    """Given an actor object, i.e. a dict, determines the fediverse handle"""
    host = urlparse(actor["id"]).netloc
    username = urlparse(actor["id"]).path.split("/")[-1]

    if "preferredUsername" in actor:
        username = actor["preferredUsername"]

    return f"{username}@{host}"

id_for_object

id_for_object(data: Optional[dict | str]) -> Optional[str]

Determines the id of an object

>>> id_for_object("http://obj.example")
'http://obj.example'

>>> id_for_object({"id": "http://obj.example"})
'http://obj.example'

>>> id_for_object({"name": "alice"}) is None
True
Source code in bovine/bovine/activitystreams/utils/__init__.py
def id_for_object(data: Optional[dict | str]) -> Optional[str]:
    """Determines the id of an object

    ```pycon
    >>> id_for_object("http://obj.example")
    'http://obj.example'

    >>> id_for_object({"id": "http://obj.example"})
    'http://obj.example'

    >>> id_for_object({"name": "alice"}) is None
    True

    ```
    """

    if data is None:
        return None
    if isinstance(data, str):
        return data
    return data.get("id", None)

is_public

is_public(data: dict) -> bool

Determines if the object should be considered public based on its recipients

Source code in bovine/bovine/activitystreams/utils/__init__.py
def is_public(data: dict) -> bool:
    """Determines if the object should be considered public based on its recipients"""
    return contains_public(recipients_for_object(data))

property_for_key_as_set

property_for_key_as_set(data, key)

Returns value as a set, useful for to and cc

>>> property_for_key_as_set({"to": "http://actor.example"}, "to")
{'http://actor.example'}
Source code in bovine/bovine/activitystreams/utils/__init__.py
def property_for_key_as_set(data, key):
    """Returns value as a set, useful for `to` and `cc`

    ```pycon
    >>> property_for_key_as_set({"to": "http://actor.example"}, "to")
    {'http://actor.example'}

    ```
    """
    if data is None:
        return set()
    result = data.get(key, [])
    if isinstance(result, str):
        return set([result])
    return set(result)

recipients_for_object

recipients_for_object(data: dict) -> set

Combines the recipients from to, cc, bto, bcc, audience into a set

>>> result = recipients_for_object({
...     "to": ["http://to.example"],
...     "cc": "http://cc.example",
...     "bcc": ["http://bcc.example"],
...     "audience": "http://audience.example"})
>>> sorted(result)
['http://audience.example', 'http://bcc.example',
    'http://cc.example', 'http://to.example']

Note

treatment of audience might change.

Source code in bovine/bovine/activitystreams/utils/__init__.py
def recipients_for_object(data: dict) -> set:
    """Combines the recipients from to, cc, bto, bcc, audience into a set

    ```pycon
    >>> result = recipients_for_object({
    ...     "to": ["http://to.example"],
    ...     "cc": "http://cc.example",
    ...     "bcc": ["http://bcc.example"],
    ...     "audience": "http://audience.example"})
    >>> sorted(result)
    ['http://audience.example', 'http://bcc.example',
        'http://cc.example', 'http://to.example']


    ```

    !!! note
        treatment of audience might change.

    """
    return set.union(
        *[
            property_for_key_as_set(data, key)
            for key in ["to", "cc", "bto", "bcc", "audience"]
        ]
    )

remove_public

remove_public(recipients)

Given a list of Recipients removes public

>>> remove_public(["Public", "as:Public",
...     "https://www.w3.org/ns/activitystreams#Public",
...     "http://alice.example"])
{'http://alice.example'}
Source code in bovine/bovine/activitystreams/utils/__init__.py
def remove_public(recipients):
    """Given a list of Recipients removes public

    ```pycon
    >>> remove_public(["Public", "as:Public",
    ...     "https://www.w3.org/ns/activitystreams#Public",
    ...     "http://alice.example"])
    {'http://alice.example'}

    ```
    """

    return {x for x in recipients if x not in uris_for_public}