Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0277.py @ 3715:b9718216a1c0 0.9
merge bookmark 0.9
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 01 Dec 2021 16:13:31 +0100 |
parents | 68f2a9c171d1 |
children | a0c08fcfe11e |
comparison
equal
deleted
inserted
replaced
3714:af09b5aaa5d7 | 3715:b9718216a1c0 |
---|---|
38 | 38 |
39 from sat.core.i18n import _ | 39 from sat.core.i18n import _ |
40 from sat.core.constants import Const as C | 40 from sat.core.constants import Const as C |
41 from sat.core.log import getLogger | 41 from sat.core.log import getLogger |
42 from sat.core import exceptions | 42 from sat.core import exceptions |
43 from sat.core.xmpp import SatXMPPEntity | 43 from sat.core.core_types import SatXMPPEntity |
44 from sat.tools import xml_tools | 44 from sat.tools import xml_tools |
45 from sat.tools import sat_defer | 45 from sat.tools import sat_defer |
46 from sat.tools import utils | 46 from sat.tools import utils |
47 from sat.tools.common import data_format | 47 from sat.tools.common import data_format |
48 from sat.tools.common import uri as xmpp_uri | 48 from sat.tools.common import uri as xmpp_uri |
52 log = getLogger(__name__) | 52 log = getLogger(__name__) |
53 | 53 |
54 | 54 |
55 NS_MICROBLOG = "urn:xmpp:microblog:0" | 55 NS_MICROBLOG = "urn:xmpp:microblog:0" |
56 NS_ATOM = "http://www.w3.org/2005/Atom" | 56 NS_ATOM = "http://www.w3.org/2005/Atom" |
57 NS_PUBSUB_EVENT = "{}{}".format(pubsub.NS_PUBSUB, "#event") | 57 NS_PUBSUB_EVENT = f"{pubsub.NS_PUBSUB}#event" |
58 NS_COMMENT_PREFIX = "{}:comments/".format(NS_MICROBLOG) | 58 NS_COMMENT_PREFIX = f"{NS_MICROBLOG}:comments/" |
59 | 59 |
60 | 60 |
61 PLUGIN_INFO = { | 61 PLUGIN_INFO = { |
62 C.PI_NAME: "Microblogging over XMPP Plugin", | 62 C.PI_NAME: "Microblogging over XMPP Plugin", |
63 C.PI_IMPORT_NAME: "XEP-0277", | 63 C.PI_IMPORT_NAME: "XEP-0277", |
64 C.PI_TYPE: "XEP", | 64 C.PI_TYPE: "XEP", |
65 C.PI_PROTOCOLS: ["XEP-0277"], | 65 C.PI_PROTOCOLS: ["XEP-0277"], |
66 C.PI_DEPENDENCIES: ["XEP-0163", "XEP-0060", "TEXT_SYNTAXES"], | 66 C.PI_DEPENDENCIES: ["XEP-0163", "XEP-0060", "TEXT_SYNTAXES"], |
67 C.PI_RECOMMENDATIONS: ["XEP-0059", "EXTRA-PEP"], | 67 C.PI_RECOMMENDATIONS: ["XEP-0059", "EXTRA-PEP", "PUBSUB_CACHE"], |
68 C.PI_MAIN: "XEP_0277", | 68 C.PI_MAIN: "XEP_0277", |
69 C.PI_HANDLER: "yes", | 69 C.PI_HANDLER: "yes", |
70 C.PI_DESCRIPTION: _("""Implementation of microblogging Protocol"""), | 70 C.PI_DESCRIPTION: _("""Implementation of microblogging Protocol"""), |
71 } | 71 } |
72 | 72 |
83 self.host = host | 83 self.host = host |
84 host.registerNamespace("microblog", NS_MICROBLOG) | 84 host.registerNamespace("microblog", NS_MICROBLOG) |
85 self._p = self.host.plugins[ | 85 self._p = self.host.plugins[ |
86 "XEP-0060" | 86 "XEP-0060" |
87 ] # this facilitate the access to pubsub plugin | 87 ] # this facilitate the access to pubsub plugin |
88 ps_cache = self.host.plugins.get("PUBSUB_CACHE") | |
89 if ps_cache is not None: | |
90 ps_cache.registerAnalyser( | |
91 { | |
92 "name": "XEP-0277", | |
93 "node": NS_MICROBLOG, | |
94 "namespace": NS_ATOM, | |
95 "type": "blog", | |
96 "to_sync": True, | |
97 "parser": self.item2mbdata, | |
98 "match_cb": self._cacheNodeMatchCb, | |
99 } | |
100 ) | |
88 self.rt_sessions = sat_defer.RTDeferredSessions() | 101 self.rt_sessions = sat_defer.RTDeferredSessions() |
89 self.host.plugins["XEP-0060"].addManagedNode( | 102 self.host.plugins["XEP-0060"].addManagedNode( |
90 NS_MICROBLOG, items_cb=self._itemsReceived | 103 NS_MICROBLOG, items_cb=self._itemsReceived |
91 ) | 104 ) |
92 | 105 |
115 async_=True, | 128 async_=True, |
116 ) | 129 ) |
117 host.bridge.addMethod( | 130 host.bridge.addMethod( |
118 "mbGet", | 131 "mbGet", |
119 ".plugin", | 132 ".plugin", |
120 in_sign="ssiasa{ss}s", | 133 in_sign="ssiasss", |
121 out_sign="s", | 134 out_sign="s", |
122 method=self._mbGet, | 135 method=self._mbGet, |
123 async_=True, | 136 async_=True, |
124 ) | 137 ) |
125 host.bridge.addMethod( | 138 host.bridge.addMethod( |
176 method=self._mbGetFromManyWithComments, | 189 method=self._mbGetFromManyWithComments, |
177 ) | 190 ) |
178 | 191 |
179 def getHandler(self, client): | 192 def getHandler(self, client): |
180 return XEP_0277_handler() | 193 return XEP_0277_handler() |
194 | |
195 def _cacheNodeMatchCb( | |
196 self, | |
197 client: SatXMPPEntity, | |
198 analyse: dict, | |
199 ) -> None: | |
200 """Check is analysed node is a comment and fill analyse accordingly""" | |
201 if analyse["node"].startswith(NS_COMMENT_PREFIX): | |
202 analyse["subtype"] = "comment" | |
181 | 203 |
182 def _checkFeaturesCb(self, available): | 204 def _checkFeaturesCb(self, available): |
183 return {"available": C.BOOL_TRUE} | 205 return {"available": C.BOOL_TRUE} |
184 | 206 |
185 def _checkFeaturesEb(self, fail): | 207 def _checkFeaturesEb(self, fail): |
679 """Generate comment node | 701 """Generate comment node |
680 | 702 |
681 @param item_id(unicode): id of the parent item | 703 @param item_id(unicode): id of the parent item |
682 @return (unicode): comment node to use | 704 @return (unicode): comment node to use |
683 """ | 705 """ |
684 return "{}{}".format(NS_COMMENT_PREFIX, item_id) | 706 return f"{NS_COMMENT_PREFIX}{item_id}" |
685 | 707 |
686 def getCommentsService(self, client, parent_service=None): | 708 def getCommentsService(self, client, parent_service=None): |
687 """Get prefered PubSub service to create comment node | 709 """Get prefered PubSub service to create comment node |
688 | 710 |
689 @param pubsub_service(jid.JID, None): PubSub service of the parent item | 711 @param pubsub_service(jid.JID, None): PubSub service of the parent item |
928 def _mbGetSerialise(self, data): | 950 def _mbGetSerialise(self, data): |
929 items, metadata = data | 951 items, metadata = data |
930 metadata['items'] = items | 952 metadata['items'] = items |
931 return data_format.serialise(metadata) | 953 return data_format.serialise(metadata) |
932 | 954 |
933 def _mbGet(self, service="", node="", max_items=10, item_ids=None, extra_dict=None, | 955 def _mbGet(self, service="", node="", max_items=10, item_ids=None, extra="", |
934 profile_key=C.PROF_KEY_NONE): | 956 profile_key=C.PROF_KEY_NONE): |
935 """ | 957 """ |
936 @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit | 958 @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit |
937 @param item_ids (list[unicode]): list of item IDs | 959 @param item_ids (list[unicode]): list of item IDs |
938 """ | 960 """ |
939 client = self.host.getClient(profile_key) | 961 client = self.host.getClient(profile_key) |
940 service = jid.JID(service) if service else None | 962 service = jid.JID(service) if service else None |
941 max_items = None if max_items == C.NO_LIMIT else max_items | 963 max_items = None if max_items == C.NO_LIMIT else max_items |
942 extra = self._p.parseExtra(extra_dict) | 964 extra = self._p.parseExtra(data_format.deserialise(extra)) |
943 d = self.mbGet(client, service, node or None, max_items, item_ids, | 965 d = defer.ensureDeferred( |
966 self.mbGet(client, service, node or None, max_items, item_ids, | |
944 extra.rsm_request, extra.extra) | 967 extra.rsm_request, extra.extra) |
968 ) | |
945 d.addCallback(self._mbGetSerialise) | 969 d.addCallback(self._mbGetSerialise) |
946 return d | 970 return d |
947 | 971 |
948 @defer.inlineCallbacks | 972 async def mbGet(self, client, service=None, node=None, max_items=10, item_ids=None, |
949 def mbGet(self, client, service=None, node=None, max_items=10, item_ids=None, | |
950 rsm_request=None, extra=None): | 973 rsm_request=None, extra=None): |
951 """Get some microblogs | 974 """Get some microblogs |
952 | 975 |
953 @param service(jid.JID, None): jid of the publisher | 976 @param service(jid.JID, None): jid of the publisher |
954 None to get profile's PEP | 977 None to get profile's PEP |
955 @param node(unicode, None): node to get (or microblog node if None) | 978 @param node(unicode, None): node to get (or microblog node if None) |
956 @param max_items(int): maximum number of item to get, None for no limit | 979 @param max_items(int): maximum number of item to get, None for no limit |
980 ignored if rsm_request is set | |
957 @param item_ids (list[unicode]): list of item IDs | 981 @param item_ids (list[unicode]): list of item IDs |
958 @param rsm_request (rsm.RSMRequest): RSM request data | 982 @param rsm_request (rsm.RSMRequest): RSM request data |
959 @param extra (dict): extra data | 983 @param extra (dict): extra data |
960 | 984 |
961 @return: a deferred couple with the list of items and metadatas. | 985 @return: a deferred couple with the list of items and metadatas. |
962 """ | 986 """ |
963 if node is None: | 987 if node is None: |
964 node = NS_MICROBLOG | 988 node = NS_MICROBLOG |
965 items_data = yield self._p.getItems( | 989 if rsm_request: |
990 max_items = None | |
991 items_data = await self._p.getItems( | |
966 client, | 992 client, |
967 service, | 993 service, |
968 node, | 994 node, |
969 max_items=max_items, | 995 max_items=max_items, |
970 item_ids=item_ids, | 996 item_ids=item_ids, |
971 rsm_request=rsm_request, | 997 rsm_request=rsm_request, |
972 extra=extra, | 998 extra=extra, |
973 ) | 999 ) |
974 mb_data = yield self._p.transItemsDataD( | 1000 mb_data = await self._p.transItemsDataD( |
975 items_data, partial(self.item2mbdata, client, service=service, node=node)) | 1001 items_data, partial(self.item2mbdata, client, service=service, node=node)) |
976 defer.returnValue(mb_data) | 1002 return mb_data |
977 | 1003 |
978 def _mbRename(self, service, node, item_id, new_id, profile_key): | 1004 def _mbRename(self, service, node, item_id, new_id, profile_key): |
979 return defer.ensureDeferred(self.mbRename( | 1005 return defer.ensureDeferred(self.mbRename( |
980 self.host.getClient(profile_key), | 1006 self.host.getClient(profile_key), |
981 jid.JID(service) if service else None, | 1007 jid.JID(service) if service else None, |
1368 prefix = key[: key.find("_")] | 1394 prefix = key[: key.find("_")] |
1369 service_s = value | 1395 service_s = value |
1370 service = jid.JID(service_s) | 1396 service = jid.JID(service_s) |
1371 node = item["{}{}".format(prefix, "_node")] | 1397 node = item["{}{}".format(prefix, "_node")] |
1372 # time to get the comments | 1398 # time to get the comments |
1373 d = self._p.getItems( | 1399 d = defer.ensureDeferred( |
1374 client, | 1400 self._p.getItems( |
1375 service, | 1401 client, |
1376 node, | 1402 service, |
1377 max_comments, | 1403 node, |
1378 rsm_request=rsm_comments, | 1404 max_comments, |
1379 extra=extra_comments, | 1405 rsm_request=rsm_comments, |
1406 extra=extra_comments, | |
1407 ) | |
1380 ) | 1408 ) |
1381 # then serialise | 1409 # then serialise |
1382 d.addCallback( | 1410 d.addCallback( |
1383 lambda items_data: self._p.transItemsDataD( | 1411 lambda items_data: self._p.transItemsDataD( |
1384 items_data, | 1412 items_data, |
1416 items_d.addCallback(lambda items_completed: (items_completed, metadata)) | 1444 items_d.addCallback(lambda items_completed: (items_completed, metadata)) |
1417 return items_d | 1445 return items_d |
1418 | 1446 |
1419 deferreds = {} | 1447 deferreds = {} |
1420 for service, node in node_data: | 1448 for service, node in node_data: |
1421 d = deferreds[(service, node)] = self._p.getItems( | 1449 d = deferreds[(service, node)] = defer.ensureDeferred(self._p.getItems( |
1422 client, service, node, max_items, rsm_request=rsm_request, extra=extra | 1450 client, service, node, max_items, rsm_request=rsm_request, extra=extra |
1423 ) | 1451 )) |
1424 d.addCallback( | 1452 d.addCallback( |
1425 lambda items_data: self._p.transItemsDataD( | 1453 lambda items_data: self._p.transItemsDataD( |
1426 items_data, | 1454 items_data, |
1427 partial(self.item2mbdata, client, service=service, node=node), | 1455 partial(self.item2mbdata, client, service=service, node=node), |
1428 ) | 1456 ) |