Mercurial > libervia-backend
diff sat/plugins/plugin_comp_file_sharing.py @ 3314:5887fb414758
component file sharing: add/parse affiliation when possible
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 17 Jul 2020 13:00:10 +0200 |
parents | 9d1c0feba048 |
children | 15612c0fb421 |
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_file_sharing.py Fri Jul 17 12:58:57 2020 +0200 +++ b/sat/plugins/plugin_comp_file_sharing.py Fri Jul 17 13:00:10 2020 +0200 @@ -23,7 +23,6 @@ import shortuuid import unicodedata from urllib.parse import urljoin, urlparse, quote, unquote -from dataclasses import dataclass from pathlib import Path from sat.core.i18n import _ from sat.core.constants import Const as C @@ -69,6 +68,7 @@ HASH_ALGO = "sha-256" NS_COMMENTS = "org.salut-a-toi.comments" +NS_FS_AFFILIATION = "org.salut-a-toi.file-sharing-affiliation" COMMENT_NODE_PREFIX = "org.salut-a-toi.file_comments/" # Directory used to buffer request body (i.e. file in case of PUT) we use more than one @ # there, to be sure than it's not conflicting with a JID @@ -270,7 +270,7 @@ try: upload_id, filename = self.upload_data except exceptions.DataError as e: - log.warning("Invalid PUT request, we stop here: {e}") + log.warning(f"Invalid PUT request, we stop here: {e}") return self.refuseRequest() try: client, upload_request, timer = self.file_sharing.expected_uploads.pop(upload_id) @@ -343,9 +343,15 @@ self.host.trigger.add( "XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000 ) - self.host.trigger.add("XEP-0234_buildFileElement", self._addFileComments) - self.host.trigger.add("XEP-0234_parseFileElement", self._getFileComments) - self.host.trigger.add("XEP-0329_compGetFilesFromNode", self._addCommentsData) + self.host.trigger.add("XEP-0234_buildFileElement", self._addFileMetadataElts) + self.host.trigger.add("XEP-0234_parseFileElement", self._getFileMetadataElts) + self.host.trigger.add("XEP-0329_compGetFilesFromNode", self._addFileMetadata) + self.host.trigger.add( + "XEP-0329_compGetFilesFromNode_build_directory", + self._addDirectoryMetadataElts) + self.host.trigger.add( + "XEP-0329_parseResult_directory", + self._getDirectoryMetadataElts) self.files_path = self.host.getLocalPath(None, C.FILES_DIR, profile=False) self.http_port = self.host.memory.getConfig( 'component file_sharing', 'http_upload_port', 8888) @@ -353,19 +359,19 @@ 'component file_sharing', 'http_upload_connection_type', 'https') if connection_type not in ('http', 'https'): raise exceptions.ConfigError( - f'bad http_upload_connection_type, you must use one of "http" or "https"' + 'bad http_upload_connection_type, you must use one of "http" or "https"' ) self.server = FileSharingSite(self) self.expected_uploads = {} if connection_type == 'http': reactor.listenTCP(self.http_port, self.server) else: - options = tls.getOptionsFromConfig(self.host.memory.config, "component file_sharing") + options = tls.getOptionsFromConfig( + self.host.memory.config, "component file_sharing") tls.TLSOptionsCheck(options) context_factory = tls.getTLSContextFactory(options) reactor.listenSSL(self.http_port, self.server, context_factory) - def getHandler(self, client): return Comments_handler(self) @@ -581,9 +587,15 @@ ) return slot - ## comments triggers ## + ## metadata triggers ## - def _addFileComments(self, file_elt, extra_args): + def _addFileMetadataElts(self, client, file_elt, extra_args): + # affiliation + affiliation = extra_args.get('affiliation') + if affiliation is not None: + file_elt.addElement((NS_FS_AFFILIATION, "affiliation"), content=affiliation) + + # comments try: comments_url = extra_args.pop("comments_url") except KeyError: @@ -599,16 +611,27 @@ comment_elt["count"] = str(count) return True - def _getFileComments(self, file_elt, file_data): + def _getFileMetadataElts(self, client, file_elt, file_data): + # affiliation + try: + affiliation_elt = next(file_elt.elements(NS_FS_AFFILIATION, "affiliation")) + except StopIteration: + pass + else: + file_data["affiliation"] = str(affiliation_elt) + + # comments try: comments_elt = next(file_elt.elements(NS_COMMENTS, "comments")) except StopIteration: - return - file_data["comments_url"] = str(comments_elt) - file_data["comments_count"] = comments_elt["count"] + pass + else: + file_data["comments_url"] = str(comments_elt) + file_data["comments_count"] = comments_elt["count"] return True - def _addCommentsData(self, client, iq_elt, owner, node_path, files_data): + def _addFileMetadata( + self, client, iq_elt, iq_result_elt, owner, node_path, files_data): for file_data in files_data: file_data["comments_url"] = uri.buildXMPPUri( "pubsub", @@ -617,12 +640,30 @@ ) return True + def _addDirectoryMetadataElts( + self, client, file_data, directory_elt, owner, node_path): + affiliation = file_data.get('affiliation') + if affiliation is not None: + directory_elt.addElement( + (NS_FS_AFFILIATION, "affiliation"), + content=affiliation + ) + + def _getDirectoryMetadataElts( + self, client, elt, file_data): + try: + affiliation_elt = next(elt.elements((NS_FS_AFFILIATION, "affiliation"))) + except StopIteration: + pass + else: + file_data['affiliation'] = str(affiliation_elt) + 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()) + super(Comments_handler, self).__init__() self.host = plugin_parent.host self.plugin_parent = plugin_parent self.discoIdentity = {