MarseyWorld/files/routes/chat.py

47 lines
1019 B
Python
Raw Normal View History

2022-03-20 20:41:54 +00:00
from files.helpers.const import SITE
2022-03-19 22:16:13 +00:00
2022-03-22 00:56:41 +00:00
if SITE in ('pcmemes.net', 'localhost'):
count = 0
2022-03-20 20:41:54 +00:00
import time
from files.helpers.wrappers import auth_required
from files.helpers.sanitize import sanitize
from datetime import datetime
from flask_socketio import SocketIO, emit
from files.__main__ import app
from flask import render_template
import sys
2022-03-22 00:56:41 +00:00
socketio = SocketIO(app, async_mode='gevent')
2022-03-20 00:13:02 +00:00
@app.get("/chat")
@auth_required
def chat( v):
return render_template("chat.html", v=v)
2022-03-19 22:16:13 +00:00
2022-03-22 00:56:41 +00:00
@socketio.on('speak')
2022-03-20 00:13:02 +00:00
@auth_required
def speak(data, v):
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-20 00:13:02 +00:00
"text":sanitize(data[:1000].strip()),
2022-03-22 00:56:41 +00:00
"time": time.strftime("%d %b %Y at %H:%M:%S", time.gmtime(int(time.time())))
2022-03-20 00:13:02 +00:00
}
2022-03-19 22:16:13 +00:00
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')
def connect():
global count
count += 1
emit("count", count)
@socketio.on('disconnect')
def disconnect():
global count
count -= 1
emit("count", count)