Titan/webapp/titanembeds/blueprints/gateway/gateway.py

43 lines
1.7 KiB
Python
Raw Normal View History

from titanembeds.utils import socketio, guild_accepts_visitors, get_client_ipaddr
from titanembeds.userbookkeeping import check_user_in_guild, get_guild_channels, update_user_status
2017-08-20 21:56:54 +02:00
from flask_socketio import Namespace, emit, disconnect, join_room
import functools
from flask import request, session
import time
2017-08-19 07:09:13 +02:00
class Gateway(Namespace):
def on_connect(self):
2017-08-20 21:56:54 +02:00
emit('hello')
def on_identify(self, data):
guild_id = data["guild_id"]
if not guild_accepts_visitors(guild_id) and not check_user_in_guild(guild_id):
disconnect()
return
channels = []
if guild_accepts_visitors(guild_id) and not check_user_in_guild(guild_id):
channels = get_guild_channels(guild_id, force_everyone=True)
else:
channels = get_guild_channels(guild_id)
join_room("GUILD_"+guild_id)
for chan in channels:
if chan["read"]:
join_room("CHANNEL_"+chan["channel"]["id"])
if session.get("unauthenticated", True) and guild_id in session.get("user_keys", {}):
join_room("IP_"+get_client_ipaddr())
elif not session.get("unauthenticated", True):
join_room("USER_"+session["user_id"])
emit("identified")
def on_heartbeat(self, data):
guild_id = data["guild_id"]
visitor_mode = data["visitor_mode"]
if not visitor_mode:
status = update_user_status(guild_id, session["username"], session["user_keys"][guild_id])
if status["revoked"] or status["banned"]:
emit("revoke")
time.sleep(1000)
disconnect()
else:
if not guild_accepts_visitors(guild_id):
disconnect()