#!/usr/bin/env python3 """ SocketIO events. """ import time import bleach from flask import session from flask_socketio import SocketIO, emit, join_room import database as db socketio = SocketIO() @socketio.on('joined') def joined(data): """ Sent by clients when they enter a room. """ room = data["room"] join_room(room) @socketio.on('message') def message(data): """ Sent by a client when the user entered a new message. """ room = data["room"] message = data["message"] date = int(time.time()) data["date"] = date 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 = '' + line + '' lines.append(line) message = "
".join(lines) data["message"] = message db.log_chat_message(data) emit("message", data, room=room) @socketio.on("new_post") def new_post(data): """ Called when the QM makes a new post. """ print(data) room = data["room"] res = db.get_quest_meta(quest_id=room) if not res: return if session.get("user_id") != res[3]: return post = data["post"] post = bleach.clean(post.strip()) post = post.replace("\n", "
") data["post"] = [post] db.insert_quest_post(room, post, int(time.time())) emit("new_post", data, room=room) @socketio.on("update_post") def update_post(data): """ Called when the QM edits and saves a post. """ room = data["room"] res = db.get_quest_meta(quest_id=room) if not res: return if session.get("user_id") != res[3]: return post = data["post"] post = post.strip().replace("
", "
") data["post"] = post post_id = data["post_id"] db.update_quest_post(post_id, post) emit("update_post", data, room=room)