Add ban tracking

- Add bans table
- Periodically refresh bans list every 1-1.25 hours
master
Dessa Simpson 2020-09-14 22:42:29 -07:00
parent 85b48f90f4
commit 4555dd6b7f
2 changed files with 26 additions and 2 deletions

3
db/50-bans.sql Normal file
View File

@ -0,0 +1,3 @@
CREATE TABLE bans (
userid integer
)

View File

@ -262,11 +262,32 @@ app.get("/", async (request, response) => {
//
//});
app.get("/streamer/auth", async ({}, response) => response.redirect(307, `https://id.twitch.tv/oauth2/authorize?client_id=${config.twitchClientId}&redirect_uri=${config.urlPrefix}/callback&response_type=code&scope=channel:read:subscriptions moderation:read`));
// Logout
app.get("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));
async function processBannedUsers() {
console.log("processBannedUsers run at " + new Date().toISOString());
var streamer = await db.query(queries.getStreamerIdToken).then((result: pg.QueryResult) => result.rows[0]);
if (streamer['tokenpair'] != null) {
var response = await twitch.apiRequest(streamer['tokenpair'],"/moderation/banned?broadcaster_id=" + streamer['userid']);
var dbconn = await db.connect();
try {
await dbconn.query('BEGIN');
for (var ban of response.data) {
if (ban.expires_at == '') dbconn.query(Object.assign(queries.insertBan,{ values: [ban.user_id] }))
}
await dbconn.query('COMMIT');
} catch (e) {
await dbconn.query('ROLLBACK');
} finally {
dbconn.release();
}
}
setTimeout(processBannedUsers,3600000+Math.floor(Math.random()*900000)) // Run every 1-1.25 hours to balance load
}
processBannedUsers();
app.listen(config.port, () => {
console.log(`Listening on port ${config.port}`);
});