Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ui/undiscord.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ <h3>Undiscord</h3>
Use the help link for more information.
</div>
</fieldset>
<fieldset>
<legend>
Retries on API returned an empty page
</legend>
<div class="input-wrapper">
<input id="emptyPageRetries" type="number" value="2" min="0" max="10">
</div>
</fieldset>
<hr>
<fieldset>
<legend>
Expand Down
22 changes: 18 additions & 4 deletions src/undiscord-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class UndiscordCore {
pattern: null, // Only delete messages that match the regex (insensitive)
searchDelay: null, // Delay each time we fetch for more messages
deleteDelay: null, // Delay between each delete operation
emptyPageRetries: 2, // Number of retries on API returned an empty page
maxAttempt: 2, // Attempts to delete a single message if it fails
askForConfirmation: true,
};
Expand All @@ -43,6 +44,7 @@ class UndiscordCore {
grandTotal: 0,
offset: 0,
iterations: 0,
emptyPageRetryCount: 0,

_seachResponse: null,
_messagesToDelete: [],
Expand Down Expand Up @@ -71,6 +73,7 @@ class UndiscordCore {
grandTotal: 0,
offset: 0,
iterations: 0,
emptyPageRetryCount: 0,

_seachResponse: null,
_messagesToDelete: [],
Expand Down Expand Up @@ -160,6 +163,7 @@ class UndiscordCore {
}

await this.deleteMessagesFromList();
this.state.emptyPageRetryCount = 0; // Reset retry count on successful deletion
}
else if (this.state._skippedMessages.length > 0) {
// There are stuff, but nothing to delete (example a page full of system messages)
Expand All @@ -168,12 +172,22 @@ class UndiscordCore {
this.state.offset += this.state._skippedMessages.length;
log.verb('There\'s nothing we can delete on this page, checking next page...');
log.verb(`Skipped ${this.state._skippedMessages.length} out of ${this.state._seachResponse.messages.length} in this page.`, `(Offset was ${oldOffset}, ajusted to ${this.state.offset})`);
this.state.emptyPageRetryCount = 0; // Reset retry count when we have messages but skip them
}
else {
log.verb('Ended because API returned an empty page.');
log.verb('[End state]', this.state);
if (isJob) break; // break without stopping if this is part of a job
this.state.running = false;
// Empty page - implement retry logic
this.state.emptyPageRetryCount++;
log.verb(`API returned an empty page. Retry ${this.state.emptyPageRetryCount}/${this.options.emptyPageRetries}`);

if (this.state.emptyPageRetryCount >= this.options.emptyPageRetries) {
log.verb('Ended because API returned empty pages after maximum retries.');
log.verb('[End state]', this.state);
if (isJob) break; // break without stopping if this is part of a job
this.state.running = false;
} else {
Comment on lines +182 to +187

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for what it's worth, if the retry count is 1 (which would imply it will retry at least once(?)), this will exit without retrying

perhaps a > instead of >= would fit the naming better ? or, renaming emptyPageRetries to maxEmptyPagesBeforeExiting or something similar would work with >=, where a value of 1 implies no need for a retry

log.verb(`Retrying empty page search in ${(this.options.searchDelay / 1000).toFixed(2)}s...`);
// Continue the loop to retry
}
}

// wait before next page (fix search page not updating fast enough)
Expand Down
6 changes: 6 additions & 0 deletions src/undiscord-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ function initUI() {
const v = parseInt(e.target.value);
if (v) undiscordCore.options.deleteDelay = v;
};
$('input#emptyPageRetries').onchange = (e) => {
const v = parseInt(e.target.value);
if (v) undiscordCore.options.emptyPageRetries = v;
};

$('input#searchDelay').addEventListener('input', (event) => {
$('div#searchDelayValue').textContent = event.target.value + 'ms';
Expand Down Expand Up @@ -268,6 +272,7 @@ async function startAction() {
//advanced
const searchDelay = parseInt($('input#searchDelay').value.trim());
const deleteDelay = parseInt($('input#deleteDelay').value.trim());
const emptyPageRetries = parseInt($('input#emptyPageRetries').value.trim());

// token
const authToken = $('input#token').value.trim() || fillToken();
Expand Down Expand Up @@ -296,6 +301,7 @@ async function startAction() {
pattern,
searchDelay,
deleteDelay,
emptyPageRetries,
// maxAttempt: 2,
};

Expand Down