Skip to content

Commit e8a7d84

Browse files
committed
Fix NameError: cannot access free variable
1 parent 39555c0 commit e8a7d84

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/chat/services.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,29 @@ async def get_chat_messages(
170170
messages = messages[:size]
171171

172172
# assuming only two read statuses
173+
# Initialize variables to prevent NameError
174+
my_last_read_message_id = None
175+
other_user_last_read_message_id = None
176+
177+
# Loop through chat.read_statuses and assign the read message IDs
173178
for read_status in chat.read_statuses:
174179
if read_status.user_id != user_id:
175180
other_user_last_read_message_id = read_status.last_read_message_id
176181
else:
177182
my_last_read_message_id = read_status.last_read_message_id
178183

184+
# If no value is assigned, you could choose to set a default value
185+
# or handle this case accordingly (e.g., using a placeholder)
186+
if my_last_read_message_id is None:
187+
my_last_read_message_id = 0 # Or some other default value
188+
189+
if other_user_last_read_message_id is None:
190+
other_user_last_read_message_id = 0 # Or some other default value
191+
192+
# Retrieve the last read message for the other user
179193
last_read_message = await db_session.get(Message, other_user_last_read_message_id)
194+
195+
# Construct GetMessageSchema list
180196
get_message_schemas = [
181197
GetMessageSchema(
182198
message_guid=message.guid,

src/contact/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import datetime
22

3-
from pydantic import UUID4, BaseModel, EmailStr, RootModel, field_validator
3+
from pydantic import UUID4, BaseModel, RootModel, field_validator
44

55
from src.config import settings
66

0 commit comments

Comments
 (0)