diff --git a/discordbot/titanembeds/database/guilds.py b/discordbot/titanembeds/database/guilds.py index 6aac8f2..2ec3172 100644 --- a/discordbot/titanembeds/database/guilds.py +++ b/discordbot/titanembeds/database/guilds.py @@ -19,6 +19,8 @@ class Guilds(Base): owner_id = db.Column(db.BigInteger) # Snowflake of the owner icon = db.Column(db.String(255)) # The icon string, null if none invite_link = db.Column(db.String(255)) # Custom Discord Invite Link + post_timeout = db.Column(db.Integer, nullable=False, server_default="5") # Seconds to elapse before another message can be posted from the widget + max_message_length = db.Column(db.Integer, nullable=False, server_default="300") # Chars length the message should be before being rejected by the server def __init__(self, guild_id, name, roles, channels, webhooks, emojis, owner_id, icon): self.guild_id = guild_id diff --git a/webapp/alembic/versions/176d26252734_add_post_timeout_and_max_message_length_.py b/webapp/alembic/versions/176d26252734_add_post_timeout_and_max_message_length_.py new file mode 100644 index 0000000..af6d7b3 --- /dev/null +++ b/webapp/alembic/versions/176d26252734_add_post_timeout_and_max_message_length_.py @@ -0,0 +1,30 @@ +"""Add post_timeout and max_message_length columns to guilds + +Revision ID: 176d26252734 +Revises: b18fcc759865 +Create Date: 2018-03-25 04:04:11.000997 + +""" + +# revision identifiers, used by Alembic. +revision = '176d26252734' +down_revision = 'b18fcc759865' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('guilds', sa.Column('max_message_length', sa.Integer(), server_default='300', nullable=False)) + op.add_column('guilds', sa.Column('post_timeout', sa.Integer(), server_default='5', nullable=False)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('guilds', 'post_timeout') + op.drop_column('guilds', 'max_message_length') + # ### end Alembic commands ### diff --git a/webapp/titanembeds/blueprints/admin/admin.py b/webapp/titanembeds/blueprints/admin/admin.py index 54e0ead..c85c899 100644 --- a/webapp/titanembeds/blueprints/admin/admin.py +++ b/webapp/titanembeds/blueprints/admin/admin.py @@ -178,6 +178,8 @@ def administrate_guild(guild_id): "icon": db_guild.icon, "invite_link": db_guild.invite_link if db_guild.invite_link != None else "", "guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "", + "post_timeout": db_guild.post_timeout, + "max_message_length": db_guild.max_message_length, } return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics) @@ -192,6 +194,8 @@ def update_administrate_guild(guild_id): db_guild.bracket_links = request.form.get("bracket_links", db_guild.bracket_links) in ["true", True] db_guild.mentions_limit = request.form.get("mentions_limit", db_guild.mentions_limit) db_guild.unauth_captcha = request.form.get("unauth_captcha", db_guild.unauth_captcha) in ["true", True] + db_guild.post_timeout = request.form.get("post_timeout", db_guild.post_timeout) + db_guild.max_message_length = request.form.get("max_message_length", db_guild.max_message_length) invite_link = request.form.get("invite_link", db_guild.invite_link) if invite_link != None and invite_link.strip() == "": invite_link = None @@ -212,6 +216,8 @@ def update_administrate_guild(guild_id): invite_link=db_guild.invite_link, guest_icon=db_guild.guest_icon, unauth_captcha=db_guild.unauth_captcha, + post_timeout=db_guild.post_timeout, + max_message_length=db_guild.max_message_length, ) @admin.route("/guilds") diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index b120ca1..357f008 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -38,6 +38,11 @@ def format_post_content(guild_id, channel_id, message, dbUser): dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() + max_len = get_post_content_max_len(guild_id) + if len(message) > max_len: + illegal_post = True + illegal_reasons.append("Exceeded the following message length: {} characters".format(max_len)) + links = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', message) if not dbguild.chat_links and len(links) > 0: illegal_post = True @@ -244,10 +249,35 @@ def fetch_visitor(): response.status_code = status_code return response +def get_guild_specific_post_limit(): + guild_id = request.form.get("guild_id", None) + try: + guild_id = int(guild_id) + except: + guild_id = None + seconds = 5 + if guild_id: + dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() + if dbguild: + seconds = dbguild.post_timeout + return "1 per {} second".format(seconds) + +def get_post_content_max_len(guild_id): + try: + guild_id = int(guild_id) + except: + guild_id = None + length = 350 + if guild_id: + dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() + if dbguild: + length = dbguild.max_message_length + return length + @api.route("/post", methods=["POST"]) @valid_session_required(api=True) @abort_if_guild_disabled() -@rate_limiter.limit("1 per 5 second", key_func = channel_ratelimit_key) +@rate_limiter.limit(get_guild_specific_post_limit, key_func = channel_ratelimit_key) def post(): guild_id = request.form.get("guild_id") channel_id = request.form.get('channel_id') diff --git a/webapp/titanembeds/blueprints/embed/embed.py b/webapp/titanembeds/blueprints/embed/embed.py index ee02bf3..f5b4a6c 100644 --- a/webapp/titanembeds/blueprints/embed/embed.py +++ b/webapp/titanembeds/blueprints/embed/embed.py @@ -70,6 +70,7 @@ def guild_embed(guild_id): "icon": guild.icon, "invite_link": guild.invite_link, "invite_domain": parse_url_domain(guild.invite_link), + "post_timeout": guild.post_timeout, } customcss = get_custom_css() return render_template("embed.html.j2", diff --git a/webapp/titanembeds/blueprints/user/user.py b/webapp/titanembeds/blueprints/user/user.py index e990f7e..90de16f 100644 --- a/webapp/titanembeds/blueprints/user/user.py +++ b/webapp/titanembeds/blueprints/user/user.py @@ -215,6 +215,8 @@ def administrate_guild(guild_id): "icon": db_guild.icon, "invite_link": db_guild.invite_link if db_guild.invite_link != None else "", "guest_icon": db_guild.guest_icon if db_guild.guest_icon != None else "", + "post_timeout": db_guild.post_timeout, + "max_message_length": db_guild.max_message_length, } return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics, disabled=(guild_id in list_disabled_guilds())) @@ -237,6 +239,8 @@ def update_administrate_guild(guild_id): db_guild.bracket_links = request.form.get("bracket_links", db_guild.bracket_links) in ["true", True] db_guild.mentions_limit = request.form.get("mentions_limit", db_guild.mentions_limit) db_guild.unauth_captcha = request.form.get("unauth_captcha", db_guild.unauth_captcha) in ["true", True] + db_guild.post_timeout = request.form.get("post_timeout", db_guild.post_timeout) + db_guild.max_message_length = request.form.get("max_message_length", db_guild.max_message_length) invite_link = request.form.get("invite_link", db_guild.invite_link) if invite_link != None and invite_link.strip() == "": @@ -260,6 +264,8 @@ def update_administrate_guild(guild_id): invite_link=db_guild.invite_link, guest_icon=guest_icon, unauth_captcha=db_guild.unauth_captcha, + post_timeout=db_guild.post_timeout, + max_message_length=db_guild.max_message_length, ) @user.route("/add-bot/") diff --git a/webapp/titanembeds/database/guilds.py b/webapp/titanembeds/database/guilds.py index 8e35fb5..32b496c 100644 --- a/webapp/titanembeds/database/guilds.py +++ b/webapp/titanembeds/database/guilds.py @@ -19,6 +19,8 @@ class Guilds(db.Model): owner_id = db.Column(db.BigInteger, nullable=False) # Snowflake of the owner icon = db.Column(db.String(255)) # The icon string, null if none invite_link = db.Column(db.String(255)) # Custom Discord Invite Link + post_timeout = db.Column(db.Integer, nullable=False, server_default="5") # Seconds to elapse before another message can be posted from the widget + max_message_length = db.Column(db.Integer, nullable=False, server_default="300") # Chars length the message should be before being rejected by the server def __init__(self, guild_id, name, roles, channels, webhooks, emojis, owner_id, icon): self.guild_id = guild_id diff --git a/webapp/titanembeds/static/js/administrate_guild.js b/webapp/titanembeds/static/js/administrate_guild.js index 1d5dba7..0ca8bef 100644 --- a/webapp/titanembeds/static/js/administrate_guild.js +++ b/webapp/titanembeds/static/js/administrate_guild.js @@ -54,6 +54,28 @@ $("#mentions_limit").keyup(function(event){ } }); +$("#post_timeout").keyup(function(event){ + if(event.keyCode == 13){ + var pathname = window.location.pathname; + var value = $("#post_timeout").val() + var payload = {"post_timeout": value} + $.post(pathname, payload, function(data) { + Materialize.toast('Updated post timeout setting!', 2000) + }); + } +}); + +$("#max_message_length").keyup(function(event){ + if(event.keyCode == 13){ + var pathname = window.location.pathname; + var value = $("#max_message_length").val() + var payload = {"max_message_length": value} + $.post(pathname, payload, function(data) { + Materialize.toast('Updated max message length setting!', 2000) + }); + } +}); + $("#invite_link").keyup(function(event){ if(event.keyCode == 13){ var pathname = window.location.pathname; diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index 0ef2636..02ac457 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -19,6 +19,7 @@ /* global disabled */ /* global wdtEmojiBundle */ /* global EmojiConvertor */ +/* global post_timeout */ (function () { const theme_options = ["DiscordDark", "MetroEdge", "BetterTitan"]; // All the avaliable theming names @@ -1589,7 +1590,7 @@ if (event.keyCode == 16) { shift_pressed = true; } - if(event.keyCode == 13 && !shift_pressed && $(this).val().length >= 1 && $(this).val().length <= 350) { + if(event.keyCode == 13 && !shift_pressed && $(this).val().length >= 1) { $(this).val($.trim($(this).val())); $(this).blur(); $("#messagebox").attr('readonly', true); @@ -1613,15 +1614,13 @@ }); funct.catch(function(data) { if (data.status == 429) { - Materialize.toast('You are sending messages too fast! 1 message per 5 seconds', 10000); + Materialize.toast('You are sending messages too fast! 1 message per ' + post_timeout + ' seconds', 10000); } }); funct.always(function() { $("#messagebox").attr('readonly', false); $("#messagebox").focus(); }); - } else if (event.keyCode == 13 && !shift_pressed && $(this).val().length > 350) { - Materialize.toast('You are sending messages too long! 350 characters limit.', 10000); } }); diff --git a/webapp/titanembeds/templates/administrate_guild.html.j2 b/webapp/titanembeds/templates/administrate_guild.html.j2 index 1fd7d75..4e68326 100644 --- a/webapp/titanembeds/templates/administrate_guild.html.j2 +++ b/webapp/titanembeds/templates/administrate_guild.html.j2 @@ -134,6 +134,18 @@
+

Send Message Timeout

+

(0 seconds to have no limit - enter to submit)

+ + +
+ +

Maximum Message Length

+

(No more than 2000 characters - enter to submit)

+ + +
+

Custom Invite Link

Have a permanent invite that you really want to use? Something like discord.io on your mind? Enter your custom invite link here to replace the default one on the embed!

(Leave blank if none - enter to submit)

diff --git a/webapp/titanembeds/templates/embed.html.j2 b/webapp/titanembeds/templates/embed.html.j2 index da6a31e..65ba2ad 100644 --- a/webapp/titanembeds/templates/embed.html.j2 +++ b/webapp/titanembeds/templates/embed.html.j2 @@ -463,6 +463,7 @@ const disabled = {{ disabled|tojson|safe }}; const guild_id = "{{ guild_id }}"; const bot_client_id = "{{ client_id }}"; + const post_timeout = {{ guild["post_timeout"]|tojson|safe }}; const visitors_enabled = {% if visitors_enabled %}true{% else %}false{% endif %}; const unauth_captcha_enabled = {% if unauth_captcha_enabled %}true{% else %}false{% endif %};