Commit 611a5c4c authored by Alexander's avatar Alexander
Browse files

[stickers] Configure node before publishing

parent 372d2c49
Pipeline #7730 passed with stage
in 1 minute and 54 seconds
......@@ -141,9 +141,9 @@ class StickersModule(BaseModule):
self._plugin.should_download_sticker_pack() and
not self._sticker_storage.is_in_progress(pack_id)):
# TODO: Add a callback for the rest
self.request_sticker_pack(pack_id, jid)
self.request_sticker_pack(pack_id, jid=jid)
def _download_sticker_pack(self, pack, publish=False):
def _download_sticker_pack(self, pack, error, publish=False):
def download_callback(session, msg, data):
# pylint: disable=unused-argument
fname = os.path.basename(data.sticker.url)
......@@ -362,7 +362,7 @@ class StickersModule(BaseModule):
# If we have uploaded all stickers, we can publish it
if all(self._pack_upload_state[pack_id].values()):
self.publish_pack(self._sticker_storage.get_sticker_pack(pack_id), upload=False)
self._publish_pack_on_pubsub(self._sticker_storage.get_sticker_pack(pack_id))
# NOTE: This seems weird and it is. This is to update the uploaded
# property of the sticker pack.
self._sticker_storage.replace_sticker_pack(self._sticker_storage.get_sticker_pack(pack_id),
......@@ -455,23 +455,6 @@ class StickersModule(BaseModule):
uri = transfer.get_transformed_uri()
self._on_sticker_uploaded(transfer.sticker, transfer.pack_id, uri)
def _discover_pubsub_max(self):
'''
Find the maximum amount of persistent items in a PubSub node out.
'''
features = self._client.get_module('Discovery').server_info.features
if PUBSUB_MAX_FEATURE in features:
log.debug('%s supports pubsub#max_items=max', self._client.account)
self._account_pubsub_max[self._client.account] = 'max'
else:
# NOTE: We could do some fancy things like discover the default
# PubSub configuration and find out what 'max' would default
# to, but that is more complicated.
# As such, we just default to a reasonable guess of how many
# sticker packs a user might use.
log.debug('%s does not support pubsub#max_items=max', self._client.account)
self._account_pubsub_max[self._client.account] = '50'
def _upload_and_publish_pack(self, sticker_pack, upload=True):
if not upload:
self._publish_pack_on_pubsub(sticker_pack)
......@@ -497,6 +480,41 @@ class StickersModule(BaseModule):
transfer.pack_id = sticker_pack.id_
module.start_transfer(transfer)
def _get_configuration_callback(self, task):
log.debug('Got PubSub node config')
try:
config = task.finish()
node_config = config.form
max_items = len(self._sticker_storage.sticker_pack_ids())
if int(node_config['pubsub#max_items'].value) < max_items:
node_config['pubsub#max_items'].value = max_items
log.debug('Setting PubSub node config')
pubsub = app.connections[self._account].get_module('PubSub')
pubsub.set_node_configuration(Namespace.STICKERS,
node_config,
callback=set_configuration_callback,
user_data=task.get_user_data())
return
except PubSubStanzaError as err:
# Node probably does not exist yet.
log.debug(err)
pass
sticker_pack, upload = task.get_user_data()
self._upload_and_publish_pack(sticker_pack, upload)
def _set_configuration_callback(self, task):
try:
config = task.finish()
log.debug('PubSub node\'s max_items updated for a new sticker pack')
except PubSubStanzaError as err:
log.warning('Failed to set node configuration: %s', err)
sticker_pack, upload = task.get_user_data()
self._upload_and_publish_pack(sticker_pack, upload)
def publish_pack(self, sticker_pack, upload=True):
'''
Publish a sticker pack on PubSub by updating the PubSub node, if
......@@ -511,37 +529,10 @@ class StickersModule(BaseModule):
'''
pubsub = app.connections[self._account].get_module('PubSub')
def get_configuration_callback(task):
try:
config = task.finish()
self._node_config = config.form
max_items = len(self._sticker_storage.sticker_pack_ids)
if self._node_config['pubsub#max_items'].value < max_items:
self._node_config['pubsub#max_items'].value = max_items
pubsub.set_node_configuration(Namespace.STICKERS,
self._node_config,
callback=set_configuration_callback)
return
except PubSubStanzaException as err:
# Node probably does not exist yet.
pass
self._upload_and_publish_pack(sticker_pack, upload)
def set_configuration_callback(task):
try:
config = task.finish()
log.debug('PubSub node\'s max_items updated for a new sticker pack')
except PubSubStanzaException as err:
log.warning('Failed to set node configuration: %s', err)
self._upload_and_publish_pack(sticker_pack, upload)
if not self._node_config:
pubsub.get_node_configuration(Namespace.STICKERS,
callback=get_configuration_callback)
else:
self._upload_and_publish_pack(sticker_pack, upload)
log.debug('Requesting PubSub node config')
pubsub.get_node_configuration(Namespace.STICKERS,
callback=self._get_configuration_callback,
user_data=(sticker_pack, upload))
def get_instance(*args, **kwargs):
return StickersModule(*args, **kwargs), 'Stickers'
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment