diff --git a/titanembeds/blueprints/api/api.py b/titanembeds/blueprints/api/api.py index d18a06f..d17b8fd 100644 --- a/titanembeds/blueprints/api/api.py +++ b/titanembeds/blueprints/api/api.py @@ -95,6 +95,13 @@ def check_user_in_guild(guild_id): else: return 200 == discord_api.get_guild_member_nocache(guild_id, session['user_id'])['code'] +def format_post_content(message): + if (session['unauthenticated']): + message = "**[{}#{}]** {}".format(session['username'], session['user_id'], message) + else: + message = "**<{}#{}>** {}".format(session['username'], session['discriminator'], message) # I would like to do a @ mention, but i am worried about notif spam + return message + @cache.cached(timeout=300, key_prefix=make_guildchannels_cache_key) def get_guild_channels(guild_id): if user_unauthenticated(): @@ -177,9 +184,10 @@ def fetch(): @rate_limiter.limit("1200/hour") @rate_limiter.limit("6/minute", key_func = channel_ratelimit_key) def post(): - guild_id = request.args.get("guild_id") + guild_id = request.form.get("guild_id") channel_id = request.form.get('channel_id') content = request.form.get('content') + content = format_post_content(content) if user_unauthenticated(): key = session['user_keys'][channel_id] else: diff --git a/titanembeds/blueprints/embed/embed.py b/titanembeds/blueprints/embed/embed.py index f81fac8..f6c7ca7 100644 --- a/titanembeds/blueprints/embed/embed.py +++ b/titanembeds/blueprints/embed/embed.py @@ -1,6 +1,7 @@ from flask import Blueprint, render_template, abort, redirect, url_for, session from titanembeds.utils import check_guild_existance, discord_api, guild_query_unauth_users_bool from titanembeds.oauth import generate_guild_icon_url, generate_avatar_url +from config import config import random embed = Blueprint("embed", __name__) @@ -26,7 +27,8 @@ def guild_embed(guild_id): login_greeting=get_logingreeting(), guild_id=guild_id, guild=guild, generate_guild_icon=generate_guild_icon_url, - unauth_enabled=guild_query_unauth_users_bool(guild_id) + unauth_enabled=guild_query_unauth_users_bool(guild_id), + client_id=config['client-id'] ) abort(404) diff --git a/titanembeds/blueprints/user/user.py b/titanembeds/blueprints/user/user.py index 2ee6953..6fc6456 100644 --- a/titanembeds/blueprints/user/user.py +++ b/titanembeds/blueprints/user/user.py @@ -36,6 +36,7 @@ def callback(): user = get_current_authenticated_user() session['user_id'] = user['id'] session['username'] = user['username'] + session['discriminator'] = user['discriminator'] session['avatar'] = generate_avatar_url(user['id'], user['avatar']) if session["redirect"]: redir = session["redirect"] diff --git a/titanembeds/static/js/embed.js b/titanembeds/static/js/embed.js index 444f756..e675807 100644 --- a/titanembeds/static/js/embed.js +++ b/titanembeds/static/js/embed.js @@ -2,6 +2,7 @@ /* global Materialize */ /* global Mustache */ /* global guild_id */ +/* global bot_client_id */ var logintimer; // timer to keep track of user inactivity after hitting login var fetchtimeout; // fetch routine timer @@ -66,6 +67,16 @@ function fetch(channel_id, after=null) { return funct.promise(); } +function post(channel_id, content) { + var funct = $.ajax({ + method: "POST", + dataType: "json", + url: "/api/post", + data: {"guild_id": guild_id, "channel_id": channel_id, "content": content} + }); + return funct.promise(); +} + $(function(){ resize_messagebox(); $("#loginmodal").modal({ @@ -119,6 +130,7 @@ function initialize_embed(guildobj) { } function prepare_guild(guildobj) { + console.log(guildobj) fill_channels(guildobj.channels); fill_discord_members(guildobj.discordmembers); fill_authenticated_users(guildobj.embedmembers.authenticated); @@ -210,6 +222,20 @@ function replace_message_mentions(message) { return message; } +function getPosition(string, subString, index) { + return string.split(subString, index).join(subString).length; +} + +function format_bot_message(message) { + if (message.author.id == bot_client_id && (message.content.includes("**") && ( (message.content.includes("<")&&message.content.includes(">")) || (message.content.includes("[") && message.content.includes("]")) ))) { + var usernamefield = message.content.substring(getPosition(message.content, "**", 1)+3, getPosition(message.content, "**", 2)-1); + message.content = message.content.substring(usernamefield.length+7); + message.author.username = usernamefield.split("#")[0]; + message.author.discriminator = usernamefield.split("#")[1]; + } + return message +} + function fill_discord_messages(messages, jumpscroll) { if (messages.length == 0) { return last_message_id; @@ -219,7 +245,9 @@ function fill_discord_messages(messages, jumpscroll) { Mustache.parse(template); for (var i = messages.length-1; i >= 0; i--) { var message = messages[i]; - var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.timestamp, "time": message.timestamp, "username": message.author.username, "discriminator": message.author.discriminator, "content": replace_message_mentions(message).content}); + message = replace_message_mentions(message); + message = format_bot_message(message); + var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.timestamp, "time": message.timestamp, "username": message.author.username, "discriminator": message.author.discriminator, "content": message.content}); $("#chatcontent").append(rendered); last = message.id; } @@ -241,7 +269,6 @@ function run_fetch_routine() { } fet.done(function(data) { var status = data.status; - console.log(data); update_embed_userchip(status.authenticated, status.avatar, status.username, status.user_id); last_message_id = fill_discord_messages(data.messages, jumpscroll); if (status.manage_embed) { @@ -304,6 +331,27 @@ $("#custom_username_field").keyup(function(event){ } }); +$("#messagebox").keyup(function(event){ + $(this).val($.trim($(this).val())); + if(event.keyCode == 13 && $(this).val().length >= 1 && $(this).val().length <= 350) { + $(this).blur(); + var funct = post(selected_channel, $(this).val()); + funct.done(function(data) { + $("#messagebox").val(""); + clearTimeout(fetchtimeout); + run_fetch_routine(); + }); + funct.fail(function(data) { + Materialize.toast('Failed to send message.', 10000); + }); + funct.catch(function(data) { + if (data.status == 429) { + Materialize.toast('You are sending messages too fast! 1 msg / 5 secs', 10000); + } + }); + } +}); + $(window).resize(function() { resize_messagebox(); }); diff --git a/titanembeds/templates/embed.html.j2 b/titanembeds/templates/embed.html.j2 index 7e36f01..aa3330b 100644 --- a/titanembeds/templates/embed.html.j2 +++ b/titanembeds/templates/embed.html.j2 @@ -88,7 +88,7 @@