Reformatted code base using black.
This commit is contained in:
parent
56319cbad3
commit
53fdaa9d09
1 changed files with 12604 additions and 8966 deletions
|
|
@ -93,12 +93,11 @@ class PubsubAttachments:
|
|||
self,
|
||||
name: str,
|
||||
namespace: str,
|
||||
get_cb: Callable[
|
||||
[SatXMPPEntity, domish.Element, Dict[str, Any]],
|
||||
None],
|
||||
get_cb: Callable[[SatXMPPEntity, domish.Element, Dict[str, Any]], None],
|
||||
set_cb: Callable[
|
||||
[SatXMPPEntity, Dict[str, Any], Optional[domish.Element]],
|
||||
Optional[domish.Element]],
|
||||
Optional[domish.Element],
|
||||
],
|
||||
) -> None:
|
||||
"""Register callbacks to handle an attachment
|
||||
|
||||
|
|
@ -120,18 +119,12 @@ class PubsubAttachments:
|
|||
raise exceptions.ConflictError(
|
||||
f"({name}, {namespace}) attachment handlers are already registered"
|
||||
)
|
||||
self.handlers[(name, namespace)] = {
|
||||
"get": get_cb,
|
||||
"set": set_cb
|
||||
}
|
||||
self.handlers[(name, namespace)] = {"get": get_cb, "set": set_cb}
|
||||
|
||||
def get_attachment_node_name(self, service: jid.JID, node: str, item: str) -> str:
|
||||
"""Generate name to use for attachment node"""
|
||||
target_item_uri = uri.build_xmpp_uri(
|
||||
"pubsub",
|
||||
path=service.userhost(),
|
||||
node=node,
|
||||
item=item
|
||||
"pubsub", path=service.userhost(), node=node, item=item
|
||||
)
|
||||
return f"{NS_PUBSUB_ATTACHMENTS}/{target_item_uri}"
|
||||
|
||||
|
|
@ -162,7 +155,7 @@ class PubsubAttachments:
|
|||
service: jid.JID,
|
||||
node: str,
|
||||
item: domish.Element,
|
||||
data: dict
|
||||
data: dict,
|
||||
) -> bool:
|
||||
"""trigger to create attachment node on each publication"""
|
||||
await self.create_attachments_node(
|
||||
|
|
@ -176,7 +169,7 @@ class PubsubAttachments:
|
|||
service: jid.JID,
|
||||
node: str,
|
||||
item_id: str,
|
||||
autocreate: bool = False
|
||||
autocreate: bool = False,
|
||||
):
|
||||
"""Create node for attachements if necessary
|
||||
|
||||
|
|
@ -190,9 +183,7 @@ class PubsubAttachments:
|
|||
except error.StanzaError as e:
|
||||
if e.condition == "item-not-found" and autocreate:
|
||||
# we auto-create the missing node
|
||||
await self._p.createNode(
|
||||
client, service, node
|
||||
)
|
||||
await self._p.createNode(client, service, node)
|
||||
node_config = await self._p.getConfiguration(client, service, node)
|
||||
elif e.condition in ("forbidden", "feature-not-implemented"):
|
||||
node_config = self._p.make_configuration_form({})
|
||||
|
|
@ -213,9 +204,7 @@ class PubsubAttachments:
|
|||
log.warning(f"Can't create attachment node {attachment_node}: {e}")
|
||||
|
||||
def items_2_attachment_data(
|
||||
self,
|
||||
client: SatXMPPEntity,
|
||||
items: List[domish.Element]
|
||||
self, client: SatXMPPEntity, items: List[domish.Element]
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Convert items from attachment node to attachment data"""
|
||||
list_data = []
|
||||
|
|
@ -249,9 +238,7 @@ class PubsubAttachments:
|
|||
f"{item.toXml}"
|
||||
)
|
||||
continue
|
||||
data = {
|
||||
"from": item_id
|
||||
}
|
||||
data = {"from": item_id}
|
||||
for handler in self.handlers.values():
|
||||
handler["get"](client, attachments_elt, data)
|
||||
if len(data) > 1:
|
||||
|
|
@ -265,7 +252,7 @@ class PubsubAttachments:
|
|||
item: str,
|
||||
senders_s: List[str],
|
||||
extra_s: str,
|
||||
profile_key: str
|
||||
profile_key: str,
|
||||
) -> defer.Deferred:
|
||||
client = self.host.get_client(profile_key)
|
||||
extra = data_format.deserialise(extra_s)
|
||||
|
|
@ -274,9 +261,7 @@ class PubsubAttachments:
|
|||
self.get_attachments(client, jid.JID(service_s), node, item, senders)
|
||||
)
|
||||
d.addCallback(
|
||||
lambda ret:
|
||||
(data_format.serialise(ret[0]),
|
||||
data_format.serialise(ret[1]))
|
||||
lambda ret: (data_format.serialise(ret[0]), data_format.serialise(ret[1]))
|
||||
)
|
||||
return d
|
||||
|
||||
|
|
@ -287,7 +272,7 @@ class PubsubAttachments:
|
|||
node: str,
|
||||
item: str,
|
||||
senders: Optional[List[jid.JID]],
|
||||
extra: Optional[dict] = None
|
||||
extra: Optional[dict] = None,
|
||||
) -> Tuple[List[Dict[str, Any]], dict]:
|
||||
"""Retrieve data attached to a pubsub item
|
||||
|
||||
|
|
@ -316,13 +301,9 @@ class PubsubAttachments:
|
|||
|
||||
return list_data, metadata
|
||||
|
||||
def _set(
|
||||
self,
|
||||
attachments_s: str,
|
||||
profile_key: str
|
||||
) -> None:
|
||||
def _set(self, attachments_s: str, profile_key: str) -> None:
|
||||
client = self.host.get_client(profile_key)
|
||||
attachments = data_format.deserialise(attachments_s) or {}
|
||||
attachments = data_format.deserialise(attachments_s) or {}
|
||||
return defer.ensureDeferred(self.set_attachements(client, attachments))
|
||||
|
||||
async def apply_set_handler(
|
||||
|
|
@ -389,9 +370,7 @@ class PubsubAttachments:
|
|||
return item_elt
|
||||
|
||||
async def set_attachements(
|
||||
self,
|
||||
client: SatXMPPEntity,
|
||||
attachments_data: Dict[str, Any]
|
||||
self, client: SatXMPPEntity, attachments_data: Dict[str, Any]
|
||||
) -> None:
|
||||
"""Set or update attachments
|
||||
|
||||
|
|
@ -414,9 +393,7 @@ class PubsubAttachments:
|
|||
node = attachments_data["node"]
|
||||
item = attachments_data["id"]
|
||||
except (KeyError, RuntimeError):
|
||||
raise ValueError(
|
||||
'data must have "service", "node" and "id" set'
|
||||
)
|
||||
raise ValueError('data must have "service", "node" and "id" set')
|
||||
attachment_node = self.get_attachment_node_name(service, node, item)
|
||||
try:
|
||||
items, __ = await self._p.get_items(
|
||||
|
|
@ -465,7 +442,6 @@ class PubsubAttachments:
|
|||
attachment_node = self.get_attachment_node_name(service, node, item)
|
||||
await self._p.subscribe(client, service, attachment_node)
|
||||
|
||||
|
||||
def set_timestamp(self, attachment_elt: domish.Element, data: dict) -> None:
|
||||
"""Check if a ``timestamp`` attribute is set, parse it, and fill data
|
||||
|
||||
|
|
@ -489,15 +465,11 @@ class PubsubAttachments:
|
|||
data: Dict[str, Any],
|
||||
) -> None:
|
||||
try:
|
||||
noticed_elt = next(
|
||||
attachments_elt.elements(NS_PUBSUB_ATTACHMENTS, "noticed")
|
||||
)
|
||||
noticed_elt = next(attachments_elt.elements(NS_PUBSUB_ATTACHMENTS, "noticed"))
|
||||
except StopIteration:
|
||||
pass
|
||||
else:
|
||||
noticed_data = {
|
||||
"noticed": True
|
||||
}
|
||||
noticed_data = {"noticed": True}
|
||||
self.set_timestamp(noticed_elt, noticed_data)
|
||||
data["noticed"] = noticed_data
|
||||
|
||||
|
|
@ -505,7 +477,7 @@ class PubsubAttachments:
|
|||
self,
|
||||
client: SatXMPPEntity,
|
||||
data: Dict[str, Any],
|
||||
former_elt: Optional[domish.Element]
|
||||
former_elt: Optional[domish.Element],
|
||||
) -> Optional[domish.Element]:
|
||||
"""add or remove a <noticed> attachment
|
||||
|
||||
|
|
@ -517,10 +489,7 @@ class PubsubAttachments:
|
|||
return former_elt
|
||||
elif noticed:
|
||||
return domish.Element(
|
||||
(NS_PUBSUB_ATTACHMENTS, "noticed"),
|
||||
attribs = {
|
||||
"timestamp": xmpp_date()
|
||||
}
|
||||
(NS_PUBSUB_ATTACHMENTS, "noticed"), attribs={"timestamp": xmpp_date()}
|
||||
)
|
||||
else:
|
||||
return None
|
||||
|
|
@ -549,7 +518,7 @@ class PubsubAttachments:
|
|||
self,
|
||||
client: SatXMPPEntity,
|
||||
data: Dict[str, Any],
|
||||
former_elt: Optional[domish.Element]
|
||||
former_elt: Optional[domish.Element],
|
||||
) -> Optional[domish.Element]:
|
||||
"""update the <reaction> attachment"""
|
||||
reactions_data = data["extra"].get("reactions")
|
||||
|
|
@ -557,9 +526,11 @@ class PubsubAttachments:
|
|||
return former_elt
|
||||
operation_type = reactions_data.get("operation", "update")
|
||||
if operation_type == "update":
|
||||
former_reactions = {
|
||||
str(r) for r in former_elt.elements(NS_PUBSUB_ATTACHMENTS, "reaction")
|
||||
} if former_elt is not None else set()
|
||||
former_reactions = (
|
||||
{str(r) for r in former_elt.elements(NS_PUBSUB_ATTACHMENTS, "reaction")}
|
||||
if former_elt is not None
|
||||
else set()
|
||||
)
|
||||
added_reactions = set(reactions_data.get("add") or [])
|
||||
removed_reactions = set(reactions_data.get("remove") or [])
|
||||
reactions = list((former_reactions | added_reactions) - removed_reactions)
|
||||
|
|
@ -569,10 +540,7 @@ class PubsubAttachments:
|
|||
raise exceptions.DataError(f"invalid reaction operation: {operation_type!r}")
|
||||
if reactions:
|
||||
reactions_elt = domish.Element(
|
||||
(NS_PUBSUB_ATTACHMENTS, "reactions"),
|
||||
attribs = {
|
||||
"timestamp": xmpp_date()
|
||||
}
|
||||
(NS_PUBSUB_ATTACHMENTS, "reactions"), attribs={"timestamp": xmpp_date()}
|
||||
)
|
||||
for reactions_data in reactions:
|
||||
reactions_elt.addElement("reaction", content=reactions_data)
|
||||
|
|
|
|||
Loading…
Reference in a new issue