Always append random suffix to shared conversations urls

This commit is contained in:
Debanjum
2025-06-17 14:24:36 -07:00
parent 68b7057a76
commit 4ca247f0bc

View File

@@ -671,23 +671,16 @@ class PublicConversation(DbBaseModel):
@receiver(pre_save, sender=PublicConversation) @receiver(pre_save, sender=PublicConversation)
def verify_public_conversation(sender, instance, **kwargs): def verify_public_conversation(sender, instance, **kwargs):
def generate_random_alphanumeric(length):
characters = "0123456789abcdefghijklmnopqrstuvwxyz"
return "".join(choice(characters) for _ in range(length))
# check if this is a new instance # check if this is a new instance
if instance._state.adding: if instance._state.adding:
slug = re.sub(r"\W+", "-", instance.slug.lower())[:50] if instance.slug else uuid.uuid4().hex base_length = 50 # Base slug length before adding random suffix
observed_random_id = set() base_slug = re.sub(r"\W+", "-", instance.slug.lower())[:base_length] if instance.slug else uuid.uuid4().hex
while PublicConversation.objects.filter(slug=slug).exists(): suffix_length = 8 # Length of the random suffix to ensure uniqueness
try: while True:
random_id = generate_random_alphanumeric(7) random_id = uuid.uuid4().hex[:suffix_length]
except IndexError: slug = f"{base_slug}-{random_id}"
raise ValidationError( if not PublicConversation.objects.filter(slug=slug).exists():
"Unable to generate a unique slug for the Public Conversation. Please try again later." break
)
observed_random_id.add(random_id)
slug = f"{slug}-{random_id}"
instance.slug = slug instance.slug = slug