Realtime updating channel list

This commit is contained in:
Jeremy Zhang 2017-08-25 06:37:14 +00:00
parent d1225e5273
commit e3f57500ff
4 changed files with 130 additions and 5 deletions

View File

@ -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()

View File

@ -111,3 +111,60 @@ 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')
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')

View File

@ -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
@ -64,3 +64,18 @@ class Gateway(Namespace):
else:
if not guild_accepts_visitors(guild_id):
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)

View File

@ -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("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});
}
}
})();