Titan/webapp/titanembeds/static/js/embed.js

791 lines
32 KiB
JavaScript
Raw Normal View History

/* global $ */
/* global Materialize */
/* global Mustache */
/* global guild_id */
2017-04-05 08:25:07 +02:00
/* global bot_client_id */
/* global moment */
/* global localStorage */
/* global visitors_enabled */
/* global cheet */
2017-04-13 02:42:32 +02:00
(function () {
const theme_options = ["DiscordDark", "BetterTitan"]; // All the avaliable theming names
var user_def_css; // Saves the user defined css
var has_already_been_focused = false; // keep track of if the embed has initially been focused.
var has_already_been_initially_resized = false; // keep track if the embed initially been resized
2017-04-13 02:42:32 +02:00
var logintimer; // timer to keep track of user inactivity after hitting login
var fetchtimeout; // fetch routine timer
2017-04-15 09:14:09 +02:00
var currently_fetching; // fetch lock- if true, do not fetch
2017-04-13 02:42:32 +02:00
var last_message_id; // last message tracked
var selected_channel = guild_id; // user selected channel, defaults to #general channel
var guild_channels = {}; // all server channels used to highlight channels in messages
2017-06-09 10:22:22 +02:00
var emoji_store = {}; // all server emojis
2017-04-25 08:52:46 +02:00
var times_fetched = 0; // kept track of how many times that it has fetched
var fetch_error_count = 0; // Number of errors fetch has encountered
var priority_query_guild = false; // So you have selected a channel? Let's populate it.
var current_username_discrim; // Current username/discrim pair, eg EndenDraogn#4151
var visitor_mode = false; // Keep track of if using the visitor mode or authenticate mode
2017-04-13 02:42:32 +02:00
function element_in_view(element, fullyInView) {
var pageTop = $(window).scrollTop();
var pageBottom = pageTop + $(window).height();
var elementTop = $(element).offset().top;
var elementBottom = elementTop + $(element).height();
2017-04-13 02:42:32 +02:00
if (fullyInView === true) {
return ((pageTop < elementTop) && (pageBottom > elementBottom));
} else {
return ((elementTop <= pageBottom) && (elementBottom >= pageTop));
}
2017-04-05 05:46:48 +02:00
}
String.prototype.replaceAll = function(target, replacement) {
return this.split(target).join(replacement);
};
2017-04-13 02:42:32 +02:00
function query_guild() {
var url = "/api/query_guild";
if (visitor_mode) {
url = url += "_visitor";
}
2017-04-13 02:42:32 +02:00
var funct = $.ajax({
dataType: "json",
url: url,
2017-04-13 02:42:32 +02:00
data: {"guild_id": guild_id}
});
2017-04-13 02:42:32 +02:00
return funct.promise();
}
2017-04-13 02:42:32 +02:00
function create_authenticated_user() {
var funct = $.ajax({
method: "POST",
dataType: "json",
url: "/api/create_authenticated_user",
data: {"guild_id": guild_id}
});
return funct.promise();
}
2017-04-13 02:42:32 +02:00
function create_unauthenticated_user(username) {
var funct = $.ajax({
method: "POST",
dataType: "json",
url: "/api/create_unauthenticated_user",
data: {"username": username, "guild_id": guild_id}
2017-04-10 01:40:11 +02:00
});
2017-04-13 02:42:32 +02:00
return funct.promise();
}
2017-04-13 02:42:32 +02:00
function fetch(channel_id, after=null) {
var url = "/api/fetch";
if (visitor_mode) {
url += "_visitor";
}
2017-04-13 02:42:32 +02:00
var funct = $.ajax({
method: "GET",
dataType: "json",
url: url,
2017-04-13 02:42:32 +02:00
data: {"guild_id": guild_id,"channel_id": channel_id, "after": after}
2017-04-10 01:40:11 +02:00
});
2017-04-13 02:42:32 +02:00
return funct.promise();
}
2017-04-13 02:42:32 +02:00
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 discord_embed() {
var funct = $.ajax({
dataType: "json",
url: "https://discordapp.com/api/guilds/" + guild_id + "/widget.json",
});
return funct.promise();
}
$(function() {
if ($("#user-defined-css").length > 0) {
user_def_css = $("#user-defined-css").text();
}
$('select').material_select();
2017-06-10 04:20:22 +02:00
$("#loginmodal").modal({
dismissible: visitors_enabled, // Modal can be dismissed by clicking outside of the modal
opacity: .5, // Opacity of modal background
inDuration: 300, // Transition in duration
outDuration: 200, // Transition out duration
startingTop: '4%', // Starting top style attribute
endingTop: '10%', // Ending top style attribute
}
);
$('#loginmodal').modal('open');
2017-05-24 05:20:23 +02:00
$("#focusmodal").modal({
dismissible: true,
opacity: .5,
inDuration: 400,
outDuration: 400,
startingTop: "4%",
endingTop: "10%",
2017-05-24 05:20:23 +02:00
});
$("#focusmodal").modal("open");
$("#userembedmodal").modal({
dismissible: true,
opacity: .5,
inDuration: 400,
outDuration: 400,
});
$("#nameplate").click(function () {
$("#userembedmodal").modal("open");
});
$("#visitor_login_btn").click(function () {
$("#loginmodal").modal("open");
});
$( "#theme-selector" ).change(function() {
var theme = $("#theme-selector option:selected").val();
changeTheme(theme);
});
var themeparam = getParameterByName('theme');
var localstore_theme = localStorage.getItem("theme");
if ((getParameterByName("css") == null) && ((themeparam && $.inArray(themeparam, theme_options) != -1) || (localstore_theme))) {
var theme;
if (themeparam) {
theme = themeparam;
} else {
theme = localstore_theme;
}
changeTheme(theme);
$("#theme-selector option").removeAttr('selected');
$("#theme-selector option[value=" + theme + "]").attr('selected', 'selected');
$('select').material_select();
}
2017-06-10 04:20:22 +02:00
var dembed = discord_embed();
dembed.done(function (data) {
$("#modal_invite_btn").attr("href", data.instant_invite);
});
$(window).resize(function(){
// For those who decides to hide the embed at first load (display: none), resulting in the messages being not scrolled down.
if (!has_already_been_initially_resized) {
has_already_been_initially_resized = true;
$("html, body").animate({ scrollTop: $(document).height() }, "fast");
}
});
if (getParameterByName("forcefocus") == "1") {
if (document.hasFocus()) {
primeEmbed();
}
$(window).focus(function() {
if (!has_already_been_focused) {
primeEmbed();
}
});
} else {
primeEmbed();
}
});
function changeTheme(theme) {
if (theme == "") {
$("#css-theme").attr("href", "");
$("#user-defined-css").text(user_def_css);
localStorage.removeItem("theme");
} else if ($.inArray(theme, theme_options) != -1) {
$("#user-defined-css").text("");
$("#css-theme").attr("href", "/static/themes/" + theme + "/css/style.css");
localStorage.setItem("theme", theme);
}
}
/* https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript */
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function setVisitorMode(enabled) {
if (!visitors_enabled) {
return;
}
visitor_mode = enabled;
if (visitor_mode) {
$("#visitor_mode_message").show();
$("#messagebox").hide();
} else {
$("#visitor_mode_message").hide();
$("#messagebox").show();
}
}
function primeEmbed() {
2017-05-24 05:20:23 +02:00
$("#focusmodal").modal("close");
has_already_been_focused = true;
2017-04-26 09:30:36 +02:00
lock_login_fields();
2017-04-13 02:42:32 +02:00
var guild = query_guild();
guild.fail(function() {
2017-04-26 09:30:36 +02:00
unlock_login_fields();
if (visitors_enabled) {
setVisitorMode(true);
var guild2 = query_guild();
guild2.done(function(data) {
initialize_embed(data);
});
guild2.fail(function() {
setVisitorMode(false);
});
}
});
2017-04-13 02:42:32 +02:00
guild.done(function(data) {
initialize_embed(data);
});
}
2017-04-13 02:42:32 +02:00
function lock_login_fields() {
$("#loginProgress").show();
$("#discordlogin_btn").attr("disabled",true);
$("#custom_username_field").prop("disabled",true);
logintimer = setTimeout(function() {
unlock_login_fields();
}, 60000);
}
2017-04-13 02:42:32 +02:00
function unlock_login_fields() {
$("#loginProgress").hide();
$("#discordlogin_btn").attr("disabled",false);
$("#custom_username_field").prop("disabled",false);
clearTimeout(logintimer);
}
2017-04-13 02:42:32 +02:00
function initialize_embed(guildobj) {
if (guildobj === undefined) {
var guild = query_guild();
guild.done(function(data) {
switch_to_default_channel(data.channels);
2017-04-13 02:42:32 +02:00
prepare_guild(data);
2017-04-26 09:30:36 +02:00
$('#loginmodal').modal('close');
unlock_login_fields();
2017-04-13 02:42:32 +02:00
});
} else {
switch_to_default_channel(guildobj.channels);
2017-04-13 02:42:32 +02:00
prepare_guild(guildobj);
2017-04-26 09:30:36 +02:00
$('#loginmodal').modal('close');
unlock_login_fields();
2017-04-09 00:32:28 +02:00
}
2017-04-12 15:15:05 +02:00
}
function switch_to_default_channel(guildchannels) {
var defaultChannel = getParameterByName("defaultchannel");
if (!defaultChannel) {
return;
}
for (var i = 0; i < guildchannels.length; i++) {
if (guildchannels[i].channel.id == defaultChannel) {
selected_channel = defaultChannel;
debugger;
return;
}
}
}
2017-04-13 02:42:32 +02:00
function prepare_guild(guildobj) {
2017-06-09 10:22:22 +02:00
emoji_store = guildobj.emojis;
2017-04-13 02:42:32 +02:00
fill_channels(guildobj.channels);
fill_discord_members(guildobj.discordmembers);
fill_authenticated_users(guildobj.embedmembers.authenticated);
fill_unauthenticated_users(guildobj.embedmembers.unauthenticated);
2017-05-24 05:41:12 +02:00
$("#instant-inv").attr("href", guildobj.instant_invite);
2017-04-13 02:42:32 +02:00
run_fetch_routine();
2017-04-05 05:46:48 +02:00
}
2017-04-13 02:42:32 +02:00
function fill_channels(channels) {
var template = $('#mustache_channellistings').html();
Mustache.parse(template);
$("#channels-list").empty();
for (var i = 0; i < channels.length; i++) {
var chan = channels[i];
guild_channels[chan.channel.id] = chan;
2017-04-13 02:42:32 +02:00
if (chan.read) {
var rendered = Mustache.render(template, {"channelid": chan.channel.id, "channelname": chan.channel.name});
$("#channels-list").append(rendered);
$("#channel-" + chan.channel.id.toString()).click({"channel_id": chan.channel.id.toString()}, function(event) {
select_channel(event.data.channel_id);
});
if (chan.channel.id == selected_channel) {
if (chan.write) {
$("#messagebox").prop('disabled', false);
$("#messagebox").prop('placeholder', "Enter message");
} else {
$("#messagebox").prop('disabled', true);
$("#messagebox").prop('placeholder', "Messages is disabled in this channel.");
}
$("#channeltopic").text(chan.channel.topic);
}
}
}
$("#channel-"+selected_channel).parent().addClass("active");
2017-04-05 05:46:48 +02:00
}
2017-04-13 02:42:32 +02:00
function mention_member(member_id) {
if (!$('#messagebox').prop('disabled')) {
$('#messagebox').val( $('#messagebox').val() + "[@" + member_id + "] " );
$('.button-collapse').sideNav('hide');
$("#messagebox").focus();
}
2017-04-05 05:46:48 +02:00
}
2017-04-13 02:42:32 +02:00
function fill_discord_members(discordmembers) {
var template = $('#mustache_authedusers').html();
Mustache.parse(template);
$("#discord-members").empty();
var guild_members = {};
2017-04-13 02:42:32 +02:00
for (var i = 0; i < discordmembers.length; i++) {
var member = discordmembers[i];
if (member["hoist-role"]) {
if (!(member["hoist-role"]["id"] in guild_members)) {
guild_members[member["hoist-role"]["id"]] = {};
guild_members[member["hoist-role"]["id"]]["name"] = member["hoist-role"]["name"];
guild_members[member["hoist-role"]["id"]]["members"] = [];
2017-05-24 05:20:23 +02:00
guild_members[member["hoist-role"]["id"]]["position"] = member["hoist-role"]["position"];
}
guild_members[member["hoist-role"]["id"]]["members"].push(member);
} else {
if (!("0" in guild_members)) {
guild_members["0"] = {};
guild_members["0"]["name"] = null;
guild_members["0"]["members"] = [];
guild_members["0"]["position"] = 0;
}
guild_members["0"]["members"].push(member);
}
}
var guild_members_arr = [];
2017-05-24 05:20:23 +02:00
for (var key in guild_members) {
guild_members_arr.push(guild_members[key]);
}
guild_members_arr.sort(function(a, b) {
return parseInt(b.position) - parseInt(a.position);
2017-04-15 09:14:09 +02:00
});
var template_role = $('#mustache_memberrole').html();
Mustache.parse(template_role);
var template_user = $('#mustache_authedusers').html();
Mustache.parse(template_user);
$("#discord-members").empty();
2017-04-22 08:35:30 +02:00
var discordmembercnt = 0;
for (var i = 0; i < guild_members_arr.length; i++) {
var roleobj = guild_members_arr[i];
if (!roleobj["name"]) {
roleobj["name"] = "Uncategorized";
}
2017-04-22 08:35:30 +02:00
var rendered_role = Mustache.render(template_role, {"name": roleobj["name"] + " - " + roleobj["members"].length});
discordmembercnt += roleobj["members"].length;
$("#discord-members").append(rendered_role);
for (var j = 0; j < roleobj.members.length; j++) {
var member = roleobj.members[j];
var rendered_user = Mustache.render(template_user, {"id": member.id.toString() + "d", "username": member.username, "avatar": member.avatar_url});
$("#discord-members").append(rendered_user);
2017-04-13 02:42:32 +02:00
$( "#discorduser-" + member.id.toString() + "d").click({"member_id": member.id.toString()}, function(event) {
mention_member(event.data.member_id);
});
if (member.color) {
$( "#discorduser-" + member.id.toString() + "d").css("color", "#" + member.color);
}
}
}
2017-04-22 08:35:30 +02:00
$("#discord-members-count").html(discordmembercnt);
2017-04-13 02:42:32 +02:00
}
2017-04-13 02:42:32 +02:00
function fill_authenticated_users(users) {
var template = $('#mustache_authedusers').html();
Mustache.parse(template);
$("#embed-discord-members").empty();
2017-04-22 08:35:30 +02:00
$("#embed-discord-members-count").html(users.length);
2017-04-13 02:42:32 +02:00
for (var i = 0; i < users.length; i++) {
var member = users[i];
var rendered = Mustache.render(template, {"id": member.id.toString() + "a", "username": member.username, "avatar": member.avatar_url});
$("#embed-discord-members").append(rendered);
$( "#discorduser-" + member.id.toString() + "a").click({"member_id": member.id.toString()}, function(event) {
mention_member(event.data.member_id);
});
2017-04-05 05:46:48 +02:00
}
2017-04-04 21:16:21 +02:00
}
2017-04-13 02:42:32 +02:00
function fill_unauthenticated_users(users) {
var template = $('#mustache_unauthedusers').html();
Mustache.parse(template);
$("#embed-unauth-users").empty();
2017-04-22 08:35:30 +02:00
$("#guest-members-count").html(users.length);
2017-04-13 02:42:32 +02:00
for (var i = 0; i < users.length; i++) {
var member = users[i];
var rendered = Mustache.render(template, {"username": member.username, "discriminator": member.discriminator});
$("#embed-unauth-users").append(rendered);
2017-04-08 23:53:58 +02:00
}
2017-04-13 02:42:32 +02:00
}
2017-04-13 02:42:32 +02:00
function wait_for_discord_login() {
_wait_for_discord_login(0);
}
2017-04-13 02:42:32 +02:00
function _wait_for_discord_login(index) {
setTimeout(function() {
var usr = create_authenticated_user();
2017-04-08 23:53:58 +02:00
usr.done(function(data) {
setVisitorMode(false);
2017-04-08 23:53:58 +02:00
initialize_embed();
2017-04-13 02:42:32 +02:00
return;
2017-04-08 23:53:58 +02:00
});
usr.fail(function(data) {
2017-04-13 02:42:32 +02:00
if (data.status == 403) {
2017-04-08 23:53:58 +02:00
Materialize.toast('Authentication error! You have been banned.', 10000);
setVisitorMode(true);
2017-04-13 02:42:32 +02:00
} else if (index < 10) {
_wait_for_discord_login(index + 1);
2017-04-08 23:53:58 +02:00
}
2017-04-13 02:42:32 +02:00
});
}, 5000);
}
2017-04-13 02:42:32 +02:00
function select_channel(channel_id) {
if (selected_channel != channel_id) {
selected_channel = channel_id;
last_message_id = null;
$("#channels-list > li.active").removeClass("active");
$("#channel-"+selected_channel).parent().addClass("active");
priority_query_guild = true;
2017-04-05 08:25:07 +02:00
clearTimeout(fetchtimeout);
run_fetch_routine();
2017-04-13 02:42:32 +02:00
}
}
2017-04-13 02:42:32 +02:00
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);
message.content = message.content.replace(new RegExp("<@!" + mention.id + ">", 'g'), "@" + mention.username + "#" + mention.discriminator);
message.content = message.content.replace("<@&" + guild_id + ">", "@everyone");
}
return message;
}
2017-04-13 02:42:32 +02:00
function getPosition(string, subString, index) {
return string.split(subString, index).join(subString).length;
}
2017-04-13 02:42:32 +02:00
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);
if (message.content.startsWith("(Titan Dev) ")) {
message.content = message.content.substring(usernamefield.length + 18);
} else {
message.content = message.content.substring(usernamefield.length + 7);
}
2017-04-13 02:42:32 +02:00
message.author.username = usernamefield.split("#")[0];
message.author.discriminator = usernamefield.split("#")[1];
} else if (message.author.bot && message.author.discriminator == "0000" && message.author.username.substring(message.author.username.length-5, message.author.username.length-4) == "#") {
var namestr = message.author.username;
if (message.content.startsWith("(Titan Dev) ")) {
message.author.username = "(Titan Dev) " + namestr.substring(0,namestr.length-5);
message.content = message.content.substring(11);
} else {
message.author.username = namestr.substring(0,namestr.length-5);
}
message.author.discriminator = namestr.substring(namestr.length-4);
2017-04-13 02:42:32 +02:00
}
return message;
}
2017-04-13 02:42:32 +02:00
function parse_message_time(message) {
var mome = moment(message.timestamp);
message.formatted_timestamp = mome.toDate().toString();
message.formatted_time = mome.calendar();
/*message.formatted_time = mome.format("h:mm A");*/
2017-04-13 02:42:32 +02:00
return message;
}
2017-04-13 02:42:32 +02:00
function parse_message_attachments(message) {
for (var i = 0; i < message.attachments.length; i++) {
var attach = "";
if (message.content.length != 0) {
attach = " ";
}
attach += message.attachments[i].url;
message.content += attach;
}
return message;
}
2017-04-13 02:42:32 +02:00
function handle_last_message_mention() {
var lastmsg = $("#chatcontent p:last-child");
var content = lastmsg.text().toLowerCase();
var username_discrim = current_username_discrim.toLowerCase();
2017-06-12 06:31:34 +02:00
if (content.includes("@everyone") || content.includes("@here") || content.includes("@" + username_discrim)) {
lastmsg.addClass( "mentioned" );
2017-04-13 02:42:32 +02:00
}
}
2017-04-13 02:42:32 +02:00
function escapeHtml(unsafe) { /* http://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript */
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
2017-04-13 02:42:32 +02:00
function nl2br (str, is_xhtml) { /* http://stackoverflow.com/questions/2919337/jquery-convert-line-breaks-to-br-nl2br-equivalent/ */
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
2017-04-13 02:42:32 +02:00
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
function parse_channels_in_message(message) {
var channelids = Object.keys(guild_channels);
for (var i = 0; i < channelids.length; i++) {
var pattern = "<#" + channelids[i] + ">";
message.content = message.content.replace(new RegExp(pattern, "g"), "#" + guild_channels[channelids[i]].channel.name);
}
return message;
}
2017-06-09 10:22:22 +02:00
function parse_emoji_in_message(message) {
var template = $('#mustache_message_emoji').html();
Mustache.parse(template);
for (var i = 0; i < emoji_store.length; i++) {
var emoji = emoji_store[i];
var emoji_format = "&lt;:" + emoji.name + ":" + emoji.id + "&gt;";
var rendered = Mustache.render(template, {"id": emoji.id, "name": emoji.name}).trim();
message.content = message.content.replaceAll(emoji_format, rendered);
2017-06-09 10:22:22 +02:00
}
return message;
}
2017-04-13 02:42:32 +02:00
function fill_discord_messages(messages, jumpscroll) {
if (messages.length == 0) {
return last_message_id;
}
var last = 0;
var template = $('#mustache_usermessage').html();
Mustache.parse(template);
for (var i = messages.length-1; i >= 0; i--) {
var message = messages[i];
message = replace_message_mentions(message);
message = format_bot_message(message);
message = parse_message_time(message);
message = parse_message_attachments(message);
message = parse_channels_in_message(message);
2017-06-09 10:22:22 +02:00
message.content = escapeHtml(message.content);
message = parse_emoji_in_message(message);
var rendered = Mustache.render(template, {"id": message.id, "full_timestamp": message.formatted_timestamp, "time": message.formatted_time, "username": message.author.username, "discriminator": message.author.discriminator, "content": nl2br(message.content)});
2017-04-13 02:42:32 +02:00
$("#chatcontent").append(rendered);
last = message.id;
handle_last_message_mention();
}
$("html, body").animate({ scrollTop: $(document).height() }, "slow");
$('#chatcontent').linkify({
target: "_blank"
2017-04-05 08:25:07 +02:00
});
2017-06-10 05:13:39 +02:00
$('.tooltipped').tooltip();
2017-04-13 02:42:32 +02:00
return last;
}
2017-04-13 02:42:32 +02:00
function run_fetch_routine() {
2017-04-15 09:14:09 +02:00
if (currently_fetching) {
return;
}
currently_fetching = true;
2017-04-26 09:30:36 +02:00
times_fetched += 1;
2017-04-13 02:42:32 +02:00
var channel_id = selected_channel;
var fet;
var jumpscroll;
2017-04-26 21:12:35 +02:00
$("#fetching-indicator").fadeIn(800);
2017-04-13 02:42:32 +02:00
if (last_message_id == null) {
$("#chatcontent").empty();
fet = fetch(channel_id);
jumpscroll = true;
} else {
fet = fetch(channel_id, last_message_id);
jumpscroll = element_in_view($('#discordmessage_'+last_message_id), true);
}
fet.done(function(data) {
var status = data.status;
if (visitor_mode) {
update_embed_userchip(false, null, "Titan", "0001", null);
} else {
update_embed_userchip(status.authenticated, status.avatar, status.username, status.user_id, status.discriminator);
}
2017-04-13 02:42:32 +02:00
last_message_id = fill_discord_messages(data.messages, jumpscroll);
if (!visitor_mode && status.manage_embed) {
2017-04-13 02:42:32 +02:00
$("#administrate_link").show();
} else {
$("#administrate_link").hide();
}
if (times_fetched % 10 == 0 || priority_query_guild) {
2017-04-25 08:52:46 +02:00
var guild = query_guild();
guild.done(function(guildobj) {
priority_query_guild = false;
2017-04-25 08:52:46 +02:00
fill_channels(guildobj.channels);
fill_discord_members(guildobj.discordmembers);
fill_authenticated_users(guildobj.embedmembers.authenticated);
fill_unauthenticated_users(guildobj.embedmembers.unauthenticated);
2017-05-24 05:41:12 +02:00
$("#instant-inv").attr("href", guildobj.instant_invite);
2017-04-25 08:52:46 +02:00
fetchtimeout = setTimeout(run_fetch_routine, 5000);
});
} else {
fetchtimeout = setTimeout(run_fetch_routine, 5000);
}
2017-04-05 08:25:07 +02:00
});
2017-04-13 02:42:32 +02:00
fet.fail(function(data) {
if (data.status == 403) {
$('#loginmodal').modal('open');
Materialize.toast('Authentication error! You have been disconnected by the server.', 10000);
} else if (data.status == 401) {
$('#loginmodal').modal('open');
Materialize.toast('Session expired! You have been logged out.', 10000);
2017-04-05 08:25:07 +02:00
}
if (data.status != 429) {
setVisitorMode(true);
if (visitor_mode) {
fetchtimeout = setTimeout(run_fetch_routine, 5000);
}
}
2017-04-05 08:25:07 +02:00
});
2017-04-13 02:42:32 +02:00
fet.catch(function(data) {
2017-04-13 21:06:21 +02:00
if (500 <= data.status && data.status < 600) {
if (fetch_error_count % 5 == 0) {
Materialize.toast('Fetching messages error! EndenDragon probably broke something. Sorry =(', 10000);
}
fetch_error_count += 1;
2017-04-13 02:42:32 +02:00
fetchtimeout = setTimeout(run_fetch_routine, 10000);
}
});
2017-04-15 09:14:09 +02:00
fet.always(function() {
currently_fetching = false;
2017-04-26 21:12:35 +02:00
$("#fetching-indicator").fadeOut(800);
2017-04-15 09:14:09 +02:00
});
2017-04-05 08:25:07 +02:00
}
2017-04-13 02:42:32 +02:00
function update_embed_userchip(authenticated, avatar, username, userid, discrim=null) {
if (authenticated) {
$("#currentuserimage").show();
$("#currentuserimage").attr("src", avatar);
$("#curuser_name").text(username);
$("#curuser_discrim").text("#" + discrim);
current_username_discrim = username + "#" + discrim;
2017-04-13 02:42:32 +02:00
} else {
$("#currentuserimage").hide();
$("#curuser_name").text(username);
$("#curuser_discrim").text("#" + userid);
current_username_discrim = username + "#" + userid;
2017-04-13 02:42:32 +02:00
}
}
2017-04-13 02:42:32 +02:00
$("#discordlogin_btn").click(function() {
lock_login_fields();
wait_for_discord_login();
});
2017-04-13 02:42:32 +02:00
$("#custom_username_field").keyup(function(event){
if (event.keyCode == 13) {
if (!(new RegExp(/^[a-z\d\-_\s]+$/i).test($(this).val()))) {
Materialize.toast('Illegal username provided! Only alphanumeric, spaces, dashes, and underscores allowed in usernames.', 10000);
return;
}
if($(this).val().length >= 2 && $(this).val().length <= 32) {
lock_login_fields();
var usr = create_unauthenticated_user($(this).val());
usr.done(function(data) {
setVisitorMode(false);
2017-04-13 02:42:32 +02:00
initialize_embed();
});
usr.fail(function(data) {
if (data.status == 429) {
Materialize.toast('Sorry! You are allowed to log in as a guest once every 15 minutes.', 10000);
} else if (data.status == 403) {
Materialize.toast('Authentication error! You have been banned.', 10000);
} else if (data.status == 406) {
Materialize.toast('Illegal username provided! Only alphanumeric, spaces, dashes, and underscores allowed in usernames.', 10000);
}
unlock_login_fields();
setVisitorMode(true);
2017-04-13 02:42:32 +02:00
});
}
}
});
2017-04-13 02:42:32 +02:00
$("#messagebox").keyup(function(event){
if ($(this).val().length == 1) {
$(this).val($.trim($(this).val()));
}
if(event.keyCode == 13 && $(this).val().length >= 1 && $(this).val().length <= 350) {
$(this).val($.trim($(this).val()));
$(this).blur();
$("#messagebox").attr('readonly', true);
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);
if (data.responseJSON) {
for (var i = 0; i < data.responseJSON.illegal_reasons.length; i++) {
Materialize.toast(data.responseJSON.illegal_reasons[i], 10000);
}
}
2017-04-13 02:42:32 +02:00
});
funct.catch(function(data) {
if (data.status == 429) {
Materialize.toast('You are sending messages too fast! 1 message per 10 seconds', 10000);
}
});
funct.always(function() {
$("#messagebox").attr('readonly', false);
});
}
});
2017-04-13 02:42:32 +02:00
$('#guild-btn').sideNav({
menuWidth: 300, // Default is 300
edge: 'left', // Choose the horizontal origin
closeOnClick: true, // Closes side-nav on <a> clicks, useful for Angular/Meteor
draggable: true // Choose whether you can drag to open on touch screens
}
);
2017-04-13 02:42:32 +02:00
$('#members-btn').sideNav({
menuWidth: 300, // Default is 300
edge: 'right', // Choose the horizontal origin
draggable: true // Choose whether you can drag to open on touch screens
}
);
// enter konami code into the embed page for some ponies action!
cheet('↑ ↑ ↓ ↓ ← → ← → b a', function () {
// basically copied and pasted of browser ponies bookmarklet
(function (srcs,cfg) { var cbcount = 1; var callback = function () { -- cbcount; if (cbcount === 0) { BrowserPonies.setBaseUrl(cfg.baseurl); if (!BrowserPoniesBaseConfig.loaded) { BrowserPonies.loadConfig(BrowserPoniesBaseConfig); BrowserPoniesBaseConfig.loaded = true; } BrowserPonies.loadConfig(cfg); if (!BrowserPonies.running()) BrowserPonies.start(); } }; if (typeof(BrowserPoniesConfig) === "undefined") { window.BrowserPoniesConfig = {}; } if (typeof(BrowserPoniesBaseConfig) === "undefined") { ++ cbcount; BrowserPoniesConfig.onbasecfg = callback; } if (typeof(BrowserPonies) === "undefined") { ++ cbcount; BrowserPoniesConfig.oninit = callback; } var node = (document.body || document.documentElement || document.getElementsByTagName('head')[0]); for (var id in srcs) { if (document.getElementById(id)) continue; if (node) { var s = document.createElement('script'); s.type = 'text/javascript'; s.id = id; s.src = srcs[id]; node.appendChild(s); } else { document.write('\u003cscript type="text/javscript" src="'+ srcs[id]+'" id="'+id+'"\u003e\u003c/script\u003e'); } } callback();})({"browser-ponies-script":"https://panzi.github.io/Browser-Ponies/browserponies.js","browser-ponies-config":"https://panzi.github.io/Browser-Ponies/basecfg.js"},{"baseurl":"https://panzi.github.io/Browser-Ponies/","fadeDuration":500,"volume":1,"fps":25,"speed":3,"audioEnabled":false,"showFps":false,"showLoadProgress":true,"speakProbability":0.1,"spawn":{"applejack":1,"fluttershy":1,"pinkie pie":1,"rainbow dash":1,"rarity":1,"twilight sparkle":1}});
});
2017-04-13 02:42:32 +02:00
})();