2017-08-21 06:26:51 +02:00
|
|
|
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-22 08:57:30 +02:00
|
|
|
from titanembeds.database import db, GuildMembers
|
2017-08-20 21:56:54 +02:00
|
|
|
from flask_socketio import Namespace, emit, disconnect, join_room
|
|
|
|
import functools
|
|
|
|
from flask import request, session
|
2017-08-21 06:26:51 +02:00
|
|
|
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):
|
2017-08-21 06:26:51 +02:00
|
|
|
guild_id = data["guild_id"]
|
|
|
|
if not guild_accepts_visitors(guild_id) and not check_user_in_guild(guild_id):
|
|
|
|
disconnect()
|
|
|
|
return
|
2017-08-22 08:57:30 +02:00
|
|
|
session["socket_guild_id"] = guild_id
|
2017-08-21 06:26:51 +02:00
|
|
|
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"])
|
2017-08-22 08:57:30 +02:00
|
|
|
visitor_mode = data["visitor_mode"]
|
|
|
|
if not visitor_mode:
|
|
|
|
if session["unauthenticated"]:
|
|
|
|
emit("embed_user_connect", {"unauthenticated": True, "username": session["username"], "discriminator": session["user_id"]}, room="GUILD_"+guild_id)
|
|
|
|
else:
|
|
|
|
nickname = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == session["user_id"]).first().nickname
|
|
|
|
emit("embed_user_connect", {"unauthenticated": False, "id": session["user_id"], "nickname": nickname, "discriminator": session["discriminator"], "avatar_url": session["avatar"]}, room="GUILD_"+guild_id)
|
2017-08-21 06:26:51 +02:00
|
|
|
emit("identified")
|
|
|
|
|
2017-08-22 08:57:30 +02:00
|
|
|
def on_disconnect(self):
|
|
|
|
if "user_keys" not in session:
|
|
|
|
return
|
|
|
|
guild_id = session["socket_guild_id"]
|
|
|
|
msg = {}
|
|
|
|
if session["unauthenticated"]:
|
|
|
|
msg = {"unauthenticated": True, "username": session["username"], "discriminator": session["user_id"]}
|
|
|
|
else:
|
|
|
|
msg = {"unauthenticated": False, "id": session["user_id"]}
|
|
|
|
emit("embed_user_disconnect", msg, room="GUILD_"+guild_id)
|
|
|
|
|
2017-08-21 06:26:51 +02:00
|
|
|
def on_heartbeat(self, data):
|
|
|
|
guild_id = data["guild_id"]
|
|
|
|
visitor_mode = data["visitor_mode"]
|
|
|
|
if not visitor_mode:
|
2017-08-22 08:57:30 +02:00
|
|
|
key = None
|
|
|
|
if session["unauthenticated"]:
|
|
|
|
key = session["user_keys"][guild_id]
|
|
|
|
status = update_user_status(guild_id, session["username"], key)
|
2017-08-21 06:26:51 +02:00
|
|
|
if status["revoked"] or status["banned"]:
|
|
|
|
emit("revoke")
|
|
|
|
time.sleep(1000)
|
|
|
|
disconnect()
|
|
|
|
else:
|
|
|
|
if not guild_accepts_visitors(guild_id):
|
|
|
|
disconnect()
|