1
- import asyncio
2
1
import logging
3
2
from datetime import datetime
4
3
5
4
import redis .asyncio as aioredis
6
5
from fastapi import WebSocket
7
- from fastapi .encoders import jsonable_encoder
8
6
from sqlalchemy .ext .asyncio import AsyncSession
9
7
10
8
from src .managers .websocket_manager import WebSocketManager
23
21
get_message_by_guid ,
24
22
mark_last_read_message ,
25
23
mark_user_as_online ,
26
- send_new_chat_created_ws_message ,
24
+ notify_friend_about_new_chat ,
27
25
)
28
26
29
27
logger = logging .getLogger (__name__ )
@@ -50,16 +48,18 @@ async def new_message_handler(
50
48
message_schema = ReceiveMessageSchema (** incoming_message )
51
49
chat_guid : str = str (message_schema .chat_guid )
52
50
53
- notify_friend_about_new_chat : bool = False
51
+ # notify_friend_about_new_chat: bool = False
54
52
# newly created chat
55
53
if not chats or chat_guid not in chats :
54
+ print ("YES" , chats )
56
55
chat_id : int | None = await get_chat_id_by_guid (db_session , chat_guid = chat_guid )
57
56
if chat_id :
57
+ print ("Chat ID" , chat_id )
58
58
# this action modifies chats variable in websocket view
59
59
chats [chat_guid ] = chat_id
60
60
await socket_manager .add_user_to_chat (chat_guid , websocket )
61
61
# must notify friend that new chat has been created
62
- notify_friend_about_new_chat = True
62
+ # notify_friend_about_new_chat = True
63
63
64
64
else :
65
65
await socket_manager .send_error ("Chat has not been added" , websocket )
@@ -113,9 +113,11 @@ async def new_message_handler(
113
113
114
114
await socket_manager .broadcast_to_chat (chat_guid , outgoing_message )
115
115
116
- if notify_friend_about_new_chat :
117
- logger .info ("Notifying friend about newly created chat" )
118
- await send_new_chat_created_ws_message (socket_manager = socket_manager , current_user = current_user , chat = chat )
116
+ await notify_friend_about_new_chat (socket_manager = socket_manager , current_user = current_user , chat = chat )
117
+
118
+ # if notify_friend_about_new_chat:
119
+ # logger.info("Notifying friend about newly created chat")
120
+ # await send_new_chat_created_ws_message(socket_manager=socket_manager, current_user=current_user, chat=chat)
119
121
120
122
121
123
@socket_manager .handler ("message_read" )
@@ -248,22 +250,24 @@ async def chat_deleted_handler(
248
250
# get all websocket connections that belong to this chat (except for ws that sent this messsage)
249
251
# and send notification that chat has been removed
250
252
251
- target_websockets : set [WebSocket ] = socket_manager .chats .get (chat_guid )
252
-
253
253
outgoing_message = {
254
254
"type" : "chat_deleted" ,
255
255
"user_guid" : str (current_user .guid ),
256
256
"user_name" : current_user .first_name ,
257
257
"chat_guid" : chat_guid ,
258
258
}
259
259
260
- if target_websockets :
261
- # Send the notification message to the target user concurrently
262
- # used to notify frontend
263
- await asyncio .gather (
264
- * [
265
- socket .send_json (jsonable_encoder (outgoing_message ))
266
- for socket in target_websockets
267
- if socket != websocket
268
- ]
269
- )
260
+ await socket_manager .broadcast_to_chat (chat_guid , outgoing_message )
261
+
262
+ # target_websockets: set[WebSocket] = socket_manager.chats.get(chat_guid)
263
+
264
+ # if target_websockets:
265
+ # # Send the notification message to the target user concurrently
266
+ # # used to notify frontend
267
+ # await asyncio.gather(
268
+ # *[
269
+ # socket.send_json(jsonable_encoder(outgoing_message))
270
+ # for socket in target_websockets
271
+ # if socket != websocket
272
+ # ]
273
+ # )
0 commit comments