@@ -50,30 +50,45 @@ def upsert(cls, conversation_json):
5050
5151 conversation = cls .query .filter_by (session_id = session_id ).first ()
5252
53- if conversation and conversation .is_owned_by (user_id ):
54- # Update existing conversation
55- conversation .updated_at = db .func .current_timestamp ()
56- conversation .payload = conversation_json
57- # Update assistant_name if provided
58- if assistant_name :
59- conversation .assistant_name = assistant_name
60- # Only generate title if we don't already have one
61- if not conversation .title :
62- new_title = cls .generate_title (conversation_json )
63- conversation .title = new_title
53+ if conversation :
54+ # Handle ownership verification based on user_id
55+ can_update = False
56+
57+ if user_id is None :
58+ # Anonymous users can update any conversation by session_id
59+ # This allows for seamless transition between authenticated and anonymous sessions
60+ can_update = True
6461 else :
65- # Check if we should update the title because we now have non-introduction content
66- cls ._update_title_if_needed (conversation , conversation_json )
67- elif conversation and not conversation .is_owned_by (user_id ):
68- # If the conversation exists but is owned by a different user, create a new one
69- # that is owned by the user and has a new session ID
70- conversation = cls ()
71- conversation .user_id = user_id
72- conversation .session_id = uuid .uuid4 ()
73- conversation .assistant_name = assistant_name
74- conversation .payload = conversation_json
75- conversation .title = cls .generate_title (conversation_json )
76- db .session .add (conversation )
62+ # Authenticated users can only update conversations they own
63+ can_update = conversation .is_owned_by (user_id )
64+
65+ if can_update :
66+ # Update existing conversation
67+ conversation .updated_at = db .func .current_timestamp ()
68+ conversation .payload = conversation_json
69+ # Update user_id if provided (allows anonymous → authenticated transition)
70+ if user_id is not None :
71+ conversation .user_id = user_id
72+ # Update assistant_name if provided
73+ if assistant_name :
74+ conversation .assistant_name = assistant_name
75+ # Only generate title if we don't already have one
76+ if not conversation .title :
77+ new_title = cls .generate_title (conversation_json )
78+ conversation .title = new_title
79+ else :
80+ # Check if we should update the title because we now have non-introduction content
81+ cls ._update_title_if_needed (conversation , conversation_json )
82+ else :
83+ # Authenticated user trying to access conversation owned by different user
84+ # Create a new conversation with a new session ID for security
85+ conversation = cls ()
86+ conversation .user_id = user_id
87+ conversation .session_id = uuid .uuid4 ()
88+ conversation .assistant_name = assistant_name
89+ conversation .payload = conversation_json
90+ conversation .title = cls .generate_title (conversation_json )
91+ db .session .add (conversation )
7792 else :
7893 # Create a new conversation
7994 conversation = cls ()
0 commit comments