2018-06-16 18:52:46 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
SocketIO events.
|
|
|
|
"""
|
|
|
|
import time
|
|
|
|
|
2018-06-17 17:49:49 -04:00
|
|
|
import bleach
|
2018-06-16 18:52:46 -04:00
|
|
|
from flask_socketio import SocketIO, emit, join_room
|
|
|
|
|
2018-06-17 17:49:49 -04:00
|
|
|
import database as db
|
|
|
|
|
2018-06-16 18:52:46 -04:00
|
|
|
socketio = SocketIO()
|
|
|
|
|
2018-06-20 13:07:11 -04:00
|
|
|
@socketio.on('joined')
|
2018-06-16 18:52:46 -04:00
|
|
|
def joined(data):
|
|
|
|
"""
|
|
|
|
Sent by clients when they enter a room.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
join_room(room)
|
|
|
|
|
|
|
|
|
2018-06-20 13:07:11 -04:00
|
|
|
@socketio.on('message')
|
|
|
|
def message(data):
|
2018-06-16 18:52:46 -04:00
|
|
|
"""
|
|
|
|
Sent by a client when the user entered a new message.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
|
|
|
|
message = data["message"]
|
|
|
|
date = int(time.time())
|
|
|
|
data["date"] = date
|
2018-06-17 17:49:49 -04:00
|
|
|
|
|
|
|
message = message.strip()
|
|
|
|
if not message:
|
|
|
|
return
|
|
|
|
tags = ["b", "code", "i", "s"]
|
|
|
|
message = bleach.clean(message, tags=tags)
|
|
|
|
lines = []
|
|
|
|
for line in message.splitlines():
|
|
|
|
if line.startswith(">"):
|
|
|
|
line = '<span class="greenText">' + line + '</span>'
|
|
|
|
lines.append(line)
|
|
|
|
message = "<br />".join(lines)
|
|
|
|
data["message"] = message
|
2018-06-16 18:52:46 -04:00
|
|
|
|
2018-06-17 21:10:41 -04:00
|
|
|
db.log_chat_message(data)
|
2018-06-17 17:49:49 -04:00
|
|
|
emit("message", data, room=room)
|
2018-06-20 13:07:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.on("new_post")
|
|
|
|
def new_post(data):
|
|
|
|
"""
|
|
|
|
Called when the QM makes a new post.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
post = data["post"]
|
|
|
|
|
|
|
|
post = bleach.clean(post.strip())
|
|
|
|
post = post.replace("\n", "<br />")
|
|
|
|
data["post"] = [post]
|
2018-06-20 22:21:22 -04:00
|
|
|
db.insert_quest_post(room, post, int(time.time()))
|
2018-06-20 13:07:11 -04:00
|
|
|
|
|
|
|
emit("new_post", data, room=room)
|