forked from Sanjeb/Chat-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_chat.py
154 lines (134 loc) · 6.8 KB
/
functions_chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import logging
import connect
mydb = connect.mydb
cursor = connect.cursor
with open('credentials.txt', 'r') as f:
CurrentUserID, email, password = f.read().split()
'''
def create_group(groupName, ownerID, participant1, participant2, *participantIDs):
try:
cursor.execute(f"INSERT INTO `groups` (`group name`) VALUES ('{groupName}')")
id = cursor.lastrowid
cursor.execute(f"INSERT INTO `group members` (`users_user id`, `groups_group id`) VALUES ('{participant1}', {id})")
cursor.execute(f"INSERT INTO `group members` (`users_user id`, `groups_group id`) VALUES ('{participant2}', {id})")
for x in participantIDs:
cursor.execute(f"INSERT INTO `group members` (`users_user id`, `groups_group id`) VALUES ('{x}', {id})")
mydb.commit()
logging.info(f"Created new group {groupName}")
except:
logging.error("Couldn't create group")
def delete_group(groupID):
try:
cursor.execute(f"DELETE FROM `group messages` WHERE (`groups_group id` = '{groupID}')")
cursor.execute(f"DELETE FROM `group members` WHERE (`groups_group id` = '{groupID}')")
cursor.execute(f"DELETE FROM `groups` WHERE (`group id` = '{groupID}')")
mydb.commit()
logging.info(f"Deleted group with ID {groupID}")
except:
logging.error("Couldn't delete group")
def add_participant_to_group(groupID, participantID, *participantIDs):
try:
cursor.execute(f"INSERT INTO `group members` (`users_user id`, `groups_group id`) VALUES ('{participantID}', '{groupID}');")
for x in participantIDs:
cursor.execute(f"INSERT INTO `group members` (`users_user id`, `groups_group id`) VALUES ('{x}', '{groupID}');")
mydb.commit()
logging.info(f"Added participant {participantID} to group {groupID}")
except:
logging.error("Couldn't add participant to group")
def remove_participant_from_group(groupID, participantID, *participantIDs):
try:
cursor.execute(f"DELETE FROM `group members` WHERE (`users_user id` = '{participantID}') and (`groups_group id` = '{groupID}');")
for x in participantIDs:
cursor.execute(f"DELETE FROM `group members` WHERE (`users_user id` = '{x}') and (`groups_group id` = '{groupID}');")
mydb.commit()
logging.info(f"Removed participant {participantID} {participantIDs} from group {groupID}")
except:
logging.error("Couldn't remove participant from group")
'''
def get_dm_messages(dmID):
cursor.execute(f"SELECT `dm messages`.*, users.`user name` FROM `dm messages`, users WHERE `dm id` = {dmID} AND `sender user id` = users.`user id` ORDER BY `time sent`;")
messages = []
for x in cursor:
messages.append(x)
return messages #Returns list in the format [(MessageID, MessageText, SenderUserID, DMID, SenderUsername), (MessageID, MessageText, SenderUserID, DMID, SenderUsername)]
def get_latest_dm_messages(dmID, lastMessageID):
cursor.execute(f"SELECT `dm messages`.*, users.`user name` FROM `dm messages`, users WHERE `message id` > {lastMessageID} AND `dm id` = {dmID} AND `sender user id` = users.`user id` ORDER BY `time sent`;")
messages = []
for x in cursor:
messages.append(x)
return messages #Returns list in the format [(MessageID, MessageText, SenderUserID, DMID, SenderUsername), (MessageID, MessageText, SenderUserID, DMID, SenderUsername)]
def get_dm_users():
cursor.execute(f"SELECT * FROM `dm members` WHERE `user id` = {CurrentUserID}")
ids = []
dms = cursor.fetchall()
for x in dms:
cursor.execute(f"SELECT `dm members`.*, users.`user name`, users.picture FROM `dm members`, users WHERE `dm members`.`dm id` = {x[1]} and `dm members`.`user id` != {CurrentUserID} AND `dm members`.`user id` = `users`.`user id`;")
dm_ids = cursor.fetchall()
ids.append(dm_ids[0])
return ids #Returns list in the format [(UserID, DMID, UserName), (UserID, DMID, UserName)]
def send_dm_messages(dmID, message):
values = (message, CurrentUserID, dmID)
command = "INSERT INTO `dm messages` (`message text`, `sender user id`, `dm id`) VALUES (%s, %s, %s);"
cursor.execute(command, values)
mydb.commit()
def get_user(email):
cursor.execute(f" SELECT * FROM users WHERE email = '{email}'")
info = cursor.fetchone()
if info == None:
return info
user = []
for index in range(len(info) - 1):
user.append(info[index])
with open('ProfilePictures/' + str(info[0]) + '.png', 'wb') as file:
file.write(info[4])
return user
def new_dm(userID):
cursor.execute("INSERT INTO `dm id`(`dm id`) VALUES(NULL)")
dmID = cursor.lastrowid
mydb.commit()
cursor.execute(f"INSERT INTO `dm members`(`user id`, `dm id`) VALUES({CurrentUserID}, {dmID})")
cursor.execute(f"INSERT INTO `dm members`(`user id`, `dm id`) VALUES({userID}, {dmID})")
mydb.commit()
def get_bio(userID):
cursor.execute(f"SELECT * FROM bio WHERE `user id` = {userID}")
bio = cursor.fetchone()
return bio
def profile_update(email, username, password):
values = (email, username, password, CurrentUserID)
command = "UPDATE `users` SET `email` = %s, `user name` = %s, `password` = %s WHERE `user id` = %s"
cursor.execute(command, values)
mydb.commit()
cursor.execute(f"SELECT * FROM `users` WHERE `user id` = {CurrentUserID}")
credentials = cursor.fetchone()
with open('credentials.txt', 'w') as f:
credentialsString = str(credentials[0]) + '\n' + credentials[1] + '\n' + credentials[3]
f.write(credentialsString)
read_credentials()
def update_bio(about):
command = "UPDATE bio SET `About Me` = %s WHERE `user id` = %s"
values = (about, CurrentUserID)
cursor.execute(command, values)
mydb.commit()
def read_credentials():
global CurrentUserID, email, password
with open('credentials.txt', 'r') as f:
CurrentUserID, email, password = f.read().split()
def unfriend(dmID):
cursor.execute(f"DELETE FROM `dm messages` WHERE `dm id` = {dmID}")
cursor.execute(f"DELETE FROM `dm members` WHERE `dm id` = {dmID}")
cursor.execute(f"DELETE FROM `dm id` WHERE (`dm id` = {dmID})")
def update_pfp(picture):
command = "UPDATE `users` SET picture = %s WHERE `user id` = %s"
values = (picture, CurrentUserID)
cursor.execute(command, values)
mydb.commit()
def update_socials(facebook,instagram,spotify,youtube):
command = f'UPDATE bio SET Instagram = %s, Spotify = %s, Facebook = %s, Youtube = %s WHERE `user id` = {CurrentUserID}'
record = (instagram,spotify,facebook,youtube)
cursor.execute(command,record)
mydb.commit()
def submit_rating(slider, message):
feedbackRecord = (CurrentUserID , slider, message)
command = f'INSERT INTO feedback(`user id`, `rating`, `feedback`) VALUES(%s,%s,%s)'
cursor.execute(command, feedbackRecord)
mydb.commit()