Skip to content
Permalink
Browse files

Beautify using js-beautify to clean up code formatting.

  • Loading branch information...
robbyoconnor authored and Justin W. Flory committed Oct 10, 2017
1 parent 96eb2a2 commit 45c9ba7c254012c8382c7902731ef66b669a68e7
Showing with 814 additions and 779 deletions.
  1. +18 −0 .jsbeautifyrc
  2. +27 −25 config.js.example
  3. +58 −58 lib/MessageRateLimiter.js
  4. +461 −460 lib/TeleIrc.js
  5. +8 −8 lib/TeleIrcErrorCodes.js
  6. +3 −3 teleirc.js
  7. +239 −225 tests/IrcConfigValidationTests.js
@@ -0,0 +1,18 @@
{
"indent_size": 2,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 10,
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse,preserve-inline",
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
"break_chained_methods": false,
"eval_code": false,
"unescape_strings": false,
"wrap_line_length": 80
}
@@ -1,29 +1,31 @@
let settings = {
token: process.env.TELEIRC_TOKEN || "000000000:AAAAAAaAAa2AaAAaoAAAA-a_aaAAaAaaaAA",
ircBlacklist: process.env.IRC_BLACKLIST ?
process.env.IRC_BLACKLIST.split(",") : [],
irc: {
server: process.env.IRC_SERVER || "irc.freenode.net",
channel: process.env.IRC_CHANNEL || "",
botName: process.env.IRC_BOT_NAME || "teleirc",
sendStickerEmoji: process.env.IRC_SEND_STICKER_EMOJI === "true"|| true,
prefix: process.env.IRC_PREFIX || "<",
suffix: process.env.IRC_SUFFIX || ">",
showJoinMessage: process.env.IRC_SHOW_JOIN_MESSAGE === "true" || true,
showLeaveMessage: process.env.IRC_SHOW_LEAVE_MESSAGE === "true" || true,
},
tg: {
chatId: process.env.TELEGRAM_CHAT_ID || "-000000000",
showJoinMessage: process.env.SHOW_JOIN_MESSAGE === "true" || false,
showActionMessage: process.env.SHOW_ACTION_MESSAGE === "true" || true,
showLeaveMessage: process.env.SHOW_LEAVE_MESSAGE === "true" || false,
showKickMessage: process.env.SHOW_KICK_MESSAGE === "true" || false,
maxMessagesPerMinute: Number(process.env.MAX_MESSAGES_PER_MINUTE) || 20,
},
imgur: {
useImgurForImageLinks: process.env.USE_IMGUR_FOR_IMAGES === "true" || false,
imgurClientId: process.env.IMGUR_CLIENT_ID || "12345",
}
token: process.env.TELEIRC_TOKEN ||
"000000000:AAAAAAaAAa2AaAAaoAAAA-a_aaAAaAaaaAA",
ircBlacklist: process.env.IRC_BLACKLIST ?
process.env.IRC_BLACKLIST.split(",") : [],
irc: {
server: process.env.IRC_SERVER || "irc.freenode.net",
channel: process.env.IRC_CHANNEL || "",
botName: process.env.IRC_BOT_NAME || "teleirc",
sendStickerEmoji: process.env.IRC_SEND_STICKER_EMOJI === "true" || true,
prefix: process.env.IRC_PREFIX || "<",
suffix: process.env.IRC_SUFFIX || ">",
showJoinMessage: process.env.IRC_SHOW_JOIN_MESSAGE === "true" || true,
showLeaveMessage: process.env.IRC_SHOW_LEAVE_MESSAGE === "true" || true,
},
tg: {
chatId: process.env.TELEGRAM_CHAT_ID || "-000000000",
showJoinMessage: process.env.SHOW_JOIN_MESSAGE === "true" || false,
showActionMessage: process.env.SHOW_ACTION_MESSAGE === "true" || true,
showLeaveMessage: process.env.SHOW_LEAVE_MESSAGE === "true" || false,
showKickMessage: process.env.SHOW_KICK_MESSAGE === "true" || false,
maxMessagesPerMinute: Number(process.env.MAX_MESSAGES_PER_MINUTE) || 20,
},
imgur: {
useImgurForImageLinks: process.env.USE_IMGUR_FOR_IMAGES === "true" ||
false,
imgurClientId: process.env.IMGUR_CLIENT_ID || "12345",
}
}

