Reformatted code base using black.

This commit is contained in:
Goffi 2024-06-19 18:44:57 +02:00
commit 53fdaa9d09
7 changed files with 12604 additions and 8966 deletions

File diff suppressed because it is too large Load diff

View file

@ -29,6 +29,7 @@ from libervia.backend.core.log import getLogger
log = getLogger(__name__)
NS_XMPP_JID_NODE_2_AP = "https://libervia.org/ap_gateway/xmpp_jid_node_2_ap_actor"
class APAdHocService:
"""Ad-Hoc commands for AP Gateway"""
@ -52,7 +53,7 @@ class APAdHocService:
command_elt: domish.Element,
session_data: dict,
action: str,
node: str
node: str,
):
try:
x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
@ -63,18 +64,15 @@ class APAdHocService:
# root request
status = self._c.STATUS.EXECUTING
form = data_form.Form(
"form", title="XMPP JID/node to AP actor conversion",
formNamespace=NS_XMPP_JID_NODE_2_AP
"form",
title="XMPP JID/node to AP actor conversion",
formNamespace=NS_XMPP_JID_NODE_2_AP,
)
field = data_form.Field(
"text-single", "jid", required=True
)
field = data_form.Field("text-single", "jid", required=True)
form.addField(field)
field = data_form.Field(
"text-single", "node", required=False
)
field = data_form.Field("text-single", "node", required=False)
form.addField(field)
payload = form.toElement()

View file

@ -55,32 +55,71 @@ SIGN_HEADERS = {
("digest", "(request-target)"),
],
b"GET": ["host"],
b"POST": ["digest"]
b"POST": ["digest"],
}
PAGE_SIZE = 10
HS2019 = "hs2019"
# delay after which a signed request is not accepted anymore
SIGN_EXP = 12*60*60 # 12 hours (same value as for Mastodon)
SIGN_EXP = 12 * 60 * 60 # 12 hours (same value as for Mastodon)
LRU_MAX_SIZE = 200
ACTIVITY_TYPES = (
"Accept", "Add", "Announce", "Arrive", "Block", "Create", "Delete", "Dislike", "Flag",
"Follow", "Ignore", "Invite", "Join", "Leave", "Like", "Listen", "Move", "Offer",
"Question", "Reject", "Read", "Remove", "TentativeReject", "TentativeAccept",
"Travel", "Undo", "Update", "View",
"Accept",
"Add",
"Announce",
"Arrive",
"Block",
"Create",
"Delete",
"Dislike",
"Flag",
"Follow",
"Ignore",
"Invite",
"Join",
"Leave",
"Like",
"Listen",
"Move",
"Offer",
"Question",
"Reject",
"Read",
"Remove",
"TentativeReject",
"TentativeAccept",
"Travel",
"Undo",
"Update",
"View",
# non-standard activities
"EmojiReact"
"EmojiReact",
)
ACTIVITY_TYPES_LOWER = [a.lower() for a in ACTIVITY_TYPES]
ACTIVITY_OBJECT_MANDATORY = (
"Create", "Update", "Delete", "Follow", "Add", "Remove", "Like", "Block", "Undo"
"Create",
"Update",
"Delete",
"Follow",
"Add",
"Remove",
"Like",
"Block",
"Undo",
)
ACTIVITY_TARGET_MANDATORY = ("Add", "Remove")
# activities which can be used with Shared Inbox (i.e. with no account specified)
# must be lowercase
ACTIVIY_NO_ACCOUNT_ALLOWED = (
"create", "update", "delete", "announce", "undo", "like", "emojireact", "join",
"leave"
"create",
"update",
"delete",
"announce",
"undo",
"like",
"emojireact",
"join",
"leave",
)
# maximum number of parents to retrieve when comments_max_depth option is set
COMMENTS_MAX_PARENTS = 100

View file

