comparison sat_pubsub/bookmark_compat.py @ 494:468b7cd6c344 default tip

bookmark compat: handle mapped errors: This convert error on request (e.g. missing node) to appropriate stanza error.
author Goffi <goffi@goffi.org>
date Fri, 13 Dec 2024 12:23:47 +0100
parents 4e8e8788bc86
children
comparison
equal deleted inserted replaced
493:33222c54d2af 494:468b7cd6c344
25 from twisted.words.xish import domish 25 from twisted.words.xish import domish
26 from wokkel import disco, iwokkel, pubsub 26 from wokkel import disco, iwokkel, pubsub
27 from wokkel.iwokkel import IPubSubService 27 from wokkel.iwokkel import IPubSubService
28 from zope.interface import implementer 28 from zope.interface import implementer
29 29
30 from sat_pubsub import backend
30 from sat_pubsub import const 31 from sat_pubsub import const
31 32
32 from . import error 33 from . import error
33 34
34 NS_IQ_PRIVATE = "jabber:iq:private" 35 NS_IQ_PRIVATE = "jabber:iq:private"
39 IQ_PRIVATE_SET = f'/iq[@type="set"]/query[@xmlns="{NS_IQ_PRIVATE}"]' 40 IQ_PRIVATE_SET = f'/iq[@type="set"]/query[@xmlns="{NS_IQ_PRIVATE}"]'
40 41
41 42
42 @implementer(iwokkel.IDisco) 43 @implementer(iwokkel.IDisco)
43 class BookmarkCompatHandler(disco.DiscoClientProtocol): 44 class BookmarkCompatHandler(disco.DiscoClientProtocol):
45 error_map = backend.PubSubResourceFromBackend.error_map
44 46
45 def __init__(self, service_jid): 47 def __init__(self, service_jid):
46 super().__init__() 48 super().__init__()
47 self.backend = None 49 self.backend = None
48 50
52 self._pubsub_service = handler 54 self._pubsub_service = handler
53 break 55 break
54 self.backend = self.parent.parent.getServiceNamed("backend") 56 self.backend = self.parent.parent.getServiceNamed("backend")
55 self.xmlstream.addObserver(IQ_PRIVATE_GET, self._on_get) 57 self.xmlstream.addObserver(IQ_PRIVATE_GET, self._on_get)
56 self.xmlstream.addObserver(IQ_PRIVATE_SET, self._on_set) 58 self.xmlstream.addObserver(IQ_PRIVATE_SET, self._on_set)
59
60 def handle_mapped_error(self, e: error.Error, iq_elt: domish.Element) -> None:
61 condition, pubsub_condition, feature = self.error_map[e.__class__] # type: ignore
62 msg = e.msg
63 if pubsub_condition:
64 exc = pubsub.PubSubError(condition, pubsub_condition, feature, msg)
65 else:
66 exc = jabber_error.StanzaError(condition, text=msg)
67 error_elt = exc.toResponse(iq_elt)
68 self.xmlstream.send(error_elt)
69 return
57 70
58 def _on_get(self, iq_elt: domish.Element) -> None: 71 def _on_get(self, iq_elt: domish.Element) -> None:
59 """Handle incoming legacy IQ GET requests for bookmarks. 72 """Handle incoming legacy IQ GET requests for bookmarks.
60 73
61 This method processes legacy XEP-0048 IQ GET requests. It does some checks and 74 This method processes legacy XEP-0048 IQ GET requests. It does some checks and
108 @param iq_elt: The incoming IQ element. 121 @param iq_elt: The incoming IQ element.
109 @param requestor: The JID of the entity requesting the bookmarks. 122 @param requestor: The JID of the entity requesting the bookmarks.
110 """ 123 """
111 assert self.backend is not None 124 assert self.backend is not None
112 assert self.xmlstream is not None 125 assert self.xmlstream is not None
113 items, __ = await self.backend.getItems( 126 try:
114 NS_BOOKMARKS2, requestor, requestor, ext_data={"pep": True} 127 items, __ = await self.backend.getItems(
115 ) 128 NS_BOOKMARKS2, requestor, requestor, ext_data={"pep": True}
129 )
130 except tuple(self.error_map.keys()) as e:
131 return self.handle_mapped_error(e, iq_elt)
132
116 iq_result_elt = xmlstream.toResponse(iq_elt, "result") 133 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
117 134
118 query_elt = iq_result_elt.addElement((NS_IQ_PRIVATE, "query")) 135 query_elt = iq_result_elt.addElement((NS_IQ_PRIVATE, "query"))
119 storage_elt = query_elt.addElement((NS_STORAGE_BOOKMARKS, "storage")) 136 storage_elt = query_elt.addElement((NS_STORAGE_BOOKMARKS, "storage"))
120 137
247 new_child = domish.Element((NS_BOOKMARKS2, child.name)) 264 new_child = domish.Element((NS_BOOKMARKS2, child.name))
248 new_child.addContent(str(child)) 265 new_child.addContent(str(child))
249 new_conference_elt.addChild(new_child) 266 new_conference_elt.addChild(new_child)
250 items.append(item_elt) 267 items.append(item_elt)
251 268
252 await self.publish_bookmarks(iq_elt, items, requestor) 269 try:
270 await self.publish_bookmarks(iq_elt, items, requestor)
271 except tuple(self.error_map.keys()) as e:
272 return self.handle_mapped_error(e, iq_elt)
253 273
254 iq_result_elt = xmlstream.toResponse(iq_elt, "result") 274 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
255 self.xmlstream.send(iq_result_elt) 275 self.xmlstream.send(iq_result_elt)
256 276
257 def getDiscoInfo(self, requestor, service, nodeIdentifier=""): 277 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):