Teardown sqlalchemy session on each emit

This commit is contained in:
Jeremy Zhang 2017-10-01 04:07:04 +00:00
parent fc8768d389
commit 370d4c6985

View File

@ -7,6 +7,10 @@ import time
import json import json
class Gateway(Namespace): class Gateway(Namespace):
def teardown_db_session(self):
db.session.commit()
db.session.remove()
def on_connect(self): def on_connect(self):
emit('hello') emit('hello')
@ -37,6 +41,7 @@ class Gateway(Namespace):
nickname = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == session["user_id"]).first().nickname 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, "username": session["username"],"discriminator": session["discriminator"], "avatar_url": session["avatar"]}, room="GUILD_"+guild_id) emit("embed_user_connect", {"unauthenticated": False, "id": session["user_id"], "nickname": nickname, "username": session["username"],"discriminator": session["discriminator"], "avatar_url": session["avatar"]}, room="GUILD_"+guild_id)
emit("identified") emit("identified")
self.teardown_db_session()
def on_disconnect(self): def on_disconnect(self):
if "user_keys" not in session: if "user_keys" not in session:
@ -62,7 +67,8 @@ class Gateway(Namespace):
for webhook in guild_webhooks: for webhook in guild_webhooks:
if webhook["name"] == name: if webhook["name"] == name:
discord_api.delete_webhook(webhook["id"], webhook["token"]) discord_api.delete_webhook(webhook["id"], webhook["token"])
self.teardown_db_session()
def on_heartbeat(self, data): def on_heartbeat(self, data):
guild_id = data["guild_id"] guild_id = data["guild_id"]
visitor_mode = data["visitor_mode"] visitor_mode = data["visitor_mode"]
@ -78,7 +84,8 @@ class Gateway(Namespace):
else: else:
if not guild_accepts_visitors(guild_id): if not guild_accepts_visitors(guild_id):
disconnect() disconnect()
self.teardown_db_session()
def on_channel_list(self, data): def on_channel_list(self, data):
guild_id = data["guild_id"] guild_id = data["guild_id"]
visitor_mode = data["visitor_mode"] visitor_mode = data["visitor_mode"]
@ -93,7 +100,8 @@ class Gateway(Namespace):
else: else:
leave_room("CHANNEL_"+chan["channel"]["id"]) leave_room("CHANNEL_"+chan["channel"]["id"])
emit("channel_list", channels) emit("channel_list", channels)
self.teardown_db_session()
def on_current_user_info(self, data): def on_current_user_info(self, data):
guild_id = data["guild_id"] guild_id = data["guild_id"]
if "user_keys" in session and not session["unauthenticated"]: if "user_keys" in session and not session["unauthenticated"]:
@ -106,7 +114,8 @@ class Gateway(Namespace):
'user_id': session['user_id'], 'user_id': session['user_id'],
} }
emit("current_user_info", usr) emit("current_user_info", usr)
self.teardown_db_session()
def get_user_color(self, guild_id, user_id): def get_user_color(self, guild_id, user_id):
color = None color = None
member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == user_id).first() member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == user_id).first()
@ -164,4 +173,5 @@ class Gateway(Namespace):
usr["color"] = self.get_user_color(guild_id, usr["id"]) usr["color"] = self.get_user_color(guild_id, usr["id"])
if (usr["avatar"]): if (usr["avatar"]):
usr["avatar_url"] = "https://cdn.discordapp.com/avatars/{}/{}.jpg".format(usr["id"], usr["avatar"]) usr["avatar_url"] = "https://cdn.discordapp.com/avatars/{}/{}.jpg".format(usr["id"], usr["avatar"])
emit("lookup_user_info", usr) emit("lookup_user_info", usr)
self.teardown_db_session()