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).
This commit is contained in:
Goffi 2018-03-16 18:43:11 +01:00
commit 76f39ca0c4

View file

@ -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())