foxhole/app/activitypub.py

184 lines
4.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2023-03-18 04:57:20 +01:00
import httpx
2023-03-19 16:13:42 +01:00
import enum
2023-03-18 04:57:20 +01:00
from app import config
from app.key import get_pubkey_as_pem
2023-03-18 04:57:20 +01:00
from app.httpsig import auth
from loguru import logger
RawObject = dict
AS_CTX = "https://www.w3.org/ns/activitystreams"
AS_PUBLIC = "https://www.w3.org/ns/activitystreams#Public"
ACTOR_TYPES = ["Application", "Group", "Organization", "Person", "Service"]
AS_EXTENDED_CTX = [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
# AS ext
"Hashtag": "as:Hashtag",
"sensitive": "as:sensitive",
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"alsoKnownAs": {"@id": "as:alsoKnownAs", "@type": "@id"},
"movedTo": {"@id": "as:movedTo", "@type": "@id"},
# toot
"toot": "http://joinmastodon.org/ns#",
"featured": {"@id": "toot:featured", "@type": "@id"},
"Emoji": "toot:Emoji",
"blurhash": "toot:blurhash",
"votersCount": "toot:votersCount",
# schema
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value",
# ostatus
"ostatus": "http://ostatus.org#",
"conversation": "ostatus:conversation",
},
]
_LOCAL_ACTOR_SUMMARY = config.CONFIG.summary
ME = {
"@context": AS_EXTENDED_CTX,
"type": "Person",
"id": config.ID,
"following": config.BASE_URL + "/following",
"followers": config.BASE_URL + "/followers",
"featured": config.BASE_URL + "/featured",
"inbox": config.BASE_URL + "/inbox",
"outbox": config.BASE_URL + "/outbox",
"preferredUsername": config.USERNAME,
"name": config.CONFIG.name,
"summary": _LOCAL_ACTOR_SUMMARY,
"endpoints": {
# For compat with servers expecting a sharedInbox...
"sharedInbox": config.BASE_URL
+ "/inbox",
},
2023-03-29 05:14:13 +02:00
"url": config.ID + "/", # the path is important for Mastodon compat
"manuallyApprovesFollowers": config.CONFIG.manually_approves_followers,
2023-03-29 05:14:13 +02:00
"attachment": [], # TODO media support
"icon": {
2023-03-29 05:14:13 +02:00
"mediaType": "image/png", # TODO media support
"type": "Image",
"url": config.CONFIG.icon_url,
},
"publicKey": {
"id": f"{config.ID}#main-key",
"owner": config.ID,
"publicKeyPem": get_pubkey_as_pem(config.KEY_PATH),
},
2023-03-29 05:14:13 +02:00
"tag": [] # TODO tag support
}
2023-03-18 04:57:20 +01:00
2023-04-01 18:13:36 +02:00
class BaseActor:
2023-07-29 04:56:39 +02:00
def __init__(self, ap_actor: RawObject, **_) -> None:
2023-04-01 18:13:36 +02:00
if (ap_type := ap_actor.get("type")) not in ACTOR_TYPES:
raise ValueError(f"Unexpected actor type: {ap_type}")
self._ap_actor = ap_actor
self._ap_type : str = ap_type # type: ignore
@property
def ap_actor(self) -> RawObject:
return self._ap_actor
2023-04-07 05:47:13 +02:00
@property
def ap_id(self) -> RawObject:
return self._ap_actor["id"]
2023-04-01 18:13:36 +02:00
@property
def inbox_url(self) -> str:
return self.ap_actor["inbox"]
@property
def ap_type(self) -> str:
return self._ap_type
@property
def share_inbox_url(self) -> str:
return self.ap_actor.get("endpoints", {}).get("sharedInbox") \
or self.inbox_url
2023-03-19 16:13:42 +01:00
class VisibilityEnum(str, enum.Enum):
PUBLIC = "public"
UNLISTED = "unlisted"
FOLLOWERS_ONLY = "followers-only"
DIRECT = "direct"
2023-03-20 10:54:54 +01:00
def handle_visibility(
ap_object: dict
) -> VisibilityEnum:
to = ap_object.get("to", [])
cc = ap_object.get("cc", [])
if AS_PUBLIC in to:
return VisibilityEnum.PUBLIC
elif AS_PUBLIC in cc:
return VisibilityEnum.UNLISTED
else:
return VisibilityEnum.DIRECT
2023-04-01 18:13:36 +02:00
def wrap_ap_object(ap_object: dict) -> dict:
if ap_object["type"] in ["Note"]:
if "@context" in ap_object:
del ap_object["@context"]
return {
"@context": AS_EXTENDED_CTX,
"actor": config.ID,
"to": ap_object.get("to", []),
"cc": ap_object.get("cc", []),
"id": ap_object["id"] + "/activity",
"object": ap_object,
"published": ap_object["published"],
"type": "Create",
}
return ap_object
2023-03-18 04:57:20 +01:00
async def post(
url: str,
payload : dict,
) -> httpx.Response :
logger.info(f"send post, {payload=}")
async with httpx.AsyncClient() as client:
resp = await client.post(
url,
headers={
"User-Agent": config.USER_AGENT,
"Content-Type": config.AP_CONTENT_TYPE,
},
json=payload,
auth=auth,
)
2023-04-01 18:13:36 +02:00
resp.raise_for_status()
2023-03-18 04:57:20 +01:00
return resp
2023-03-18 13:02:38 +01:00
2023-03-20 10:54:54 +01:00
2023-03-18 13:02:38 +01:00
async def fetch(
url: str,
) -> dict:
logger.info(f"fetch {url}")
async with httpx.AsyncClient() as client:
resp = await client.get(
url,
headers={
"User-Agent": config.USER_AGENT,
"Accept": config.AP_CONTENT_TYPE,
},
)
return resp.json()