From 4555dd6b7fe0bb07559489be4e68319528c48351 Mon Sep 17 00:00:00 2001 From: Dessa Simpson Date: Mon, 14 Sep 2020 22:42:29 -0700 Subject: [PATCH] Add ban tracking - Add bans table - Periodically refresh bans list every 1-1.25 hours --- db/50-bans.sql | 3 +++ src/app.ts | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 db/50-bans.sql diff --git a/db/50-bans.sql b/db/50-bans.sql new file mode 100644 index 0000000..42e7b68 --- /dev/null +++ b/db/50-bans.sql @@ -0,0 +1,3 @@ +CREATE TABLE bans ( + userid integer +) diff --git a/src/app.ts b/src/app.ts index 47effec..de650de 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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}`); });