comparison sat/plugins/plugin_exp_list_of_interest.py @ 3350:cc6164a4973b

plugin list of interests: normalize item ID + added `get` method
author Goffi <goffi@goffi.org>
date Sat, 05 Sep 2020 20:22:23 +0200
parents 7515e1878004
children be6d91572633
comparison
equal deleted inserted replaced
3349:2a7e36b69fd2 3350:cc6164a4973b
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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.xmpp import SatXMPPEntity
23 from sat.core import exceptions
22 from sat.core.log import getLogger 24 from sat.core.log import getLogger
23 from sat.tools.common import data_format 25 from sat.tools.common import data_format
24 from sat.tools.common import uri 26 from sat.tools.common import uri
25 from wokkel import disco, iwokkel, pubsub 27 from wokkel import disco, iwokkel, pubsub
26 from zope.interface import implementer 28 from zope.interface import implementer
35 PLUGIN_INFO = { 37 PLUGIN_INFO = {
36 C.PI_NAME: "List of Interest", 38 C.PI_NAME: "List of Interest",
37 C.PI_IMPORT_NAME: "LIST_INTEREST", 39 C.PI_IMPORT_NAME: "LIST_INTEREST",
38 C.PI_TYPE: "EXP", 40 C.PI_TYPE: "EXP",
39 C.PI_PROTOCOLS: [], 41 C.PI_PROTOCOLS: [],
40 C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0329"], 42 C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0329", "XEP-0106"],
41 C.PI_RECOMMENDATIONS: [], 43 C.PI_RECOMMENDATIONS: [],
42 C.PI_MAIN: "ListInterest", 44 C.PI_MAIN: "ListInterest",
43 C.PI_HANDLER: "yes", 45 C.PI_HANDLER: "yes",
44 C.PI_DESCRIPTION: _("Experimental handling of interesting XMPP locations"), 46 C.PI_DESCRIPTION: _("Experimental handling of interesting XMPP locations"),
45 } 47 }
158 return defer.ensureDeferred(self.registerFileSharing( 160 return defer.ensureDeferred(self.registerFileSharing(
159 client, jid.JID(service), repos_type or None, namespace or None, path or None, 161 client, jid.JID(service), repos_type or None, namespace or None, path or None,
160 name or None, extra 162 name or None, extra
161 )) 163 ))
162 164
165 def normaliseFileSharingService(self, client, service):
166 # FIXME: Q&D fix as the bare file sharing service JID will lead to user own
167 # repository, which thus would not be the same for the host and the guest.
168 # By specifying the user part, we for the use of the host repository.
169 # A cleaner way should be implemented
170 if service.user is None:
171 service.user = self.host.plugins['XEP-0106'].escape(client.jid.user)
172
173 def getFileSharingId(self, service, namespace, path):
174 return f"{service}_{namespace or ''}_{path or ''}"
163 175
164 async def registerFileSharing( 176 async def registerFileSharing(
165 self, client, service, repos_type=None, namespace=None, path=None, name=None, 177 self, client, service, repos_type=None, namespace=None, path=None, name=None,
166 extra=None): 178 extra=None):
167 """Register an interesting file repository in personal list 179 """Register an interesting file repository in personal list
173 @param name(unicode, None): name of the repository 185 @param name(unicode, None): name of the repository
174 @param extra(dict, None): same as [registerPubsub] 186 @param extra(dict, None): same as [registerPubsub]
175 """ 187 """
176 if extra is None: 188 if extra is None:
177 extra = {} 189 extra = {}
190 self.normaliseFileSharingService(client, service)
178 await self.createNode(client) 191 await self.createNode(client)
192 item_id = self.getFileSharingId(service, namespace, path)
179 interest_elt = domish.Element((NS_LIST_INTEREST, "interest")) 193 interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
180 interest_elt["namespace"] = self.host.getNamespace("fis") 194 interest_elt["namespace"] = self.host.getNamespace("fis")
181 if name is not None: 195 if name is not None:
182 interest_elt['name'] = name 196 interest_elt['name'] = name
183 thumb_url = extra.get('thumb_url') 197 thumb_url = extra.get('thumb_url')
184 if thumb_url: 198 if thumb_url:
185 interest_elt['thumb_url'] = thumb_url 199 interest_elt['thumb_url'] = thumb_url
200
186 file_sharing_elt = interest_elt.addElement("file_sharing") 201 file_sharing_elt = interest_elt.addElement("file_sharing")
187 file_sharing_elt["service"] = service.full() 202 file_sharing_elt["service"] = service.full()
188 if repos_type is not None: 203 if repos_type is not None:
189 file_sharing_elt["type"] = repos_type 204 file_sharing_elt["type"] = repos_type
190 if namespace is not None: 205 if namespace is not None:
191 file_sharing_elt["namespace"] = namespace 206 file_sharing_elt["namespace"] = namespace
192 if path is not None: 207 if path is not None:
193 file_sharing_elt["path"] = path 208 file_sharing_elt["path"] = path
194 item_id = f"{service}_{namespace or ''}_{path or ''}"
195 item_elt = pubsub.Item(item_id, payload=interest_elt) 209 item_elt = pubsub.Item(item_id, payload=interest_elt)
196 await self._p.publish( 210 await self._p.publish(
197 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt] 211 client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
198 ) 212 )
199 213
283 d = self._p._retractItem( 297 d = self._p._retractItem(
284 service_s, NS_LIST_INTEREST, item_id, True, profile_key) 298 service_s, NS_LIST_INTEREST, item_id, True, profile_key)
285 d.addCallback(lambda __: None) 299 d.addCallback(lambda __: None)
286 return d 300 return d
287 301
302 async def get(self, client: SatXMPPEntity, item_id: str) -> dict:
303 """Retrieve a specific interest in profile's list"""
304 items_data = await self._p.getItems(client, None, NS_LIST_INTEREST, item_ids=[item_id])
305 try:
306 return self._listInterestsSerialise(items_data)[0]
307 except IndexError:
308 raise exceptions.NotFound
309
288 310
289 @implementer(iwokkel.IDisco) 311 @implementer(iwokkel.IDisco)
290 class ListInterestHandler(XMPPHandler): 312 class ListInterestHandler(XMPPHandler):
291 313
292 def __init__(self, plugin_parent): 314 def __init__(self, plugin_parent):