Mercurial > libervia-backend
diff sat/plugins/plugin_comp_ap_gateway/pubsub_service.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | 78b5f356900c |
children |
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_ap_gateway/pubsub_service.py Fri Apr 07 15:18:39 2023 +0200 +++ b/sat/plugins/plugin_comp_ap_gateway/pubsub_service.py Sat Apr 08 13:54:42 2023 +0200 @@ -34,7 +34,7 @@ from sat.core.constants import Const as C from sat.tools import image from sat.tools.utils import ensure_deferred -from sat.tools.web import downloadFile +from sat.tools.web import download_file from sat.memory.sqla_mapping import PubsubSub, SubscriptionState from .constants import ( @@ -74,7 +74,7 @@ "name": "Libervia ActivityPub Gateway", } - async def getAPActorIdsAndInbox( + async def get_ap_actor_ids_and_inbox( self, requestor: jid.JID, recipient: jid.JID, @@ -92,16 +92,16 @@ "item-not-found", text="No user part specified" ) - requestor_actor_id = self.apg.buildAPURL(TYPE_ACTOR, requestor.userhost()) + 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.getAPActorIdFromAccount(recipient_account) - inbox = await self.apg.getAPInboxFromId(recipient_actor_id, use_shared=False) + 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.isLocal(requestor): + if self.apg.local_only and not self.apg.is_local(requestor): raise error.StanzaError( "forbidden", "Only local users can publish on this gateway." @@ -118,19 +118,19 @@ f"{ap_account!r} is not a valid ActivityPub actor account." ) - client = self.apg.client.getVirtualClient(requestor) - if self.apg._pa.isAttachmentNode(nodeIdentifier): - await self.apg.convertAndPostAttachments( + client = self.apg.client.get_virtual_client(requestor) + if self.apg._pa.is_attachment_node(nodeIdentifier): + await self.apg.convert_and_post_attachments( client, ap_account, service, nodeIdentifier, items, publisher=requestor ) else: - await self.apg.convertAndPostItems( + await self.apg.convert_and_post_items( client, ap_account, service, nodeIdentifier, items ) - cached_node = await self.host.memory.storage.getPubsubNode( + cached_node = await self.host.memory.storage.get_pubsub_node( client, service, nodeIdentifier, with_subscriptions=True, create=True ) - await self.host.memory.storage.cachePubsubItems( + await self.host.memory.storage.cache_pubsub_items( client, cached_node, items @@ -144,27 +144,27 @@ [(subscription.subscriber, None, items)] ) - async def apFollowing2Elt(self, ap_item: dict) -> domish.Element: + async def ap_following_2_elt(self, ap_item: dict) -> domish.Element: """Convert actor ID from following collection to XMPP item""" actor_id = ap_item["id"] - actor_jid = await self.apg.getJIDFromId(actor_id) - subscription_elt = self.apg._pps.buildSubscriptionElt( + actor_jid = await self.apg.get_jid_from_id(actor_id) + subscription_elt = self.apg._pps.build_subscription_elt( self.apg._m.namespace, actor_jid ) item_elt = pubsub.Item(id=actor_id, payload=subscription_elt) return item_elt - async def apFollower2Elt(self, ap_item: dict) -> domish.Element: + async def ap_follower_2_elt(self, ap_item: dict) -> domish.Element: """Convert actor ID from followers collection to XMPP item""" actor_id = ap_item["id"] - actor_jid = await self.apg.getJIDFromId(actor_id) - subscriber_elt = self.apg._pps.buildSubscriberElt(actor_jid) + actor_jid = await self.apg.get_jid_from_id(actor_id) + subscriber_elt = self.apg._pps.build_subscriber_elt(actor_jid) item_elt = pubsub.Item(id=actor_id, payload=subscriber_elt) return item_elt - async def generateVCard(self, ap_account: str) -> domish.Element: + async def generate_v_card(self, ap_account: str) -> domish.Element: """Generate vCard4 (XEP-0292) item element from ap_account's metadata""" - actor_data = await self.apg.getAPActorDataFromAccount(ap_account) + actor_data = await self.apg.get_ap_actor_data_from_account(ap_account) identity_data = {} summary = actor_data.get("summary") @@ -181,13 +181,13 @@ value = actor_data.get(field) if value: identity_data.setdefault("nicknames", []).append(value) - vcard_elt = self.apg._v.dict2VCard(identity_data) + vcard_elt = self.apg._v.dict_2_v_card(identity_data) item_elt = domish.Element((pubsub.NS_PUBSUB, "item")) item_elt.addChild(vcard_elt) item_elt["id"] = self.apg._p.ID_SINGLETON return item_elt - async def getAvatarData( + async def get_avatar_data( self, client: SatXMPPEntity, ap_account: str @@ -197,9 +197,9 @@ ``cache_uid``, `path``` and ``media_type`` keys are always files ``base64`` key is only filled if the file was not already in cache """ - actor_data = await self.apg.getAPActorDataFromAccount(ap_account) + actor_data = await self.apg.get_ap_actor_data_from_account(ap_account) - for icon in await self.apg.apGetList(actor_data, "icon"): + for icon in await self.apg.ap_get_list(actor_data, "icon"): url = icon.get("url") if icon["type"] != "Image" or not url: continue @@ -221,19 +221,19 @@ if cache_uid is None: cache = None else: - cache = self.apg.host.common_cache.getMetadata(cache_uid) + cache = self.apg.host.common_cache.get_metadata(cache_uid) if cache is None: with tempfile.TemporaryDirectory() as dir_name: dest_path = Path(dir_name, filename) - await downloadFile(url, dest_path, max_size=MAX_AVATAR_SIZE) + await download_file(url, dest_path, max_size=MAX_AVATAR_SIZE) avatar_data = { "path": dest_path, "filename": filename, 'media_type': image.guess_type(dest_path), } - await self.apg._i.cacheAvatar( + await self.apg._i.cache_avatar( self.apg.IMPORT_NAME, avatar_data ) @@ -246,7 +246,7 @@ return avatar_data - async def generateAvatarMetadata( + async def generate_avatar_metadata( self, client: SatXMPPEntity, ap_account: str @@ -256,14 +256,14 @@ @raise StanzaError("item-not-found"): no avatar is present in actor data (in ``icon`` field) """ - avatar_data = await self.getAvatarData(client, ap_account) - return self.apg._a.buildItemMetadataElt(avatar_data) + avatar_data = await self.get_avatar_data(client, ap_account) + return self.apg._a.build_item_metadata_elt(avatar_data) - def _blockingB64EncodeAvatar(self, avatar_data: Dict[str, Any]) -> None: + def _blocking_b_6_4_encode_avatar(self, avatar_data: Dict[str, Any]) -> None: with avatar_data["path"].open("rb") as f: avatar_data["base64"] = b64encode(f.read()).decode() - async def generateAvatarData( + async def generate_avatar_data( self, client: SatXMPPEntity, ap_account: str, @@ -274,9 +274,9 @@ @raise StanzaError("item-not-found"): no avatar cached with requested ID """ if not itemIdentifiers: - avatar_data = await self.getAvatarData(client, ap_account) + avatar_data = await self.get_avatar_data(client, ap_account) if "base64" not in avatar_data: - await threads.deferToThread(self._blockingB64EncodeAvatar, avatar_data) + await threads.deferToThread(self._blocking_b_6_4_encode_avatar, avatar_data) else: if len(itemIdentifiers) > 1: # only a single item ID is supported @@ -284,16 +284,16 @@ item_id = itemIdentifiers[0] # just to be sure that that we don't have an empty string assert item_id - cache_data = self.apg.host.common_cache.getMetadata(item_id) + 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"] } - await threads.deferToThread(self._blockingB64EncodeAvatar, avatar_data) + await threads.deferToThread(self._blocking_b_6_4_encode_avatar, avatar_data) - return self.apg._a.buildItemDataElt(avatar_data) + return self.apg._a.build_item_data_elt(avatar_data) @ensure_deferred async def items( @@ -320,31 +320,31 @@ if node == self.apg._pps.subscriptions_node: collection_name = "following" - parser = self.apFollowing2Elt + parser = self.ap_following_2_elt kwargs["only_ids"] = True use_cache = False elif node.startswith(self.apg._pps.subscribers_node_prefix): collection_name = "followers" - parser = self.apFollower2Elt + parser = self.ap_follower_2_elt kwargs["only_ids"] = True use_cache = False elif node == self.apg._v.node: # vCard4 request - item_elt = await self.generateVCard(ap_account) + item_elt = await self.generate_v_card(ap_account) return [item_elt], None elif node == self.apg._a.namespace_metadata: - item_elt = await self.generateAvatarMetadata(self.apg.client, ap_account) + item_elt = await self.generate_avatar_metadata(self.apg.client, ap_account) return [item_elt], None elif node == self.apg._a.namespace_data: - item_elt = await self.generateAvatarData( + item_elt = await self.generate_avatar_data( self.apg.client, ap_account, itemIdentifiers ) return [item_elt], None - elif self.apg._pa.isAttachmentNode(node): + elif self.apg._pa.is_attachment_node(node): use_cache = True # we check cache here because we emit an item-not-found error if the node is # not in cache, as we are not dealing with real AP items - cached_node = await self.host.memory.storage.getPubsubNode( + cached_node = await self.host.memory.storage.get_pubsub_node( client, service, node ) if cached_node is None: @@ -365,14 +365,14 @@ if use_cache: if cached_node is None: - cached_node = await self.host.memory.storage.getPubsubNode( + cached_node = await self.host.memory.storage.get_pubsub_node( client, service, node ) # TODO: check if node is synchronised if cached_node is not None: # the node is cached, we return items from cache log.debug(f"node {node!r} from {service} is in cache") - pubsub_items, metadata = await self.apg._c.getItemsFromCache( + pubsub_items, metadata = await self.apg._c.get_items_from_cache( client, cached_node, maxItems, itemIdentifiers, rsm_request=rsm_req ) try: @@ -384,7 +384,7 @@ if itemIdentifiers: items = [] for item_id in itemIdentifiers: - item_data = await self.apg.apGet(item_id) + item_data = await self.apg.ap_get(item_id) item_elt = await parser(item_data) items.append(item_elt) return items, None @@ -419,11 +419,11 @@ f"No cache found for node {node} at {service} (AP account {ap_account}), " "using Collection Paging to RSM translation" ) - if self.apg._m.isCommentNode(node): - parent_item = self.apg._m.getParentItem(node) + if self.apg._m.is_comment_node(node): + parent_item = self.apg._m.get_parent_item(node) try: - parent_data = await self.apg.apGet(parent_item) - collection = await self.apg.apGetObject( + parent_data = await self.apg.ap_get(parent_item) + collection = await self.apg.ap_get_object( parent_data.get("object", {}), "replies" ) @@ -433,8 +433,8 @@ text=e ) else: - actor_data = await self.apg.getAPActorDataFromAccount(ap_account) - collection = await self.apg.apGetObject(actor_data, collection_name) + actor_data = await self.apg.get_ap_actor_data_from_account(ap_account) + collection = await self.apg.ap_get_object(actor_data, collection_name) if not collection: raise error.StanzaError( "item-not-found", @@ -442,7 +442,7 @@ ) kwargs["parser"] = parser - return await self.apg.getAPItems(collection, **kwargs) + return await self.apg.get_ap_items(collection, **kwargs) @ensure_deferred async def retract(self, requestor, service, nodeIdentifier, itemIdentifiers): @@ -459,11 +459,11 @@ sub_state = SubscriptionState.PENDING else: sub_state = SubscriptionState.SUBSCRIBED - node = await self.host.memory.storage.getPubsubNode( + node = await self.host.memory.storage.get_pubsub_node( client, service, nodeIdentifier, with_subscriptions=True ) if node is None: - node = await self.host.memory.storage.setPubsubNode( + node = await self.host.memory.storage.set_pubsub_node( client, service, nodeIdentifier, @@ -510,13 +510,13 @@ if nodeIdentifier in (self.apg._m.namespace, self.apg._events.namespace): # if we subscribe to microblog or events node, we follow the corresponding # account - req_actor_id, recip_actor_id, inbox = await self.getAPActorIdsAndInbox( + req_actor_id, recip_actor_id, inbox = await self.get_ap_actor_ids_and_inbox( requestor, service ) data = self.apg.create_activity("Follow", req_actor_id, recip_actor_id) - resp = await self.apg.signAndPost(inbox, req_actor_id, data) + resp = await self.apg.sign_and_post(inbox, req_actor_id, data) if resp.code >= 300: text = await resp.text() raise error.StanzaError("service-unavailable", text=text) @@ -524,7 +524,7 @@ @ensure_deferred async def unsubscribe(self, requestor, service, nodeIdentifier, subscriber): - req_actor_id, recip_actor_id, inbox = await self.getAPActorIdsAndInbox( + req_actor_id, recip_actor_id, inbox = await self.get_ap_actor_ids_and_inbox( requestor, service ) data = self.apg.create_activity( @@ -537,7 +537,7 @@ ) ) - resp = await self.apg.signAndPost(inbox, req_actor_id, data) + resp = await self.apg.sign_and_post(inbox, req_actor_id, data) if resp.code >= 300: text = await resp.text() raise error.StanzaError("service-unavailable", text=text)