diff --git a/titanembeds/blueprints/api/api.py b/titanembeds/blueprints/api/api.py index b0f7e90..d18a06f 100644 --- a/titanembeds/blueprints/api/api.py +++ b/titanembeds/blueprints/api/api.py @@ -154,16 +154,17 @@ def get_online_embed_users(guild_id): @rate_limiter.limit("2500/hour") @rate_limiter.limit("12/minute", key_func = channel_ratelimit_key) def fetch(): + guild_id = request.args.get("guild_id") channel_id = request.args.get('channel_id') after_snowflake = request.args.get('after', None, type=int) if user_unauthenticated(): key = session['user_keys'][channel_id] else: key = None - status = update_user_status(channel_id, session['username'], key) + status = update_user_status(guild_id, session['username'], key) if status['banned'] or status['revoked']: messages = {} - status_code = 401 + status_code = 403 else: messages = discord_api.get_channel_messages(channel_id, after_snowflake) status_code = messages['code'] @@ -176,13 +177,14 @@ 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") channel_id = request.form.get('channel_id') content = request.form.get('content') if user_unauthenticated(): key = session['user_keys'][channel_id] else: key = None - status = update_user_status(channel_id, session['username'], key) + status = update_user_status(guild_id, session['username'], key) if status['banned'] or status['revoked']: message = {} status_code = 401 diff --git a/titanembeds/static/js/embed.js b/titanembeds/static/js/embed.js index 371821d..444f756 100644 --- a/titanembeds/static/js/embed.js +++ b/titanembeds/static/js/embed.js @@ -4,7 +4,9 @@ /* global guild_id */ var logintimer; // timer to keep track of user inactivity after hitting login -var last_message_id; +var fetchtimeout; // fetch routine timer +var last_message_id; // last message tracked +var selected_channel = guild_id; // user selected channel, defaults to #general channel function element_in_view(element, fullyInView) { var pageTop = $(window).scrollTop(); @@ -59,7 +61,7 @@ function fetch(channel_id, after=null) { method: "GET", dataType: "json", url: "/api/fetch", - data: {"channel_id": channel_id, "after": after} + data: {"guild_id": guild_id,"channel_id": channel_id, "after": after} }); return funct.promise(); } @@ -133,6 +135,7 @@ function fill_channels(channels) { var rendered = Mustache.render(template, {"channelid": chan.id, "channelname": chan.name}); $("#channels-list").append(rendered); } + $("#channel-"+selected_channel).parent().addClass("active"); } function fill_discord_members(discordmembers) { @@ -189,6 +192,24 @@ function _wait_for_discord_login(index) { }, 5000); } +function select_channel(channel_id) { + selected_channel = channel_id; + last_message_id = null; + $("#channels-list > li.active").removeClass("active"); + $("#channel-"+selected_channel).parent().addClass("active"); + clearTimeout(fetchtimeout); + run_fetch_routine(); +} + +function replace_message_mentions(message) { + var mentions = message.mentions; + for (var i = 0; i < mentions.length; i++) { + var mention = mentions[i]; + message.content = message.content.replace(new RegExp("<@" + mention.id + ">", 'g'), "@" + mention.username + "#" + mention.discriminator); + } + return message; +} + function fill_discord_messages(messages, jumpscroll) { if (messages.length == 0) { return last_message_id; @@ -198,7 +219,7 @@ 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": message.content}); + 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}); $("#chatcontent").append(rendered); last = message.id; } @@ -207,7 +228,7 @@ function fill_discord_messages(messages, jumpscroll) { } function run_fetch_routine() { - var channel_id = guild_id; //TODO: implement channel selector + var channel_id = selected_channel; var fet; var jumpscroll; if (last_message_id == null) { @@ -220,6 +241,7 @@ 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) { @@ -233,7 +255,7 @@ function run_fetch_routine() { fill_discord_members(guildobj.discordmembers); fill_authenticated_users(guildobj.embedmembers.authenticated); fill_unauthenticated_users(guildobj.embedmembers.unauthenticated); - setTimeout(run_fetch_routine, 10000); + fetchtimeout = setTimeout(run_fetch_routine, 10000); }); }); fet.fail(function(data) { @@ -246,7 +268,7 @@ function run_fetch_routine() { } }); fet.catch(function(data) { - setTimeout(run_fetch_routine, 10000); + fetchtimeout = setTimeout(run_fetch_routine, 10000); }); } diff --git a/titanembeds/templates/embed.html.j2 b/titanembeds/templates/embed.html.j2 index bafb90f..7e36f01 100644 --- a/titanembeds/templates/embed.html.j2 +++ b/titanembeds/templates/embed.html.j2 @@ -25,17 +25,7 @@
-
-

3:30PM EndenDragon#69420 Hello everyone!

-

3:30PM EndenDragon#69420 Hello everyone!

-

3:30PM EndenDragon#69420 Hello everyone!

-

3:30PM EndenDragon#69420 Hello everyone!

-

3:30PM EndenDragon#69420 Hello everyone!

-

3:30PM EndenDragon#69420 Hello everyone!

-

3:30PM EndenDragon#69420 Hello everyone!

-

3:30PM EndenDragon#69420 Hello everyone!

-

3:30PM EndenDragon#69420 adfsadfsdfas

-
+