plugin pubsub signing: pubsub items signature implementation:

- this is based on a protoXEP, not yet an official XEP: https://github.com/xsf/xeps/pull/1228
- XEP-0470: `set` attachment handler can now be async

rel 381
This commit is contained in:
Goffi 2022-10-28 18:47:17 +02:00
commit 4f13a79f87

View file

@ -30,7 +30,7 @@ from sat.core.log import getLogger
from sat.core.core_types import SatXMPPEntity
from sat.core import exceptions
from sat.tools.common import uri, data_format, date_utils
from sat.tools.utils import xmpp_date
from sat.tools.utils import asDeferred, xmpp_date
log = getLogger(__name__)
@ -63,10 +63,10 @@ class PubsubAttachments:
self._p = host.plugins["XEP-0060"]
self.handlers: Dict[Tuple[str, str], dict[str, Any]] = {}
host.trigger.add("XEP-0277_send", self.onMBSend)
self.registerAttachmentHandler(
self.register_attachment_handler(
"noticed", NS_PUBSUB_ATTACHMENTS, self.noticedGet, self.noticedSet
)
self.registerAttachmentHandler(
self.register_attachment_handler(
"reactions", NS_PUBSUB_ATTACHMENTS, self.reactionsGet, self.reactionsSet
)
host.bridge.addMethod(
@ -89,7 +89,7 @@ class PubsubAttachments:
def getHandler(self, client):
return PubsubAttachments_Handler()
def registerAttachmentHandler(
def register_attachment_handler(
self,
name: str,
namespace: str,
@ -113,6 +113,7 @@ class PubsubAttachments:
it will be called with (client, data, former_elt of None if there was no
former element). When suitable, ``operation`` should be used to check if we
request an ``update`` or a ``replace``.
The callback can be either a blocking method, a Deferred or a coroutine
"""
key = (name, namespace)
if key in self.handlers:
@ -299,7 +300,7 @@ class PubsubAttachments:
@return: A tuple with:
- the list of attachments data, one item per found sender. The attachments
data are dict containing attachment, no ``extra`` field is used here
(contrarily to attachments data used with ``setAttachments``).
(contrarily to attachments data used with ``set_attachements``).
- metadata returned by the call to ``getItems``
"""
if extra is None:
@ -320,9 +321,9 @@ class PubsubAttachments:
) -> None:
client = self.host.getClient(profile_key)
attachments = data_format.deserialise(attachments_s) or {}
return defer.ensureDeferred(self.setAttachments(client, attachments))
return defer.ensureDeferred(self.set_attachements(client, attachments))
def applySetHandler(
async def apply_set_handler(
self,
client: SatXMPPEntity,
attachments_data: dict,
@ -375,7 +376,9 @@ class PubsubAttachments:
former_elt = next(attachments_elt.elements(namespace, name))
except StopIteration:
former_elt = None
new_elt = handler["set"](client, attachments_data, former_elt)
new_elt = await asDeferred(
handler["set"], client, attachments_data, former_elt
)
if new_elt != former_elt:
if former_elt is not None:
attachments_elt.children.remove(former_elt)
@ -383,7 +386,7 @@ class PubsubAttachments:
attachments_elt.addChild(new_elt)
return item_elt
async def setAttachments(
async def set_attachements(
self,
client: SatXMPPEntity,
attachments_data: Dict[str, Any]
@ -425,7 +428,7 @@ class PubsubAttachments:
else:
item_elt = items[0]
item_elt = self.applySetHandler(
item_elt = await self.apply_set_handler(
client,
attachments_data,
item_elt=item_elt,