From ceb19cd1af24020ab2bcccc5f545ae73a8265cdf Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sun, 9 Apr 2017 16:40:11 -0700 Subject: [PATCH] Send mentions from embed --- titanembeds/blueprints/api/api.py | 9 ++++++++- titanembeds/static/js/embed.js | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/titanembeds/blueprints/api/api.py b/titanembeds/blueprints/api/api.py index eecf7d2..c3603c8 100644 --- a/titanembeds/blueprints/api/api.py +++ b/titanembeds/blueprints/api/api.py @@ -8,6 +8,7 @@ import random import requests import json import datetime +import re from config import config api = Blueprint("api", __name__) @@ -99,7 +100,13 @@ def check_user_in_guild(guild_id): def format_post_content(message): message = message.replace("<", "\<") - message = message.replace(">", "\>") #escape mentions for now + message = message.replace(">", "\>") + + pattern = re.compile(r'\[@[0-9]+\]') + for match in re.findall(pattern, message): + mention = "<@" + match[2: len(match) - 1] + ">" + message = message.replace(match, mention, 1) + if (session['unauthenticated']): message = "**[{}#{}]** {}".format(session['username'], session['user_id'], message) else: diff --git a/titanembeds/static/js/embed.js b/titanembeds/static/js/embed.js index 87d39c5..98c4d74 100644 --- a/titanembeds/static/js/embed.js +++ b/titanembeds/static/js/embed.js @@ -155,14 +155,25 @@ function fill_channels(channels) { $("#channel-"+selected_channel).parent().addClass("active"); } +function mention_member(member_id) { + if (!$('#messagebox').prop('disabled')) { + $('#messagebox').val( $('#messagebox').val() + "[@" + member_id + "] " ); + $('.button-collapse').sideNav('hide'); + $("#messagebox").focus(); + } +} + function fill_discord_members(discordmembers) { var template = $('#mustache_authedusers').html(); Mustache.parse(template); $("#discord-members").empty(); for (var i = 0; i < discordmembers.length; i++) { var member = discordmembers[i]; - var rendered = Mustache.render(template, {"id": member.id, "username": member.username, "avatar": member.avatar_url}); + var rendered = Mustache.render(template, {"id": member.id.toString() + "d", "username": member.username, "avatar": member.avatar_url}); $("#discord-members").append(rendered); + $( "#discorduser-" + member.id.toString() + "d").click(function() { + mention_member(member.id.toString()); + }); } } @@ -172,8 +183,11 @@ function fill_authenticated_users(users) { $("#embed-discord-members").empty(); for (var i = 0; i < users.length; i++) { var member = users[i]; - var rendered = Mustache.render(template, {"id": member.id, "username": member.username, "avatar": member.avatar_url}); + var rendered = Mustache.render(template, {"id": member.id.toString() + "a", "username": member.username, "avatar": member.avatar_url}); $("#embed-discord-members").append(rendered); + $( "#discorduser-" + member.id.toString() + "a").click(function() { + mention_member(member.id.toString()); + }); } }