From e3f57500ffbf060b75fe87c00c0927a8d7c0beff Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Fri, 25 Aug 2017 06:37:14 +0000 Subject: [PATCH] Realtime updating channel list --- discordbot/titanembeds/bot.py | 7 +++ .../titanembeds/socketio/socketiointerface.py | 59 ++++++++++++++++++- .../titanembeds/blueprints/gateway/gateway.py | 19 +++++- webapp/titanembeds/static/js/embed.js | 50 +++++++++++++++- 4 files changed, 130 insertions(+), 5 deletions(-) diff --git a/discordbot/titanembeds/bot.py b/discordbot/titanembeds/bot.py index 8533e36..21ac976 100644 --- a/discordbot/titanembeds/bot.py +++ b/discordbot/titanembeds/bot.py @@ -171,34 +171,41 @@ class Titan(discord.Client): async def on_server_update(self, guildbefore, guildafter): await self.wait_until_dbonline() await self.database.update_guild(guildafter) + await self.socketio.on_guild_update(guildafter) async def on_server_role_create(self, role): await self.wait_until_dbonline() if role.name == self.user.name and role.managed: await asyncio.sleep(2) await self.database.update_guild(role.server) + await self.socketio.on_guild_role_create(role) async def on_server_role_delete(self, role): await self.wait_until_dbonline() if role.server.me not in role.server.members: return await self.database.update_guild(role.server) + await self.socketio.on_guild_role_delete(role) async def on_server_role_update(self, rolebefore, roleafter): await self.wait_until_dbonline() await self.database.update_guild(roleafter.server) + await self.socketio.on_guild_role_update(role) async def on_channel_delete(self, channel): await self.wait_until_dbonline() await self.database.update_guild(channel.server) + await self.socketio.on_channel_delete(channel) async def on_channel_create(self, channel): await self.wait_until_dbonline() await self.database.update_guild(channel.server) + await self.socketio.on_channel_create(channel) async def on_channel_update(self, channelbefore, channelafter): await self.wait_until_dbonline() await self.database.update_guild(channelafter.server) + await self.socketio.on_channel_update(channelafter) async def on_member_join(self, member): await self.wait_until_dbonline() diff --git a/discordbot/titanembeds/socketio/socketiointerface.py b/discordbot/titanembeds/socketio/socketiointerface.py index bba0e46..4e7950d 100644 --- a/discordbot/titanembeds/socketio/socketiointerface.py +++ b/discordbot/titanembeds/socketio/socketiointerface.py @@ -110,4 +110,61 @@ class SocketIOInterface: async def on_guild_emojis_update(self, emojis): emotes = self.get_formatted_emojis(emojis) - await self.io.emit('GUILD_EMOJIS_UPDATE', data=emotes, room=str("GUILD_"+emojis[0].server.id), namespace='/gateway') \ No newline at end of file + await self.io.emit('GUILD_EMOJIS_UPDATE', data=emotes, room=str("GUILD_"+emojis[0].server.id), namespace='/gateway') + + def get_formatted_guild(self, guild): + guil = { + "id": guild.id, + "name": guild.name, + "icon": guild.icon, + "icon_url": guild.icon_url, + } + return guil + + async def on_guild_update(self, guild): + guildobj = self.get_formatted_guild(guild) + await self.io.emit('GUILD_UPDATE', data=guildobj, room=str("GUILD_"+guild.id), namespace='/gateway') + + def get_formatted_channel(self, channel): + chan = { + "id": channel.id, + "guild_id": channel.server.id, + } + return chan + + async def on_channel_delete(self, channel): + if str(channel.type) != "text": + return + chan = self.get_formatted_channel(channel) + await self.io.emit('CHANNEL_DELETE', data=chan, room=str("GUILD_"+channel.server.id), namespace='/gateway') + + async def on_channel_create(self, channel): + if str(channel.type) != "text": + return + chan = self.get_formatted_channel(channel) + await self.io.emit('CHANNEL_CREATE', data=chan, room=str("GUILD_"+channel.server.id), namespace='/gateway') + + async def on_channel_update(self, channel): + if str(channel.type) != "text": + return + chan = self.get_formatted_channel(channel) + await self.io.emit('CHANNEL_UPDATE', data=chan, room=str("GUILD_"+channel.server.id), namespace='/gateway') + + def get_formatted_role(self, role): + rol = { + "id": role.id, + "guild_id": role.server.id, + } + return rol + + async def on_guild_role_create(self, role): + rol = self.get_formatted_role(role) + await self.io.emit('GUILD_ROLE_CREATE', data=rol, room=str("GUILD_"+role.server.id), namespace='/gateway') + + async def on_guild_role_update(self, role): + rol = self.get_formatted_role(role) + await self.io.emit('GUILD_ROLE_UPDATE', data=rol, room=str("GUILD_"+role.server.id), namespace='/gateway') + + async def on_guild_role_delete(self, role): + rol = self.get_formatted_role(role) + await self.io.emit('GUILD_ROLE_DELETE', data=rol, room=str("GUILD_"+role.server.id), namespace='/gateway') \ No newline at end of file diff --git a/webapp/titanembeds/blueprints/gateway/gateway.py b/webapp/titanembeds/blueprints/gateway/gateway.py index c784f13..0461938 100644 --- a/webapp/titanembeds/blueprints/gateway/gateway.py +++ b/webapp/titanembeds/blueprints/gateway/gateway.py @@ -1,7 +1,7 @@ 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 from titanembeds.database import db, GuildMembers -from flask_socketio import Namespace, emit, disconnect, join_room +from flask_socketio import Namespace, emit, disconnect, join_room, leave_room import functools from flask import request, session import time @@ -63,4 +63,19 @@ class Gateway(Namespace): disconnect() else: if not guild_accepts_visitors(guild_id): - disconnect() \ No newline at end of file + disconnect() + + def on_channel_list(self, data): + guild_id = data["guild_id"] + visitor_mode = data["visitor_mode"] + channels = None + if visitor_mode or session.get("unauthenticated", True): + channels = get_guild_channels(guild_id, True) + else: + channels = get_guild_channels(guild_id) + for chan in channels: + if chan["read"]: + join_room("CHANNEL_"+chan["channel"]["id"]) + else: + leave_room("CHANNEL_"+chan["channel"]["id"]) + emit("channel_list", channels) \ No newline at end of file diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index a252361..93ebcb3 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -27,6 +27,7 @@ var authenticated_users_list = []; // List of all authenticated users var unauthenticated_users_list = []; // List of all guest users var discord_users_list = []; // List of all discord users that are probably online + var guild_channels_list = []; // guild channels, but as a list of them function element_in_view(element, fullyInView) { var pageTop = $(window).scrollTop(); @@ -387,6 +388,7 @@ } function fill_channels(channels) { + guild_channels_list = channels; var template = $('#mustache_channellistings').html(); Mustache.parse(template); $("#channels-list").empty(); @@ -1045,6 +1047,7 @@ }); socket.on("GUILD_MEMBER_UPDATE", function (usr) { + update_socket_channels(); for (var i = 0; i < discord_users_list.length; i++) { if (usr.id == discord_users_list[i].id) { discord_users_list.splice(i, 1); @@ -1072,12 +1075,55 @@ socket.on("GUILD_EMOJIS_UPDATE", function (emo) { emoji_store = emo; }); + + socket.on("CHANNEL_DELETE", function (chan) { + for (var i = 0; i < guild_channels_list.length; i++) { + var thatchannel = guild_channels_list[i]; + if (thatchannel.channel.id == chan.id) { + guild_channels_list.splice(i, 1); + fill_channels(guild_channels_list); + return; + } + } + }); + + socket.on("CHANNEL_UPDATE", function (chan) { + update_socket_channels(); + }); + + socket.on("CHANNEL_CREATE", function (chan) { + update_socket_channels(); + }); + + socket.on("GUILD_ROLE_UPDATE", function (chan) { + update_socket_channels(); + }); + + socket.on("GUILD_ROLE_DELETE", function (chan) { + update_socket_channels(); + }); + + socket.on("channel_list", function (chans) { + fill_channels(chans); + for (var i = 0; i < chans.length; i++) { + var thischan = chans[i]; + if (thischan.channel.id == selected_channel) { + $("#channeltopic").text(thischan.channel.topic); + } + } + }); } - function send_socket_heartbeat() { + function update_socket_channels() { if (!socket) { return; } - socket.emit("heartbeat", {"guild_id": guild_id, "visitor_mode": visitor_mode}); + socket.emit("channel_list", {"guild_id": guild_id, "visitor_mode": visitor_mode}); + } + + function send_socket_heartbeat() { + if (socket) { + socket.emit("heartbeat", {"guild_id": guild_id, "visitor_mode": visitor_mode}); + } } })();