annotate src/plugins/plugin_comp_file_sharing.py @ 2527:a201194fc461

component file sharing: comments handling first draft: comments use a minimal pubsub service which create virtual nodes for each files. A pubsub request to org.salut-a-toi.file_comments/[FILE_ID] allow to handle comments in classic way. Permissions are the same as for files (i.e. if an entity can see a file, she can comment it).
author Goffi <goffi@goffi.org>
date Fri, 16 Mar 2018 17:06:35 +0100
parents 95c31756944c
children 65e278997715
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for parrot mode (experimental)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import exceptions
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.log import getLogger
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.tools.common import regex
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.tools import stream
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.internet import defer
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
28 from twisted.words.protocols.jabber import error
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
29 from wokkel import pubsub
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
30 from wokkel import generic
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
31 from functools import partial
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import os
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 import os.path
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
34 import mimetypes
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 PLUGIN_INFO = {
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_NAME: "File sharing component",
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_IMPORT_NAME: "file_sharing",
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_MODES: [C.PLUG_MODE_COMPONENT],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_PROTOCOLS: [],
2522
95c31756944c component file sharing, plugin XEP-0231: thumbnail are now returned by component using Bits of Binary:
Goffi <goffi@goffi.org>
parents: 2519
diff changeset
43 C.PI_DEPENDENCIES: ["FILE", "XEP-0231", "XEP-0234", "XEP-0260", "XEP-0261", "XEP-0264", "XEP-0329"],
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_RECOMMENDATIONS: [],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_MAIN: "FileSharing",
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
46 C.PI_HANDLER: C.BOOL_TRUE,
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_DESCRIPTION: _(u"""Component hosting and sharing files""")
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 }
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 HASH_ALGO = u'sha-256'
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
51 COMMENT_NODE_PREFIX = 'org.salut-a-toi.file_comments/'
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 class FileSharing(object):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def __init__(self, host):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 log.info(_(u"File Sharing initialization"))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.host = host
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self._f = host.plugins['FILE']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self._jf = host.plugins['XEP-0234']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self._h = host.plugins['XEP-0300']
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
62 self._t = host.plugins['XEP-0264']
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 host.trigger.add("FILE_getDestDir", self._getDestDirTrigger)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
67 def getHandler(self, client):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
68 return Comments_handler(self)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
69
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def profileConnected(self, client):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 path = client.file_tmp_dir = os.path.join(
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.host.memory.getConfig('', 'local_dir'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 C.FILES_TMP_DIR,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 regex.pathEscape(client.profile))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 if not os.path.exists(path):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 os.makedirs(path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 @defer.inlineCallbacks
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def _fileTransferedCb(self, dummy, client, peer_jid, file_data, file_path):
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
80 name = file_data[u'name']
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
81 extra = {}
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
82
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 if file_data[u'hash_algo'] == HASH_ALGO:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 log.debug(_(u"Reusing already generated hash"))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 file_hash = file_data[u'hash_hasher'].hexdigest()
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 else:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 hasher = self._h.getHasher(HASH_ALGO)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 with open('file_path') as f:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 file_hash = yield self._h.calculateHash(f, hasher)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 final_path = os.path.join(self.files_path, file_hash)
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
91
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 if os.path.isfile(final_path):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 log.debug(u"file [{file_hash}] already exists, we can remove temporary one".format(file_hash = file_hash))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 os.unlink(file_path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 else:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 os.rename(file_path, final_path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 log.debug(u"file [{file_hash}] moved to {files_path}".format(file_hash=file_hash, files_path=self.files_path))
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
98
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
99 mime_type = file_data.get(u'mime_type')
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
100 if not mime_type or mime_type == u'application/octet-stream':
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
101 mime_type = mimetypes.guess_type(name)[0]
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
102
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
103 if mime_type is not None and mime_type.startswith(u'image'):
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
104 thumbnails = extra.setdefault(C.KEY_THUMBNAILS, [])
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
105 for max_thumb_size in (self._t.SIZE_SMALL, self._t.SIZE_MEDIUM):
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
106 try:
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
107 thumb_size, thumb_id = yield self._t.generateThumbnail(final_path,
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
108 max_thumb_size,
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
109 # we keep thumbnails for 6 months
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
110 60*60*24*31*6)
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
111 except Exception as e:
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
112 log.warning(_(u"Can't create thumbnail: {reason}").format(reason=e))
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
113 break
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
114 thumbnails.append({u'id': thumb_id, u'size': thumb_size})
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
115
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 self.host.memory.setFile(client,
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
117 name=name,
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 version=u'',
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 file_hash=file_hash,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 hash_algo=HASH_ALGO,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 size=file_data[u'size'],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 path=file_data.get(u'path'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 namespace=file_data.get(u'namespace'),
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
124 mime_type=mime_type,
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
125 owner=peer_jid,
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
126 extra=extra)
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 def _getDestDirTrigger(self, client, peer_jid, transfer_data, file_data, stream_object):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 if not client.is_component:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 return True, None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 assert stream_object
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 assert 'stream_object' not in transfer_data
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
133 assert C.KEY_PROGRESS_ID in file_data
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 filename = file_data['name']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 assert filename and not '/' in filename
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 file_tmp_dir = self.host.getLocalPath(client, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 file_tmp_path = file_data['file_path'] = os.path.join(file_tmp_dir, file_data['name'])
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 transfer_data['finished_d'].addCallback(self._fileTransferedCb, client, peer_jid, file_data, file_tmp_path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 self._f.openFileWrite(client, file_tmp_path, transfer_data, file_data, stream_object)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 return False, defer.succeed(True)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 @defer.inlineCallbacks
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 def _retrieveFiles(self, client, session, content_data, content_name, file_data, file_elt):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 peer_jid = session[u'peer_jid']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 try:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 found_files = yield self.host.memory.getFiles(client,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 peer_jid=peer_jid,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 name=file_data.get(u'name'),
2519
353880a5c363 component file sharing: fixed hash handling (key has been renamed to "file_hash")
Goffi <goffi@goffi.org>
parents: 2514
diff changeset
151 file_hash=file_data.get(u'file_hash'),
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 hash_algo=file_data.get(u'hash_algo'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 path=file_data.get(u'path'),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 namespace=file_data.get(u'namespace'))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 except exceptions.NotFound:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 found_files = None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 except exceptions.PermissionError:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 log.warning(_(u"{peer_jid} is trying to access an unauthorized file: {name}").format(
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 peer_jid=peer_jid, name=file_data.get(u'name')))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 defer.returnValue(False)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if not found_files:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 log.warning(_(u"no matching file found ({file_data})").format(file_data=file_data))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 defer.returnValue(False)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 # we only use the first found file
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 found_file = found_files[0]
2519
353880a5c363 component file sharing: fixed hash handling (key has been renamed to "file_hash")
Goffi <goffi@goffi.org>
parents: 2514
diff changeset
168 file_hash = found_file[u'file_hash']
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 file_path = os.path.join(self.files_path, file_hash)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 file_data[u'hash_hasher'] = hasher = self._h.getHasher(found_file[u'hash_algo'])
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 size = file_data[u'size'] = found_file[u'size']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 file_data[u'file_hash'] = file_hash
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 file_data[u'hash_algo'] = found_file[u'hash_algo']
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 # we complete file_elt so peer can have some details on the file
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 if u'name' not in file_data:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 file_elt.addElement(u'name', content=found_file[u'name'])
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 file_elt.addElement(u'size', content=unicode(size))
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 content_data['stream_object'] = stream.FileStreamObject(
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 self.host,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 client,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 file_path,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 uid=self._jf.getProgressId(session, content_name),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 size=size,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 data_cb=lambda data: hasher.update(data),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 )
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 defer.returnValue(True)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 def _fileSendingRequestTrigger(self, client, session, content_data, content_name, file_data, file_elt):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 if not client.is_component:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 return True, None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 else:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 return False, self._retrieveFiles(client, session, content_data, content_name, file_data, file_elt)
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
194
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
195
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
196 class Comments_handler(pubsub.PubSubService):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
197
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
198 def __init__(self, plugin_parent):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
199 super(Comments_handler, self).__init__() # PubsubVirtualResource())
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
200 self.host = plugin_parent.host
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
201 self.plugin_parent = plugin_parent
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
202
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
203 def _getFileId(self, nodeIdentifier):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
204 if not nodeIdentifier.startswith(COMMENT_NODE_PREFIX):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
205 raise error.StanzaError('item-not-found')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
206 file_id = nodeIdentifier[len(COMMENT_NODE_PREFIX):]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
207 if not file_id:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
208 raise error.StanzaError('item-not-found')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
209 return file_id
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
210
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
211 @defer.inlineCallbacks
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
212 def getFileData(self, requestor, nodeIdentifier):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
213 file_id = self._getFileId(nodeIdentifier)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
214 try:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
215 files = yield self.host.memory.getFiles(self.parent, requestor, file_id)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
216 except (exceptions.NotFound, exceptions.PermissionError):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
217 # we don't differenciate between NotFound and PermissionError
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
218 # to avoid leaking information on existing files
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
219 raise error.StanzaError('item-not-found')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
220 if not files:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
221 raise error.StanzaError('item-not-found')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
222 if len(files) > 1:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
223 raise error.InternalError('there should be only one file')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
224 defer.returnValue(files[0])
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
225
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
226 def commentsUpdate(self, extra, new_comments, peer_jid):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
227 """update comments (replace or insert new_comments)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
228
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
229 @param extra(dict): extra data to update
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
230 @param new_comments(list[tuple(unicode, unicode, unicode)]): comments to update or insert
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
231 @param peer_jid(unicode, None): bare jid of the requestor, or None if request is done by owner
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
232 """
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
233 current_comments = extra.setdefault('comments', [])
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
234 new_comments_by_id = {c[0]:c for c in new_comments}
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
235 updated = []
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
236 # we now check every current comment, to see if one id in new ones
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
237 # exist, in which case we must update
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
238 for idx, comment in enumerate(current_comments):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
239 comment_id = comment[0]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
240 if comment_id in new_comments_by_id:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
241 # a new comment has an existing id, update is requested
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
242 if peer_jid and comment[1] != peer_jid:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
243 # requestor has not the right to modify the comment
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
244 raise exceptions.PermissionError
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
245 # we replace old_comment with updated one
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
246 new_comment = new_comments_by_id[comment_id]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
247 current_comments[idx] = new_comment
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
248 updated.append(new_comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
249
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
250 # we now remove every updated comments, to only keep
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
251 # the ones to insert
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
252 for comment in updated:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
253 new_comments.remove(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
254
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
255 current_comments.extend(new_comments)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
256
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
257 def commentsDelete(self, extra, comments):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
258 try:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
259 comments_dict = extra['comments']
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
260 except KeyError:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
261 return
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
262 for comment in comments:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
263 try:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
264 comments_dict.remove(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
265 except ValueError:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
266 continue
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
267
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
268 def _getFrom(self, item_elt):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
269 """retrieve published of an item
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
270
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
271 @param item_elt(domish.element): <item> element
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
272 @return (unicode): full jid as string
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
273 """
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
274 iq_elt = item_elt
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
275 while iq_elt.parent != None:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
276 iq_elt = iq_elt.parent
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
277 return iq_elt['from']
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
278
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
279 @defer.inlineCallbacks
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
280 def publish(self, requestor, service, nodeIdentifier, items):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
281 # we retrieve file a first time to check authorisations
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
282 file_data = yield self.getFileData(requestor, nodeIdentifier)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
283 file_id = file_data['id']
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
284 comments = [(item['id'], self._getFrom(item), item.toXml()) for item in items]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
285 if requestor.userhostJID() == file_data['owner']:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
286 peer_jid = None
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
287 else:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
288 peer_jid = requestor.userhost()
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
289 update_cb = partial(self.commentsUpdate, new_comments=comments, peer_jid=peer_jid)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
290 try:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
291 yield self.host.memory.fileUpdate(file_id, 'extra', update_cb)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
292 except exceptions.PermissionError:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
293 raise error.StanzaError('not-authorized')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
294
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
295 @defer.inlineCallbacks
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
296 def items(self, requestor, service, nodeIdentifier, maxItems,
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
297 itemIdentifiers):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
298 file_data = yield self.getFileData(requestor, nodeIdentifier)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
299 comments = file_data['extra'].get('comments', [])
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
300 if itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
301 defer.returnValue([generic.parseXml(c[2]) for c in comments if c[0] in itemIdentifiers])
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
302 else:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
303 defer.returnValue([generic.parseXml(c[2]) for c in comments])
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
304
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
305 @defer.inlineCallbacks
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
306 def retract(self, requestor, service, nodeIdentifier, itemIdentifiers):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
307 file_data = yield self.getFileData(requestor, nodeIdentifier)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
308 file_id = file_data['id']
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
309 try:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
310 comments = file_data['extra']['comments']
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
311 except KeyError:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
312 raise error.StanzaError('item-not-found')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
313
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
314 to_remove = []
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
315 for comment in comments:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
316 comment_id = comment[0]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
317 if comment_id in itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
318 to_remove.append(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
319 itemIdentifiers.remove(comment_id)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
320 if not itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
321 break
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
322
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
323 if itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
324 # not all items have been to_remove, we can't continue
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
325 raise error.StanzaError('item-not-found')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
326
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
327 if requestor.userhostJID() != file_data['owner']:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
328 if not all([c[1] == requestor.userhost() for c in to_remove]):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
329 raise error.StanzaError('not-authorized')
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
330
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
331 remove_cb = partial(self.commentsDelete, comments=to_remove)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
332 yield self.host.memory.fileUpdate(file_id, 'extra', remove_cb)