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
|
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 00:56:41 +00:00
|
|
|
socketio = SocketIO(app, async_mode='gevent')
|
2022-03-22 02:35:12 +00:00
|
|
|
typing = []
|
2022-03-22 00:56:41 +00:00
|
|
|
|
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-22 03:11:10 +00:00
|
|
|
@limiter.limit("3/second;30/minute")
|
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
|
2022-03-22 02:35:12 +00:00
|
|
|
emit("count", count, broadcast=True)
|
|
|
|
|
|
|
|
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 00:56:41 +00:00
|
|
|
global count
|
|
|
|
count -= 1
|
2022-03-22 02:35:12 +00:00
|
|
|
emit("count", count, broadcast=True)
|
|
|
|
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
|