From 80d89b49f851ea5795fd2b9c2ae50c5f0ec57b37 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Fri, 15 Sep 2017 06:25:03 +0000 Subject: [PATCH] Chat username discord colors! --- webapp/titanembeds/blueprints/api/api.py | 14 ++--- .../titanembeds/blueprints/gateway/gateway.py | 61 ++++++++++++++++++- webapp/titanembeds/static/js/embed.js | 56 +++++++++++++++++ 3 files changed, 122 insertions(+), 9 deletions(-) diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index 51b6095..1d3789a 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -241,9 +241,8 @@ def post(): userid = session["user_id"] content = format_everyone_mention(chan, content) webhook = get_channel_webhook_url(guild_id, channel_id) - if userid in get_administrators_list(): - oldcontent = content - content = "(Titan Dev) " + oldcontent + # if userid in get_administrators_list(): + # content = "(Titan Dev) " + content if webhook: if (session['unauthenticated']): username = session["username"] + "#" + str(session["user_id"]) @@ -258,11 +257,10 @@ def post(): if dbUser: if dbUser.nickname: username = dbUser.nickname - if content.startswith("(Titan Dev) "): - content = content[12:] - username = "(Titan Dev) " + username - else: - username = username + "#" + str(session['discriminator']) + # if content.startswith("(Titan Dev) "): + # content = content[12:] + # username = "(Titan Dev) " + username + username = username + "#" + str(session['discriminator']) avatar = session['avatar'] message = discord_api.execute_webhook(webhook.get("id"), webhook.get("token"), username, avatar, content) else: diff --git a/webapp/titanembeds/blueprints/gateway/gateway.py b/webapp/titanembeds/blueprints/gateway/gateway.py index 1e6dabd..cde0f7b 100644 --- a/webapp/titanembeds/blueprints/gateway/gateway.py +++ b/webapp/titanembeds/blueprints/gateway/gateway.py @@ -105,4 +105,63 @@ class Gateway(Namespace): 'discriminator': dbMember.discriminator, 'user_id': session['user_id'], } - emit("current_user_info", usr) \ No newline at end of file + emit("current_user_info", usr) + + def get_user_color(self, guild_id, user_id): + color = None + member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == user_id).first() + if not member: + return None + guild_roles = json.loads(db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().roles) + guildroles_filtered = {} + for role in guild_roles: + guildroles_filtered[role["id"]] = role + member_roleids = json.loads(member.roles) + member_roles = [] + for roleid in member_roleids: + role = guildroles_filtered.get(roleid) + if not role: + continue + member_roles.append(role) + member_roles = sorted(member_roles, key=lambda k: k['position']) + for role in member_roles: + if role["color"] != 0: + color = '{0:02x}'.format(role["color"]) + while len(color) < 6: + color = "0" + color + return color + + def on_lookup_user_info(self, data): + guild_id = data["guild_id"] + name = data["name"] + discriminator = data["discriminator"] + usr = { + "name": name, + "id": None, + "username": None, + "nickname": None, + "discriminator": discriminator, + "avatar": None, + "color": None, + "avatar_url": None, + } + member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.username == name, GuildMembers.discriminator == discriminator).first() + if member: + usr["id"] = member.user_id + usr["username"] = member.username + usr["nickname"] = member.nickname + usr["avatar"] = member.avatar + usr["color"] = self.get_user_color(guild_id, usr["id"]) + if (usr["avatar"]): + usr["avatar_url"] = "https://cdn.discordapp.com/avatars/{}/{}.jpg".format(usr["id"], usr["avatar"]) + else: + member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.nickname == name, GuildMembers.discriminator == discriminator).first() + if member: + usr["id"] = member.user_id + usr["username"] = member.username + usr["nickname"] = member.nickname + usr["avatar"] = member.avatar + usr["color"] = self.get_user_color(guild_id, usr["id"]) + if (usr["avatar"]): + usr["avatar_url"] = "https://cdn.discordapp.com/avatars/{}/{}.jpg".format(usr["id"], usr["avatar"]) + emit("lookup_user_info", usr) \ No newline at end of file diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index bae8065..919dd38 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -10,6 +10,7 @@ /* global location */ /* global io */ /* global twemoji */ +/* global jQuery */ (function () { const theme_options = ["DiscordDark", "BetterTitan"]; // All the avaliable theming names @@ -29,6 +30,7 @@ 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 + var message_users_cache = {}; // {"name#discrim": {"data": {}, "msgs": []} Cache of the users fetched from websockets to paint the messages var shift_pressed = false; // Track down if shift pressed on messagebox function element_in_view(element, fullyInView) { @@ -797,6 +799,12 @@ replace.html($(rendered).html()); replace.find(".blockcode").find("br").remove(); } + var usrcachekey = username + "#" + message.author.discriminator; + if (!(usrcachekey in message_users_cache)) { + message_users_cache[usrcachekey] = {"data": {}, "msgs": []}; + + } + message_users_cache[usrcachekey]["msgs"].push(message.id); last = message.id; } if (replace == null) { @@ -806,6 +814,7 @@ target: "_blank" }); $('.tooltipped').tooltip(); + process_message_users_cache(); return last; } @@ -861,6 +870,42 @@ $("#fetching-indicator").fadeOut(800); }); } + + function process_message_users_cache() { + var keys = Object.keys(message_users_cache); + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + var hashpos = key.lastIndexOf("#"); + var name = key.substring(0, hashpos); + var discriminator = key.substring(hashpos+1); + if (name.startsWith("(Titan Dev) ")) { + name = name.substring(12); + } + var key_helper = name + "#" + discriminator; + if (jQuery.isEmptyObject(message_users_cache[key_helper]["data"])) { + if (socket) { + socket.emit("lookup_user_info", {"guild_id": guild_id, "name": name, "discriminator": discriminator}); + } + } else { + process_message_users_cache_helper(key_helper, message_users_cache[key_helper]["data"]); + } + + } + } + + function process_message_users_cache_helper(key, usr) { + var msgs = message_users_cache[key]["msgs"]; + while (msgs.length > 0) { + var element = $("#discordmessage_"+msgs.pop()); + var parent = element.parent(); + if (usr.color) { + parent.find(".chatusername").css("color", "#"+usr.color); + } else { + parent.find(".chatusername").css("color", null); + } + parent.attr("discord_userid", usr.id); + } + } function update_embed_userchip(authenticated, avatar, username, nickname, userid, discrim=null) { if (authenticated) { @@ -1032,6 +1077,7 @@ socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + "/gateway", {path: '/gateway', transports: ['websocket']}); socket.on('connect', function () { socket.emit('identify', {"guild_id": guild_id, "visitor_mode": visitor_mode}); + process_message_users_cache(); }); socket.on("disconnect", function () { @@ -1207,6 +1253,16 @@ socket.on("current_user_info", function (usr) { update_embed_userchip(true, usr.avatar, usr.username, usr.nickname, usr.userid, usr.discriminator); }); + + socket.on("lookup_user_info", function (usr) { + var key = usr.name + "#" + usr.discriminator; + var cache = message_users_cache[key]; + if (!cache) { + return; + } + cache["data"] = usr; + process_message_users_cache_helper(key, usr); + }); } function update_socket_channels() {