@ -45,11 +45,12 @@ AP_EVENTS_CONTEXT = {
"addressCountry": "sc:addressCountry",
"addressLocality": "sc:addressLocality",
"addressRegion": "sc:addressRegion",
"anonymousParticipationEnabled": {"@id": "mz:anonymousParticipationEnabled",
"@type": "sc:Boolean"},
"anonymousParticipationEnabled": {
"@id": "mz:anonymousParticipationEnabled",
"@type": "sc:Boolean",
},
"category": "sc:category",
"commentsEnabled": {"@id": "pt:commentsEnabled",
"@type": "sc:Boolean"},
"commentsEnabled": {"@id": "pt:commentsEnabled", "@type": "sc:Boolean"},
"discoverable": "toot:discoverable",
"discussions": {"@id": "mz:discussions", "@type": "@id"},
"events": {"@id": "mz:events", "@type": "@id"},
@ -57,8 +58,7 @@ AP_EVENTS_CONTEXT = {
"inLanguage": "sc:inLanguage",
"isOnline": {"@id": "mz:isOnline", "@type": "sc:Boolean"},
"joinMode": {"@id": "mz:joinMode", "@type": "mz:joinModeType"},
"joinModeType": {"@id": "mz:joinModeType",
"@type": "rdfs:Class"},
"joinModeType": {"@id": "mz:joinModeType", "@type": "rdfs:Class"},
"location": {"@id": "sc:location", "@type": "sc:Place"},
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"maximumAttendeeCapacity": "sc:maximumAttendeeCapacity",
@ -66,19 +66,21 @@ AP_EVENTS_CONTEXT = {
"members": {"@id": "mz:members", "@type": "@id"},
"mz": "https://joinmobilizon.org/ns#",
"openness": {"@id": "mz:openness", "@type": "@id"},
"participantCount": {"@id": "mz:participantCount",
"@type": "sc:Integer"},
"participationMessage": {"@id": "mz:participationMessage",
"@type": "sc:Text"},
"participantCount": {"@id": "mz:participantCount", "@type": "sc:Integer"},
"participationMessage": {"@id": "mz:participationMessage", "@type": "sc:Text"},
"postalCode": "sc:postalCode",
"posts": {"@id": "mz:posts", "@type": "@id"},
"propertyID": "sc:propertyID",
"pt": "https://joinpeertube.org/ns#",
"remainingAttendeeCapacity": "sc:remainingAttendeeCapacity",
"repliesModerationOption": {"@id": "mz:repliesModerationOption",
"@type": "mz:repliesModerationOptionType"},
"repliesModerationOptionType": {"@id": "mz:repliesModerationOptionType",
"@type": "rdfs:Class"},
"repliesModerationOption": {
"@id": "mz:repliesModerationOption",
"@type": "mz:repliesModerationOptionType",
},
"repliesModerationOptionType": {
"@id": "mz:repliesModerationOptionType",
"@type": "rdfs:Class",
},
"resources": {"@id": "mz:resources", "@type": "@id"},
"sc": "http://schema.org#",
"streetAddress": "sc:streetAddress",
@ -86,7 +88,7 @@ AP_EVENTS_CONTEXT = {
"todos": {"@id": "mz:todos", "@type": "@id"},
"toot": "http://joinmastodon.org/ns#",
"uuid": "sc:identifier",
"value": "sc:value"
"value": "sc:value",
}
@ -99,7 +101,7 @@ class APEvents:
self._events = self.host.plugins["XEP-0471"]
async def event_data_2_ap_item(
self, event_data: dict, author_jid: jid.JID, is_new: bool=True
self, event_data: dict, author_jid: jid.JID, is_new: bool = True
) -> dict:
"""Convert event data to AP activity
@ -114,8 +116,7 @@ class APEvents:
if not event_data.get("id"):
event_data["id"] = shortuuid.uuid()
ap_account = await self.apg.get_ap_account_from_jid_and_node(
author_jid,
self._events.namespace
author_jid, self._events.namespace
)
url_actor = self.apg.build_apurl(TYPE_ACTOR, ap_account)
url_item = self.apg.build_apurl(TYPE_ITEM, ap_account, event_data["id"])
@ -141,12 +142,14 @@ class APEvents:
pass
else:
media_type = mimetypes.guess_type(head_picture_url, False)[0] or "image/jpeg"
attachment.append({
"name": "Banner",
"type": "Document",
"mediaType": media_type,
"url": head_picture_url,
})
attachment.append(
{
"name": "Banner",
"type": "Document",
"mediaType": media_type,
"url": head_picture_url,
}
)
descriptions = event_data.get("descriptions")
if descriptions:
@ -162,10 +165,12 @@ class APEvents:
if categories:
tag = ap_object["tag"] = []
for category in categories:
tag.append({
"name": f"#{category['term']}",
"type": "Hashtag",
})
tag.append(
{
"name": f"#{category['term']}",
"type": "Hashtag",
}
)
locations = event_data.get("locations")
if locations:
@ -203,12 +208,14 @@ class APEvents:
website = extra.get("website")
if website:
attachment.append({
"href": website,
"mediaType": "text/html",
"name": "Website",
"type": "Link"
})
attachment.append(
{
"href": website,
"mediaType": "text/html",
"name": "Website",
"type": "Link",
}
)
accessibility = extra.get("accessibility")
if accessibility:
@ -224,11 +231,13 @@ class APEvents:
log.error(f"unexpected wheelchair value: {wheelchair}")
ap_wc_value = None
if ap_wc_value is not None:
attachment.append({
"propertyID": "mz:accessibility:wheelchairAccessible",
"type": "PropertyValue",
"value": ap_wc_value
})
attachment.append(
{
"propertyID": "mz:accessibility:wheelchairAccessible",
"type": "PropertyValue",
"value": ap_wc_value,
}
)
activity = self.apg.create_activity(
"Create" if is_new else "Update", url_actor, ap_object, activity_id=url_item
@ -236,11 +245,7 @@ class APEvents:
activity["@context"].append(AP_EVENTS_CONTEXT)
return activity
async def ap_item_2_event_data(
self,
requestor_actor_id: str,
ap_item: dict
) -> dict:
async def ap_item_2_event_data(self, requestor_actor_id: str, ap_item: dict) -> dict:
"""Convert AP activity or object to event data
@param requestor_actor_id: ID of the actor doing the request.
@ -251,7 +256,9 @@ class APEvents:
"""
is_activity = self.apg.is_activity(ap_item)
if is_activity:
ap_object = await self.apg.ap_get_object(requestor_actor_id, ap_item, "object")
ap_object = await self.apg.ap_get_object(
requestor_actor_id, ap_item, "object"
)
if not ap_object:
log.warning(f'No "object" found in AP item {ap_item!r}')
raise exceptions.DataError
@ -311,10 +318,7 @@ class APEvents:
# description
content = ap_object.get("content")
if content:
event_data["descriptions"] = [{
"type": "xhtml",
"description": content
}]
event_data["descriptions"] = [{"type": "xhtml", "description": content}]
# categories
tags = ap_object.get("tag")
@ -329,7 +333,7 @@ class APEvents:
continue
categories.append({"term": term})
#location
# location
ap_location = ap_object.get("location")
if ap_location:
location = {}
@ -361,24 +365,24 @@ class APEvents:
# a simple "yes"/"no" form.
rsvp_data = {"fields": []}
event_data["rsvp"] = [rsvp_data]
rsvp_data["fields"].append({
"type": "list-single",
"name": "attending",
"label": "Attending",
"options": [
{"label": "yes", "value": "yes"},
{"label": "no", "value": "no"}
],
"required": True
})
rsvp_data["fields"].append(
{
"type": "list-single",
"name": "attending",
"label": "Attending",
"options": [
{"label": "yes", "value": "yes"},
{"label": "no", "value": "no"},
],
"required": True,
}
)
# comments
if ap_object.get("commentsEnabled"):
__, comments_node = await self.apg.get_comments_nodes(
requestor_actor_id,
object_id,
None
requestor_actor_id, object_id, None
)
event_data["comments"] = {
"service": author_jid,
@ -399,15 +403,10 @@ class APEvents:
return event_data
async def ap_item_2_event_data_and_elt(
self,
requestor_actor_id: str,
ap_item: dict
self, requestor_actor_id: str, ap_item: dict
) -> tuple[dict, domish.Element]:
"""Convert AP item to parsed event data and corresponding item element"""
event_data = await self.ap_item_2_event_data(
requestor_actor_id,
ap_item
)
event_data = await self.ap_item_2_event_data(requestor_actor_id, ap_item)
event_elt = self._events.event_data_2_event_elt(event_data)
item_elt = domish.Element((None, "item"))
item_elt["id"] = event_data["id"]
@ -415,13 +414,10 @@ class APEvents:
return event_data, item_elt
async def ap_item_2_event_elt(
self,
requestor_actor_id: str,
ap_item: dict
self, requestor_actor_id: str, ap_item: dict
) -> domish.Element:
"""Convert AP item to XMPP item element"""
__, item_elt = await self.ap_item_2_event_data_and_elt(
requestor_actor_id,
ap_item
requestor_actor_id, ap_item
)
return item_elt

View file

@ -41,11 +41,29 @@ from libervia.backend.tools.common import date_utils, uri
from libervia.backend.memory.sqla_mapping import SubscriptionState
from .constants import (
NS_AP, MEDIA_TYPE_AP, MEDIA_TYPE_AP_ALT, CONTENT_TYPE_WEBFINGER, CONTENT_TYPE_AP,
TYPE_ACTOR, TYPE_INBOX, TYPE_SHARED_INBOX, TYPE_OUTBOX, TYPE_EVENT, AP_REQUEST_TYPES,
PAGE_SIZE, ACTIVITY_TYPES_LOWER, ACTIVIY_NO_ACCOUNT_ALLOWED, SIGN_HEADERS, HS2019,
SIGN_EXP, TYPE_FOLLOWERS, TYPE_FOLLOWING, TYPE_ITEM, TYPE_LIKE, TYPE_REACTION,
ST_AP_CACHE
NS_AP,
MEDIA_TYPE_AP,
MEDIA_TYPE_AP_ALT,
CONTENT_TYPE_WEBFINGER,
CONTENT_TYPE_AP,
TYPE_ACTOR,
TYPE_INBOX,
TYPE_SHARED_INBOX,
TYPE_OUTBOX,
TYPE_EVENT,
AP_REQUEST_TYPES,
PAGE_SIZE,
ACTIVITY_TYPES_LOWER,
ACTIVIY_NO_ACCOUNT_ALLOWED,
SIGN_HEADERS,
HS2019,
SIGN_EXP,
TYPE_FOLLOWERS,
TYPE_FOLLOWING,
TYPE_ITEM,
TYPE_LIKE,
TYPE_REACTION,
ST_AP_CACHE,
)
from .regex import RE_SIG_PARAM
@ -53,13 +71,13 @@ from .regex import RE_SIG_PARAM
log = getLogger(__name__)
VERSION = unicodedata.normalize(
'NFKD',
f"{C.APP_NAME} ActivityPub Gateway {C.APP_VERSION}"
"NFKD", f"{C.APP_NAME} ActivityPub Gateway {C.APP_VERSION}"
)
class HTTPAPGServer(web_resource.Resource):
"""HTTP Server handling ActivityPub S2S protocol"""
isLeaf = True
def __init__(self, ap_gateway):
@ -68,30 +86,23 @@ class HTTPAPGServer(web_resource.Resource):
super().__init__()
def response_code(
self,
request: "HTTPRequest",
http_code: int,
msg: Optional[str] = None
self, request: "HTTPRequest", http_code: int, msg: Optional[str] = None
) -> None:
"""Log and set HTTP return code and associated message"""
if msg is not None:
log.warning(msg)
request.setResponseCode(http_code, None if msg is None else msg.encode())
def _on_request_error(self, failure_: failure.Failure, request: "HTTPRequest") -> None:
def _on_request_error(
self, failure_: failure.Failure, request: "HTTPRequest"
) -> None:
exc = failure_.value
if isinstance(exc, exceptions.NotFound):
self.response_code(
request,
http.NOT_FOUND,
str(exc)
)
self.response_code(request, http.NOT_FOUND, str(exc))
else:
log.exception(f"Internal error: {failure_.value}")
self.response_code(
request,
http.INTERNAL_SERVER_ERROR,
f"internal error: {failure_.value}"
request, http.INTERNAL_SERVER_ERROR, f"internal error: {failure_.value}"
)
request.finish()
raise failure_
@ -105,7 +116,7 @@ class HTTPAPGServer(web_resource.Resource):
account = resource[5:].strip()
if not resource.startswith("acct:") or not account:
return web_resource.ErrorPage(
http.BAD_REQUEST, "Bad Request" , "Invalid webfinger resource"
http.BAD_REQUEST, "Bad Request", "Invalid webfinger resource"
).render(request)
actor_url = self.apg.build_apurl(TYPE_ACTOR, account)
@ -114,12 +125,8 @@ class HTTPAPGServer(web_resource.Resource):
"aliases": [actor_url],
"subject": resource,
"links": [
{
"rel": "self",
"type": "application/activity+json",
"href": actor_url
}
]
{"rel": "self", "type": "application/activity+json", "href": actor_url}
],
}
request.setHeader("content-type", CONTENT_TYPE_WEBFINGER)
request.write(json.dumps(resp).encode())
@ -134,7 +141,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
if node is None:
node = self.apg._m.namespace
@ -181,9 +188,11 @@ class HTTPAPGServer(web_resource.Resource):
elif type_ == TYPE_LIKE:
await self.handle_attachment_item(client, obj, {"noticed": False})
elif type_ == TYPE_REACTION:
await self.handle_attachment_item(client, obj, {
"reactions": {"operation": "update", "remove": [obj["content"]]}
})
await self.handle_attachment_item(
client,
obj,
{"reactions": {"operation": "update", "remove": [obj["content"]]}},
)
else:
log.warning(f"Unmanaged undo type: {type_!r}")
@ -196,7 +205,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
if node is None:
node = self.apg._m.namespace
@ -207,7 +216,7 @@ class HTTPAPGServer(web_resource.Resource):
account_jid,
node,
# subscriptions from AP are always public
options=self.apg._pps.set_public_opt()
options=self.apg._pps.set_public_opt(),
)
except pubsub.SubscriptionPending:
log.info(f"subscription to node {node!r} of {account_jid} is pending")
@ -218,9 +227,7 @@ class HTTPAPGServer(web_resource.Resource):
raise exceptions.InternalError('"subscribed" state was expected')
inbox = await self.apg.get_ap_inbox_from_id(signing_actor, use_shared=False)
actor_id = self.apg.build_apurl(TYPE_ACTOR, ap_account)
accept_data = self.apg.create_activity(
"Accept", actor_id, object_=data
)
accept_data = self.apg.create_activity("Accept", actor_id, object_=data)
await self.apg.sign_and_post(inbox, actor_id, accept_data)
await self.apg._c.synchronise(client, account_jid, node, resync=False)
@ -233,7 +240,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
if node is None:
node = self.apg._m.namespace
@ -253,7 +260,9 @@ class HTTPAPGServer(web_resource.Resource):
continue
try:
sub = next(
s for s in follow_node.subscriptions if s.subscriber==account_jid
s
for s in follow_node.subscriptions
if s.subscriber == account_jid
)
except StopIteration:
log.warning(
@ -283,7 +292,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
):
if node is None:
node = self.apg._m.namespace
@ -325,13 +334,9 @@ class HTTPAPGServer(web_resource.Resource):
if repeated:
# we don't check sender when item is repeated, as it should be different
# from post author in this case
sender_jid = await self.apg.get_jid_from_id(
requestor_actor_id,
sender
)
sender_jid = await self.apg.get_jid_from_id(requestor_actor_id, sender)
repeater_jid = await self.apg.get_jid_from_id(
requestor_actor_id,
signing_actor
requestor_actor_id, signing_actor
)
repeated_item_id = obj["id"]
if self.apg.is_local_url(repeated_item_id):
@ -347,13 +352,14 @@ class HTTPAPGServer(web_resource.Resource):
if not url_account or not url_item_id:
raise ValueError
except (RuntimeError, ValueError):
raise exceptions.DataError(
"local URI is invalid: {repeated_id}"
)
raise exceptions.DataError("local URI is invalid: {repeated_id}")
else:
url_jid, url_node = await self.apg.get_jid_and_node(url_account)
if ((url_jid != sender_jid
or url_node and url_node != self.apg._m.namespace)):
if (
url_jid != sender_jid
or url_node
and url_node != self.apg._m.namespace
):
raise exceptions.DataError(
"announced ID doesn't match sender ({sender}): "
f"[repeated_item_id]"
@ -368,17 +374,15 @@ class HTTPAPGServer(web_resource.Resource):
"pubsub",
path=sender_jid.full(),
node=self.apg._m.namespace,
item=repeated_item_id
)
item=repeated_item_id,
),
}
# we must use activity's id and targets, not the original item ones
for field in ("id", "to", "bto", "cc", "bcc"):
obj[field] = data.get(field)
else:
if sender != signing_actor:
log.warning(
"Ignoring object not attributed to signing actor: {obj}"
)
log.warning("Ignoring object not attributed to signing actor: {obj}")
continue
await self.apg.new_ap_item(client, account_jid, node, obj)
@ -392,7 +396,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
):
await self.handle_new_ap_items(
requestor_actor_id, request, data, account_jid, node, signing_actor
@ -407,7 +411,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
):
# Update is the same as create: the item ID stays the same, thus the item will be
# overwritten
@ -424,7 +428,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
):
# we create a new item
await self.handle_new_ap_items(
@ -434,14 +438,11 @@ class HTTPAPGServer(web_resource.Resource):
account_jid,
node,
signing_actor,
repeated=True
repeated=True,
)
async def handle_attachment_item(
self,
client: SatXMPPEntity,
data: dict,
attachment_data: dict
self, client: SatXMPPEntity, data: dict, attachment_data: dict
) -> None:
target_ids = data.get("object")
if not target_ids:
@ -485,11 +486,7 @@ class HTTPAPGServer(web_resource.Resource):
author_jid, item_node, item_id
)
cached_node = await self.apg.host.memory.storage.get_pubsub_node(
client,
author_jid,
attachment_node,
with_subscriptions=True,
create=True
client, author_jid, attachment_node, with_subscriptions=True, create=True
)
found_items, __ = await self.apg.host.memory.storage.get_items(
cached_node, item_ids=[client.jid.userhost()]
@ -501,19 +498,13 @@ class HTTPAPGServer(web_resource.Resource):
old_item_elt = found_item.data
item_elt = await self.apg._pa.apply_set_handler(
client,
{"extra": attachment_data},
old_item_elt,
None
client, {"extra": attachment_data}, old_item_elt, None
)
# we reparse the element, as there can be other attachments
attachments_data = self.apg._pa.items_2_attachment_data(client, [item_elt])
# and we update the cache
await self.apg.host.memory.storage.cache_pubsub_items(
client,
cached_node,
[item_elt],
attachments_data or [{}]
client, cached_node, [item_elt], attachments_data or [{}]
)
if self.apg.is_virtual_jid(author_jid):
@ -525,7 +516,7 @@ class HTTPAPGServer(web_resource.Resource):
self.apg.pubsub_service.notifyPublish(
author_jid,
attachment_node,
[(subscription.subscriber, None, [item_elt])]
[(subscription.subscriber, None, [item_elt])],
)
else:
# the attachment is on an XMPP item, we publish it to the attachment node
@ -542,7 +533,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor)
await self.handle_attachment_item(client, data, {"noticed": True})
@ -556,12 +547,12 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor)
await self.handle_attachment_item(client, data, {
"reactions": {"operation": "update", "add": [data["content"]]}
})
await self.handle_attachment_item(
client, data, {"reactions": {"operation": "update", "add": [data["content"]]}}
)
async def handle_join_activity(
self,
@ -572,7 +563,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor)
await self.handle_attachment_item(client, data, {"rsvp": {"attending": "yes"}})
@ -586,7 +577,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor)
await self.handle_attachment_item(client, data, {"rsvp": {"attending": "no"}})
@ -599,7 +590,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> dict:
inbox = self.apg.build_apurl(TYPE_INBOX, ap_account)
shared_inbox = self.apg.build_apurl(TYPE_SHARED_INBOX)
@ -623,9 +614,8 @@ class HTTPAPGServer(web_resource.Resource):
actor_data = {
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1"
"https://w3id.org/security/v1",
],
# XXX: Mastodon doesn't like percent-encode arobas, so we have to unescape it
# if it is escaped
"id": ap_url.replace("%40", "@"),
@ -639,7 +629,7 @@ class HTTPAPGServer(web_resource.Resource):
"publicKey": {
"id": f"{ap_url}#main-key",
"owner": ap_url,
"publicKeyPem": self.apg.public_key_pem
"publicKeyPem": self.apg.public_key_pem,
},
"endpoints": {
"sharedInbox": shared_inbox,
@ -664,7 +654,7 @@ class HTTPAPGServer(web_resource.Resource):
actor_data["icon"] = {
"type": "Image",
"url": avatar_url,
"mediaType": media_type
"mediaType": media_type,
}
return actor_data
@ -672,13 +662,12 @@ class HTTPAPGServer(web_resource.Resource):
def get_canonical_url(self, request: "HTTPRequest") -> str:
return parse.urljoin(
f"https://{self.apg.public_url}",
request.path.decode().rstrip("/")
# we unescape "@" for the same reason as in [ap_actor_request]
request.path.decode().rstrip("/"),
# we unescape "@" for the same reason as in [ap_actor_request]
).replace("%40", "@")
def query_data_2_rsm_request(
self,
query_data: Dict[str, List[str]]
self, query_data: Dict[str, List[str]]
) -> rsm.RSMRequest:
"""Get RSM kwargs to use with RSMRequest from query data"""
page = query_data.get("page")
@ -690,7 +679,7 @@ class HTTPAPGServer(web_resource.Resource):
else:
for query_key in ("index", "before", "after"):
try:
kwargs={query_key: query_data[query_key][0], "max_": PAGE_SIZE}
kwargs = {query_key: query_data[query_key][0], "max_": PAGE_SIZE}
except (KeyError, IndexError, ValueError):
pass
else:
@ -705,7 +694,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
query_data: Dict[str, List[str]]
query_data: Dict[str, List[str]],
) -> dict:
if node is None:
node = self.apg._m.namespace
@ -719,7 +708,7 @@ class HTTPAPGServer(web_resource.Resource):
service=account_jid,
node=node,
rsm_request=self.query_data_2_rsm_request(query_data),
extra = {C.KEY_USE_CACHE: False}
extra={C.KEY_USE_CACHE: False},
)
except error.StanzaError as e:
log.warning(f"Can't get data from pubsub node {node} at {account_jid}: {e}")
@ -730,8 +719,7 @@ class HTTPAPGServer(web_resource.Resource):
if node and node.startswith(self.apg._events.namespace):
ordered_items = [
await self.apg.ap_events.event_data_2_ap_item(
self.apg._events.event_elt_2_event_data(item),
account_jid
self.apg._events.event_elt_2_event_data(item), account_jid
)
for item in reversed(items)
]
@ -740,11 +728,8 @@ class HTTPAPGServer(web_resource.Resource):
await self.apg.mb_data_2_ap_item(
self.apg.client,
await self.apg._m.item_2_mb_data(
self.apg.client,
item,
account_jid,
node
)
self.apg.client, item, account_jid, node
),
)
for item in reversed(items)
]
@ -753,7 +738,7 @@ class HTTPAPGServer(web_resource.Resource):
"id": url,
"type": "OrderedCollectionPage",
"partOf": base_url,
"orderedItems": ordered_items
"orderedItems": ordered_items,
}
if "rsm" not in metadata:
@ -764,13 +749,13 @@ class HTTPAPGServer(web_resource.Resource):
# of what we get with RSM (at least with Libervia Pubsub)
if not metadata["complete"]:
try:
last= metadata["rsm"]["last"]
last = metadata["rsm"]["last"]
except KeyError:
last = None
ret_data["prev"] = f"{base_url}?{parse.urlencode({'after': last})}"
if metadata["rsm"]["index"] != 0:
try:
first= metadata["rsm"]["first"]
first = metadata["rsm"]["first"]
except KeyError:
first = None
ret_data["next"] = f"{base_url}?{parse.urlencode({'before': first})}"
@ -785,7 +770,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> dict:
if node is None:
node = self.apg._m.namespace
@ -809,7 +794,7 @@ class HTTPAPGServer(web_resource.Resource):
node=node,
max_items=0,
rsm_request=rsm.RSMRequest(max_=0),
extra = {C.KEY_USE_CACHE: False}
extra={C.KEY_USE_CACHE: False},
)
except error.StanzaError as e:
log.warning(f"Can't get data from pubsub node {node} at {account_jid}: {e}")
@ -844,7 +829,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> None:
assert data is not None
if signing_actor is None:
@ -855,14 +840,14 @@ class HTTPAPGServer(web_resource.Resource):
return self.response_code(
request,
http.UNSUPPORTED_MEDIA_TYPE,
f"request is not an activity, ignoring"
f"request is not an activity, ignoring",
)
if account_jid is None and activity_type not in ACTIVIY_NO_ACCOUNT_ALLOWED:
return self.response_code(
request,
http.UNSUPPORTED_MEDIA_TYPE,
f"{activity_type.title()!r} activity must target an account"
f"{activity_type.title()!r} activity must target an account",
)
try:
@ -871,12 +856,18 @@ class HTTPAPGServer(web_resource.Resource):
return self.response_code(
request,
http.UNSUPPORTED_MEDIA_TYPE,
f"{activity_type.title()} activity is not yet supported"
f"{activity_type.title()} activity is not yet supported",
)
else:
await method(
requestor_actor_id, request, data, account_jid, node, ap_account, ap_url,
signing_actor
requestor_actor_id,
request,
data,
account_jid,
node,
ap_account,
ap_url,
signing_actor,
)
async def ap_followers_request(
@ -887,7 +878,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> dict:
if node is None:
node = self.apg._m.namespace
@ -902,20 +893,22 @@ class HTTPAPGServer(web_resource.Resource):
ap_account = self.apg._e.unescape(subscriber.user)
else:
# regular XMPP user
ap_account = await self.apg.get_ap_account_from_jid_and_node(subscriber, node)
ap_account = await self.apg.get_ap_account_from_jid_and_node(
subscriber, node
)
followers.append(ap_account)
url = self.get_canonical_url(request)
return {
"@context": ["https://www.w3.org/ns/activitystreams"],
"type": "OrderedCollection",
"id": url,
"totalItems": len(subscribers),
"first": {
"type": "OrderedCollectionPage",
"@context": ["https://www.w3.org/ns/activitystreams"],
"type": "OrderedCollection",
"id": url,
"orderedItems": followers
}
"totalItems": len(subscribers),
"first": {
"type": "OrderedCollectionPage",
"id": url,
"orderedItems": followers,
},
}
async def ap_following_request(
@ -926,12 +919,10 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> dict[str, Any]:
client = self.apg.client
subscriptions = await self.apg._pps.subscriptions(
client, account_jid, node
)
subscriptions = await self.apg._pps.subscriptions(client, account_jid, node)
following = []
for sub_dict in subscriptions:
service = jid.JID(sub_dict["service"])
@ -947,15 +938,15 @@ class HTTPAPGServer(web_resource.Resource):
url = self.get_canonical_url(request)
return {
"@context": ["https://www.w3.org/ns/activitystreams"],
"type": "OrderedCollection",
"id": url,
"totalItems": len(subscriptions),
"first": {
"type": "OrderedCollectionPage",
"@context": ["https://www.w3.org/ns/activitystreams"],
"type": "OrderedCollection",
"id": url,
"orderedItems": following
}
"totalItems": len(subscriptions),
"first": {
"type": "OrderedCollectionPage",
"id": url,
"orderedItems": following,
},
}
def _get_to_log(
@ -965,24 +956,23 @@ class HTTPAPGServer(web_resource.Resource):
) -> List[str]:
"""Get base data to logs in verbose mode"""
from pprint import pformat
to_log = [
"",
f"<<< got {request.method.decode()} request - {request.uri.decode()}"
f"<<< got {request.method.decode()} request - {request.uri.decode()}",
]
if data is not None:
to_log.append(pformat(data))
if self.apg.verbose>=3:
if self.apg.verbose >= 3:
headers = "\n".join(
f" {k.decode()}: {v.decode()}"
for k,v in request.getAllHeaders().items()
for k, v in request.getAllHeaders().items()
)
to_log.append(f" headers:\n{headers}")
return to_log
def get_requestor_actor_id(
self,
data: dict|None = None,
uri_extra_args: list[str]|None = None
self, data: dict | None = None, uri_extra_args: list[str] | None = None
) -> str:
"""Find the actor ID of the requestor.
@ -1034,33 +1024,30 @@ class HTTPAPGServer(web_resource.Resource):
# Still nothing, we'll have to use a generic actor.
log.warning(
"Can't find destinee in \"to\" field, using generic requestor for signature."
)
return self.apg.build_apurl(
TYPE_ACTOR, f"libervia@{self.apg.public_url}"
'Can\'t find destinee in "to" field, using generic requestor for signature.'
)
return self.apg.build_apurl(TYPE_ACTOR, f"libervia@{self.apg.public_url}")
async def ap_request(
self,
request: "HTTPRequest",
data: dict|None = None,
signing_actor: str|None = None,
requestor_actor_id: str|None = None,
data: dict | None = None,
signing_actor: str | None = None,
requestor_actor_id: str | None = None,
) -> None:
if self.apg.verbose:
to_log = self._get_to_log(request, data)
path = request.path.decode()
ap_url = parse.urljoin(
f"https://{self.apg.public_url}",
path
)
ap_url = parse.urljoin(f"https://{self.apg.public_url}", path)
request_type, extra_args = self.apg.parse_apurl(ap_url)
header_accept = request.getHeader("accept") or ""
if ((MEDIA_TYPE_AP not in header_accept
and MEDIA_TYPE_AP_ALT not in header_accept
and request_type in self.apg.html_redirect)):
if (
MEDIA_TYPE_AP not in header_accept
and MEDIA_TYPE_AP_ALT not in header_accept
and request_type in self.apg.html_redirect
):
# this is not a AP request, and we have a redirections for it
kw = {}
if extra_args:
@ -1081,14 +1068,14 @@ class HTTPAPGServer(web_resource.Resource):
if not filters:
break
# if we have filter, they must all match
elif all(v in kw[k] for k,v in filters.items()):
elif all(v in kw[k] for k, v in filters.items()):
break
else:
# no redirection is matching
redirection = None
if redirection is not None:
kw = {k: parse.quote(str(v), safe="") for k,v in kw.items()}
kw = {k: parse.quote(str(v), safe="") for k, v in kw.items()}
target_url = redirection["url"].format(**kw)
content = web_util.redirectTo(target_url.encode(), request)
request.write(content)
@ -1096,9 +1083,7 @@ class HTTPAPGServer(web_resource.Resource):
return
if requestor_actor_id is None:
requestor_actor_id = self.get_requestor_actor_id(
data, extra_args
)
requestor_actor_id = self.get_requestor_actor_id(data, extra_args)
if len(extra_args) == 0:
if request_type != "shared_inbox":
raise exceptions.DataError(f"Invalid request type: {request_type!r}")
@ -1121,21 +1106,29 @@ class HTTPAPGServer(web_resource.Resource):
ap_account = extra_args[0]
account_jid, node = await self.apg.get_jid_and_node(ap_account)
if request_type not in AP_REQUEST_TYPES.get(
request.method.decode().upper(), []
request.method.decode().upper(), []
):
raise exceptions.DataError(f"Invalid request type: {request_type!r}")
method = getattr(self, f"ap_{request_type}_request")
ret_data = await method(
requestor_actor_id, request, data, account_jid, node, ap_account, ap_url, signing_actor
requestor_actor_id,
request,
data,
account_jid,
node,
ap_account,
ap_url,
signing_actor,
)
if ret_data is not None:
request.setHeader("content-type", CONTENT_TYPE_AP)
request.write(json.dumps(ret_data).encode())
if self.apg.verbose:
to_log.append(f"--- RET (code: {request.code})---")
if self.apg.verbose>=2:
if self.apg.verbose >= 2:
if ret_data is not None:
from pprint import pformat
to_log.append(f"{pformat(ret_data)}")
to_log.append("---")
log.info("\n".join(to_log))
@ -1149,15 +1142,13 @@ class HTTPAPGServer(web_resource.Resource):
self.response_code(
request,
http.BAD_REQUEST,
f"invalid body, was expecting a JSON object"
f"invalid body, was expecting a JSON object",
)
request.finish()
return
except (json.JSONDecodeError, ValueError) as e:
self.response_code(
request,
http.BAD_REQUEST,
f"invalid json in inbox request: {e}"
request, http.BAD_REQUEST, f"invalid json in inbox request: {e}"
)
request.finish()
return
@ -1185,18 +1176,12 @@ class HTTPAPGServer(web_resource.Resource):
to_log.append(f" body: {request.content.read()!r}")
request.content.seek(0)
log.info("\n".join(to_log))
self.response_code(
request,
http.FORBIDDEN,
f"invalid signature: {e}"
)
self.response_code(request, http.FORBIDDEN, f"invalid signature: {e}")
request.finish()
return
except Exception as e:
self.response_code(
request,
http.INTERNAL_SERVER_ERROR,
f"Can't check signature: {e}"
request, http.INTERNAL_SERVER_ERROR, f"Can't check signature: {e}"
)
request.finish()
return
@ -1219,10 +1204,7 @@ class HTTPAPGServer(web_resource.Resource):
self._on_request_error(failure.Failure(e), request)
async def check_signing_actor(
self,
requestor_actor_id: str,
data: dict,
signing_actor: str
self, requestor_actor_id: str, data: dict, signing_actor: str
) -> None:
"""That that signing actor correspond to actor declared in data
@ -1241,9 +1223,7 @@ class HTTPAPGServer(web_resource.Resource):
)
async def check_signature(
self,
requestor_actor_id: str,
request: "HTTPRequest"
self, requestor_actor_id: str, request: "HTTPRequest"
) -> str:
"""Check and validate HTTP signature
@ -1264,10 +1244,11 @@ class HTTPAPGServer(web_resource.Resource):
except KeyError:
raise exceptions.EncryptionError('"keyId" is missing from signature')
algorithm = sign_data.get("algorithm", HS2019)
signed_headers = sign_data.get(
"headers",
"(created)" if algorithm==HS2019 else "date"
).lower().split()
signed_headers = (
sign_data.get("headers", "(created)" if algorithm == HS2019 else "date")
.lower()
.split()
)
try:
headers_to_check = SIGN_HEADERS[None] + SIGN_HEADERS[request.method]
except KeyError:
@ -1284,9 +1265,7 @@ class HTTPAPGServer(web_resource.Resource):
f"at least one of following header must be signed: {header}"
)
elif header not in signed_headers:
raise exceptions.EncryptionError(
f"the {header!r} header must be signed"
)
raise exceptions.EncryptionError(f"the {header!r} header must be signed")
body = request.content.read()
request.content.seek(0)
@ -1329,7 +1308,8 @@ class HTTPAPGServer(web_resource.Resource):
if forwarded is not None:
try:
host = [
f[5:] for f in forwarded.split(";")
f[5:]
for f in forwarded.split(";")
if f.startswith("host=")
][0] or None
except IndexError:
@ -1342,7 +1322,8 @@ class HTTPAPGServer(web_resource.Resource):
value = host
elif to_sign == "digest":
hashes = {
algo.lower(): hash_ for algo, hash_ in (
algo.lower(): hash_
for algo, hash_ in (
digest.split("=", 1) for digest in value.split(",")
)
}
@ -1367,7 +1348,6 @@ class HTTPAPGServer(web_resource.Resource):
else:
created = date_utils.date_parse(headers["date"])
try:
expires = float(headers["expires"])
except KeyError:
@ -1386,23 +1366,17 @@ class HTTPAPGServer(web_resource.Resource):
try:
return await self.apg.check_signature(
requestor_actor_id,
sign_data["signature"],
key_id,
headers
requestor_actor_id, sign_data["signature"], key_id, headers
)
except exceptions.EncryptionError:
method, url = headers["(request-target)"].rsplit(' ', 1)
method, url = headers["(request-target)"].rsplit(" ", 1)
headers["(request-target)"] = f"{method} {parse.unquote(url)}"
log.debug(
"Using workaround for (request-target) encoding bug in signature, "
"see https://github.com/mastodon/mastodon/issues/18871"
)
return await self.apg.check_signature(
requestor_actor_id,
sign_data["signature"],
key_id,
headers
requestor_actor_id, sign_data["signature"], key_id, headers
)
def render(self, request):

View file

@ -37,11 +37,7 @@ from libervia.backend.tools.utils import ensure_deferred
from libervia.backend.tools.web import download_file
from libervia.backend.memory.sqla_mapping import PubsubSub, SubscriptionState
from .constants import (
TYPE_ACTOR,
ST_AVATAR,
MAX_AVATAR_SIZE
)
from .constants import TYPE_ACTOR, ST_AVATAR, MAX_AVATAR_SIZE
log = getLogger(__name__)
@ -52,13 +48,14 @@ NODE_CONFIG = [
{"var": "pubsub#max_items", "value": "max"},
{"var": "pubsub#access_model", "type": "list-single", "value": "open"},
{"var": "pubsub#publish_model", "type": "list-single", "value": "open"},
]
NODE_CONFIG_VALUES = {c["var"]: c["value"] for c in NODE_CONFIG}
NODE_OPTIONS = {c["var"]: {} for c in NODE_CONFIG}
for c in NODE_CONFIG:
NODE_OPTIONS[c["var"]].update({k:v for k,v in c.items() if k not in ("var", "value")})
NODE_OPTIONS[c["var"]].update(
{k: v for k, v in c.items() if k not in ("var", "value")}
)
class APPubsubService(rsm.PubSubService):
@ -88,34 +85,30 @@ class APPubsubService(rsm.PubSubService):
in requestor
"""
if not recipient.user:
raise error.StanzaError(
"item-not-found",
text="No user part specified"
)
raise error.StanzaError("item-not-found", text="No user part specified")
requestor_actor_id = self.apg.build_apurl(TYPE_ACTOR, requestor.userhost())
recipient_account = self.apg._e.unescape(recipient.user)
recipient_actor_id = await self.apg.get_ap_actor_id_from_account(recipient_account)
recipient_actor_id = await self.apg.get_ap_actor_id_from_account(
recipient_account
)
inbox = await self.apg.get_ap_inbox_from_id(recipient_actor_id, use_shared=False)
return requestor_actor_id, recipient_actor_id, inbox
@ensure_deferred
async def publish(self, requestor, service, nodeIdentifier, items):
if self.apg.local_only and not self.apg.is_local(requestor):
raise error.StanzaError(
"forbidden",
"Only local users can publish on this gateway."
"forbidden", "Only local users can publish on this gateway."
)
if not service.user:
raise error.StanzaError(
"bad-request",
"You must specify an ActivityPub actor account in JID user part."
"You must specify an ActivityPub actor account in JID user part.",
)
ap_account = self.apg._e.unescape(service.user)
if ap_account.count("@") != 1:
raise error.StanzaError(
"bad-request",
f"{ap_account!r} is not a valid ActivityPub actor account."
"bad-request", f"{ap_account!r} is not a valid ActivityPub actor account."
)
client = self.apg.client.get_virtual_client(requestor)
@ -130,21 +123,17 @@ class APPubsubService(rsm.PubSubService):
cached_node = await self.host.memory.storage.get_pubsub_node(
client, service, nodeIdentifier, with_subscriptions=True, create=True
)
await self.host.memory.storage.cache_pubsub_items(
client,
cached_node,
items
)
await self.host.memory.storage.cache_pubsub_items(client, cached_node, items)
for subscription in cached_node.subscriptions:
if subscription.state != SubscriptionState.SUBSCRIBED:
continue
self.notifyPublish(
service,
nodeIdentifier,
[(subscription.subscriber, None, items)]
service, nodeIdentifier, [(subscription.subscriber, None, items)]
)
async def ap_following_2_elt(self, requestor_actor_id: str, ap_item: dict) -> domish.Element:
async def ap_following_2_elt(
self, requestor_actor_id: str, ap_item: dict
) -> domish.Element:
"""Convert actor ID from following collection to XMPP item
@param requestor_actor_id: ID of the actor doing the request.
@ -159,9 +148,7 @@ class APPubsubService(rsm.PubSubService):
return item_elt
async def ap_follower_2_elt(
self,
requestor_actor_id: str,
ap_item: dict
self, requestor_actor_id: str, ap_item: dict
) -> domish.Element:
"""Convert actor ID from followers collection to XMPP item
@ -175,9 +162,7 @@ class APPubsubService(rsm.PubSubService):
return item_elt
async def generate_v_card(
self,
requestor_actor_id: str,
ap_account: str
self, requestor_actor_id: str, ap_account: str
) -> domish.Element:
"""Generate vCard4 (XEP-0292) item element from ap_account's metadata
@ -186,8 +171,7 @@ class APPubsubService(rsm.PubSubService):
@return: <item> with the <vcard> element
"""
actor_data = await self.apg.get_ap_actor_data_from_account(
requestor_actor_id,
ap_account
requestor_actor_id, ap_account
)
identity_data = {}
@ -212,10 +196,7 @@ class APPubsubService(rsm.PubSubService):
return item_elt
async def get_avatar_data(
self,
client: SatXMPPEntity,
requestor_actor_id: str,
ap_account: str
self, client: SatXMPPEntity, requestor_actor_id: str, ap_account: str
) -> dict[str, Any]:
"""Retrieve actor's avatar if any, cache it and file actor_data
@ -259,27 +240,21 @@ class APPubsubService(rsm.PubSubService):
avatar_data = {
"path": dest_path,
"filename": filename,
'media_type': image.guess_type(dest_path),
"media_type": image.guess_type(dest_path),
}
await self.apg._i.cache_avatar(
self.apg.IMPORT_NAME,
avatar_data
)
await self.apg._i.cache_avatar(self.apg.IMPORT_NAME, avatar_data)
else:
avatar_data = {
"cache_uid": cache["uid"],
"path": cache["path"],
"media_type": cache["mime_type"]
}
"cache_uid": cache["uid"],
"path": cache["path"],
"media_type": cache["mime_type"],
}
return avatar_data
async def generate_avatar_metadata(
self,
client: SatXMPPEntity,
requestor_actor_id: str,
ap_account: str
self, client: SatXMPPEntity, requestor_actor_id: str, ap_account: str
) -> domish.Element:
"""Generate the metadata element for user avatar
@ -308,14 +283,11 @@ class APPubsubService(rsm.PubSubService):
"""
if not itemIdentifiers:
avatar_data = await self.get_avatar_data(
client,
requestor_actor_id,
ap_account
client, requestor_actor_id, ap_account
)
if "base64" not in avatar_data:
await threads.deferToThread(
self._blocking_b_6_4_encode_avatar,
avatar_data
self._blocking_b_6_4_encode_avatar, avatar_data
)
else:
if len(itemIdentifiers) > 1:
@ -327,10 +299,7 @@ class APPubsubService(rsm.PubSubService):
cache_data = self.apg.host.common_cache.get_metadata(item_id)
if cache_data is None:
raise error.StanzaError("item-not-found")
avatar_data = {
"cache_uid": item_id,
"path": cache_data["path"]
}
avatar_data = {"cache_uid": item_id, "path": cache_data["path"]}
await threads.deferToThread(self._blocking_b_6_4_encode_avatar, avatar_data)
return self.apg._a.build_item_data_elt(avatar_data)
@ -343,7 +312,7 @@ class APPubsubService(rsm.PubSubService):
node: str,
maxItems: Optional[int],
itemIdentifiers: Optional[List[str]],
rsm_req: Optional[rsm.RSMRequest]
rsm_req: Optional[rsm.RSMRequest],
) -> Tuple[List[domish.Element], Optional[rsm.RSMResponse]]:
if not service.user:
return [], None
@ -353,8 +322,7 @@ class APPubsubService(rsm.PubSubService):
return [], None
requestor_actor_id = self.apg.build_apurl(
TYPE_ACTOR,
await self.apg.get_ap_account_from_jid_and_node(service, node)
TYPE_ACTOR, await self.apg.get_ap_account_from_jid_and_node(service, node)
)
# cached_node may be pre-filled with some nodes (e.g. attachments nodes),
@ -379,17 +347,12 @@ class APPubsubService(rsm.PubSubService):
return [item_elt], None
elif node == self.apg._a.namespace_metadata:
item_elt = await self.generate_avatar_metadata(
self.apg.client,
requestor_actor_id,
ap_account
self.apg.client, requestor_actor_id, ap_account
)
return [item_elt], None
elif node == self.apg._a.namespace_data:
item_elt = await self.generate_avatar_data(
self.apg.client,
requestor_actor_id,
ap_account,
itemIdentifiers
self.apg.client, requestor_actor_id, ap_account, itemIdentifiers
)
return [item_elt], None
elif self.apg._pa.is_attachment_node(node):
@ -410,7 +373,7 @@ class APPubsubService(rsm.PubSubService):
raise error.StanzaError(
"feature-not-implemented",
text=f"AP Gateway {C.APP_VERSION} only supports "
f"{self.apg._m.namespace} node for now"
f"{self.apg._m.namespace} node for now",
)
collection_name = "outbox"
use_cache = True
@ -444,18 +407,26 @@ class APPubsubService(rsm.PubSubService):
if rsm_req is None:
if maxItems is None:
maxItems = 20
kwargs.update({
"max_items": maxItems,
"chronological_pagination": False,
})
kwargs.update(
{
"max_items": maxItems,
"chronological_pagination": False,
}
)
else:
if len(
[v for v in (rsm_req.after, rsm_req.before, rsm_req.index)
if v is not None]
) > 1:
if (
len(
[
v
for v in (rsm_req.after, rsm_req.before, rsm_req.index)
if v is not None
]
)
> 1
):
raise error.StanzaError(
"bad-request",
text="You can't use after, before and index at the same time"
text="You can't use after, before and index at the same time",
)
kwargs.update({"max_items": rsm_req.max})
if rsm_req.after is not None:
@ -476,25 +447,21 @@ class APPubsubService(rsm.PubSubService):
try:
parent_data = await self.apg.ap_get(parent_item, requestor_actor_id)
collection = await self.apg.ap_get_object(
requestor_actor_id,
parent_data.get("object", {}),
"replies"
requestor_actor_id, parent_data.get("object", {}), "replies"
)
except Exception as e:
raise error.StanzaError(
"item-not-found",
text=str(e)
)
raise error.StanzaError("item-not-found", text=str(e))
else:
actor_data = await self.apg.get_ap_actor_data_from_account(
requestor_actor_id,
ap_account
requestor_actor_id, ap_account
)
collection = await self.apg.ap_get_object(
requestor_actor_id, actor_data, collection_name
)
collection = await self.apg.ap_get_object(requestor_actor_id, actor_data, collection_name)
if not collection:
raise error.StanzaError(
"item-not-found",
text=f"No collection found for node {node!r} (account: {ap_account})"
text=f"No collection found for node {node!r} (account: {ap_account})",
)
kwargs["parser"] = parser
@ -528,17 +495,15 @@ class APPubsubService(rsm.PubSubService):
else:
try:
subscription = next(
s for s in node.subscriptions
s
for s in node.subscriptions
if s.subscriber == requestor.userhostJID()
)
except StopIteration:
subscription = None
if subscription is None:
subscription = PubsubSub(
subscriber=requestor.userhostJID(),
state=sub_state
)
subscription = PubsubSub(subscriber=requestor.userhostJID(), state=sub_state)
node.subscriptions.append(subscription)
await self.host.memory.storage.add(node)
else:
@ -586,11 +551,7 @@ class APPubsubService(rsm.PubSubService):
data = self.apg.create_activity(
"Undo",
req_actor_id,
self.apg.create_activity(
"Follow",
req_actor_id,
recip_actor_id
)
self.apg.create_activity("Follow", req_actor_id, recip_actor_id),
)
resp = await self.apg.sign_and_post(inbox, req_actor_id, data)
@ -602,10 +563,7 @@ class APPubsubService(rsm.PubSubService):
return NODE_OPTIONS
def getConfiguration(
self,
requestor: jid.JID,
service: jid.JID,
nodeIdentifier: str
self, requestor: jid.JID, service: jid.JID, nodeIdentifier: str
) -> defer.Deferred:
return defer.succeed(NODE_CONFIG_VALUES)
@ -615,12 +573,9 @@ class APPubsubService(rsm.PubSubService):
service: jid.JID,
nodeIdentifier: str,
pep: bool = False,
recipient: Optional[jid.JID] = None
recipient: Optional[jid.JID] = None,
) -> Optional[dict]:
if not nodeIdentifier:
return None
info = {
"type": "leaf",
"meta-data": NODE_CONFIG
}
info = {"type": "leaf", "meta-data": NODE_CONFIG}
return info

View file

@ -42,17 +42,17 @@ import re
# obs-text = %x80-FF
# ---
ows = '[ \t]*'
bws = f'{ows}'
obs_text = '[\\x80-\\xff]'
qdtext = f'([\t !#-\\[\\]-~]|{obs_text})'
quoted_pair = f'\\\\([\t !-~]|{obs_text})'
ows = "[ \t]*"
bws = f"{ows}"
obs_text = "[\\x80-\\xff]"
qdtext = f"([\t !#-\\[\\]-~]|{obs_text})"
quoted_pair = f"\\\\([\t !-~]|{obs_text})"
quoted_string = f'"({qdtext}|{quoted_pair})*"'
tchar = "([!#$%&`*+\\-.^_]|\\\\'|[|~0-9a-zA-Z])"
token = f'({tchar})+'
token = f"({tchar})+"
RE_SIG_PARAM = re.compile(
f'(?P<key>{token}{bws})={bws}'
f'((?P<uq_value>{token})|(?P<quoted_value>{quoted_string}))'
f"(?P<key>{token}{bws})={bws}"
f"((?P<uq_value>{token})|(?P<quoted_value>{quoted_string}))"
)