comparison sat/plugins/plugin_exp_list_of_interest.py @ 3322:56ee491c0df6

plugin list for interest: build ID using URI or file path metadata: by using those metadata, we get the same ID for 2 interests linking to the same location. This has been done to avoid duplicated interests.
author Goffi <goffi@goffi.org>
date Sat, 01 Aug 2020 16:17:19 +0200
parents 9a174abafdee
children 7515e1878004
comparison
equal deleted inserted replaced
3321:8bbd2ed924e8 3322:56ee491c0df6
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 from sat.tools.common import data_format 23 from sat.tools.common import data_format
24 from sat.tools.common import uri
24 from wokkel import disco, iwokkel, pubsub 25 from wokkel import disco, iwokkel, pubsub
25 from zope.interface import implementer 26 from zope.interface import implementer
26 from twisted.internet import defer 27 from twisted.internet import defer
27 from twisted.words.protocols.jabber import error as jabber_error, jid 28 from twisted.words.protocols.jabber import error as jabber_error, jid
28 from twisted.words.protocols.jabber.xmlstream import XMPPHandler 29 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
132 pubsub_elt["item"] = item_id 133 pubsub_elt["item"] = item_id
133 if creator: 134 if creator:
134 pubsub_elt["creator"] = C.BOOL_TRUE 135 pubsub_elt["creator"] = C.BOOL_TRUE
135 if element is not None: 136 if element is not None:
136 pubsub_elt.addChild(element) 137 pubsub_elt.addChild(element)
137 item_elt = pubsub.Item(payload=interest_elt) 138 uri_kwargs = {
139 "path": service,
140 "node": node
141 }
142 if item_id:
143 uri_kwargs['id'] = item_id
144 interest_uri = uri.buildXMPPUri("pubsub", **uri_kwargs)
145 # we use URI of the interest as item id to avoid duplicates
146 item_elt = pubsub.Item(interest_uri, payload=interest_elt)
138 yield self._p.publish( 147 yield self._p.publish(
139 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] 148 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
140 ) 149 )
141 150
142 def _registerFileSharing( 151 def _registerFileSharing(
180 file_sharing_elt["type"] = repos_type 189 file_sharing_elt["type"] = repos_type
181 if namespace is not None: 190 if namespace is not None:
182 file_sharing_elt["namespace"] = namespace 191 file_sharing_elt["namespace"] = namespace
183 if path is not None: 192 if path is not None:
184 file_sharing_elt["path"] = path 193 file_sharing_elt["path"] = path
185 item_elt = pubsub.Item(payload=interest_elt) 194 item_id = f"{service}_{namespace or ''}_{path or ''}"
195 item_elt = pubsub.Item(item_id, payload=interest_elt)
186 await self._p.publish( 196 await self._p.publish(
187 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] 197 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
188 ) 198 )
189 199
190 def _listInterestsSerialise(self, interests_data): 200 def _listInterestsSerialise(self, interests_data):