Fix user card for offline members to work

This commit is contained in:
Jeremy Zhang 2017-12-09 18:47:22 +00:00
parent cb05b44100
commit 1785cb81c9
3 changed files with 74 additions and 39 deletions

View File

@ -1,6 +1,6 @@
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, get_badges 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, get_badges
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, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled 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, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled, get_member_roles
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
from flask import Blueprint, abort, jsonify, session, request, url_for from flask import Blueprint, abort, jsonify, session, request, url_for
from flask import current_app as app from flask import current_app as app
@ -429,9 +429,35 @@ def create_authenticated_user():
response.status_code = 403 response.status_code = 403
return response return response
@api.route("/badges/<user_id>") @api.route("/user/<guild_id>/<user_id>")
def badges(user_id): def user_info(guild_id, user_id):
return jsonify(get_badges(user_id)) usr = {
"id": None,
"username": None,
"nickname": None,
"discriminator": None,
"avatar": None,
"avatar_url": None,
"roles": [],
"badges": [],
}
member = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.user_id == user_id).first()
if member:
usr["id"] = member.user_id
usr["username"] = member.username
usr["nickname"] = member.nickname
usr["discriminator"] = member.discriminator
usr["avatar"] = member.avatar
usr["avatar_url"] = generate_avatar_url(usr["id"], usr["avatar"], usr["discriminator"])
roles = get_member_roles(guild_id, user_id)
dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first()
guild_roles = json.loads(dbguild.roles)
for r in roles:
for gr in guild_roles:
if gr["id"] == r:
usr["roles"].append(gr)
usr["badges"] = get_badges(user_id)
return jsonify(usr)
def canCleanupDB(): def canCleanupDB():
canclean = False canclean = False

View File

@ -135,10 +135,10 @@
return funct.promise(); return funct.promise();
} }
function api_badges(user_id) { function api_user(user_id) {
var funct = $.ajax({ var funct = $.ajax({
dataType: "json", dataType: "json",
url: "/api/badges/" + user_id, url: "/api/user/" + guild_id + "/" + user_id,
}); });
return funct.promise(); return funct.promise();
} }
@ -692,54 +692,59 @@
} }
function openUserCard(user_id) { function openUserCard(user_id) {
var bgs = api_badges(user_id); var usr = api_user(user_id);
bgs.done(function (data) { usr.done(function (data) {
for (var i = 0; i < badges_options.length; i++) { for (var i = 0; i < badges_options.length; i++) {
var badge = badges_options[i]; var badge = badges_options[i];
if (data.indexOf(badge) != -1) { if (data.badges.indexOf(badge) != -1) {
$(`#usercard .badges .${badge}`).show(); $(`#usercard .badges .${badge}`).show();
} else { } else {
$(`#usercard .badges .${badge}`).hide(); $(`#usercard .badges .${badge}`).hide();
} }
} }
$("#usercard .avatar").attr("src", data.avatar_url);
$("#usercard .identity .username").text(data.username);
$("#usercard .identity .discriminator").text(data.discriminator);
$("#usercard .identity .discriminator").text(data.discriminator);
var template = $('#mustache_rolebubble').html();
Mustache.parse(template);
data.roles.sort(function(a, b) {
return parseFloat(b.position) - parseFloat(a.position);
}); });
$("#usercard .role .roles").empty();
for (var j = 0; j < data.roles.length; j++) {
var role = data.roles[j];
var color = null;
if (role.color) {
color = "#" + role.color.toString(16);
}
var rol = Mustache.render(template, {name: role.name, color: color});
$("#usercard .role .roles").append(rol);
}
$("#usercard-mention-btn").off("click");
$("#usercard-mention-btn").click(function () {
mention_member(data.id);
$("#usercard").modal('close');
});
});
$("#usercard .offline-text").show();
$("#usercard .game").hide();
$("#usercard .bottag").hide();
for (var i = 0; i < discord_users_list.length; i++) { for (var i = 0; i < discord_users_list.length; i++) {
var usr = discord_users_list[i]; var usr = discord_users_list[i];
if (usr.id == user_id) { if (usr.id == user_id) {
$("#usercard .avatar").attr("src", usr.avatar_url); $("#usercard .offline-text").hide();
$("#usercard .identity .username").text(usr.username);
$("#usercard .identity .discriminator").text(usr.discriminator);
if (usr.bot) { if (usr.bot) {
$("#usercard .bottag").show(); $("#usercard .bottag").show();
} else {
$("#usercard .bottag").hide();
}
if (usr.status == "offline") {
$("#usercard .offline-text").show();
} else {
$("#usercard .offline-text").hide();
}
if (usr["hoist-role"]) {
$("#usercard .role").show();
$("#usercard .role .text").text(usr["hoist-role"].name);
if (usr.color) {
$("#usercard .role .bubble").css("color", "#" + usr.color);
$("#usercard .role .color").css("background-color", "#" + usr.color);
}
} else {
$("#usercard .role").hide();
} }
if (usr.game) { if (usr.game) {
$("#usercard .game").show(); $("#usercard .game").show();
$("#usercard .game .text").text(usr.game.name); $("#usercard .game .text").text(usr.game.name);
} else {
$("#usercard .game").hide();
} }
$("#usercard-mention-btn").off("click"); break;
$("#usercard-mention-btn").click(function () {
mention_member(user_id);
$("#usercard").modal('close');
});
} }
} }
$("#usercard").modal('open'); $("#usercard").modal('open');

View File

@ -191,9 +191,9 @@
<a class="supporter tooltipped" data-tooltip="TitanEmbeds Supporter" data-position="top" href="https://titanembeds.com/user/donate" target="_blank"><i class="material-icons">attach_money</i></a> <a class="supporter tooltipped" data-tooltip="TitanEmbeds Supporter" data-position="top" href="https://titanembeds.com/user/donate" target="_blank"><i class="material-icons">attach_money</i></a>
<a class="star tooltipped" data-tooltip="GitHub Stargazer" data-position="top" href="https://github.com/TitanEmbeds/Titan" target="_blank"><i class="material-icons">star</i></a> <a class="star tooltipped" data-tooltip="GitHub Stargazer" data-position="top" href="https://github.com/TitanEmbeds/Titan" target="_blank"><i class="material-icons">star</i></a>
</div> </div>
<p class="offline-text">User is Offline.</p> <p class="offline-text">User is OFFLINE in Discord.</p>
<p class="game">Playing <span class="text"></span></p> <p class="game">Playing <span class="text"></span></p>
<p class="role">Highest Role: <span class="bubble"><span class="color"></span> <span class="text"></span></span></p> <p class="role">Roles: <span class="roles"></span></p>
</div> </div>
</div> </div>
</div> </div>
@ -345,6 +345,10 @@
</div> </div>
</div> </div>
</script> </script>
<script id="mustache_rolebubble" type="text/template">
<span class="bubble" {{#color}}style="color: {{color}};"{{/color}}><span class="color" {{#color}}style="background-color: {{color}};"{{/color}}></span> <span class="text">{{name}}</span></span>
</script>
{% endraw %} {% endraw %}
<script> <script>