Skip to content

Commit

Permalink
Sync: automatically update whitelist/blacklist after first full sync …
Browse files Browse the repository at this point in the history
…and prevent users from performing full sync too often (close #1727)
  • Loading branch information
rafaelgomesxyz committed Jun 29, 2021
1 parent f739e76 commit 0151f9b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 64 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esgst",
"version": "8.8.8",
"betaVersion": "8.8.9-beta.14",
"betaVersion": "8.8.9-beta.15",
"title": "Enhanced SteamGifts & SteamTrades (ESGST)",
"description": "A script that adds some cool features to SteamGifts and SteamTrades.",
"author": "rafaelgomesxyz",
Expand Down
2 changes: 2 additions & 0 deletions src/class/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,9 @@ class _Settings {
lastBackup: 0,
lastSyncGroups: 0,
lastSyncWhitelist: 0,
lastPageWhitelist: 0,
lastSyncBlacklist: 0,
lastPageBlacklist: 0,
lastSyncSteamFriends: 0,
lastSyncHiddenGames: 0,
lastPageHiddenGames: 0,
Expand Down
19 changes: 6 additions & 13 deletions src/modules/Common.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -953,19 +953,6 @@ class Common extends Module {
name: 'Open automatic sync in a new tab.',
sg: true,
},
updateWhitelistBlacklist: {
description: () => (
<ul>
<li>
With this enabled, you no longer have to sync your whitelist/blacklist every time
you add/remove a user to/from those lists.
</li>
</ul>
),
name:
'Automatically update whitelist/blacklist when adding/removing a user to/from those lists.',
sg: true,
},
calculateDelete: {
name: 'Calculate and show data sizes when opening the delete menu.',
sg: true,
Expand Down Expand Up @@ -3188,6 +3175,12 @@ class Common extends Module {
}

updateWhitelistBlacklist(key, profile, event) {
if (
(key === 'whitelisted' && Settings.get('lastSyncWhitelist') === 0) ||
(key === 'blacklisted' && Settings.get('lastSyncBlacklist') === 0)
) {
return;
}
let user;
user = {
steamId: profile.steamId,
Expand Down
15 changes: 7 additions & 8 deletions src/modules/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { Logger } from '../class/Logger';
import { Shared } from '../class/Shared';

const checkUsernameChange = common.checkUsernameChange.bind(common),
saveUser = common.saveUser.bind(common),
updateWhitelistBlacklist = common.updateWhitelistBlacklist.bind(common);
saveUser = common.saveUser.bind(common);
class Profile extends Module {
constructor() {
super();
Expand All @@ -18,8 +17,8 @@ class Profile extends Module {

async init() {
if (
Settings.get('updateWhitelistBlacklist') &&
(Shared.esgst.whitelistPath || Shared.esgst.blacklistPath)
(Settings.get('lastSyncWhitelist') > 0 && Shared.esgst.whitelistPath) ||
(Settings.get('lastSyncBlacklist') > 0 && Shared.esgst.blacklistPath)
) {
const key = Shared.esgst.whitelistPath ? 'whitelisted' : 'blacklisted';
Shared.esgst.endlessFeatures.push(this.getUsers.bind(this, key));
Expand Down Expand Up @@ -144,18 +143,18 @@ class Profile extends Module {
'sidebar__shortcut__blacklist'
)[0];
if (profile.whitelistButton) {
if (Settings.get('updateWhitelistBlacklist')) {
if (Settings.get('lastSyncWhitelist') > 0) {
profile.whitelistButton.addEventListener(
'click',
updateWhitelistBlacklist.bind(common, 'whitelisted', profile)
common.updateWhitelistBlacklist.bind(common, 'whitelisted', profile)
);
}
}
if (profile.blacklistButton) {
if (Settings.get('updateWhitelistBlacklist')) {
if (Settings.get('lastSyncBlacklist') > 0) {
profile.blacklistButton.addEventListener(
'click',
updateWhitelistBlacklist.bind(common, 'blacklisted', profile)
common.updateWhitelistBlacklist.bind(common, 'blacklisted', profile)
);
}
}
Expand Down
116 changes: 74 additions & 42 deletions src/modules/Sync.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ async function setSync(isPopup = false, isSilent = false) {
{checkbox.checkbox} <span>{info.name}</span>
</div>
);
if (info.key !== 'HiddenGames') {
if (info.key !== 'HiddenGames' && info.key !== 'Whitelist' && info.key !== 'Blacklist') {
setAutoSync(info.key, info.name, syncer);
}
addNotificationBars(syncer, info);
Expand Down Expand Up @@ -449,6 +449,33 @@ async function sync(syncer) {
return;
}
}
if (
(syncer.parameters && (syncer.parameters.Whitelist || syncer.parameters.Blacklist)) ||
(!syncer.parameters && (Settings.get('syncWhitelist') || Settings.get('syncBlacklist')))
) {
if (
now - Settings.get('lastSyncWhitelist') > 2592000000 &&
now - Settings.get('lastSyncBlacklist') > 2592000000
) {
if (Settings.get('lastPageWhitelist') === 0 || Settings.get('lastPageBlacklist') === 0) {
const doContinue = await Shared.common.createConfirmationAsync(
`WARNING: You are going to sync your whitelist or blacklist. This is a process that may take a very long time, depending on how many users you have on your list, as the requests will be limited to 1 per second to avoid making a lot of requests to SteamGifts in a short period of time. Also keep in mind that this is a cumulative sync, which means that if you cancel the sync and sync again later, it will pick up from where it left off.${
Settings.get('lastSyncWhitelist') > 0 || Settings.get('lastSyncBlacklist') > 0
? ' Since you have already synced your whitelist or blacklist once before, you do not need to do it ever again, because ESGST detects when you add/remove a user to/from the list and automatically updates your data.'
: ''
} If you're sure you want to continue, click 'Yes', otherwise click 'No' and disable whitelist or blacklist sync. You can only sync your whitelist or blacklist once per 30 days.`
);
if (!doContinue) {
return;
}
}
} else {
Shared.common.createAlert(
'WARNING: You have synced your whitelist or blacklist in the last 30 days. Please disable it in order to continue.'
);
return;
}
}

syncer.canceled = false;

Expand Down Expand Up @@ -616,57 +643,58 @@ async function sync(syncer) {

// sync whitelist and blacklist
if (
(syncer.parameters && (syncer.parameters.Whitelist || syncer.parameters.Blacklist)) ||
(!syncer.parameters && (Settings.get('syncWhitelist') || Settings.get('syncBlacklist')))
(((syncer.parameters && syncer.parameters.Whitelist) ||
(!syncer.parameters && Settings.get('syncWhitelist'))) &&
(now - Settings.get('lastSyncWhitelist') > 2592000000 ||
Settings.get('lastPageWhitelist') > 0)) ||
(((syncer.parameters && syncer.parameters.Blacklist) ||
(!syncer.parameters && Settings.get('syncBlacklist'))) &&
(now - Settings.get('lastSyncBlacklist') > 2592000000 ||
Settings.get('lastPageBlacklist') > 0))
) {
if (
(syncer.parameters && syncer.parameters.Whitelist && syncer.parameters.Blacklist) ||
(!syncer.parameters && Settings.get('syncWhitelist') && Settings.get('syncBlacklist'))
) {
await Shared.common.deleteUserValues([
'whitelisted',
'whitelistedDate',
'blacklisted',
'blacklistedDate',
]);
syncer.users = [];
syncer.progressBar.setMessage('Syncing your whitelist...');
await syncWhitelistBlacklist(
'whitelisted',
syncer,
`https://www.steamgifts.com/account/manage/whitelist/search?page=`
);
syncer.progressBar.setMessage('Syncing your blacklist...');
await syncWhitelistBlacklist(
'blacklisted',
syncer,
`https://www.steamgifts.com/account/manage/blacklist/search?page=`
);
} else if (
(syncer.parameters && syncer.parameters.Whitelist) ||
(!syncer.parameters && Settings.get('syncWhitelist'))
) {
await Shared.common.deleteUserValues(['whitelisted', 'whitelistedDate']);
syncer.users = [];
if (Settings.get('lastPageWhitelist') === 0) {
await Shared.common.deleteUserValues(['whitelisted', 'whitelistedDate']);
}
syncer.progressBar.setMessage('Syncing your whitelist...');
await syncWhitelistBlacklist(
'whitelisted',
syncer,
`https://www.steamgifts.com/account/manage/whitelist/search?page=`
);
} else {
await Shared.common.deleteUserValues(['blacklisted', 'blacklistedDate']);
syncer.users = [];
if (!syncer.canceled) {
await Shared.common.setSetting('lastPageWhitelist', 0);
}
}

if (syncer.canceled) {
return;
}

if (
(syncer.parameters && syncer.parameters.Blacklist) ||
(!syncer.parameters && Settings.get('syncBlacklist'))
) {
if (Settings.get('lastPageBlacklist') === 0) {
await Shared.common.deleteUserValues(['blacklisted', 'blacklistedDate']);
}
syncer.progressBar.setMessage('Syncing your blacklist...');
await syncWhitelistBlacklist(
'blacklisted',
syncer,
`https://www.steamgifts.com/account/manage/blacklist/search?page=`
);
if (!syncer.canceled) {
await Shared.common.setSetting('lastPageBlacklist', 0);
}
}

if (!syncer.canceled) {
DOM.insert(syncer.results, 'beforeend', <div>Whitelist/blacklist synced.</div>);
}
syncer.progressBar.setMessage(`Saving your whitelist/blacklist (this may take a while)...`);
await Shared.common.saveUsers(syncer.users);
DOM.insert(syncer.results, 'beforeend', <div>Whitelist/blacklist synced.</div>);
}

// if sync has been canceled stop
Expand Down Expand Up @@ -725,11 +753,10 @@ async function sync(syncer) {

// sync hidden games
if (
(syncer.parameters && syncer.parameters.HiddenGames) ||
(!syncer.parameters &&
Settings.get('syncHiddenGames') &&
(now - Settings.get('lastSyncHiddenGames') > 2592000000 ||
Settings.get('lastPageHiddenGames') > 0))
((syncer.parameters && syncer.parameters.HiddenGames) ||
(!syncer.parameters && Settings.get('syncHiddenGames'))) &&
(now - Settings.get('lastSyncHiddenGames') > 2592000000 ||
Settings.get('lastPageHiddenGames') > 0)
) {
syncer.progressBar.setMessage('Syncing your hidden games...');
if (Settings.get('lastPageHiddenGames') === 0) {
Expand Down Expand Up @@ -1336,11 +1363,14 @@ async function syncNoCvGames() {
}

async function syncWhitelistBlacklist(key, syncer, url) {
let nextPage = 1;
const name = key === 'whitelisted' ? 'Whitelist' : 'Blacklist';
let nextPage = Settings.get(`lastPage${key}`) > 0 ? Settings.get(`lastPage${key}`) : 1;
let pagination = null;
do {
const users = [];
let elements, responseHtml;
responseHtml = (await FetchRequest.get(`${url}${nextPage}`, { queue: 100 })).html;
syncer.progressBar.setMessage(`Syncing your ${name.toLowerCase()} (page ${nextPage})...`);
responseHtml = (await FetchRequest.get(`${url}${nextPage}`, { queue: true })).html;
elements = responseHtml.getElementsByClassName('table__row-outer-wrap');
for (let i = 0, n = elements.length; i < n; ++i) {
let element, user;
Expand All @@ -1353,10 +1383,12 @@ async function syncWhitelistBlacklist(key, syncer, url) {
user.values[key] = true;
user.values[`${key}Date`] =
parseInt(element.querySelector(`[data-timestamp]`).getAttribute('data-timestamp')) * 1e3;
syncer.users.push(user);
users.push(user);
}
pagination = responseHtml.getElementsByClassName('pagination__navigation')[0];
nextPage += 1;
await Shared.common.saveUsers(users);
await Shared.common.setSetting(`lastPage${key}`, nextPage);
} while (
!syncer.canceled &&
pagination &&
Expand Down

0 comments on commit 0151f9b

Please sign in to comment.