Implement user avatars into the embed

This commit is contained in:
Jeremy Zhang 2017-09-24 04:17:06 +00:00
parent ad833434dd
commit e30171a8de
7 changed files with 55 additions and 13 deletions

View File

@ -1,4 +1,5 @@
from flask import Blueprint, url_for, redirect, session, render_template, abort, request, jsonify from flask import Blueprint, url_for, redirect, session, render_template, abort, request, jsonify
from flask_socketio import emit
from functools import wraps from functools import wraps
from titanembeds.database import db, get_administrators_list, Cosmetics, Guilds, UnauthenticatedUsers, UnauthenticatedBans, TitanTokens, TokenTransactions, get_titan_token, set_titan_token from titanembeds.database import db, get_administrators_list, Cosmetics, Guilds, UnauthenticatedUsers, UnauthenticatedBans, TitanTokens, TokenTransactions, get_titan_token, set_titan_token
from titanembeds.oauth import generate_guild_icon_url from titanembeds.oauth import generate_guild_icon_url
@ -179,6 +180,7 @@ def update_administrate_guild(guild_id):
guest_icon = None guest_icon = None
db_guild.guest_icon = guest_icon db_guild.guest_icon = guest_icon
db.session.commit() db.session.commit()
emit("guest_icon_change", {"guest_icon": guest_icon}, room="GUILD_"+guild_id, namespace="/gateway")
return jsonify( return jsonify(
id=db_guild.id, id=db_guild.id,
guild_id=db_guild.guild_id, guild_id=db_guild.guild_id,

View File

@ -358,6 +358,10 @@ def change_unauthenticated_username():
response.status_code = 403 response.status_code = 403
return response return response
def get_guild_guest_icon(guild_id):
return db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first().guest_icon
def process_query_guild(guild_id, visitor=False): def process_query_guild(guild_id, visitor=False):
widget = discord_api.get_widget(guild_id) widget = discord_api.get_widget(guild_id)
channels = get_guild_channels(guild_id, visitor) channels = get_guild_channels(guild_id, visitor)
@ -367,10 +371,11 @@ def process_query_guild(guild_id, visitor=False):
discordmembers = [{"id": 0, "color": "FFD6D6", "status": "dnd", "username": "Discord Server Widget is Currently Disabled"}] discordmembers = [{"id": 0, "color": "FFD6D6", "status": "dnd", "username": "Discord Server Widget is Currently Disabled"}]
embedmembers = get_online_embed_users(guild_id) embedmembers = get_online_embed_users(guild_id)
emojis = get_guild_emojis(guild_id) emojis = get_guild_emojis(guild_id)
guest_icon = get_guild_guest_icon(guild_id)
if visitor: if visitor:
for channel in channels: for channel in channels:
channel["write"] = False channel["write"] = False
return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, emojis=emojis, instant_invite=widget.get("instant_invite", None)) return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, emojis=emojis, guest_icon=guest_icon, instant_invite=widget.get("instant_invite", None))
@api.route("/query_guild", methods=["GET"]) @api.route("/query_guild", methods=["GET"])
@valid_session_required(api=True) @valid_session_required(api=True)

View File

