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 13:54:17 +00:00
|
|
|
if SITE in ('pcmemes.net', 'localhost'):
|
|
|
|
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-22 03:11:43 +00:00
|
|
|
from files.__main__ import app, limiter
|
2022-03-20 20:41:54 +00:00
|
|
|
from flask import render_template
|
|
|
|
import sys
|
|
|
|
|
2022-03-22 13:54:17 +00:00
|
|
|
socketio = SocketIO(app, async_mode='gevent')
|
2022-03-22 02:35:12 +00:00
|
|
|
typing = []
|
2022-03-22 03:32:24 +00:00
|
|
|
online = []
|
2022-03-22 05:19:56 +00:00
|
|
|
messages = []
|
2022-03-22 00:56:41 +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-22 00:56:41 +00:00
|
|
|
@socketio.on('speak')
|
2022-03-22 03:13:11 +00:00
|
|
|
@limiter.limit("5/second;30/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 04:14:16 +00:00
|
|
|
data = data[:1000].strip()
|
2022-03-22 03:38:02 +00:00
|
|
|
if not data: 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 04:14:16 +00:00
|
|
|
"text":sanitize(data),
|
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)
|
|
|
|
messages = messages[:20]
|
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)
|
|
|
|
return '', 204
|