forked from soris2000/Realtime-Chat-App
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
419 lines (370 loc) · 13.3 KB
/
Copy pathmain.py
File metadata and controls
419 lines (370 loc) · 13.3 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import datetime
import flet as ft
from signin_form import *
from signup_form import *
from chat_message import *
import predict
import complaint
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import hashlib
cred = credentials.Certificate(
"your_firebase_API_key.json")
firebase_admin.initialize_app(cred)
print("Server in ONLINE!")
db = firestore.client()
def main(page: ft.Page):
def sha256_hash(input_string):
sha256 = hashlib.sha256()
sha256.update(input_string.encode('utf-8'))
hashed_string = sha256.hexdigest()
return hashed_string
page.title = "Safe Messenger"
page.vertical_alignment = ft.MainAxisAlignment.CENTER
page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
def dropdown_changed(e):
new_message.value = new_message.value + emoji_list.value
page.update()
def close_banner(e):
page.banner.open = False
page.update()
def open_dlg():
page.dialog = dlg
dlg.open = True
page.update()
def close_dlg(e):
dlg.open = False
page.route = "/"
page.update()
def open_aff():
page.dialog = aff
aff.open = True
page.update()
def close_aff(e):
aff.open = False
page.route = "/"
page.update()
def req_unban(user, password):
users_ref = db.collection("users").document(user)
docs = users_ref.get()
data = docs.to_dict()
if data["password"] != sha256_hash(password):
ShowBanner("Password Incorrect")
elif not docs.exists:
ShowBanner("User Not Found")
elif not data["is_banned"]:
ShowBanner("User is not banned")
else:
users_ref.update({"raised": True})
open_aff()
def sign_in(user: str, password: str):
users_ref = db.collection("users").document(user)
docs = users_ref.get()
auth = False
ban = False
msg = 'No user with the Username Found!'
if docs.exists:
auth = True
msg = ''
data = docs.to_dict()
if data["password"] != sha256_hash(password):
auth = False
msg = "Log in failed, Incorrect User Name or Password"
if data["is_banned"]:
ban = True
msg = "You have been Banned from the chat room!"
if not auth or ban:
ShowBanner(msg)
print("LOG ERR")
else:
print("Redirecting to chat...")
page.session.set("user", user)
page.route = "/chat"
page.pubsub.send_all(
Message(
user=user,
text=f"{user} has joined the chat.",
message_type="login_message",
)
)
page.update()
def sign_up(user: str, password: str, mobile):
doc_ref = db.collection("users").document(user)
if doc_ref.set({"username": user, "password": sha256_hash(password), "mobile": mobile, "is_banned": False, "warnings": 0, "prev_bans": 0, "total_warnings": 0}):
print("Successfully Registered User...")
open_dlg()
def on_message(message: Message):
if message.message_type == "chat_message":
m = ChatMessage(message)
else:
m = ft.Text(message.text, italic=True,
color=ft.colors.WHITE, size=12)
chat.controls.append(m)
page.update()
page.pubsub.subscribe(on_message)
def send_message_click(e):
db = firestore.client()
message = new_message.value
prediction = predict.predict_text(message)
user = page.session.get("user")
users_ref = db.collection("users").document(user)
docs = users_ref.get()
data = docs.to_dict()
# Track the number of warnings given to the user
warning_count = data["warnings"]
if prediction > 50:
users_ref.update({"warnings": warning_count+1})
# Show an alert message to the user if the warning count is greater than 0
if warning_count < 3:
page.pubsub.send_all(
Message(
user=page.session.get("user"),
text=f"The message doesn't follow the policy of the company.{user} has {3 - warning_count} warnings remaining.",
message_type="alert",
)
)
# If the warning count is equal to 3, generate a complaint document and remove the user from the chat room
elif warning_count == 3:
number = data["mobile"]
complaint.generate_complaint_document(
page.session.get("user"), number, message, datetime.datetime.now())
page.pubsub.send_all(
Message(
user=page.session.get("user"),
text=f"{user} is banned from the chat room due to incomplaince with company policy.Their future messages will not reflect in the chat room",
message_type="alert",
)
)
users_ref.update({"is_banned": True})
users_ref.update({
"prev_bans": data["prev_bans"]+1})
users_ref.update({"message": message})
page.session.remove("user")
page.route = "/"
page.update()
else:
page.pubsub.send_all(
Message(
user=page.session.get("user"),
text="This message is not displayed due to company policy",
message_type="alert",
)
)
else:
if warning_count >= 3:
page.pubsub.send_all(
Message(
user=page.session.get("user"),
text="This message is not displayed due to company policy",
message_type="alert",
)
)
else:
page.pubsub.send_all(
Message(
user=page.session.get("user"),
text=message,
message_type="chat_message",
)
)
# Clear the message input field
new_message.value = ""
# Update the page
page.update()
def btn_signin(e):
page.route = "/"
page.update()
def btn_signup(e):
page.route = "/signup"
page.update()
def btn_exit(e):
page.session.remove("user")
page.route = "/"
page.update()
principal_content = ft.Column(
[
ft.Icon(ft.icons.WECHAT, size=200, color=ft.colors.BLUE),
ft.Text(value="Safe Messenger",
size=50, color=ft.colors.BLACK),
],
height=400,
width=600,
alignment=ft.MainAxisAlignment.CENTER,
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
)
emoji_list = ft.Dropdown(
on_change=dropdown_changed,
options=[
ft.dropdown.Option("😃"),
ft.dropdown.Option("😊"),
ft.dropdown.Option("😂"),
ft.dropdown.Option("🤔"),
ft.dropdown.Option("😭"),
ft.dropdown.Option("😉"),
ft.dropdown.Option("🤩"),
ft.dropdown.Option("🥰"),
ft.dropdown.Option("😎"),
ft.dropdown.Option("❤️"),
ft.dropdown.Option("🔥"),
ft.dropdown.Option("✅"),
ft.dropdown.Option("✨"),
ft.dropdown.Option("👍"),
ft.dropdown.Option("🎉"),
ft.dropdown.Option("👉"),
ft.dropdown.Option("⭐"),
ft.dropdown.Option("☀️"),
ft.dropdown.Option("👀"),
ft.dropdown.Option("👇"),
ft.dropdown.Option("🚀"),
ft.dropdown.Option("🎂"),
ft.dropdown.Option("💕"),
ft.dropdown.Option("🏡"),
ft.dropdown.Option("🍎"),
ft.dropdown.Option("🎁"),
ft.dropdown.Option("💯"),
ft.dropdown.Option("💤"),
],
width=50,
value="😃",
alignment=ft.alignment.center,
border_color=ft.colors.AMBER,
color=ft.colors.AMBER,
)
signin_UI = SignInForm(sign_in, req_unban, btn_signup)
signup_UI = SignUpForm(sign_up, btn_signin)
chat = ft.ListView(
expand=True,
spacing=10,
auto_scroll=True,
)
new_message = ft.TextField(
hint_text="Write a message...",
autofocus=True,
shift_enter=True,
min_lines=1,
max_lines=5,
filled=True,
expand=True,
on_submit=send_message_click,
)
def ShowBanner(msg):
page.banner = ft.Banner(
bgcolor=ft.colors.BLACK45,
leading=ft.Icon(ft.icons.ERROR, color=ft.colors.RED, size=40),
content=ft.Text(msg),
actions=[
ft.TextButton("Ok", on_click=close_banner),
],
)
page.banner.open = True
page.update()
dlg = ft.AlertDialog(
modal=True,
title=ft.Container(
content=ft.Icon(
name=ft.icons.CHECK_CIRCLE_OUTLINED, color=ft.colors.GREEN, size=100
),
width=120,
height=120,
),
content=ft.Text(
value="Congratulations,\n your account has been successfully created\n Please Sign In",
text_align=ft.TextAlign.CENTER,
),
actions=[
ft.ElevatedButton(
text="Continue", color=ft.colors.WHITE, on_click=close_dlg
)
],
actions_alignment="center",
on_dismiss=lambda e: print("Dialog dismissed!"),
)
aff = ft.AlertDialog(
modal=True,
title=ft.Container(
content=ft.Icon(
name=ft.icons.CHECK_CIRCLE_OUTLINED, color=ft.colors.GREEN, size=100
),
width=120,
height=120,
),
content=ft.Text(
value="Request Raised!",
text_align=ft.TextAlign.CENTER,
),
actions=[
ft.ElevatedButton(
text="Continue", color=ft.colors.WHITE, on_click=close_aff
)
],
actions_alignment="center",
on_dismiss=lambda e: print("Dialog dismissed!"),
)
def route_change(route):
if page.route == "/admin":
admin.initialize()
if page.route == "/":
page.clean()
page.add(
ft.Row(
[principal_content, signin_UI],
alignment=ft.MainAxisAlignment.CENTER,
)
)
if page.route == "/signup":
page.clean()
page.add(
ft.Row(
[principal_content, signup_UI],
alignment=ft.MainAxisAlignment.CENTER,
)
)
if page.route == "/chat":
if page.session.contains_key("user"):
page.clean()
page.add(
ft.Row(
[
ft.Text(value="Safe Messenger",
color=ft.colors.WHITE),
ft.ElevatedButton(
text="Log Out",
bgcolor=ft.colors.RED_800,
on_click=btn_exit,
),
],
alignment=ft.MainAxisAlignment.SPACE_AROUND,
)
)
page.add(
ft.Container(
content=chat,
border=ft.border.all(1, ft.colors.OUTLINE),
border_radius=5,
padding=10,
expand=True,
)
)
page.add(
ft.Row(
controls=[
emoji_list,
new_message,
ft.IconButton(
icon=ft.icons.SEND_ROUNDED,
tooltip="Send message",
on_click=send_message_click,
),
],
)
)
else:
page.route = "/"
page.update()
page.on_route_change = route_change
page.add(
ft.Row([principal_content, signin_UI],
alignment=ft.MainAxisAlignment.CENTER)
)
ft.app(target=main, view=ft.WEB_BROWSER, )