From d80c4e89dcf08e4f480c7840296552f745f7560e Mon Sep 17 00:00:00 2001 From: Goffi Date: Tue, 5 Dec 2023 13:39:03 +0100 Subject: [PATCH] plugin XEP-0277, doc: conflict is now checked and avoided if `user_friendly_id_suffix` is `False`, doc updated --- doc/libervia-cli/blog.rst | 12 ++++++++--- libervia/backend/plugins/plugin_xep_0277.py | 22 +++++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/doc/libervia-cli/blog.rst b/doc/libervia-cli/blog.rst index c678b521..031e40b6 100644 --- a/doc/libervia-cli/blog.rst +++ b/doc/libervia-cli/blog.rst @@ -17,11 +17,17 @@ The `set` and `edit` commands support a range of options for customizing blog po ``--no-id-suffix`` - This option removes the randomly generated suffix from the auto-generated, user-friendly ID. The suffix is initially added to ensure unique IDs, especially useful when two posts have similar titles. Use this option with caution, as it may lead to ID conflicts and potential overwriting of existing posts. + This option removes the randomly generated suffix from the auto-generated, + user-friendly ID. The suffix is initially added to ensure unique IDs, especially useful + when two posts have similar titles. - .. warning:: + .. note:: - Use this option judiciously to avoid unintended overwriting of blog posts. + When the ``--no-id-suffix`` option is selected, an extra validation is performed to + check for ID uniqueness. If an existing item already uses the same ID, a suffix is + added to the new ID regardless of the option, ensuring no existing content is + overwritten. This additional check incurs a slight performance impact due to the + extra request needed. ``--alt-link URL [MEDIA_TYPE]`` diff --git a/libervia/backend/plugins/plugin_xep_0277.py b/libervia/backend/plugins/plugin_xep_0277.py index 87e651eb..e861990d 100644 --- a/libervia/backend/plugins/plugin_xep_0277.py +++ b/libervia/backend/plugins/plugin_xep_0277.py @@ -1068,8 +1068,26 @@ class XEP_0277(object): if item_id is None: if data.get("user_friendly_id", True): item_id = self.friendly_id(data) - else: - item_id = str(shortuuid.uuid()) + if not data.get("user_friendly_id_suffix", True): + # we have no random suffix, which can lead to conflict, so we check if + # the item doesn't already exist, and change ID if it's the case. + try: + items, __ = await self._p.get_items( + client, + service, + node, + item_ids = [item_id] + ) + except exceptions.NotFound: + pass + else: + # the item already exists + log.info( + f"there is already an item with ID {item_id}, we have to " + ' set the "user_friendly_id_suffix" flag.' + ) + data["user_friendly_id_suffix"] = True + item_id = self.friendly_id(data) try: await self._manage_comments(client, data, service, node, item_id, access=None)