From 76f39ca0c44466e51d7f0cb32d8b468e479993f5 Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 16 Mar 2018 18:43:11 +0100 Subject: [PATCH] component file sharing: comments metadata: new 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). --- src/plugins/plugin_comp_file_sharing.py | 47 +++++++++++++++++++++++++ src/plugins/plugin_xep_0264.py | 2 ++ src/plugins/plugin_xep_0329.py | 7 ++++ 3 files changed, 56 insertions(+) diff --git a/src/plugins/plugin_comp_file_sharing.py b/src/plugins/plugin_comp_file_sharing.py index 396cdf3f..2e03377e 100755 --- a/src/plugins/plugin_comp_file_sharing.py +++ b/src/plugins/plugin_comp_file_sharing.py @@ -23,6 +23,7 @@ from sat.core import exceptions 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 @@ PLUGIN_INFO = { } 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 @@ class FileSharing(object): 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 @@ class FileSharing(object): @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 @@ class FileSharing(object): 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 @@ class FileSharing(object): @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 @@ class FileSharing(object): 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()) diff --git a/src/plugins/plugin_xep_0264.py b/src/plugins/plugin_xep_0264.py index 17923717..b60a5376 100755 --- a/src/plugins/plugin_xep_0264.py +++ b/src/plugins/plugin_xep_0264.py @@ -91,6 +91,7 @@ class XEP_0264(object): 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 @@ class XEP_0264(object): if thumbnails: file_data.setdefault('extra', {})[C.KEY_THUMBNAILS] = thumbnails + return True ## thumbnails generation ## diff --git a/src/plugins/plugin_xep_0329.py b/src/plugins/plugin_xep_0329.py index 6e2ff9c2..adede619 100644 --- a/src/plugins/plugin_xep_0329.py +++ b/src/plugins/plugin_xep_0329.py @@ -423,6 +423,11 @@ class XEP_0329(object): @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 @@ class XEP_0329(object): 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']))