Skip to content

Commit

Permalink
Merge pull request #163 from SpyTec/feat/nuke-lock
Browse files Browse the repository at this point in the history
feat(Nuke): Ability to lock instead of remove
  • Loading branch information
creesch committed Oct 11, 2019
2 parents 8117ec7 + 7ffb684 commit 90f333d
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions extension/data/modules/nukecomments.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ function nukecomments () {
title: 'Ignore distinguished comments from mods and admins when nuking a chain.',
});

self.register_setting('executionType', {
type: 'selector',
values: ['remove', 'lock'],
default: 'remove',
advanced: true,
title: 'Default nuke type selected when nuking',
});

// Settings for old reddit only
self.register_setting('showNextToUser', {
type: 'boolean',
Expand All @@ -36,7 +44,8 @@ function nukecomments () {
const $body = $('body');

const ignoreDistinguished = self.setting('ignoreDistinguished'),
showNextToUser = self.setting('showNextToUser');
showNextToUser = self.setting('showNextToUser'),
executionType = self.setting('executionType');

// Nuke button clicked
$body.on('click', '.tb-nuke-button', function (event) {
Expand Down Expand Up @@ -94,11 +103,14 @@ function nukecomments () {
const removalChainLength = removalChain.length;
// Distinguished chain
const distinguishedCommentsLength = distinguishedComments.length;

$popup.find('.tb-nuke-details').html(TBStorage.purify(`
<p>${removalChainLength + distinguishedCommentsLength} comments found (Already removed comments not included).</p>
<p>${distinguishedCommentsLength} distinguished comments found.</p>
<p><label><input type="checkbox" class="tb-ignore-distinguished-checkbox" ${ignoreDistinguished ? ' checked="checked"' : ''}>Ignore distinguished comments from mods and admins</label></p>
<p>
<label><input type="radio" value="remove" name="tb-execution-type-radio" class="tb-execution-type-radio" ${executionType === 'remove' ? ' checked="checked"' : ''}>Remove comments</label>
<label><input type="radio" value="lock" name="tb-execution-type-radio" class="tb-execution-type-radio" ${executionType === 'lock' ? ' checked="checked"' : ''}>Lock comments</label>
</p>
`));
$popup.find('.tb-execute-nuke').show();
});
Expand All @@ -109,43 +121,52 @@ function nukecomments () {
TB.ui.longLoadSpinner(true);
const $this = $(this);
$this.hide();
let removalArray;
let commentArray;
const $nukeFeedback = $popup.find('.tb-nuke-feedback');
const $nukeDetails = $popup.find('.tb-nuke-details');
const temptIgnoreDistinguished = $popup.find('.tb-ignore-distinguished-checkbox').prop('checked');
const executionType = $popup.find('.tb-execution-type-radio:checked').val();
if ($this.hasClass('tb-retry-nuke')) {
removalArray = missedComments;
commentArray = missedComments;
missedComments = [];
} else {
if (temptIgnoreDistinguished) {
removalArray = removalChain;
commentArray = removalChain;
} else {
removalArray = removalChain.concat(distinguishedComments);
commentArray = removalChain.concat(distinguishedComments);
}
}

$nukeFeedback.text('Removing comments.');
$nukeFeedback.text(`${executionType === 'remove' ? 'Removing' : 'Locking'} comments.`);
$nukeDetails.html('');

// Oldest comments first.
removalArray = TBUtils.saneSort(removalArray);
const removalArrayLength = removalArray.length;
commentArray = TBUtils.saneSort(commentArray);
const removalArrayLength = commentArray.length;
let removalCount = 0;
TBUtils.forEachChunkedRateLimit(removalArray, 20, comment => {
TBUtils.forEachChunkedRateLimit(commentArray, 20, comment => {
removalCount++;
TB.ui.textFeedback(`Removing comment ${removalCount}/${removalArrayLength}`, TB.ui.FEEDBACK_NEUTRAL);
TBUtils.removeThing(`t1_${comment}`, false, result => {
if (!result) {
missedComments.push(comment);
}
});
TB.ui.textFeedback(`${executionType === 'remove' ? 'Removing' : 'Locking'} comment ${removalCount}/${removalArrayLength}`, TB.ui.FEEDBACK_NEUTRAL);
if (executionType === 'remove') {
TBUtils.removeThing(`t1_${comment}`, false, result => {
if (!result) {
missedComments.push(comment);
}
});
} else if (executionType === 'lock') {
TBUtils.lock(`t1_${comment}`, result => {
if (!result) {
missedComments.push(comment);
}
});
}
}, () => {
removalRunning = false;
TB.ui.longLoadSpinner(false);
$nukeFeedback.text('Done removing comments.');
$nukeFeedback.text(`Done ${executionType === 'remove' ? 'removing' : 'locking'} comments.`);
const missedLength = missedComments.length;
if (missedLength) {
$nukeDetails.text(`${missedLength}: not removed because of API errors. Hit retry to attempt removing them again.`);
$nukeDetails.text(`${missedLength}: not ${executionType === 'remove' ? 'removed' : 'locked'} because of API errors. Hit retry to attempt removing them again.`);
$popup.find('.tb-retry-nuke').show;
} else {
setTimeout(() => {
Expand Down

0 comments on commit 90f333d

Please sign in to comment.