@ -1,5 +1,6 @@
from flask import Blueprint, request, redirect, jsonify, abort, session, url_for, render_template from flask import Blueprint, request, redirect, jsonify, abort, session, url_for, render_template
from flask import current_app as app from flask import current_app as app
from flask_socketio import emit
from config import config from config import config
from titanembeds.decorators import discord_users_only from titanembeds.decorators import discord_users_only
from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, Cosmetics, UserCSS, set_titan_token, get_titan_token from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, Cosmetics, UserCSS, set_titan_token, get_titan_token
@ -242,6 +243,7 @@ def update_administrate_guild(guild_id):
db_guild.guest_icon = guest_icon db_guild.guest_icon = guest_icon
db.session.commit() db.session.commit()
emit("guest_icon_change", {"guest_icon": guest_icon}, room="GUILD_"+guild_id, namespace="/gateway")
return jsonify( return jsonify(
id=db_guild.id, id=db_guild.id,
guild_id=db_guild.guild_id, guild_id=db_guild.guild_id,

View File

@ -172,11 +172,14 @@ nav .brand-logo {
#chatcontent > p { #chatcontent > p {
display: table; display: table;
width: 90%; width: 90%;
border-top: solid 1px rgba(0, 0, 0, 0.1);
padding-top: 10px;
margin-bottom: 11px;
} }
#chatcontent > p > span { /*#chatcontent > p > span {*/
display: table-row /* display: table-row*/
} /*}*/
::-webkit-input-placeholder { ::-webkit-input-placeholder {
color: var(--placeholder); color: var(--placeholder);
@ -224,14 +227,25 @@ nav .brand-logo {
.chatusername { .chatusername {
font-weight: bold; font-weight: bold;
color: #eceff1; color: #eceff1;
margin-right: 10px;
vertical-align: middle;
font-size: 110%;
} }
.chattimestamp { .chattimestamp {
font-size: 10px; font-size: 11px;
color: #90a4ae; color: #90a4ae;
margin-right: 3px; margin-right: 3px;
} }
.authoravatar {
width: 100%;
max-width: 30px;
border-radius: 50%;
vertical-align: middle;
margin-right: 10px;
}
.footercontainer { .footercontainer {
width: 100%; width: 100%;
position: relative; position: relative;
@ -413,12 +427,13 @@ a {
height: 30px; height: 30px;
} }
.chatusername { /*.chatusername {*/
display: table-header-group; /* display: table-header-group;*/
} /*}*/
.chatmessage { .chatmessage {
display: inline; display: block;
color: var(--chatmessage); color: var(--chatmessage);
margin-top: 5px;
} }
p.mentioned { p.mentioned {
@ -443,6 +458,7 @@ p.mentioned span.chatmessage {
white-space: pre-wrap; white-space: pre-wrap;
line-height: 15px; line-height: 15px;
padding: 5px; padding: 5px;
margin-bottom: 3px;
} }
#emoji-picker { #emoji-picker {

View File

@ -35,6 +35,7 @@
var guild_channels_list = []; // guild channels, but as a list of them 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 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 var shift_pressed = false; // Track down if shift pressed on messagebox
var global_guest_icon = null; // Guest icon
function element_in_view(element, fullyInView) { function element_in_view(element, fullyInView) {
var pageTop = $(window).scrollTop(); var pageTop = $(window).scrollTop();
@ -810,6 +811,14 @@
} }
} }
function generate_avatar_url(user_id, avatar_hash, message_contents=null) {
if (user_id == bot_client_id && (message_contents.includes("**") && ( (message_contents.includes("<")&&message_contents.includes(">")) || (message_contents.includes("[") && message_contents.includes("]")) ))) {
return global_guest_icon;
} else {
return "https://cdn.discordapp.com/avatars/" + user_id + "/" + avatar_hash + ".png";
}
}
function fill_discord_messages(messages, jumpscroll, replace=null) { function fill_discord_messages(messages, jumpscroll, replace=null) {
if (messages.length == 0) { if (messages.length == 0) {
return last_message_id; return last_message_id;
@ -833,7 +842,8 @@
if (message.author.nickname) { if (message.author.nickname) {
username = 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)}); var avatar = generate_avatar_url(message.author.id, message.author.avatar, message.content);
var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.formatted_timestamp, "time": message.formatted_time, "username": username, "discriminator": message.author.discriminator, "avatar": avatar, "content": nl2br(message.content)});
if (replace == null) { if (replace == null) {
$("#chatcontent").append(rendered); $("#chatcontent").append(rendered);
handle_last_message_mention(); handle_last_message_mention();
@ -951,7 +961,10 @@
} else { } else {
parent.find(".chatusername").css("color", null); parent.find(".chatusername").css("color", null);
} }
parent.attr("discord_userid", usr.id); if (usr.avatar_url) {
parent.attr("discord_userid", usr.id);
parent.find(".authoravatar").prop("src", usr.avatar_url);
}
} }
} }
@ -1319,6 +1332,10 @@
cache["data"] = usr; cache["data"] = usr;
process_message_users_cache_helper(key, usr); process_message_users_cache_helper(key, usr);
}); });
socket.on("guest_icon_change", function (icon) {
global_guest_icon = icon.guest_icon;
});
} }
function update_socket_channels() { function update_socket_channels() {

View File

@ -76,7 +76,7 @@ margin-right:4px
#chatcontent > p { display: table; } #chatcontent > p { display: table; }
#chatcontent > p > span { display: table-row } #chatcontent > p > span { display: table-row }
/*#chatcontent > p > span:nth-child(1):before { content:"Today at " }*/ /*#chatcontent > p > span:nth-child(1):before { content:"Today at " }*/
#chatcontent > p > span.chatusername { display: table-header-group } /*#chatcontent > p > span.chatusername { display: table-header-group }*/
#chatcontent > p > span.chatmessage { display: table-footer-group;display:inline-block!important;color:var(--chatmessage) } #chatcontent > p > span.chatmessage { display: table-footer-group;display:inline-block!important;color:var(--chatmessage) }
#chatcontent { #chatcontent {
background:var(--main) background:var(--main)

View File

@ -218,7 +218,7 @@
</script> </script>
<script id="mustache_usermessage" type="text/template"> <script id="mustache_usermessage" type="text/template">
<p><span class="chatusername"><span class="authorname">{{username}}</span><span class="authorhash">#</span><span class="authordiscriminator">{{discriminator}}</span></span> <span id="discordmessage_{{id}}" title="{{full_timestamp}}" class="chattimestamp">{{time}}</span> <span class="chatmessage">{{{content}}}</span></p> <p><img class="authoravatar" src="{{avatar}}"> <span class="chatusername"><span class="authorname">{{username}}</span><span class="authorhash">#</span><span class="authordiscriminator">{{discriminator}}</span></span> <span id="discordmessage_{{id}}" title="{{full_timestamp}}" class="chattimestamp">{{time}}</span> <span class="chatmessage">{{{content}}}</span></p>
</script> </script>
<script id="mustache_memberrole" type="text/template"> <script id="mustache_memberrole" type="text/template">