fix: drop dangling folder references when importing chats

Chat exports serialize each chat's `folder_id`, but the export does not
include the folders themselves. When such an export is imported into a
different instance (or the same instance after the folders were deleted),
those `folder_id` values point at folders that no longer exist for the
importing user.

`POST /api/v1/chats/import` stored the values verbatim. Because the chat
list query filters out every chat with a non-null `folder_id` (and there
is no matching folder to surface them from), the imported chats became
invisible even though the import reported success.

Sanitize folder references on import: keep folder_ids the user actually
owns (so re-importing on the same instance restores the original
grouping) and reset unknown ones to NULL so the chats land in the main
chat list. This mirrors the existing folder validation in
`POST /api/v1/chats/new` and leaves the clone endpoints, which pass an
already-owned folder_id, unaffected.

Closes #24910
This commit is contained in:
Asish Kumar 2026-05-25 08:42:47 +05:30
parent 42783881e9
commit 67a7e0b1d1
No known key found for this signature in database
GPG key ID: 0EF072B1E0BA6FBA

View file

@ -581,6 +581,19 @@ async def import_chats(
db: AsyncSession = Depends(get_async_session),
):
try:
# Exported chats carry the source instance's folder_id values, but the
# export does not include the folders themselves. On a fresh instance
# (or after the folders were deleted) those references dangle: the chat
# list query hides any chat with a non-null folder_id, and there is no
# matching folder to surface it from, so the import reports success yet
# the chats are nowhere to be seen. Drop folder references the user does
# not own so imported chats land in the main chat list; references that
# are still valid (re-importing on the same instance) are preserved.
owned_folder_ids = {folder.id for folder in await Folders.get_folders_by_user_id(user.id, db=db)}
for chat in form_data.chats:
if chat.folder_id is not None and chat.folder_id not in owned_folder_ids:
chat.folder_id = None
chats = await Chats.import_chats(user.id, form_data.chats, db=db)
return chats
except Exception as e: