changeset 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 2a7e36b69fd2
children 19bad15f4c0a
files sat/plugins/plugin_exp_invitation.py sat/plugins/plugin_exp_list_of_interest.py
diffstat 2 files changed, 27 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_invitation.py	Sat Sep 05 20:20:05 2020 +0200
+++ b/sat/plugins/plugin_exp_invitation.py	Sat Sep 05 20:22:23 2020 +0200
@@ -35,7 +35,7 @@
     C.PI_IMPORT_NAME: "INVITATION",
     C.PI_TYPE: "EXP",
     C.PI_PROTOCOLS: [],
-    C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0106", "XEP-0329"],
+    C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0329", "LIST_INTEREST"],
     C.PI_RECOMMENDATIONS: ["EMAIL_INVITATION"],
     C.PI_MAIN: "Invitation",
     C.PI_HANDLER: "yes",
@@ -163,12 +163,8 @@
         """
         if extra is None:
             extra = {}
-        # FIXME: Q&D fix as the bare file sharing service JID will lead to user own
-        #   repository, which thus would not be the same for the host and the guest.
-        #   By specifying the user part, we for the use of the host repository.
-        #   A cleaner way should be implemented
-        if service.user is None:
-            service.user = self.host.plugins['XEP-0106'].escape(client.jid.user)
+        li_plg = self.host.plugins["LIST_INTEREST"]
+        li_plg.normaliseFileSharingService(client, service)
 
         # FIXME: not the best place to adapt permission, but it's necessary to check them
         #   for UX
--- a/sat/plugins/plugin_exp_list_of_interest.py	Sat Sep 05 20:20:05 2020 +0200
+++ b/sat/plugins/plugin_exp_list_of_interest.py	Sat Sep 05 20:22:23 2020 +0200
@@ -19,6 +19,8 @@
 
 from sat.core.i18n import _
 from sat.core.constants import Const as C
+from sat.core.xmpp import SatXMPPEntity
+from sat.core import exceptions
 from sat.core.log import getLogger
 from sat.tools.common import data_format
 from sat.tools.common import uri
@@ -37,7 +39,7 @@
     C.PI_IMPORT_NAME: "LIST_INTEREST",
     C.PI_TYPE: "EXP",
     C.PI_PROTOCOLS: [],
-    C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0329"],
+    C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0329", "XEP-0106"],
     C.PI_RECOMMENDATIONS: [],
     C.PI_MAIN: "ListInterest",
     C.PI_HANDLER: "yes",
@@ -160,6 +162,16 @@
             name or None, extra
         ))
 
+    def normaliseFileSharingService(self, client, service):
+        # FIXME: Q&D fix as the bare file sharing service JID will lead to user own
+        #   repository, which thus would not be the same for the host and the guest.
+        #   By specifying the user part, we for the use of the host repository.
+        #   A cleaner way should be implemented
+        if service.user is None:
+            service.user = self.host.plugins['XEP-0106'].escape(client.jid.user)
+
+    def getFileSharingId(self, service, namespace, path):
+        return f"{service}_{namespace or ''}_{path or ''}"
 
     async def registerFileSharing(
             self, client, service, repos_type=None, namespace=None, path=None, name=None,
@@ -175,7 +187,9 @@
         """
         if extra is None:
             extra = {}
+        self.normaliseFileSharingService(client, service)
         await self.createNode(client)
+        item_id = self.getFileSharingId(service, namespace, path)
         interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
         interest_elt["namespace"] = self.host.getNamespace("fis")
         if name is not None:
@@ -183,6 +197,7 @@
         thumb_url = extra.get('thumb_url')
         if thumb_url:
             interest_elt['thumb_url'] = thumb_url
+
         file_sharing_elt = interest_elt.addElement("file_sharing")
         file_sharing_elt["service"] = service.full()
         if repos_type is not None:
@@ -191,7 +206,6 @@
             file_sharing_elt["namespace"] = namespace
         if path is not None:
             file_sharing_elt["path"] = path
-        item_id = f"{service}_{namespace or ''}_{path or ''}"
         item_elt = pubsub.Item(item_id, payload=interest_elt)
         await self._p.publish(
             client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
@@ -285,6 +299,14 @@
         d.addCallback(lambda __: None)
         return d
 
+    async def get(self, client: SatXMPPEntity, item_id: str) -> dict:
+        """Retrieve a specific interest in profile's list"""
+        items_data = await self._p.getItems(client, None, NS_LIST_INTEREST, item_ids=[item_id])
+        try:
+            return self._listInterestsSerialise(items_data)[0]
+        except IndexError:
+            raise exceptions.NotFound
+
 
 @implementer(iwokkel.IDisco)
 class ListInterestHandler(XMPPHandler):