From 105fe24a40eaa5240426b670f1b3c9ebd26c1ab6 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Mon, 19 Mar 2018 19:04:51 +0000 Subject: [PATCH] Support mentions for offline members --- webapp/titanembeds/blueprints/api/api.py | 17 +++++++- webapp/titanembeds/static/js/embed.js | 49 +++++++++++++++++++++--- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index 8cb5be4..b0469f2 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -176,6 +176,20 @@ def get_channel_webhook_url(guild_id, channel_id): webhook = discord_api.create_webhook(channel_id, name) return webhook["content"] +def get_all_users(guild_id): + users = db.session.query(GuildMembers).filter(GuildMembers.guild_id == guild_id, GuildMembers.active == True).all() + mem = [] + for u in users: + mem.append({ + "id": str(u.user_id), + "avatar": u.avatar, + "avatar_url": generate_avatar_url(u.user_id, u.avatar, u.discriminator, True), + "username": u.username, + "nickname": u.nickname, + "discriminator": u.discriminator + }) + return mem + @api.route("/fetch", methods=["GET"]) @valid_session_required(api=True) @abort_if_guild_disabled() @@ -393,10 +407,11 @@ def process_query_guild(guild_id, visitor=False): emojis = get_guild_emojis(guild_id) roles = get_guild_roles(guild_id) guest_icon = get_guild_guest_icon(guild_id) + all_users = get_all_users(guild_id) if visitor: for channel in channels: channel["write"] = False - return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, emojis=emojis, roles=roles, guest_icon=guest_icon, instant_invite=widget.get("instant_invite", None)) + return jsonify(channels=channels, discordmembers=discordmembers, embedmembers=embedmembers, allusers=all_users, emojis=emojis, roles=roles, guest_icon=guest_icon, instant_invite=widget.get("instant_invite", None)) @api.route("/query_guild", methods=["GET"]) @valid_session_required(api=True) diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index 74d4c61..e779d82 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -47,6 +47,7 @@ var notification_sound_setting; // nothing, mentions, newmsgs - to control what new sound it makes var display_richembeds; // true/false - if rich embeds should be displayed var guild_roles_list = []; // List of all guild roles + var all_users = []; // List of all the users in guild function element_in_view(element, fullyInView) { var pageTop = $(window).scrollTop(); @@ -496,6 +497,7 @@ } function prepare_guild(guildobj) { + all_users = guildobj.allusers; global_guest_icon = guildobj.guest_icon; emoji_store = guildobj.emojis; update_emoji_picker(); @@ -1471,12 +1473,12 @@ var template = $('#mustache_usermentionchoices').html(); Mustache.parse(template); var users = []; - for (var i = 0; i < discord_users_list.length; i++) { - var usr = discord_users_list[i]; - if (usr.username.toLowerCase().indexOf(lastWord.toLowerCase()) > -1 || (usr.nick && usr.nick.toLowerCase().indexOf(lastWord.toLowerCase()) > -1)) { + for (var i = 0; i < all_users.length; i++) { + var usr = all_users[i]; + if (usr.username.toLowerCase().indexOf(lastWord.toLowerCase()) > -1 || (usr.nickname && usr.nickname.toLowerCase().indexOf(lastWord.toLowerCase()) > -1)) { var displayname = usr.username; if (usr.nick) { - displayname = usr.nick; + displayname = usr.nickname; } users.push({ id: usr.id, @@ -1715,6 +1717,14 @@ discord_users_list.push(usr); fill_discord_members(discord_users_list); } + all_users.push({ + "id": usr.id, + "avatar": usr.avatar, + "avatar_url": generate_avatar_url(usr.user_id, usr.avatar), + "username": usr.username, + "nickname": usr.nickname, + "discriminator": usr.discriminator + }); }); socket.on("GUILD_MEMBER_UPDATE", function (usr) { @@ -1722,6 +1732,19 @@ update_socket_channels(); socket.emit("current_user_info", {"guild_id": guild_id}); } + var updatedUser = false; + for (var i = 0; i < all_users.length; i++) { + if (usr.id == all_users[i].id) { + var u = all_users[i]; + u.avatar = usr.avatar; + u.avatar_url = generate_avatar_url(usr.user_id, usr.avatar); + u.username = usr.username; + u.nickname = usr.nickname; + u.discriminator = usr.discriminator; + updatedUser = true; + break; + } + } for (var i = 0; i < discord_users_list.length; i++) { if (usr.id == discord_users_list[i].id) { discord_users_list.splice(i, 1); @@ -1734,14 +1757,30 @@ } discord_users_list.push(usr); fill_discord_members(discord_users_list); + if (!updatedUser) { + all_users.push({ + "id": usr.id, + "avatar": usr.avatar, + "avatar_url": generate_avatar_url(usr.user_id, usr.avatar), + "username": usr.username, + "nickname": usr.nickname, + "discriminator": usr.discriminator + }); + } }); socket.on("GUILD_MEMBER_REMOVE", function (usr) { + for (var i = 0; i < all_users.length; i++) { + if (usr.id == all_users[i].id) { + all_users.splice(i, 1); + break; + } + } for (var i = 0; i < discord_users_list.length; i++) { if (usr.id == discord_users_list[i].id) { discord_users_list.splice(i, 1); fill_discord_members(discord_users_list); - return; + break; } } });