foxhole/app/activitypub.py

133 lines
3.5 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-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-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,
)
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()