core: various changes and fixes to work with new storage and D-Bus bridge:

- fixes coroutines handling in various places
- fixes types which are not serialised by Tx DBus
- XEP-0384: call storage methods in main thread in XEP: Python OMEMO's Promise use thread
  which prevent the use of AsyncIO loop. To work around that, callLater is used to launch
  storage method in main thread. This is a temporary workaround, as Python OMEMO should
  get rid of Promise implementation and threads soon.
This commit is contained in:
Goffi 2021-06-03 15:21:43 +02:00
commit 97df5f8be3

View file

@ -149,8 +149,7 @@ class FileSharingManagement(object):
payload = form.toElement()
return payload, status, None, None
@defer.inlineCallbacks
def _getFileData(self, client, session_data, command_form):
async def _getFileData(self, client, session_data, command_form):
"""Retrieve field requested in root form
"found_file" will also be set in session_data
@ -177,7 +176,7 @@ class FileSharingManagement(object):
# this must be managed
try:
found_files = yield self.host.memory.getFiles(
found_files = await self.host.memory.getFiles(
client, requestor_bare, path=parent_path, name=basename,
namespace=namespace)
found_file = found_files[0]
@ -193,7 +192,7 @@ class FileSharingManagement(object):
session_data['found_file'] = found_file
session_data['namespace'] = namespace
defer.returnValue(found_file)
return found_file
def _updateReadPermission(self, access, allowed_jids):
if not allowed_jids:
@ -209,29 +208,27 @@ class FileSharingManagement(object):
"jids": [j.full() for j in allowed_jids]
}
@defer.inlineCallbacks
def _updateDir(self, client, requestor, namespace, file_data, allowed_jids):
async def _updateDir(self, client, requestor, namespace, file_data, allowed_jids):
"""Recursively update permission of a directory and all subdirectories
@param file_data(dict): metadata of the file
@param allowed_jids(list[jid.JID]): list of entities allowed to read the file
"""
assert file_data['type'] == C.FILE_TYPE_DIRECTORY
files_data = yield self.host.memory.getFiles(
files_data = await self.host.memory.getFiles(
client, requestor, parent=file_data['id'], namespace=namespace)
for file_data in files_data:
if not file_data['access'].get(C.ACCESS_PERM_READ, {}):
log.debug("setting {perm} read permission for {name}".format(
perm=allowed_jids, name=file_data['name']))
yield self.host.memory.fileUpdate(
await self.host.memory.fileUpdate(
file_data['id'], 'access',
partial(self._updateReadPermission, allowed_jids=allowed_jids))
if file_data['type'] == C.FILE_TYPE_DIRECTORY:
yield self._updateDir(client, requestor, namespace, file_data, 'PUBLIC')
await self._updateDir(client, requestor, namespace, file_data, 'PUBLIC')
@defer.inlineCallbacks
def _onChangeFile(self, client, command_elt, session_data, action, node):
async def _onChangeFile(self, client, command_elt, session_data, action, node):
try:
x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
command_form = data_form.Form.fromElement(x_elt)
@ -244,14 +241,14 @@ class FileSharingManagement(object):
if command_form is None or len(command_form.fields) == 0:
# root request
defer.returnValue(self._getRootArgs())
return self._getRootArgs()
elif found_file is None:
# file selected, we retrieve it and ask for permissions
try:
found_file = yield self._getFileData(client, session_data, command_form)
found_file = await self._getFileData(client, session_data, command_form)
except WorkflowError as e:
defer.returnValue(e.err_args)
return e.err_args
# management request
if found_file['type'] == C.FILE_TYPE_DIRECTORY:
@ -284,7 +281,7 @@ class FileSharingManagement(object):
status = self._c.STATUS.EXECUTING
payload = form.toElement()
defer.returnValue((payload, status, None, None))
return (payload, status, None, None)
else:
# final phase, we'll do permission change here
@ -307,7 +304,7 @@ class FileSharingManagement(object):
self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
if found_file['type'] == C.FILE_TYPE_FILE:
yield self.host.memory.fileUpdate(
await self.host.memory.fileUpdate(
found_file['id'], 'access',
partial(self._updateReadPermission, allowed_jids=allowed_jids))
else:
@ -315,7 +312,7 @@ class FileSharingManagement(object):
recursive = command_form.fields['recursive']
except KeyError:
self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
yield self.host.memory.fileUpdate(
await self.host.memory.fileUpdate(
found_file['id'], 'access',
partial(self._updateReadPermission, allowed_jids=allowed_jids))
if recursive:
@ -323,17 +320,16 @@ class FileSharingManagement(object):
# already a permission set), so allowed entities of root directory
# can read them.
namespace = session_data['namespace']
yield self._updateDir(
await self._updateDir(
client, requestor_bare, namespace, found_file, 'PUBLIC')
# job done, we can end the session
status = self._c.STATUS.COMPLETED
payload = None
note = (self._c.NOTE.INFO, _("management session done"))
defer.returnValue((payload, status, None, note))
return (payload, status, None, note)
@defer.inlineCallbacks
def _onDeleteFile(self, client, command_elt, session_data, action, node):
async def _onDeleteFile(self, client, command_elt, session_data, action, node):
try:
x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
command_form = data_form.Form.fromElement(x_elt)
@ -346,14 +342,14 @@ class FileSharingManagement(object):
if command_form is None or len(command_form.fields) == 0:
# root request
defer.returnValue(self._getRootArgs())
return self._getRootArgs()
elif found_file is None:
# file selected, we need confirmation before actually deleting
try:
found_file = yield self._getFileData(client, session_data, command_form)
found_file = await self._getFileData(client, session_data, command_form)
except WorkflowError as e:
defer.returnValue(e.err_args)
return e.err_args
if found_file['type'] == C.FILE_TYPE_DIRECTORY:
msg = D_("Are you sure to delete directory {name} and all files and "
"directories under it?").format(name=found_file['name'])
@ -370,7 +366,7 @@ class FileSharingManagement(object):
form.addField(field)
status = self._c.STATUS.EXECUTING
payload = form.toElement()
defer.returnValue((payload, status, None, None))
return (payload, status, None, None)
else:
# final phase, we'll do deletion here
@ -382,27 +378,26 @@ class FileSharingManagement(object):
note = None
else:
recursive = found_file['type'] == C.FILE_TYPE_DIRECTORY
yield self.host.memory.fileDelete(
await self.host.memory.fileDelete(
client, requestor_bare, found_file['id'], recursive)
note = (self._c.NOTE.INFO, _("file deleted"))
status = self._c.STATUS.COMPLETED
payload = None
defer.returnValue((payload, status, None, note))
return (payload, status, None, note)
def _updateThumbs(self, extra, thumbnails):
extra[C.KEY_THUMBNAILS] = thumbnails
@defer.inlineCallbacks
def _genThumbs(self, client, requestor, namespace, file_data):
async def _genThumbs(self, client, requestor, namespace, file_data):
"""Recursively generate thumbnails
@param file_data(dict): metadata of the file
"""
if file_data['type'] == C.FILE_TYPE_DIRECTORY:
sub_files_data = yield self.host.memory.getFiles(
sub_files_data = await self.host.memory.getFiles(
client, requestor, parent=file_data['id'], namespace=namespace)
for sub_file_data in sub_files_data:
yield self._genThumbs(client, requestor, namespace, sub_file_data)
await self._genThumbs(client, requestor, namespace, sub_file_data)
elif file_data['type'] == C.FILE_TYPE_FILE:
media_type = file_data['media_type']
@ -412,7 +407,7 @@ class FileSharingManagement(object):
for max_thumb_size in self._t.SIZES:
try:
thumb_size, thumb_id = yield self._t.generateThumbnail(
thumb_size, thumb_id = await self._t.generateThumbnail(
file_path,
max_thumb_size,
#  we keep thumbnails for 6 months
@ -424,7 +419,7 @@ class FileSharingManagement(object):
break
thumbnails.append({"id": thumb_id, "size": thumb_size})
yield self.host.memory.fileUpdate(
await self.host.memory.fileUpdate(
file_data['id'], 'extra',
partial(self._updateThumbs, thumbnails=thumbnails))
@ -434,8 +429,7 @@ class FileSharingManagement(object):
else:
log.warning("unmanaged file type: {type_}".format(type_=file_data['type']))
@defer.inlineCallbacks
def _onGenThumbnails(self, client, command_elt, session_data, action, node):
async def _onGenThumbnails(self, client, command_elt, session_data, action, node):
try:
x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
command_form = data_form.Form.fromElement(x_elt)
@ -447,23 +441,23 @@ class FileSharingManagement(object):
if command_form is None or len(command_form.fields) == 0:
# root request
defer.returnValue(self._getRootArgs())
return self._getRootArgs()
elif found_file is None:
# file selected, we retrieve it and ask for permissions
try:
found_file = yield self._getFileData(client, session_data, command_form)
found_file = await self._getFileData(client, session_data, command_form)
except WorkflowError as e:
defer.returnValue(e.err_args)
return e.err_args
log.info("Generating thumbnails as requested")
yield self._genThumbs(client, requestor, found_file['namespace'], found_file)
await self._genThumbs(client, requestor, found_file['namespace'], found_file)
# job done, we can end the session
status = self._c.STATUS.COMPLETED
payload = None
note = (self._c.NOTE.INFO, _("thumbnails generated"))
defer.returnValue((payload, status, None, note))
return (payload, status, None, note)
async def _onQuota(self, client, command_elt, session_data, action, node):
requestor = session_data['requestor']