diff libervia/backend/plugins/plugin_xep_0277.py @ 4178:cf0ea77f9537

plugin XEP-0277, doc: conflict is now checked and avoided if `user_friendly_id_suffix` is `False`, doc updated
author Goffi <goffi@goffi.org>
date Tue, 05 Dec 2023 13:39:03 +0100
parents 30f7513e5590
children c86a22009c1f
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0277.py	Tue Dec 05 13:14:55 2023 +0100
+++ b/libervia/backend/plugins/plugin_xep_0277.py	Tue Dec 05 13:39:03 2023 +0100
@@ -1068,8 +1068,26 @@
         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)