Skip to content

bovine.activitystreams.utils

actor_for_object(data)

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__"

copy_to_and_cc(origin, destination)

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(actor)

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(data)

Determines the id of an object

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"""

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

is_public(data)

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"""
    recipients = recipients_for_object(data)

    return any(
        x in recipients
        for x in ["Public", "as:Public", "https://www.w3.org/ns/activitystreams#Public"]
    )

recipients_for_object(data)

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

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"""
    return set.union(
        *[
            property_for_key_as_set(data, key)
            for key in ["to", "cc", "bto", "bcc", "audience"]
        ]
    )