Skip to content

Commit b582b60

Browse files
Ensured smoudth message arrival to all clints
1 parent 0039224 commit b582b60

23 files changed

+142
-126
lines changed
25 Bytes
Binary file not shown.
-30 Bytes
Binary file not shown.
72 Bytes
Binary file not shown.
9 Bytes
Binary file not shown.
82 Bytes
Binary file not shown.

API/DATABASE/connect.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def connect_messages():
2222
if not os.path.exists('DB'):
2323
os.makedirs('DB')
2424
return sqlite3.connect(path)
25-
25+
26+
27+
@staticmethod
2628
def connect_chat_lists_db():
2729
path = "DB/chats_lists.db"
2830
if not os.path.exists("DB"):

API/DATABASE/createDatabase.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import sqlite3
2-
import os
1+
# import sqlite3
2+
# import os
33
from .connect import database
44

5+
56
class create_database():
67
def __init__(self):
78
pass
8-
99
@staticmethod
1010
def create_users_database():
1111
CREDENTIAL_DATABASE_CONNECTED = database.connect_users_db()
@@ -34,8 +34,8 @@ def create_users_database():
3434
cursor.close()
3535
return True
3636
except Exception as e:
37-
print("error on create_users_database():",e)
38-
return False
37+
print("error on create_users_database():", e)
38+
return None
3939
finally:
4040
if CREDENTIAL_DATABASE_CONNECTED:
4141
CREDENTIAL_DATABASE_CONNECTED.close()
@@ -49,7 +49,8 @@ def create_chats_table_for_this_new_user(RAW_UID):
4949
if CHATS_DATABASE_CONNECTED:
5050
try:
5151
cursor = CHATS_DATABASE_CONNECTED.cursor()
52-
cursor.execute(f"""CREATE TABLE IF NOT EXISTS CHATS_{str(UID)} (
52+
cursor.execute(
53+
f"""CREATE TABLE IF NOT EXISTS CHATS_{str(UID)} (
5354
GUID INTEGER PRIMARY KEY AUTOINCREMENT,
5455
GNAME TEXT,
5556
PROFILE_PIC,
@@ -59,8 +60,10 @@ def create_chats_table_for_this_new_user(RAW_UID):
5960
cursor.close()
6061
return True
6162
except Exception as e:
62-
print("error on create_chats_table_for_this_new_user(RAW_UID):",e)
63-
return False
63+
print(
64+
"error on create_chats_table_for_this_new_user(RAW_UID):",
65+
e)
66+
return None
6467
finally:
6568
if CHATS_DATABASE_CONNECTED:
6669
CHATS_DATABASE_CONNECTED.close()
@@ -69,12 +72,13 @@ def create_chats_table_for_this_new_user(RAW_UID):
6972

7073
@staticmethod
7174
def create_chat_database_table(GROUP_ID):
72-
# TABLE_NAME = str(GUID)
75+
# TABLE_NAME = str(GUID)
7376
CHAT_DATABASE_CONNECTED = database.connect_messages()
7477
if CHAT_DATABASE_CONNECTED:
7578
try:
7679
cursor = CHAT_DATABASE_CONNECTED.cursor()
77-
cursor.execute(f"""CREATE TABLE IF NOT EXISTS G_{str(GROUP_ID)} (
80+
cursor.execute(
81+
f"""CREATE TABLE IF NOT EXISTS G_{str(GROUP_ID)} (
7882
MESSAGE_ID INTEGER PRIMARY KEY AUTOINCREMENT,
7983
SENDER_ID INTEGER,
8084
SENDER_NAME TEXT,
@@ -87,18 +91,19 @@ def create_chat_database_table(GROUP_ID):
8791
return True
8892
except Exception as e:
8993
print("error on create_chat_database_table(GROUP_ID):", e)
90-
return False
94+
return None
9195
finally:
9296
if CHAT_DATABASE_CONNECTED:
9397
CHAT_DATABASE_CONNECTED.close()
9498
else:
9599
print(' unable to comunicate with chat tables database')
96-
100+
97101
@staticmethod
98102
def create_message_list_database():
103+
connection = None
99104
try:
100105
connection = database.connect_chat_lists_db()
101-
if connection:
106+
if connection:
102107
cursor = connection.cursor()
103108
cursor.execute("""
104109
CREATE TABLE IF NOT EXISTS all_chats (
@@ -117,5 +122,3 @@ def create_message_list_database():
117122
finally:
118123
if connection:
119124
connection.close()
120-
121-

API/DATABASE/get_data.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from .connect import database
22
from .createDatabase import create_database
3-
from datetime import datetime
4-
import sys
5-
import json
3+
# from datetime import datetime
4+
# import sys
5+
# import json
66

77

88
class get:
@@ -188,17 +188,14 @@ def uid_by_cookie(COOKIE):
188188
connection = None
189189
uid = None
190190
try:
191-
192191
connection = database.connect_users_db()
193192
if connection:
193+
print("connection sucessfull")
194194
cursor = connection.cursor()
195-
196-
cursor.execute(
197-
"""
198-
SELECT UID FROM users WHERE COOKIE=?
199-
""", (COOKIE, ))
200-
195+
cursor.execute("""SELECT UID FROM users WHERE COOKIE=?""",
196+
(COOKIE, ))
201197
row = cursor.fetchone()
198+
print(row)
202199
uid = row[0] if row else None
203200
cursor.close()
204201
return uid
@@ -276,9 +273,10 @@ def profile_pic(*, UID=None, GUID=None):
276273
finally:
277274
if connection:
278275
connection.close()
279-
276+
280277
@staticmethod
281278
def first_name(RAW_UID: str):
279+
connection = None
282280
UID = int(RAW_UID)
283281
try:
284282
connection = database.connect_users_db()
@@ -300,7 +298,9 @@ def first_name(RAW_UID: str):
300298
if connection:
301299
connection.close()
302300

301+
@staticmethod
303302
def last_name(RAW_UID):
303+
connection = None
304304
UID = int(RAW_UID)
305305
try:
306306
connection = database.connect_users_db()
@@ -317,8 +317,11 @@ def last_name(RAW_UID):
317317
if connection:
318318
connection.close()
319319

320+
@staticmethod
320321
def all_chats_json(RAW_UID):
322+
connection = None
321323
UID = int(RAW_UID)
324+
chats_json = {}
322325
try:
323326
connection = database.connect_chat_lists_db()
324327
if connection:
@@ -337,15 +340,17 @@ def all_chats_json(RAW_UID):
337340
"CREATION_DATE": chat[3]
338341
}
339342
cursor.close()
340-
return chats_json
343+
return chats_json if chats_json else {}
341344
except Exception as e:
342345
print(f"erro while getting the chats of {UID} ==>> {e}")
343-
return False
346+
return {}
344347
finally:
345348
if connection:
346349
connection.close()
347-
350+
@staticmethod
348351
def all_messages_json(limit, guid):
352+
connection = None
353+
messages_of_this_group_in_json_dict = {}
349354
try:
350355
connection = database.connect_messages()
351356
if connection:
@@ -360,7 +365,7 @@ def all_messages_json(limit, guid):
360365
""")
361366
messages_of_this_group_in_json_dict = {}
362367
rows_list_type = cursor.fetchall()
363-
print(rows_list_type)
368+
# print(rows_list_type)
364369
for row_tupple in rows_list_type:
365370
messages_of_this_group_in_json_dict[str(row_tupple[0])] = {
366371
"SENDER_ID": row_tupple[1],
@@ -373,12 +378,13 @@ def all_messages_json(limit, guid):
373378
return messages_of_this_group_in_json_dict
374379
except Exception as e:
375380
print("err on all_messages_json", e)
376-
return False
381+
return {}
377382
finally:
378383
if connection:
379384
connection.close()
380-
385+
@staticmethod
381386
def all_personal_data(UID):
387+
connection = None
382388
try:
383389
connection = database.connect_users_db()
384390
if connection:
@@ -389,9 +395,9 @@ def all_personal_data(UID):
389395
""", (UID, ))
390396

391397
fc = cursor.fetchall()
392-
print('its fc data', fc)
398+
# print('its fc data', fc)
393399
raw_data = fc[0]
394-
print('its raw data', raw_data)
400+
# print('its raw data', raw_data)
395401
data = {
396402
'FIRST_NAME': raw_data[1],
397403
'LAST_NAME': raw_data[2],
@@ -405,7 +411,7 @@ def all_personal_data(UID):
405411
return data
406412
except Exception as e:
407413
print(e)
408-
return None
414+
return {}
409415
finally:
410416
if connection:
411417
connection.close()

API/DATABASE/update_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#in future ill add some more metjods like passwod mail number dob changing
44
class update:
55
def otp(new_otp, UID, COOKIE):
6+
connection = None
67
try:
78
connection = database.connect_users_db()
89
if connection:

API/DATABASE/write_data.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
from .createDatabase import create_database
33
from datetime import datetime
44
class write_in_database:
5+
6+
@staticmethod
57
def add_user(FIRST_NAME, LAST_NAME, EMAIL, IS_MAIL_OTP, DOB, PHONE_NO, IP, CITY,PASSWORD, TOKEN, COOKIE, PROFILE_PIC):
68
create_database.create_users_database()
9+
connection = None
710
try:
811
connection = database.connect_users_db()
912
cursor = connection.cursor()
@@ -35,9 +38,11 @@ def add_user(FIRST_NAME, LAST_NAME, EMAIL, IS_MAIL_OTP, DOB, PHONE_NO, IP, CITY,
3538
# ill use it when a user creates a group
3639
# it will add new group to all chats database
3740
# and additionally return that new groups uid
41+
@staticmethod
3842
def add_group(GNAME, GUID=None):
3943
#create table if not exists
4044
create_database.create_message_list_database()
45+
connection = None
4146
try:
4247
# imsert new group
4348
connection = database.connect_chat_lists_db()
@@ -71,9 +76,11 @@ def add_group(GNAME, GUID=None):
7176
# this will add grpup to users table
7277
# this table will named by each users UID
7378
# this table will be returned to show all chat list for user
79+
@staticmethod
7480
def add_user_in_group(RAW_UID ,RAW_GUID, GNAME):
7581
UID = int(RAW_UID)
7682
GUID = int(RAW_GUID)
83+
connection = None
7784
try:
7885
## uid is hader or name of create_chats_table_for_this_new_user
7986
## GUID is groups uniq id
@@ -100,6 +107,7 @@ def add_user_in_group(RAW_UID ,RAW_GUID, GNAME):
100107
# it will store messages
101108
# each group id represents that specific table
102109
# this table contains all messages
110+
@staticmethod
103111
def store_this_message(GROUP_ID, SENDER_ID, SENDER_NAME, MESSAGE, PROFILE_PIC):
104112
create_database.create_chat_database_table(GROUP_ID)
105113
connection = None
@@ -112,13 +120,13 @@ def store_this_message(GROUP_ID, SENDER_ID, SENDER_NAME, MESSAGE, PROFILE_PIC):
112120
""", (int(SENDER_ID), SENDER_NAME, MESSAGE, PROFILE_PIC))
113121
connection.commit()
114122
MESSAGE_ID = cursor.lastrowid
115-
print(MESSAGE_ID)
123+
# print(MESSAGE_ID)
116124
cursor.execute(f"""
117125
SELECT TIME_STAMP, PROFILE_PIC FROM G_{str(GROUP_ID)} WHERE MESSAGE_ID = ?
118126
""",(MESSAGE_ID,))
119127

120128
result = cursor.fetchone()
121-
print(result)
129+
# print(result)
122130
return MESSAGE_ID, result[0], result [1]
123131
except Exception as e:
124132
print("error on store_this_message(GROUP_ID, SENDER_ID, SENDER_NAME, MESSAGE):", e)

0 commit comments

Comments
 (0)