changeset 2528:65e278997715

component file sharing: comments metadata: new <comments> element is added to file metadata, it contains the URL to the comments virtual node and the count of comments (this way client knows if it make sense to request comments or not). Fixed triggers in plugin XEP-0264 (return value was missing). New trigger in plugin XEP-0329 to allow component to add metadata (used here for comments url).
author Goffi <goffi@goffi.org>
date Fri, 16 Mar 2018 18:43:11 +0100
parents a201194fc461
children cbbf2ff2ef3f
files src/plugins/plugin_comp_file_sharing.py src/plugins/plugin_xep_0264.py src/plugins/plugin_xep_0329.py
diffstat 3 files changed, 56 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_comp_file_sharing.py	Fri Mar 16 17:06:35 2018 +0100
+++ b/src/plugins/plugin_comp_file_sharing.py	Fri Mar 16 18:43:11 2018 +0100
@@ -23,6 +23,7 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 from sat.tools.common import regex
+from sat.tools.common import uri
 from sat.tools import stream
 from twisted.internet import defer
 from twisted.words.protocols.jabber import error
@@ -48,6 +49,7 @@
 }
 
 HASH_ALGO = u'sha-256'
+NS_COMMENTS = 'org.salut-a-toi.comments'
 COMMENT_NODE_PREFIX = 'org.salut-a-toi.file_comments/'
 
 
@@ -62,6 +64,9 @@
         self._t = host.plugins['XEP-0264']
         host.trigger.add("FILE_getDestDir", self._getDestDirTrigger)
         host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000)
+        host.trigger.add("XEP-0234_buildFileElement", self._addFileComments)
+        host.trigger.add("XEP-0234_parseFileElement", self._getFileComments)
+        host.trigger.add("XEP-0329_compGetFilesFromNode", self._addCommentsData)
         self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False)
 
     def getHandler(self, client):
@@ -77,6 +82,11 @@
 
     @defer.inlineCallbacks
     def _fileTransferedCb(self, dummy, client, peer_jid, file_data, file_path):
+        """post file reception tasks
+
+        on file is received, this method create hash/thumbnails if necessary
+        move the file to the right location, and create metadata entry in database
+        """
         name = file_data[u'name']
         extra = {}
 
@@ -126,6 +136,7 @@
                                  extra=extra)
 
     def _getDestDirTrigger(self, client, peer_jid, transfer_data, file_data, stream_object):
+        """This trigger accept file sending request, and store file locally"""
         if not client.is_component:
             return True, None
         assert stream_object
@@ -143,6 +154,7 @@
 
     @defer.inlineCallbacks
     def _retrieveFiles(self, client, session, content_data, content_name, file_data, file_elt):
+        """This method retrieve a file on request, and send if after checking permissions"""
         peer_jid = session[u'peer_jid']
         try:
             found_files = yield self.host.memory.getFiles(client,
@@ -192,8 +204,43 @@
         else:
             return False, self._retrieveFiles(client, session, content_data, content_name, file_data, file_elt)
 
+    ## comments triggers ##
+
+    def _addFileComments(self, file_elt, extra_args):
+        try:
+            comments_url = extra_args.pop('comments_url')
+        except KeyError:
+            return
+
+        comment_elt = file_elt.addElement((NS_COMMENTS, 'comments'), content=comments_url)
+
+        try:
+            count = len(extra_args[u'extra'][u'comments'])
+        except KeyError:
+            count = 0
+
+        comment_elt['count'] = unicode(count)
+        return True
+
+    def _getFileComments(self, file_elt, file_data):
+        try:
+            comments_elt = next(file_elt.elements(NS_COMMENTS, 'comments'))
+        except StopIteration:
+            return
+        file_data['comments_url'] = unicode(comments_elt)
+        file_data['comments_count'] = comments_elt['count']
+        return True
+
+    def _addCommentsData(self, client, iq_elt, owner, node_path, files_data):
+        for file_data in files_data:
+            file_data['comments_url'] = uri.buildXMPPUri('pubsub',
+                                                         path=client.jid.full(),
+                                                         node=COMMENT_NODE_PREFIX + file_data['id'])
+        return True
+
 
 class Comments_handler(pubsub.PubSubService):
+    """This class is a minimal Pubsub service handling virtual nodes for comments"""
 
     def __init__(self, plugin_parent):
         super(Comments_handler, self).__init__() # PubsubVirtualResource())
--- a/src/plugins/plugin_xep_0264.py	Fri Mar 16 17:06:35 2018 +0100
+++ b/src/plugins/plugin_xep_0264.py	Fri Mar 16 18:43:11 2018 +0100
@@ -91,6 +91,7 @@
             width, height = thumbnail['size']
             thumbnail_elt['width'] = unicode(width)
             thumbnail_elt['height'] = unicode(height)
+        return True
 
     def _getFileThumbnails(self, file_elt, file_data):
         thumbnails = []
@@ -113,6 +114,7 @@
 
         if thumbnails:
             file_data.setdefault('extra', {})[C.KEY_THUMBNAILS] = thumbnails
+        return True
 
     ## thumbnails generation ##
 
--- a/src/plugins/plugin_xep_0329.py	Fri Mar 16 17:06:35 2018 +0100
+++ b/src/plugins/plugin_xep_0329.py	Fri Mar 16 18:43:11 2018 +0100
@@ -423,6 +423,11 @@
 
     @defer.inlineCallbacks
     def _compGetFilesFromNodeCb(self, client, iq_elt, owner, node_path):
+        """retrieve files from local files repository according to permissions
+
+        result stanza is then built and sent to requestor
+        @trigger XEP-0329_compGetFilesFromNode(client, iq_elt, owner, node_path, files_data): can be used to add data/elements
+        """
         peer_jid = jid.JID(iq_elt['from'])
         try:
             files_data = yield self.host.memory.getFiles(client, peer_jid=peer_jid, path=node_path, owner=owner)
@@ -432,6 +437,8 @@
         iq_result_elt = xmlstream.toResponse(iq_elt, 'result')
         query_elt = iq_result_elt.addElement((NS_FIS, 'query'))
         query_elt[u'node'] = node_path
+        if not self.host.trigger.point(u'XEP-0329_compGetFilesFromNode', client, iq_elt, owner, node_path, files_data):
+            return
         for file_data in files_data:
             file_elt = self._jf.buildFileElementFromDict(file_data,
                                                          modified=file_data.get(u'modified', file_data[u'created']))