Skip to content

Commit 0039224

Browse files
added one more route app route for loading all the chats for hoeme page
1 parent 566899b commit 0039224

File tree

10 files changed

+162
-214
lines changed

10 files changed

+162
-214
lines changed
320 Bytes
Binary file not shown.
-40 Bytes
Binary file not shown.

API/DATABASE/get_data.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ def all_users():
7272
if connection:
7373
connection.close()
7474

75+
@staticmethod
7576
def stored_otp(RAW_UID, COOKIE):
7677
UID = int(RAW_UID)
78+
connection = None
7779
try:
7880
connection = database.connect_users_db()
7981
cursor = connection.cursor()
@@ -95,7 +97,9 @@ def stored_otp(RAW_UID, COOKIE):
9597
if connection:
9698
connection.close()
9799

100+
@staticmethod
98101
def password(EMAIL):
102+
connection = None
99103
try:
100104
connection = database.connect_users_db()
101105
if connection:
@@ -114,7 +118,9 @@ def password(EMAIL):
114118
if connection:
115119
connection.close()
116120

121+
@staticmethod
117122
def email(COOKIE):
123+
connection = None
118124
try:
119125
connection = database.connect_users_db()
120126
if connection:
@@ -133,7 +139,9 @@ def email(COOKIE):
133139
if connection:
134140
connection.close()
135141

142+
@staticmethod
136143
def cookie(EMAIL):
144+
connection = None
137145
try:
138146
connection = database.connect_users_db()
139147
if connection:
@@ -152,7 +160,9 @@ def cookie(EMAIL):
152160
if connection:
153161
connection.close()
154162

163+
@staticmethod
155164
def uid_by_email(EMAIL):
165+
connection = None
156166
try:
157167
connection = database.connect_users_db()
158168
if connection:
@@ -173,9 +183,12 @@ def uid_by_email(EMAIL):
173183
if connection:
174184
connection.close()
175185

186+
@staticmethod
176187
def uid_by_cookie(COOKIE):
188+
connection = None
189+
uid = None
177190
try:
178-
uid = None
191+
179192
connection = database.connect_users_db()
180193
if connection:
181194
cursor = connection.cursor()
@@ -196,7 +209,9 @@ def uid_by_cookie(COOKIE):
196209
if connection:
197210
connection.close()
198211

212+
@staticmethod
199213
def uid_by_token(TOKEN):
214+
connection = None
200215
try:
201216
connection = database.connect_users_db()
202217
if connection:
@@ -217,7 +232,9 @@ def uid_by_token(TOKEN):
217232
if connection:
218233
connection.close()
219234

235+
@staticmethod
220236
def token(COOKIE):
237+
connection = None
221238
try:
222239
connection = database.connect_users_db()
223240
if connection:
@@ -259,8 +276,9 @@ def profile_pic(*, UID=None, GUID=None):
259276
finally:
260277
if connection:
261278
connection.close()
262-
263-
def first_name(RAW_UID):
279+
280+
@staticmethod
281+
def first_name(RAW_UID: str):
264282
UID = int(RAW_UID)
265283
try:
266284
connection = database.connect_users_db()

API/DATABASE/write_data.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ def store_this_message(GROUP_ID, SENDER_ID, SENDER_NAME, MESSAGE, PROFILE_PIC):
119119

120120
result = cursor.fetchone()
121121
print(result)
122-
cursor.close()
123-
return MESSAGE_ID, result[0], result [1] #.isoformat()
122+
return MESSAGE_ID, result[0], result [1]
124123
except Exception as e:
125124
print("error on store_this_message(GROUP_ID, SENDER_ID, SENDER_NAME, MESSAGE):", e)
126-
return False, False
125+
return False, False, False
127126
finally:
128127
if connection:
129128
connection.close()

API/GENERAL/ws_manager.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from types import CodeType
2+
from fastapi import WebSocket
3+
4+
5+
class SocketManager:
6+
""" IT MANAGES THE MESSAGE SENDING PROCESS LIKE BROADCASTING """
7+
8+
def __init__(self, all_online_users):
9+
self.all_online_users = all_online_users
10+
self.__rooms = dict() #for storing all rooms and its an nested dict
11+
12+
def emit(self, event: str, data: dict, ws: WebSocket, room: int | str):
13+
""" Sends the message to specific chat group"""
14+
if (room in self.__rooms) and (ws in self.__rooms[room].values()):
15+
for uid, wss in self.__rooms[room].items():
16+
if uid == data["UID"]:
17+
continue
18+
wss.send_jsomn({"event": event, "data": data})
19+
20+
def join_room(self, room_id: str, user_id: str, ws: str):
21+
""" adds user to an group for listenong messages """
22+
self.__rooms.setdefault(str(room_id), {})[str(user_id)] = str(ws)
23+
return True
24+
25+
def leave_room(self, user_id: str, room_id: str):
26+
""" MAINLY DESIGNED FOR WHEN USER LEAVES AN GROUP CHAT """
27+
self.__rooms[room_id].remove(user_id) if self.__rooms.get(
28+
room_id) else print(
29+
'user not avilable in rooms skipped message sending')
30+
return True
31+
32+
def leave_rooms(self, user_id: str):
33+
""" REMOVES USER FROM ALL GROUPS MAINLY DESIGNED FOR OFFLINE, WHEN USER GOES OFFLINE """
34+
for room in self.__rooms:
35+
for uid in self.__rooms[room]:
36+
if uid == user_id:
37+
self.__rooms[room].remove(uid)
38+
return True
39+
40+
def broadCastMessage(self, message, type: CodeType | str):
41+
""" Designed for any important notice by application admin to broadcast in all connected users """
42+
for ws in self.all_online_users:
43+
ws.send({
44+
"event": "application_admin",
45+
"type": type,
46+
"data": {
47+
"message": message
48+
}
49+
})

DB/all_users.db

0 Bytes
Binary file not shown.

DB/chats_lists.db

-12 KB
Binary file not shown.

DB/messages.db

-28 KB
Binary file not shown.

0 commit comments

Comments
 (0)