Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MassUnban #3117

Merged
merged 9 commits into from May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/server.js
Expand Up @@ -12,7 +12,7 @@ function server(req, res) {

fs.exists(file, exists => {
if (!exists) {
request.get(`${process.env.PROD_CDN_ENDPOINT}${url}`).pipe(res);
request.get(`${process.env.PROD_CDN_ENDPOINT}${uri}`).pipe(res);
return;
}

Expand Down
33 changes: 14 additions & 19 deletions src/modules/chat_commands/index.js
@@ -1,4 +1,3 @@
const $ = require('jquery');
const moment = require('moment');
const twitch = require('../../utils/twitch');
const twitchAPI = require('../../utils/twitch-api');
Expand Down Expand Up @@ -72,33 +71,29 @@ function massUnban() {
function getBannedChatters() {
twitch.sendChatAdminMessage('Fetching banned users...');

$.get('/settings/channel').then(data => {
const users = [];

const $chatterList = $(data).find('#banned_chatter_list');
if ($chatterList.length) {
$chatterList.find('.ban .obj').each(function() {
const user = $(this).text().trim();
if (users.indexOf(user) === -1 && unbannedChatters.indexOf(user) === -1) {
users.push(user);
const query = `
query Settings_ChannelChat_BannedChatters {
currentUser {
bannedUsers {
bannedUser {
login
}
}
});
}
}
`;

twitchAPI.graphqlQuery(query).then(({data}) => {
const users = data.currentUser.bannedUsers.filter(({bannedUser: {login}}) => login && !unbannedChatters.includes(login));

if (users.length === 0) {
twitch.sendChatAdminMessage(`You have no more banned users. Total Unbanned Users: ${unbanCount}`);
twitch.sendChatAdminMessage(`You have no banned users. Total Unbanned Users: ${unbanCount}`);
return;
}

unbanCount += users.length;

twitch.sendChatAdminMessage('Starting purge process in 5 seconds.');
twitch.sendChatAdminMessage(`Starting purge of ${users.length} users in 5 seconds.`);
twitch.sendChatAdminMessage(`This block of users will take ${(users.length / 3).toFixed(1)} seconds to unban.`);

if (users.length > 70) {
twitch.sendChatAdminMessage('Twitch only provides up to 100 users at a time (some repeat), but this script will cycle through all of the blocks of users.');
}

setTimeout(() => (
unbanChatters(users, () => {
twitch.sendChatAdminMessage('This block of users has been purged. Checking for more..');
Expand Down
11 changes: 10 additions & 1 deletion src/utils/twitch-api.js
Expand Up @@ -2,14 +2,16 @@ const $ = require('jquery');
const querystring = require('querystring');

const API_ENDPOINT = 'https://api.twitch.tv/v5/';
const GQL_ENDPOINT = 'https://gql.twitch.tv/gql';
const CLIENT_ID = '6x8avioex0zt85ht6py4sq55z6avsea';

let accessToken;

function request(method, path, options = {}) {
const url = options.url || `${API_ENDPOINT}${path}${options.qs ? `?${querystring.stringify(options.qs)}` : ''}`;
return new Promise((resolve, reject) => {
$.ajax({
url: `${API_ENDPOINT}${path}${options.qs ? `?${querystring.stringify(options.qs)}` : ''}`,
url,
method,
dataType: 'json',
data: options.body ? JSON.stringify(options.body) : undefined,
Expand All @@ -32,6 +34,13 @@ module.exports = {
accessToken = newAccessToken;
},

graphqlQuery(query) {
return request('POST', null, {
url: GQL_ENDPOINT,
body: {query}
});
},

get(path, options) {
return request('GET', path, options);
},
Expand Down