-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve WebSocketWorker class and user connection management
- Loading branch information
1 parent
7b64ffa
commit 873393d
Showing
4 changed files
with
218 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import asyncio | ||
import sys | ||
import traceback | ||
from queue import Queue | ||
from typing import Dict | ||
|
||
from fastapi import Depends, WebSocket, WebSocketDisconnect | ||
from jose import JWTError, jwt | ||
|
||
from r3almX_backend.auth_service.auth_utils import TokenData | ||
from r3almX_backend.auth_service.Config import UsersConfig | ||
from r3almX_backend.auth_service.user_handler_utils import get_db, get_user_by_username | ||
from r3almX_backend.auth_service.user_models import User | ||
from r3almX_backend.chat_service.chat_service_endpoints import get_user_from_token | ||
from r3almX_backend.chat_service.main import chat_service | ||
|
||
|
||
class Connection: | ||
def __init__(self): | ||
# use redis to cache users connected | ||
|
||
# connection cache status contains {user_id, status} | ||
self.connection_status_cache: Dict[str, str] = self.get_status_cache() | ||
|
||
# connection cache sockets contain {user_id, websocket} | ||
self.connection_sockets: Dict[str, WebSocket] = {} | ||
|
||
def get_status_cache(self) -> Dict[str, str]: | ||
# we get cache from redis and construct it into dict and return it | ||
... | ||
|
||
def set_online(self): | ||
# class A: we're gonna push users notifications | ||
pass | ||
|
||
def set_offline(self): | ||
# class B: mobile notif push (web:toast, mobile: notif) | ||
pass | ||
|
||
def set_dnd(self): | ||
# class C: integer notif push (silent number increment) | ||
pass | ||
|
||
def is_connected(self, user_id) -> bool: | ||
try: | ||
if self.connection_cache_status.get( | ||
f"{user_id}" | ||
) or self.connection_cache_sockets.get(f"{user_id}"): | ||
return True | ||
return False | ||
except KeyError as k: | ||
return False | ||
|
||
def get_status(self, user_id) -> str: | ||
return self.connection_cache_status.get(f"{user_id}") | ||
|
||
def set_status(self, user_id): | ||
# this is the function exposed to the websocket | ||
# so we can update the status | ||
# we also call set_status_cache | ||
... | ||
|
||
def set_status_cache(self, user_id): | ||
# modifies the redis cache status of the user | ||
... | ||
|
||
|
||
connection_manager = Connection() | ||
|
||
|
||
@chat_service.websocket("/connect") | ||
async def connect(websocket: WebSocket, token: str, db=Depends(get_db)): | ||
# write a wholistic solution for connection so that we can send notif | ||
user = get_user_from_token(token) | ||
if user: | ||
await websocket.accept() | ||
while True: | ||
# check the status of the user. | ||
# if they're not in the connection sockets, then just connect them | ||
# otherwise we need to check their last set status from the cache | ||
# we need to also check the websocket request | ||
# it should contain json with user id and requested change in connection | ||
if connection_manager.is_connected(user.id) is False: | ||
connection_manager.connection_sockets.append({user.id: websocket}) | ||
connection_manager.set_status_cache( | ||
connection_manager.get_status(user.id) | ||
) | ||
connection_change_request = await websocket.receive_json() | ||
# through this we can just check for keys inside of the change request | ||
if connection_change_request["status"]: | ||
connection_manager.set_status( | ||
user.id, connection_change_request["status"] | ||
) | ||
else: | ||
return websocket.close(1001) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import asyncio | ||
import sys | ||
import traceback | ||
from queue import Queue | ||
from typing import Dict | ||
|
||
from fastapi import Depends, WebSocket, WebSocketDisconnect | ||
from jose import JWTError, jwt | ||
|
||
from r3almX_backend.auth_service.auth_utils import TokenData | ||
from r3almX_backend.auth_service.Config import UsersConfig | ||
from r3almX_backend.auth_service.user_handler_utils import get_db, get_user_by_username | ||
from r3almX_backend.auth_service.user_models import User | ||
from r3almX_backend.chat_service.chat_service_endpoints import get_user_from_token | ||
from r3almX_backend.chat_service.connection_service import connection_manager | ||
from r3almX_backend.chat_service.main import chat_service | ||
|
||
|
||
class NotificationSystem: | ||
def __init__(self): | ||
self.types = { | ||
1: "RoomPost", | ||
2: "FriendRequest", | ||
3: "RoomInvitation", | ||
4: "DM", | ||
} | ||
self.connections = connection_manager | ||
|
||
def return_user(self, user_id): | ||
return self.connections.connection_cache_list.get(user_id) | ||
|
||
|
||
notification = NotificationSystem() | ||
|
||
|
||
@chat_service.websocket("/notify") | ||
async def notification_pusher(websocket: WebSocket, token: str, db=Depends(get_db)): | ||
# we need to manage a middleware so that if a user has associated events, we need to trigger the | ||
# notification system so we push notifs to that user | ||
# need to figure out how users would receive | ||
|
||
pass |
Oops, something went wrong.