module.exports = settings;
@@ -2,79 +2,79 @@

class MessageBundle {

constructor() {
this.queue = [];
}
constructor() {
this.queue = [];
}

addMessage(message) {
this.queue.push(message);
}
addMessage(message) {
this.queue.push(message);
}

implodeAndClear() {
let m = this.queue.join("\n");
this.queue = [];
return m;
}
implodeAndClear() {
let m = this.queue.join("\n");
this.queue = [];
return m;
}
}

class MessageRateLimiter {

/**
* rate: how many messages we can send...
* per: ...per this many seconds
* sendAction: function to send a message
*
* Providing a rate of "0" disables rate limiting.
*/
constructor(rate, per, sendAction) {
this.rate = rate;
this.per = per;
this.allowance = rate;
this.last_check = Date.now()/1000;
this.bundle = new MessageBundle();
this.sendAction = sendAction;
/**
* rate: how many messages we can send...
* per: ...per this many seconds
* sendAction: function to send a message
*
* Providing a rate of "0" disables rate limiting.
*/
constructor(rate, per, sendAction) {
this.rate = rate;
this.per = per;
this.allowance = rate;
this.last_check = Date.now() / 1000;
this.bundle = new MessageBundle();
this.sendAction = sendAction;

// We need to run periodically to make sure messages don't get stuck
// in the queue.
if (this.rate > 0) {
setInterval(this.run.bind(this), 2000);
}
// We need to run periodically to make sure messages don't get stuck
// in the queue.
if (this.rate > 0) {
setInterval(this.run.bind(this), 2000);
}
}

queueMessage(message) {
this.bundle.addMessage(message);
// We call run here just in case we can immediately send
// the message, instead of waiting for the setInterval to call
// run for us.
this.run();
}
queueMessage(message) {
this.bundle.addMessage(message);
// We call run here just in case we can immediately send
// the message, instead of waiting for the setInterval to call
// run for us.
this.run();
}

run() {
this.bumpAllowance();
run() {
this.bumpAllowance();

if (this.rate > 0 && this.allowance < 1) {
console.log("A message has been received and rate limited");
// Currently rate-limiting, so don't do anything.
} else {
if (this.bundle.queue.length > 0) {
this.sendAction(this.bundle.implodeAndClear());
this.allowance--;
}
}
if (this.rate > 0 && this.allowance < 1) {
console.log("A message has been received and rate limited");
// Currently rate-limiting, so don't do anything.
} else {
if (this.bundle.queue.length > 0) {
this.sendAction(this.bundle.implodeAndClear());
this.allowance--;
}
}
}

bumpAllowance() {
let current = Date.now()/1000;
let timePassed = current - this.last_check;
this.last_check = current;
this.allowance = this.allowance + (timePassed * this.rate/this.per);
bumpAllowance() {
let current = Date.now() / 1000;
let timePassed = current - this.last_check;
this.last_check = current;
this.allowance = this.allowance + (timePassed * this.rate / this.per);

// Make sure we don't get to an allowance that's higher than the
// rate we're actually allowed to send.
if (this.allowance > this.rate) {
this.allowance = this.rate;
}
// Make sure we don't get to an allowance that's higher than the
// rate we're actually allowed to send.
if (this.allowance > this.rate) {
this.allowance = this.rate;
}
}
}

module.exports = MessageRateLimiter;

0 comments on commit 45c9ba7

Please sign in to comment.
You can’t perform that action at this time.