diff --git a/lib/action/model/corpus.py b/lib/action/model/corpus.py index 15ad2be980..0854488f99 100644 --- a/lib/action/model/corpus.py +++ b/lib/action/model/corpus.py @@ -190,10 +190,11 @@ async def _restore_prev_query_params(self, req_args: Union[RequestArgsProxy, JSO True if query params have been loaded else False (which is still not an error) """ url_q = req_args.getlist('q')[:] - with plugins.runtime.QUERY_PERSISTENCE as query_persistence: + with plugins.runtime.QUERY_PERSISTENCE as query_persistence, plugins.runtime.DISPATCH_HOOK as dh: if len(url_q) > 0 and query_persistence.is_valid_id(url_q[0]): self._q_code = url_q[0][1:] - self._active_q_data = await query_persistence.open(self._q_code) + aqdata = await query_persistence.open(self._q_code) + self._active_q_data = await dh.transform_stored_query_data(aqdata) # !!! must create a copy here otherwise _q_data (as prev query) # will be rewritten by self.args.q !!! if self._active_q_data is not None: diff --git a/lib/plugin_types/dispatch_hook.py b/lib/plugin_types/dispatch_hook.py index 54387f75d6..a512da5800 100644 --- a/lib/plugin_types/dispatch_hook.py +++ b/lib/plugin_types/dispatch_hook.py @@ -54,3 +54,14 @@ async def post_dispatch(self, plugin_ctx, methodname, action_metadata): methodname -- processed action method action_metadata -- action metadata (added by @inject) """ + + async def transform_stored_query_data(self, data): + """ + When KonText processes a URL with a query ID, it always restores required + internal arguments representing the URL using the loaded values. This + function transforms values immediately after they are loaded, + i.e. before they are applied elsewhere. + + It can be used e.g. to solve legacy values stored in query persistence database. + """ + return data diff --git a/lib/plugins/ucnk_dispatch_hook/__init__.py b/lib/plugins/ucnk_dispatch_hook/__init__.py index cd65558afb..40744cb2a3 100644 --- a/lib/plugins/ucnk_dispatch_hook/__init__.py +++ b/lib/plugins/ucnk_dispatch_hook/__init__.py @@ -22,7 +22,7 @@ from dataclasses import dataclass import plugins -from action.errors import ServiceUnavailableException +from action.errors import ServiceUnavailableException, ImmediateRedirectException from action.plugin.ctx import PluginCtx from action.props import ActionProps from dataclasses_json import LetterCase, dataclass_json @@ -73,8 +73,23 @@ async def _check_client(self, plugin_ctx: PluginCtx): await self._db.hash_del(self.bot_clients_key, client_ip) async def pre_dispatch(self, plugin_ctx, action_props: ActionProps, request): + arg = request.args.get('corpname', [''])[0] + if arg.startswith('aranea/'): + raise ImmediateRedirectException(plugin_ctx.updated_current_url(dict(corpname=arg[len('aranea/'):]))) await self._check_client(plugin_ctx) + async def transform_stored_query_data(self, data): + if 'corpora' in data: + normalized_corpora = [] + for corp in data['corpora']: + if corp.startswith('aranea/'): + normalized_corpora.append(corp[len('aranea/'):]) + else: + normalized_corpora.append(corp) + data['corpora'] = normalized_corpora + return data + + @inject(plugins.runtime.DB) def create_instance(conf, db: KeyValueStorage):