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
This commit is contained in:
Jeremy Zhang 2017-08-10 02:05:05 +00:00
parent 98d13e9feb
commit 77bde8f258
6 changed files with 43 additions and 17 deletions

View File

@ -69,8 +69,6 @@ class DatabaseInterface(object):
def get_message_author(self, message): def get_message_author(self, message):
author = message.author author = message.author
if hasattr(author, 'nick') and author.nick:
author.name = author.nick
obj = { obj = {
"username": author.name, "username": author.name,
"discriminator": author.discriminator, "discriminator": author.discriminator,

View File

@ -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.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.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 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, 'manage_embed': False,
'ip_address': ip_address, 'ip_address': ip_address,
'username': username, 'username': username,
'nickname': None,
'user_key': user_key, 'user_key': user_key,
'guild_id': guild_id, 'guild_id': guild_id,
'user_id': session['user_id'], 'user_id': session['user_id'],
@ -81,6 +82,7 @@ def update_user_status(guild_id, username, user_key=None):
'avatar': session["avatar"], 'avatar': session["avatar"],
'manage_embed': check_user_can_administrate_guild(guild_id), 'manage_embed': check_user_can_administrate_guild(guild_id),
'username': username, 'username': username,
'nickname': None,
'discriminator': session['discriminator'], 'discriminator': session['discriminator'],
'guild_id': guild_id, 'guild_id': guild_id,
'user_id': session['user_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']: if status['banned'] or status['revoked']:
return status 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 = db.session.query(AuthenticatedUsers).filter(and_(AuthenticatedUsers.guild_id == guild_id, AuthenticatedUsers.client_id == status['user_id'])).first()
dbUser.bumpTimestamp() dbUser.bumpTimestamp()
return status return status
@ -296,6 +301,7 @@ def get_online_embed_users(guild_id):
meta = { meta = {
'id': usrdb.user_id, 'id': usrdb.user_id,
'username': usrdb.username, 'username': usrdb.username,
'nickname': usrdb.nickname,
'discriminator': usrdb.discriminator, 'discriminator': usrdb.discriminator,
'avatar_url': generate_avatar_url(usrdb.user_id, usrdb.avatar), 'avatar_url': generate_avatar_url(usrdb.user_id, usrdb.avatar),
} }
@ -342,7 +348,7 @@ def fetch():
if not chan.get("read"): if not chan.get("read"):
status_code = 401 status_code = 401
else: else:
messages = get_channel_messages(channel_id, after_snowflake) messages = get_channel_messages(guild_id, channel_id, after_snowflake)
status_code = 200 status_code = 200
response = jsonify(messages=messages, status=status) response = jsonify(messages=messages, status=status)
response.status_code = status_code response.status_code = status_code
@ -363,7 +369,7 @@ def fetch_visitor():
if not chan.get("read"): if not chan.get("read"):
status_code = 401 status_code = 401
else: else:
messages = get_channel_messages(channel_id, after_snowflake) messages = get_channel_messages(guild_id, channel_id, after_snowflake)
status_code = 200 status_code = 200
response = jsonify(messages=messages) response = jsonify(messages=messages)
response.status_code = status_code response.status_code = status_code

View File

@ -6,7 +6,7 @@ from guilds import Guilds
from unauthenticated_users import UnauthenticatedUsers from unauthenticated_users import UnauthenticatedUsers
from unauthenticated_bans import UnauthenticatedBans from unauthenticated_bans import UnauthenticatedBans
from authenticated_users import AuthenticatedUsers 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 keyvalue_properties import KeyValueProperties, set_keyvalproperty, get_keyvalproperty, getexpir_keyvalproperty, setexpir_keyvalproperty, ifexists_keyvalproperty, delete_keyvalproperty
from messages import Messages, get_channel_messages from messages import Messages, get_channel_messages
from cosmetics import Cosmetics from cosmetics import Cosmetics

View File

@ -43,3 +43,6 @@ def list_all_guild_members(guild_id):
"nickname": member.nickname, "nickname": member.nickname,
}) })
return memlist 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()

View File

@ -1,4 +1,4 @@
from titanembeds.database import db from titanembeds.database import db, get_guild_member
from sqlalchemy import cast from sqlalchemy import cast
import json import json
@ -29,7 +29,7 @@ class Messages(db.Model):
def __repr__(self): def __repr__(self):
return '<Messages {0} {1} {2} {3} {4}>'.format(self.id, self.guild_id, self.guild_id, self.channel_id, self.message_id) return '<Messages {0} {1} {2} {3} {4}>'.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: if not after_snowflake:
q = db.session.query(Messages).filter(Messages.channel_id == channel_id).order_by(Messages.timestamp.desc()).limit(50) q = db.session.query(Messages).filter(Messages.channel_id == channel_id).order_by(Messages.timestamp.desc()).limit(50)
else: else:
@ -40,7 +40,7 @@ def get_channel_messages(channel_id, after_snowflake=None):
if x.message_id in snowflakes: if x.message_id in snowflakes:
continue continue
snowflakes.append(x.message_id) snowflakes.append(x.message_id)
msgs.append({ message = {
"attachments": json.loads(x.attachments), "attachments": json.loads(x.attachments),
"timestamp": x.timestamp, "timestamp": x.timestamp,
"id": x.message_id, "id": x.message_id,
@ -49,5 +49,10 @@ def get_channel_messages(channel_id, after_snowflake=None):
"content": x.content, "content": x.content,
"channel_id": x.channel_id, "channel_id": x.channel_id,
"mentions": json.loads(x.mentions) "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 return msgs

View File

@ -447,7 +447,11 @@
$("#embed-discord-members-count").html(users.length); $("#embed-discord-members-count").html(users.length);
for (var i = 0; i < users.length; i++) { for (var i = 0; i < users.length; i++) {
var member = users[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); $("#embed-discord-members").append(rendered);
$( "#discorduser-" + member.id.toString() + "a").click({"member_id": member.id.toString()}, function(event) { $( "#discorduser-" + member.id.toString() + "a").click({"member_id": member.id.toString()}, function(event) {
mention_member(event.data.member_id); mention_member(event.data.member_id);
@ -632,7 +636,11 @@
message.content = escapeHtml(message.content); message.content = escapeHtml(message.content);
message.content = parse_message_markdown(message.content); message.content = parse_message_markdown(message.content);
message = parse_emoji_in_message(message); 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); $("#chatcontent").append(rendered);
last = message.id; last = message.id;
handle_last_message_mention(); handle_last_message_mention();
@ -667,9 +675,9 @@
fet.done(function(data) { fet.done(function(data) {
var status = data.status; var status = data.status;
if (visitor_mode) { if (visitor_mode) {
update_embed_userchip(false, null, "Titan", "0001", null); update_embed_userchip(false, null, "Titan", null, "0001", null);
} else { } 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); last_message_id = fill_discord_messages(data.messages, jumpscroll);
if (!visitor_mode && status.manage_embed) { 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) { if (authenticated) {
$("#currentuserimage").show(); $("#currentuserimage").show();
$("#currentuserimage").attr("src", avatar); $("#currentuserimage").attr("src", avatar);
$("#curuser_name").text(username); $("#curuser_name").text(username);
$("#curuser_discrim").text("#" + discrim); $("#curuser_discrim").text("#" + discrim);
current_username_discrim = username + "#" + discrim; current_username_discrim = "#" + discrim;
} else { } else {
$("#currentuserimage").hide(); $("#currentuserimage").hide();
$("#curuser_name").text(username); $("#curuser_name").text(username);
$("#curuser_discrim").text("#" + userid); $("#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;
} }
} }