-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
140 lines (108 loc) · 3.38 KB
/
application.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
from flask import Flask, render_template, redirect, request, session
from flask_socketio import SocketIO, emit, send, join_room, leave_room
from tools import channel_find, login_required
import secrets
app = Flask(__name__)
app.config["SECRET_KEY"] = secrets.token_hex()
socketio = SocketIO(app, cors_allowed_origins="*")
# FLASK's variables
CHANNELS = ["General"]
MESSAGES = {CHANNELS[0]: []}
# SocketIO's variable
previous_channel = {}
@app.route("/")
@login_required
def index():
return render_template(
"index.html",
CHANNELS=CHANNELS,
USER=session["username"],
STR_CURRENT_CHANNEL="Inicio",
)
@app.route("/<channel>")
@login_required
def redirect_channel(channel):
if not channel_find(channel, MESSAGES):
return redirect("/")
session["current_channel"] = channel
return render_template(
"channel.html",
USER=session["username"],
STR_CURRENT_CHANNEL=session["current_channel"],
LIST_MESSAGES=MESSAGES[channel],
)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
session["username"] = request.form.get("username")
session["current_channel"] = None
return redirect("/")
else:
return render_template("login.html")
@app.route("/logout", methods=["POST"])
@login_required
def logout():
session.clear()
return redirect("/login")
@app.context_processor
def charge_channel_list():
return dict(CHANNELS=CHANNELS)
@socketio.on("create_channel")
def create_channel(new_chnl):
lwr_channel = []
for channel_item in CHANNELS:
lwr_channel.append(channel_item.lower())
if new_chnl.lower() in lwr_channel:
emit("error", "That name is already used", broadcast=False)
else:
CHANNELS.append(new_chnl)
MESSAGES[new_chnl] = []
emit("showChannel", new_chnl, broadcast=True)
lwr_channel = []
@socketio.on("exitRoom")
def exit_room():
user = session["username"]
channel = previous_channel[user]
if channel != None:
leave_room(channel)
emit(
"alertStatus",
f"{user} leaves {channel}",
broadcast=True,
include_self=False,
to=channel,
)
@socketio.on("enterRoom")
def enter_room():
user = session["username"]
channel = session["current_channel"]
previous_channel[user] = channel
join_room(channel)
emit("alertStatus", f"You have joined {channel}", broadcast=False)
emit(
"alertStatus",
f"{user} has joined {channel}",
broadcast=True,
include_self=False,
to=channel,
)
@socketio.on("message")
def process_messages(msg, user, date, current_channel):
MESSAGES[current_channel].append({"username": user, "msg": msg, "date": date})
# Only save 100 messages
len_c = len(MESSAGES[current_channel])
if len_c > 100:
for i in range(0, len_c - 100):
MESSAGES[current_channel].pop(0)
send((msg, user, date), to=current_channel)
@socketio.on("get_previous_channel")
def get_previous_channel():
if session["current_channel"] is None:
emit("redirect_channel", (False, "/"), broadcast=False)
else:
emit("redirect_channel", (True, session["current_channel"]), broadcast=False)
@socketio.on("delMsg")
def del_msg(i, cc):
del MESSAGES[cc][i]
if __name__ == "__main__":
socketio.run(app)