From 77bde8f258a15116e4955a5d9bdd3d1f98dc3b94 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Thu, 10 Aug 2017 02:05:05 +0000 Subject: [PATCH] Better nickname support - Username field will not be overwritten with the nickname. - Mentions work again! - User chip will show nickname instead of username if present - Authenticated embed users sidebar nickname support --- discordbot/titanembeds/database/__init__.py | 2 -- webapp/titanembeds/blueprints/api/api.py | 12 ++++++--- webapp/titanembeds/database/__init__.py | 2 +- webapp/titanembeds/database/guild_members.py | 3 +++ webapp/titanembeds/database/messages.py | 13 ++++++--- webapp/titanembeds/static/js/embed.js | 28 +++++++++++++++----- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/discordbot/titanembeds/database/__init__.py b/discordbot/titanembeds/database/__init__.py index 8b3b2c0..1911793 100644 --- a/discordbot/titanembeds/database/__init__.py +++ b/discordbot/titanembeds/database/__init__.py @@ -69,8 +69,6 @@ class DatabaseInterface(object): def get_message_author(self, message): author = message.author - if hasattr(author, 'nick') and author.nick: - author.name = author.nick obj = { "username": author.name, "discriminator": author.discriminator, diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index c796a73..1fa4133 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -1,4 +1,4 @@ -from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, KeyValueProperties, GuildMembers, Messages, get_channel_messages, list_all_guild_members, get_administrators_list +from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, KeyValueProperties, GuildMembers, Messages, get_channel_messages, list_all_guild_members, get_guild_member, get_administrators_list from titanembeds.decorators import valid_session_required, discord_users_only from titanembeds.utils import check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild @@ -60,6 +60,7 @@ def update_user_status(guild_id, username, user_key=None): 'manage_embed': False, 'ip_address': ip_address, 'username': username, + 'nickname': None, 'user_key': user_key, 'guild_id': guild_id, 'user_id': session['user_id'], @@ -81,6 +82,7 @@ def update_user_status(guild_id, username, user_key=None): 'avatar': session["avatar"], 'manage_embed': check_user_can_administrate_guild(guild_id), 'username': username, + 'nickname': None, 'discriminator': session['discriminator'], 'guild_id': guild_id, 'user_id': session['user_id'], @@ -89,6 +91,9 @@ def update_user_status(guild_id, username, user_key=None): } if status['banned'] or status['revoked']: return status + dbMember = get_guild_member(guild_id, status["user_id"]) + if dbMember: + status["nickname"] = dbMember.nickname dbUser = db.session.query(AuthenticatedUsers).filter(and_(AuthenticatedUsers.guild_id == guild_id, AuthenticatedUsers.client_id == status['user_id'])).first() dbUser.bumpTimestamp() return status @@ -296,6 +301,7 @@ def get_online_embed_users(guild_id): meta = { 'id': usrdb.user_id, 'username': usrdb.username, + 'nickname': usrdb.nickname, 'discriminator': usrdb.discriminator, 'avatar_url': generate_avatar_url(usrdb.user_id, usrdb.avatar), } @@ -342,7 +348,7 @@ def fetch(): if not chan.get("read"): status_code = 401 else: - messages = get_channel_messages(channel_id, after_snowflake) + messages = get_channel_messages(guild_id, channel_id, after_snowflake) status_code = 200 response = jsonify(messages=messages, status=status) response.status_code = status_code @@ -363,7 +369,7 @@ def fetch_visitor(): if not chan.get("read"): status_code = 401 else: - messages = get_channel_messages(channel_id, after_snowflake) + messages = get_channel_messages(guild_id, channel_id, after_snowflake) status_code = 200 response = jsonify(messages=messages) response.status_code = status_code diff --git a/webapp/titanembeds/database/__init__.py b/webapp/titanembeds/database/__init__.py index 6e6bbf0..7a2d9b3 100644 --- a/webapp/titanembeds/database/__init__.py +++ b/webapp/titanembeds/database/__init__.py @@ -6,7 +6,7 @@ from guilds import Guilds from unauthenticated_users import UnauthenticatedUsers from unauthenticated_bans import UnauthenticatedBans from authenticated_users import AuthenticatedUsers -from guild_members import GuildMembers, list_all_guild_members +from guild_members import GuildMembers, list_all_guild_members, get_guild_member from keyvalue_properties import KeyValueProperties, set_keyvalproperty, get_keyvalproperty, getexpir_keyvalproperty, setexpir_keyvalproperty, ifexists_keyvalproperty, delete_keyvalproperty from messages import Messages, get_channel_messages from cosmetics import Cosmetics diff --git a/webapp/titanembeds/database/guild_members.py b/webapp/titanembeds/database/guild_members.py index 1ea147f..1f735a1 100644 --- a/webapp/titanembeds/database/guild_members.py +++ b/webapp/titanembeds/database/guild_members.py @@ -43,3 +43,6 @@ def list_all_guild_members(guild_id): "nickname": member.nickname, }) return memlist + +def get_guild_member(guild_id, member_id): + return db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == member_id).first() diff --git a/webapp/titanembeds/database/messages.py b/webapp/titanembeds/database/messages.py index 0142a75..16f1f04 100644 --- a/webapp/titanembeds/database/messages.py +++ b/webapp/titanembeds/database/messages.py @@ -1,4 +1,4 @@ -from titanembeds.database import db +from titanembeds.database import db, get_guild_member from sqlalchemy import cast import json @@ -29,7 +29,7 @@ class Messages(db.Model): def __repr__(self): return ''.format(self.id, self.guild_id, self.guild_id, self.channel_id, self.message_id) -def get_channel_messages(channel_id, after_snowflake=None): +def get_channel_messages(guild_id, channel_id, after_snowflake=None): if not after_snowflake: q = db.session.query(Messages).filter(Messages.channel_id == channel_id).order_by(Messages.timestamp.desc()).limit(50) else: @@ -40,7 +40,7 @@ def get_channel_messages(channel_id, after_snowflake=None): if x.message_id in snowflakes: continue snowflakes.append(x.message_id) - msgs.append({ + message = { "attachments": json.loads(x.attachments), "timestamp": x.timestamp, "id": x.message_id, @@ -49,5 +49,10 @@ def get_channel_messages(channel_id, after_snowflake=None): "content": x.content, "channel_id": x.channel_id, "mentions": json.loads(x.mentions) - }) + } + member = get_guild_member(guild_id, message["author"]["id"]) + message["author"]["nickname"] = None + if member: + message["author"]["nickname"] = member.nickname + msgs.append(message) return msgs diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index 37c5331..64e5de5 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -447,7 +447,11 @@ $("#embed-discord-members-count").html(users.length); for (var i = 0; i < users.length; i++) { var member = users[i]; - var rendered = Mustache.render(template, {"id": member.id.toString() + "a", "username": member.username, "avatar": member.avatar_url}); + var username = member.username; + if (member.nickname) { + username = member.nickname; + } + var rendered = Mustache.render(template, {"id": member.id.toString() + "a", "username": username, "avatar": member.avatar_url}); $("#embed-discord-members").append(rendered); $( "#discorduser-" + member.id.toString() + "a").click({"member_id": member.id.toString()}, function(event) { mention_member(event.data.member_id); @@ -632,7 +636,11 @@ message.content = escapeHtml(message.content); message.content = parse_message_markdown(message.content); message = parse_emoji_in_message(message); - var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.formatted_timestamp, "time": message.formatted_time, "username": message.author.username, "discriminator": message.author.discriminator, "content": nl2br(message.content)}); + var username = message.author.username; + if (message.author.nickname) { + username = message.author.nickname; + } + var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.formatted_timestamp, "time": message.formatted_time, "username": username, "discriminator": message.author.discriminator, "content": nl2br(message.content)}); $("#chatcontent").append(rendered); last = message.id; handle_last_message_mention(); @@ -667,9 +675,9 @@ fet.done(function(data) { var status = data.status; if (visitor_mode) { - update_embed_userchip(false, null, "Titan", "0001", null); + update_embed_userchip(false, null, "Titan", null, "0001", null); } else { - update_embed_userchip(status.authenticated, status.avatar, status.username, status.user_id, status.discriminator); + update_embed_userchip(status.authenticated, status.avatar, status.username, status.nickname, status.user_id, status.discriminator); } last_message_id = fill_discord_messages(data.messages, jumpscroll); if (!visitor_mode && status.manage_embed) { @@ -723,18 +731,24 @@ }); } - function update_embed_userchip(authenticated, avatar, username, userid, discrim=null) { + function update_embed_userchip(authenticated, avatar, username, nickname, userid, discrim=null) { if (authenticated) { $("#currentuserimage").show(); $("#currentuserimage").attr("src", avatar); $("#curuser_name").text(username); $("#curuser_discrim").text("#" + discrim); - current_username_discrim = username + "#" + discrim; + current_username_discrim = "#" + discrim; } else { $("#currentuserimage").hide(); $("#curuser_name").text(username); $("#curuser_discrim").text("#" + userid); - current_username_discrim = username + "#" + userid; + current_username_discrim = "#" + userid; + } + if (nickname) { + $("#curuser_name").text(nickname); + current_username_discrim = nickname + current_username_discrim; + } else { + current_username_discrim = username + current_username_discrim; } }