-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
324 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# How to Update `piazza-link-bot` | ||
|
||
So you've got an outdated Piazza bot, huh? Well don't worry, here's how to quickly update your deployment! | ||
|
||
## Method A: Command Line | ||
|
||
This is the most straightforward method. It requires that you have the heroku CLI installed. | ||
|
||
1. Check to see if any new Environment variables were added in the update. If so, you should update these in your Heroku app's settings before proceeding. | ||
1. Clone the up-to-date `piazza-link-bot` repo to your local machine | ||
``` | ||
$ git clone https://github.com/BGR360/piazza-link-bot | ||
``` | ||
1. Force push the code directly to your Heroku app instance | ||
``` | ||
$ heroku login | ||
$ git remote add heroku https://git.heroku.com/YOUR-PIAZZA-LINK-BOT-APP-NAME.git | ||
$ git push -f heroku master | ||
``` | ||
|
||
## Method B: Fork and Browser | ||
|
||
If you really don't wanna install Heroku CLI, then you can try this method. | ||
|
||
1. Check to see if any new Environment variables were added in the update. If so, you should update these in your Heroku app's settings before proceeding. | ||
1. Fork this repo to your GitHub account. | ||
1. Go to your heroku dashboard and click on your piazza link bot app. | ||
1. Go to the "Deploy" menu. | ||
1. Under "Deployment method", select GitHub as the option. | ||
1. Grant Heroku permissions. | ||
1. Select your fork of the repo and use that to deploy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Slack API will retry messages if they take too long. | ||
* When a Heroku dyno is starting up, it typically receives | ||
* the same message 3 times due to this retry, causing the bot | ||
* to send 3 identical replies. This is a solution to this problem. | ||
* | ||
* SECURITY / PRIVACY NOTE: | ||
* None of the contents of messages are kept in memory longer than needed. | ||
* Only the unique timestamp and channel id of the message are stored | ||
* in the previousMessages array. | ||
*/ | ||
function RetryFilter (config) { | ||
this.config = config; | ||
this.previousMessages = []; | ||
|
||
this.addMessage = addMessage; | ||
this.filter = filter; | ||
this.isRetry = isRetry; | ||
|
||
/** | ||
* Adds a message to the memory and removes an old one if necessary. | ||
* Returns the message that was popped from the front, if it was, otherwise null. | ||
*/ | ||
function addMessage (message) { | ||
this.previousMessages.push({ | ||
ts: message.ts, | ||
channel: message.channel | ||
}); | ||
|
||
// Remove first item of array if array too big | ||
while (this.previousMessages.length > this.config.retryMemory) { | ||
let popped = this.previousMessages[0]; | ||
this.previousMessages.shift(); | ||
return popped; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Calls callback only if the message is not a repeat, | ||
* otherwise calls ifRetry | ||
*/ | ||
function filter (message, callback, ifRetry) { | ||
if (this.isRetry(message)) { | ||
if (ifRetry !== undefined) { | ||
ifRetry(message); | ||
} | ||
} else { | ||
callback(message); | ||
} | ||
} | ||
|
||
function isRetry (message) { | ||
if (this.previousMessages.length === 0) { | ||
return false; | ||
} | ||
|
||
// Search through to see if any of them have the same timestamp and channel number | ||
for (let i = 0; i < this.previousMessages.length; i++) { | ||
let prevMsg = this.previousMessages[i]; | ||
if (prevMsg.ts === message.ts && prevMsg.channel === message.channel) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
module.exports = RetryFilter; |
Oops, something went wrong.