Users able to send messages

This commit is contained in:
Jeremy Zhang 2017-04-05 06:25:07 +00:00
parent 78986c6803
commit 035e0d9836
5 changed files with 65 additions and 5 deletions

View File

@ -95,6 +95,13 @@ def check_user_in_guild(guild_id):
else: else:
return 200 == discord_api.get_guild_member_nocache(guild_id, session['user_id'])['code'] 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) @cache.cached(timeout=300, key_prefix=make_guildchannels_cache_key)
def get_guild_channels(guild_id): def get_guild_channels(guild_id):
if user_unauthenticated(): if user_unauthenticated():
@ -177,9 +184,10 @@ def fetch():
@rate_limiter.limit("1200/hour") @rate_limiter.limit("1200/hour")
@rate_limiter.limit("6/minute", key_func = channel_ratelimit_key) @rate_limiter.limit("6/minute", key_func = channel_ratelimit_key)
def post(): def post():
guild_id = request.args.get("guild_id") guild_id = request.form.get("guild_id")
channel_id = request.form.get('channel_id') channel_id = request.form.get('channel_id')
content = request.form.get('content') content = request.form.get('content')
content = format_post_content(content)
if user_unauthenticated(): if user_unauthenticated():
key = session['user_keys'][channel_id] key = session['user_keys'][channel_id]
else: else:

View File

@ -1,6 +1,7 @@
from flask import Blueprint, render_template, abort, redirect, url_for, session 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.utils import check_guild_existance, discord_api, guild_query_unauth_users_bool
from titanembeds.oauth import generate_guild_icon_url, generate_avatar_url from titanembeds.oauth import generate_guild_icon_url, generate_avatar_url
from config import config
import random import random
embed = Blueprint("embed", __name__) embed = Blueprint("embed", __name__)
@ -26,7 +27,8 @@ def guild_embed(guild_id):
login_greeting=get_logingreeting(), login_greeting=get_logingreeting(),
guild_id=guild_id, guild=guild, guild_id=guild_id, guild=guild,
generate_guild_icon=generate_guild_icon_url, 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) abort(404)

View File

@ -36,6 +36,7 @@ def callback():
user = get_current_authenticated_user() user = get_current_authenticated_user()
session['user_id'] = user['id'] session['user_id'] = user['id']
session['username'] = user['username'] session['username'] = user['username']
session['discriminator'] = user['discriminator']
session['avatar'] = generate_avatar_url(user['id'], user['avatar']) session['avatar'] = generate_avatar_url(user['id'], user['avatar'])
if session["redirect"]: if session["redirect"]:
redir = session["redirect"] redir = session["redirect"]

View File

@ -2,6 +2,7 @@
/* global Materialize */ /* global Materialize */
/* global Mustache */ /* global Mustache */
/* global guild_id */ /* global guild_id */
/* global bot_client_id */
var logintimer; // timer to keep track of user inactivity after hitting login var logintimer; // timer to keep track of user inactivity after hitting login
var fetchtimeout; // fetch routine timer var fetchtimeout; // fetch routine timer
@ -66,6 +67,16 @@ function fetch(channel_id, after=null) {
return funct.promise(); 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(){ $(function(){
resize_messagebox(); resize_messagebox();
$("#loginmodal").modal({ $("#loginmodal").modal({
@ -119,6 +130,7 @@ function initialize_embed(guildobj) {
} }
function prepare_guild(guildobj) { function prepare_guild(guildobj) {
console.log(guildobj)
fill_channels(guildobj.channels); fill_channels(guildobj.channels);
fill_discord_members(guildobj.discordmembers); fill_discord_members(guildobj.discordmembers);
fill_authenticated_users(guildobj.embedmembers.authenticated); fill_authenticated_users(guildobj.embedmembers.authenticated);
@ -210,6 +222,20 @@ function replace_message_mentions(message) {
return 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) { function fill_discord_messages(messages, jumpscroll) {
if (messages.length == 0) { if (messages.length == 0) {
return last_message_id; return last_message_id;
@ -219,7 +245,9 @@ function fill_discord_messages(messages, jumpscroll) {
Mustache.parse(template); Mustache.parse(template);
for (var i = messages.length-1; i >= 0; i--) { for (var i = messages.length-1; i >= 0; i--) {
var message = messages[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); $("#chatcontent").append(rendered);
last = message.id; last = message.id;
} }
@ -241,7 +269,6 @@ function run_fetch_routine() {
} }
fet.done(function(data) { fet.done(function(data) {
var status = data.status; var status = data.status;
console.log(data);
update_embed_userchip(status.authenticated, status.avatar, status.username, status.user_id); update_embed_userchip(status.authenticated, status.avatar, status.username, status.user_id);
last_message_id = fill_discord_messages(data.messages, jumpscroll); last_message_id = fill_discord_messages(data.messages, jumpscroll);
if (status.manage_embed) { 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() { $(window).resize(function() {
resize_messagebox(); resize_messagebox();
}); });

View File

@ -88,7 +88,7 @@
<div class="left"><img id="currentuserimage" src="https://cdn.discordapp.com/icons/213459805048668160/14aa7c0ad94e3c80d2eeef3a83cae1c1.jpg" class="circle left currentuserimage" style="display: none;"></div> <div class="left"><img id="currentuserimage" src="https://cdn.discordapp.com/icons/213459805048668160/14aa7c0ad94e3c80d2eeef3a83cae1c1.jpg" class="circle left currentuserimage" style="display: none;"></div>
<div id="currentusername" class="currentusername left">Titan#0001</div> <div id="currentusername" class="currentusername left">Titan#0001</div>
</div> </div>
<div id="messageboxouter" class="input-field inline"><textarea placeholder="Enter message" id="messagebox" type="text" class="materialize-textarea"></textarea></div> <div id="messageboxouter" class="input-field inline"><textarea placeholder="Enter message" id="messagebox" type="text" class="materialize-textarea" rows="1"></textarea></div>
</div> </div>
</footer> </footer>
@ -117,6 +117,7 @@
<script> <script>
var guild_id = "{{ guild_id }}"; var guild_id = "{{ guild_id }}";
var bot_client_id = "{{ client_id }}";
</script> </script>
<script type="text/javascript" src="{{ url_for('static', filename='js/embed.js') }}"></script> <script type="text/javascript" src="{{ url_for('static', filename='js/embed.js') }}"></script>