/
application.py
190 lines (164 loc) · 6.27 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
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
import os
from flask import Flask, session, render_template, url_for, request, redirect, jsonify, Response
from flask_session import Session
import random, json, time, datetime
from flask_socketio import SocketIO, emit, join_room, leave_room
# configure the flask_socketio
app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)
# list of channels
channel_list = ["General"]
user_list = []
# Dictionary of users & messages
user_dm_list = {}
# dictionary to track rooms, or private channels
# Rooms = {"dn:" displayname, "room": room}
Rooms = {}
now = datetime.datetime.now()
startup_message = {
"channel": "General",
"user_from": "Flack Bot",
"user_to": "",
"timestamp": now.strftime("%a %b %d %I:%M:%S %Y"),
"msg_txt": "Welcome to Flack Messaging"}
channel_messages = {
"General": {
'messages': [startup_message]
}}
# this is our index page
@app.route("/")
def index():
return render_template("index.html")
# this function handles the logout button and returns
# index page as a result
@app.route("/logout", methods=["POST"])
def logout():
return render_template("index.html")
# this handles the chatrooms
@app.route("/chatbox", methods=["POST", "GET"])
def chatbox():
user = request.form.get("displayname")
return render_template("chatbox.html", name=user)
# this is a event of submitting channel to create
# a new channel
@socketio.on("submit channel")
def new_channel(data):
channel = data["channel"]
channel_list.append(channel)
emit("announce channel", {"channel": channel}, broadcast=True)
# this is the backend to support querying for
# list of new available channels
@app.route("/get_channels", methods=["POST"])
def get_channels():
return jsonify({"success": True, "channel_list": channel_list})
# this is the backend to support querying for
# users in a channel
@app.route("/get_users", methods=["POST"])
def get_users():
return jsonify({"success": True, "active_users": user_list})
# this is the backend to support querying for
# messages in a channel
@app.route("/get_messages", methods=["POST"])
def fetch_messages():
channel = request.form.get("channel")
dn = request.form.get("displayname")
msg_status = request.form.get("msg_type")
if (msg_status == "PUBLIC"):
my_msgs = channel_messages.get(channel)
else:
my_msgs = user_dm_list.get(channel)
if my_msgs:
msglist = my_msgs['messages']
if ((msg_status == 'PUBLIC') or (channel == dn)):
return jsonify({"success": True, "channel_msgs": msglist})
else:
all_msgs = []
for msg in msglist:
if((msg["user_from"] == dn) or (msg["user_to"] == dn)):
all_msgs.append(msg)
return jsonify({"success": True, "channel_msgs": all_msgs})
else:
return jsonify({"success": False, "error_msg": "No messages"})
# this is the socketio backend to handle the submit messages
# event in the flask, it shows public channel
# first of all we see if channel is in channel_messages as those
# are public channel with messages , then we check public channel
# with first message and then we go for private messages
@socketio.on("submit message")
def new_message(data):
channel = data["channel"]
user_from = data["user_from"]
msg_txt = data["msg_txt"]
timestamp = time.asctime( time.localtime( time.time() ) )
msg = {"channel": channel,
"user_from": user_from,
"user_to": channel,
"timestamp": timestamp,
"msg_txt": msg_txt}
if channel in channel_messages:
msgs = channel_messages[channel]
msg["msg_type"] = "PUBLIC"
if len(msgs['messages']) >= 100:
del msgs['messages'][0]
msgs['messages'].append(msg)
emit("announce message", msg, broadcast=True)
return jsonify ({"success": True, "msg_type": "PUBLIC"})
else:
if (not (channel in user_dm_list)):
msg["msg_type"] = "PUBLIC"
channel_messages[channel] = {"channel": channel, "messages": [msg]}
emit("announce message", msg, broadcast=True)
return jsonify ({"success": True, "msg_type": "PUBLIC"})
else:
msg["msg_type"] = "PRIVATE"
if (channel in user_dm_list):
for user in [user_from, channel]:
msgs = user_dm_list[user]
if len(msgs['messages']) >= 100:
del msgs['messages'][0]
msgs['messages'].append(msg)
else:
user_dm_list[user] = {"channel": channel, "messages": [msg]}
print (f"NM: emit msg to ", user_from)
msg["channel"] = channel
emit("announce message", msg, room=Rooms[user_from])
print (f"NM: emit msg to ", channel)
msg["channel"] = user_from
emit("announce message", msg, room=Rooms[channel])
return jsonify ({"success": True, "msg_type": "PRIVATE"})
# join channel event for joining the channel or room
@socketio.on('join')
def on_join(data):
username = data['displayname']
if (username == ""):
return jsonify ({"success": False, "error_msg": "No text entered"})
if (not (username in user_list)):
user_list.append(username)
user_dm_list[username] = ({"channel": username, "messages": []})
emit("new user", {"username": username}, broadcast= True)
else:
emit("user logged in", {"username": username}, broadcast=True)
room = data['room']
join_room(room)
Rooms[username] = room
print (f"username ", username, "has room ", Rooms[username])
return jsonify ({"success": True})
# socketio logout event to log user out
@socketio.on('logout user')
def on_leave(data):
username = data['displayname']
print (f"username ", username, " logging out")
room = Rooms[username]
leave_room(room)
del Rooms[username]
emit("user logged out", {"username": username}, broadcast=True)
# socketio leave backend to support to leave a channel or room
@socketio.on('leave')
def on_leave(data):
username = data['displayname']
print (f"username ", username, " logging out")
room = Rooms[username]
leave_room(room)
del Rooms[username]
emit("user logged out", {"username": username}, broadcast=True)