diff --git a/webapp/titanembeds/app.py b/webapp/titanembeds/app.py index 3159ec0..fb72eb4 100644 --- a/webapp/titanembeds/app.py +++ b/webapp/titanembeds/app.py @@ -20,6 +20,7 @@ import os from titanembeds.database import get_administrators_list import titanembeds.constants as constants from datetime import timedelta +import datetime os.chdir(config['app-location']) app = Flask(__name__, static_folder="static") @@ -81,4 +82,4 @@ def before_first_request(): @app.context_processor def context_processor(): - return {"devs": get_administrators_list(), "constants": constants} + return {"devs": get_administrators_list(), "constants": constants, "af_mode_enabled": datetime.datetime.now().date() == datetime.date(datetime.datetime.now().year, 4, 1)} diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index b0469f2..b120ca1 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -521,3 +521,12 @@ def webhook_discordbotsorg_vote(): DBLTrans = DiscordBotsOrgTransactions(int(user_id), vote_type, referrer) db.session.add(DBLTrans) return ('', 204) + +@api.route("/af/direct_message", methods=["POST"]) +def af_direct_message_post(): + cs = request.form.get('cs', None) + input = request.form.get('input') + cleverbot_url = "http://www.cleverbot.com/getreply" + payload = {'key': config["cleverbot-api-key"], 'cs': cs, 'input': input} + r = requests.get(cleverbot_url, params=payload) + return jsonify(r.json()) \ No newline at end of file diff --git a/webapp/titanembeds/static/js/embed.af.directmessage.js b/webapp/titanembeds/static/js/embed.af.directmessage.js new file mode 100644 index 0000000..81cd072 --- /dev/null +++ b/webapp/titanembeds/static/js/embed.af.directmessage.js @@ -0,0 +1,175 @@ +(function () { + /* global $, Mustache, soundManager, Materialize */ + + const MODAL_TEMPLATE = ` + + `; + + const MESSAGE_TEMPLATE = ` +
+ + {{ username }}#{{ discriminator }} + {{ message }} +
+ `; + + var notification_sound = soundManager.createSound({ + id: 'notification_sound_id', + url: "/static/audio/demonstrative.mp3", + volume: 8, + }); + + var dmStorage = {}; // {"EndenDragon#1337": {cs: "code", conversation: [{me: true, message: "stuff"}, ...]}, ...} + + function cleverJax(cs, input) { + var data = {"input": input}; + if (cs) { + data.cs = cs; + } + var funct = $.ajax({ + method: "POST", + dataType: "json", + url: "/api/af/direct_message", + data: data + }); + return funct.promise(); + } + + function getRandomInt(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; + } + + function getID(user) { + return user.username + "#" + user.discriminator; + } + + function getCS(id) { + if (!dmStorage[id]) { + return null; + } + return dmStorage[id].cs; + } + + function setCS(id, cs) { + if (!dmStorage[id]) { + createDM(id); + } + dmStorage[id].cs = cs; + } + + function createDM(id) { + dmStorage[id] = { + cs: null, + conversation: [], + }; + } + + function populateDM(id) { + $("#af_dm_modal .dmcontent").empty(); + if (!dmStorage[id]) { + createDM(id); + } + var msgs = dmStorage[id].conversation; + for (var i = 0; i < msgs.length; i++) { + var user; + if (msgs[i].me) { + user = getMySelfUser(); + } else { + user = getCurrentUser(); + } + var rendered = Mustache.render(MESSAGE_TEMPLATE, { + avatar: user.avatar, + username: user.username, + discriminator: user.discriminator, + message: msgs[i].message + }); + $("#af_dm_modal .dmcontent").append(rendered); + } + } + + function addMessage(id, me, message) { + if (!dmStorage[id]) { + createDM(id); + } + dmStorage[id].conversation.push({ + me: me, + message: message + }); + populateDM(id); + } + + function getMySelfUser() { + var username = $("#curuser_name").text(); + var discriminator = $("#curuser_discrim").text(); + var avatar = $("#currentuserimage").attr("src"); + return { + "username": username, + "discriminator": discriminator, + "avatar": avatar, + }; + } + + function getCurrentUser() { + var username = $("#usercard .identity .username").text(); + var discriminator = $("#usercard .identity .discriminator").text(); + var avatar = $("#usercard .avatar").attr("src"); + return { + "username": username, + "discriminator": discriminator, + "avatar": avatar, + }; + } + + function sendDM(value) { + var id = getID(getCurrentUser()); + var cs = getCS(id); + var cj = cleverJax(cs, value); + addMessage(id, true, value); + cj.done(function (data) { + setCS(id, data.cs); + setTimeout(function () { + addMessage(id, false, data.output); + if (notification_sound.playState == 0) { + notification_sound.play(); + } + }, getRandomInt(2000, 3500)); + }); + } + + function openDM() { + var curUser = getCurrentUser(); + $("#af_dm_modal h4 .username").text(curUser.username); + $("#af_dm_modal h4 .discriminator").text(curUser.discriminator); + populateDM(getID(curUser)); + $('#af_dm_modal').modal('open'); + } + + $(function() { + $(MODAL_TEMPLATE).insertAfter("#usercard").modal({ + dismissible: true, + }); + var openDMbtn = $("DM User"); + openDMbtn.bind("click", openDM); + openDMbtn.insertAfter("#usercard-mention-btn"); + $("#af_dm_msgbox").bind("keyup", function (e) { + if (e.which == 13) { + e.preventDefault(); + sendDM($("#af_dm_msgbox").val()); + $("#af_dm_msgbox").val(""); + $("#af_dm_msgbox").focus(); + } + }); + Mustache.parse(MESSAGE_TEMPLATE); + Materialize.toast('We now support Direct Messages! Click on a username in chat/sidebar to start sending message directly to each other.', 5000); + }); +})(); \ No newline at end of file diff --git a/webapp/titanembeds/static/js/site.af.sausage.js b/webapp/titanembeds/static/js/site.af.sausage.js new file mode 100644 index 0000000..cf58c0e --- /dev/null +++ b/webapp/titanembeds/static/js/site.af.sausage.js @@ -0,0 +1,14 @@ +/* global $ */ +(function () { + $(function() { + $(".brand-logo").html($(".brand-logo").html().replace(/Titan/g,'Sausage')); + $(".brand-logo .betatag").text("ALPHA"); + try { + $("main > .container").html($("main > .container").html().replace(/Titan/g,'Sausage')); + $("#dblbanner").html($("#dblbanner").html().replace(/Titan/g,'Sausage')); + } catch (e) { + // nothing + } + $("nav .brand-logo img").attr("src", "https://i.imgur.com/6xPdXFV.png"); + }); +})(); \ No newline at end of file diff --git a/webapp/titanembeds/templates/embed.html.j2 b/webapp/titanembeds/templates/embed.html.j2 index 37dfd52..da6a31e 100644 --- a/webapp/titanembeds/templates/embed.html.j2 +++ b/webapp/titanembeds/templates/embed.html.j2 @@ -469,6 +469,11 @@ + {% if af_mode_enabled %} + + + {% endif %} + + {% if af_mode_enabled %} + + {% endif %} + {% block script %}{% endblock %}