Skip to content

Commit 3889bdd

Browse files
Merge pull request #9 from CarterPerez-dev/project/secure-p2p-messaging
in progress debugging checkpoint
2 parents 1335034 + bee650d commit 3889bdd

File tree

26 files changed

+824
-92
lines changed

26 files changed

+824
-92
lines changed

PROJECTS/encrypted-p2p-chat/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ wheels/
2626
.installed.cfg
2727
*.egg
2828
venv/
29+
.venv/
2930
ENV/
3031
env/
3132

PROJECTS/encrypted-p2p-chat/backend/app/api/rooms.py

Lines changed: 138 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
RoomAPIResponse,
2222
RoomListResponse,
2323
)
24+
from app.schemas.websocket import RoomCreatedWS
2425
from app.core.enums import RoomType
2526
from app.models.Base import get_session
2627
from app.core.surreal_manager import surreal_db
28+
from app.core.websocket_manager import connection_manager
2729
from app.services.auth_service import auth_service
2830

2931

@@ -41,6 +43,17 @@ async def create_room(
4143
"""
4244
Create a new chat room
4345
"""
46+
creator = await auth_service.get_user_by_id(
47+
session,
48+
UUID(request.creator_id),
49+
)
50+
51+
if not creator:
52+
raise HTTPException(
53+
status_code = status.HTTP_404_NOT_FOUND,
54+
detail = "Creator not found",
55+
)
56+
4457
participant = await auth_service.get_user_by_id(
4558
session,
4659
UUID(request.participant_id),
@@ -55,35 +68,63 @@ async def create_room(
5568
now = datetime.now(UTC)
5669

5770
room_data = {
58-
"name": participant.display_name,
71+
"name": None,
5972
"room_type": request.room_type.value,
60-
"created_by": request.participant_id,
73+
"created_by": request.creator_id,
6174
"created_at": now.isoformat(),
6275
"updated_at": now.isoformat(),
6376
"is_ephemeral": request.room_type == RoomType.EPHEMERAL,
6477
}
6578

6679
room = await surreal_db.create_room(room_data)
6780

81+
await surreal_db.add_room_participant(room.id, request.creator_id, "owner")
82+
await surreal_db.add_room_participant(room.id, request.participant_id, "member")
83+
6884
logger.info(
69-
"Created room %s with participant %s",
85+
"Created room %s with creator %s and participant %s",
7086
room.id,
87+
request.creator_id,
7188
request.participant_id,
7289
)
7390

91+
participants_list = [
92+
ParticipantResponse(
93+
user_id = str(creator.id),
94+
username = creator.username,
95+
display_name = creator.display_name,
96+
role = "owner",
97+
joined_at = now.isoformat(),
98+
),
99+
ParticipantResponse(
100+
user_id = str(participant.id),
101+
username = participant.username,
102+
display_name = participant.display_name,
103+
role = "member",
104+
joined_at = now.isoformat(),
105+
)
106+
]
107+
108+
room_ws_notification = RoomCreatedWS(
109+
room_id = room.id,
110+
room_type = room.room_type.value,
111+
name = creator.display_name,
112+
participants = [p.model_dump() for p in participants_list],
113+
is_encrypted = True,
114+
created_at = room.created_at.isoformat(),
115+
updated_at = room.updated_at.isoformat(),
116+
)
117+
118+
await connection_manager.send_message(
119+
UUID(request.participant_id),
120+
room_ws_notification.model_dump(mode = "json"),
121+
)
122+
74123
return RoomAPIResponse(
75124
id = room.id,
76125
type = RoomType(room.room_type),
77126
name = participant.display_name,
78-
participants = [
79-
ParticipantResponse(
80-
user_id = str(participant.id),
81-
username = participant.username,
82-
display_name = participant.display_name,
83-
role = "member",
84-
joined_at = now.isoformat(),
85-
)
86-
],
127+
participants = participants_list,
87128
unread_count = 0,
88129
is_encrypted = True,
89130
created_at = room.created_at.isoformat(),
@@ -92,11 +133,75 @@ async def create_room(
92133

93134

94135
@router.get("", status_code = status.HTTP_200_OK)
95-
async def list_rooms() -> RoomListResponse:
136+
async def list_rooms(
137+
user_id: str,
138+
session: AsyncSession = Depends(get_session),
139+
) -> RoomListResponse:
96140
"""
97-
List all rooms for the current user
141+
List all rooms for the specified user
98142
"""
99-
return RoomListResponse(rooms = [])
143+
logger.info("list_rooms called for user_id: %s", user_id)
144+
145+
user = await auth_service.get_user_by_id(session, UUID(user_id))
146+
147+
if not user:
148+
raise HTTPException(
149+
status_code = status.HTTP_404_NOT_FOUND,
150+
detail = "User not found",
151+
)
152+
153+
room_data_list = await surreal_db.get_rooms_for_user(user_id)
154+
logger.info("SurrealDB returned %d rooms for user %s", len(room_data_list), user_id)
155+
logger.info("Room data: %s", room_data_list)
156+
157+
rooms: list[RoomAPIResponse] = []
158+
159+
for room_data in room_data_list:
160+
if not room_data:
161+
continue
162+
163+
room_id = str(room_data.get("id", ""))
164+
participants_data = await surreal_db.get_room_participants(room_id)
165+
166+
participants: list[ParticipantResponse] = []
167+
168+
for p_data in participants_data:
169+
p_user_id = p_data.get("user_id")
170+
if not p_user_id:
171+
continue
172+
173+
p_user = await auth_service.get_user_by_id(session, UUID(p_user_id))
174+
if p_user:
175+
participants.append(
176+
ParticipantResponse(
177+
user_id = str(p_user.id),
178+
username = p_user.username,
179+
display_name = p_user.display_name,
180+
role = p_data.get("role", "member"),
181+
joined_at = str(p_data.get("joined_at", "")),
182+
)
183+
)
184+
185+
other_participant = next(
186+
(p for p in participants if p.user_id != user_id),
187+
None
188+
)
189+
room_name = other_participant.display_name if other_participant else None
190+
191+
rooms.append(
192+
RoomAPIResponse(
193+
id = room_id,
194+
type = RoomType(room_data.get("room_type", "direct")),
195+
name = room_name,
196+
participants = participants,
197+
unread_count = 0,
198+
is_encrypted = True,
199+
created_at = str(room_data.get("created_at", "")),
200+
updated_at = str(room_data.get("updated_at", "")),
201+
)
202+
)
203+
204+
return RoomListResponse(rooms = rooms)
100205

101206

102207
@router.get("/{room_id}", status_code = status.HTTP_200_OK)
@@ -110,6 +215,22 @@ async def get_room(room_id: str) -> RoomAPIResponse:
110215
)
111216

112217

218+
@router.get("/{room_id}/messages", status_code = status.HTTP_200_OK)
219+
async def get_room_messages(
220+
room_id: str,
221+
limit: int = 50,
222+
offset: int = 0,
223+
) -> dict:
224+
"""
225+
Get messages for a specific room
226+
"""
227+
messages = await surreal_db.get_room_messages(room_id, limit, offset)
228+
return {
229+
"messages": [msg.model_dump(mode = "json") for msg in messages],
230+
"has_more": len(messages) == limit
231+
}
232+
233+
113234
@router.delete("/{room_id}", status_code = status.HTTP_204_NO_CONTENT)
114235
async def delete_room(room_id: str) -> None:
115236
"""
@@ -119,3 +240,5 @@ async def delete_room(room_id: str) -> None:
119240
status_code = status.HTTP_404_NOT_FOUND,
120241
detail = "Room not found",
121242
)
243+
244+

0 commit comments

Comments
 (0)