MarseyWorld/files/routes/chat.py

89 lines
2.1 KiB
Python
Raw Normal View History

2022-03-22 14:17:43 +00:00
import sys
2022-03-23 16:15:59 +00:00
from files.helpers.const import SITE, SITE_FULL
if "load_chat" in sys.argv or SITE == 'localhost':
2022-03-22 13:54:17 +00:00
import time
2022-03-20 20:41:54 +00:00
from files.helpers.wrappers import auth_required
from files.helpers.sanitize import sanitize
from datetime import datetime
from flask_socketio import SocketIO, emit
2022-03-23 16:04:46 +00:00
from files.__main__ import app, limiter, cache
2022-03-24 03:00:39 +00:00
from flask import render_template, make_response, send_from_directory
2022-03-20 20:41:54 +00:00
import sys
2022-03-23 16:01:10 +00:00
import atexit
2022-03-20 20:41:54 +00:00
2022-03-22 14:11:33 +00:00
socketio = SocketIO(app, async_mode='gevent', cors_allowed_origins=[SITE_FULL])
2022-03-22 02:35:12 +00:00
typing = []
2022-03-22 03:32:24 +00:00
online = []
2022-03-23 16:04:20 +00:00
messages = cache.get('chat') or []
2022-03-22 00:56:41 +00:00
2022-03-23 16:05:11 +00:00
2022-03-20 00:13:02 +00:00
@app.get("/chat")
@auth_required
def chat( v):
2022-03-22 05:19:56 +00:00
return render_template("chat.html", v=v, messages=messages)
2022-03-19 22:16:13 +00:00
2022-03-24 02:55:19 +00:00
@app.get('/static/chat.js')
@limiter.exempt
2022-03-24 03:00:39 +00:00
def chatjs():
2022-03-24 02:55:19 +00:00
resp = make_response(send_from_directory('assets', 'js/chat.js'))
return resp
2022-03-22 00:56:41 +00:00
@socketio.on('speak')
2022-03-24 02:47:02 +00:00
@limiter.limit("3/second;10/minute")
2022-03-20 00:13:02 +00:00
@auth_required
def speak(data, v):
2022-03-22 05:19:56 +00:00
global messages
2022-03-22 14:26:42 +00:00
text = data[:1000].strip()
if not text: abort(403)
2022-03-19 23:32:41 +00:00
2022-03-20 00:13:02 +00:00
data={
"avatar": v.profile_url,
"username":v.username,
2022-03-22 00:30:20 +00:00
"namecolor":v.namecolor,
2022-03-22 14:26:42 +00:00
"text":text,
"text_html":sanitize(text),
2022-03-20 00:13:02 +00:00
}
2022-03-19 22:16:13 +00:00
2022-03-22 05:19:56 +00:00
messages.append(data)
2022-03-24 02:37:48 +00:00
messages = messages[-500:]
2022-03-20 00:13:02 +00:00
emit('speak', data, broadcast=True)
2022-03-22 00:56:41 +00:00
return '', 204
@socketio.on('connect')
2022-03-22 03:32:24 +00:00
@auth_required
def connect(v):
if v.username not in online:
online.append(v.username)
emit("online", online, broadcast=True)
2022-03-22 02:35:12 +00:00
emit('typing', typing)
return '', 204
2022-03-22 00:56:41 +00:00
@socketio.on('disconnect')
2022-03-22 02:35:12 +00:00
@auth_required
def disconnect(v):
2022-03-22 03:32:24 +00:00
if v.username in online:
online.remove(v.username)
emit("online", online, broadcast=True)
2022-03-22 02:35:12 +00:00
if v.username in typing: typing.remove(v.username)
emit('typing', typing, broadcast=True)
return '', 204
@socketio.on('typing')
@auth_required
def typing_indicator(data, v):
if data and v.username not in typing: typing.append(v.username)
elif not data and v.username in typing: typing.remove(v.username)
emit('typing', typing, broadcast=True)
2022-03-23 16:01:10 +00:00
return '', 204
def close_running_threads():
2022-03-23 16:04:05 +00:00
cache.set('chat', messages)
2022-03-23 16:01:10 +00:00
atexit.register(close_running_threads)