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

Suggested removal reasons: proof-of-concept and discussion #451

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 extension/data/libs/yaml.js

Large diffs are not rendered by default.

162 changes: 162 additions & 0 deletions extension/data/modules/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ function tbconfig () {
`,
footer: '<input class="save-removal-sorting tb-action-button" type="button" value="Save removal reasons order">',
},
{
title: 'edit suggested removal reasons',
tooltip: 'Set up automatically suggested removal reasons for specific automoderator rules.',
content: `
<p>
Toolbox can suggest a removal reason based on your configured automoderator rules. It will attempt to extract all the valid report reasons
from your automoderator config, then add a button to instantly confirm the removal for posts that have a matching report attached.
</p>
<div id="tb-magic-removal-reasons-list">
</div>
`
},
{
title: 'edit mod macros',
tooltip: 'Edit and add your mod macros here.',
Expand Down Expand Up @@ -736,6 +748,126 @@ function tbconfig () {
}
}

// magic suggested removal reasons, neat
function magicRemovalReasonsEditContent (automodReasons, configuredRemovalReasons) {
const currentReasons = config.suggestedRemovalReasons || {};
config.suggestedRemovalReasons = currentReasons;
const $removalReasonsList = $body.find("#tb-magic-removal-reasons-list");

$removalReasonsList.empty();

for (const reason of automodReasons) {
const $el = $(/* html */`
<div class="magic-removal-reason">
<td>
<div class="magic-removal-reason-title">
<div class="left">
Items reported with "<code>${reason.reason}</code>"
</div>

<div class="right">
<a href="javascript:;" id="tb-toggle-automod-source" class="tb-general-button">
<i class="tb-icons">${TBui.icons.dotMenu}</i> Show AutoModerator rule
</a>
</div>
</div>

<div class="automod-source" style="display: none">
<b>AutoModerator rule:</b>
<pre><code>${reason.snippet}</code></pre>
</div>

<div class="magic-removal-reason-content">

</div>
</td>
</div>
`);

const $toggleButton = $el.find("#tb-toggle-automod-source");
const $toggleContent = $el.find(".automod-source");
const $content = $el.find(".magic-removal-reason-content");

function renderContent() {
const removal = currentReasons[reason.reasonRegex];

if (!removal) {
$content.html(/* html */`
<p>No suggested removal reason configured.</p>
<input class="set-magic-removal-reason tb-action-button" type="button" value="Configure suggested removal reason">
`);
} else {
let actions = [];
if (removal.comment) actions.push("leave a removal comment");
if (removal.dm) actions.push("send a removal DM");
if (removal.logSubmission) actions.push("log the removed submission to /r/" + removal.logSubmission.subreddit);

$content.html(/* html */`
<p>A suggested removal reason is configured to ${actions.join(", ")}.</p>
<input class="view-magic-removal-reason tb-action-button" type="button" value="View removal message">
<input class="set-magic-removal-reason tb-action-button" type="button" value="Update suggested removal reason">
<input class="remove-magic-removal-reason tb-action-button" type="button" value="Remove suggested removal reason">
`);
}
}

$el.on("click", ".set-magic-removal-reason", async () => {
// TODO
try {
const selected = await window.promptRemovalReason(
configuredRemovalReasons,
"Removal reasons for /r/" + subreddit + ":",
`Select the removal reason to be suggested for posts reported with <code>${reason.reason}</code>:`,
"all"
);

if (!selected) return;

currentReasons[reason.reasonRegex] = selected;

postToWiki('toolbox', config, 'added suggested removal reason', true);

renderContent();
} catch {
// nothing selected
return;
}
});

$el.on("click", ".view-magic-removal-reason", async () => {
const removal = currentReasons[reason.reasonRegex];
const message = removal.comment ? removal.comment.content : removal.dm.content;

// todo: ui?
prompt("Suggested Removal Reason Content", message);
});

$el.on("click", ".remove-magic-removal-reason", async () => {
delete currentReasons[reason.reasonRegex];

postToWiki('toolbox', config, 'removed suggested removal reason', true);

renderContent();
});

renderContent();

$toggleButton.on("click", e => {
e.preventDefault();

if ($toggleContent.is(":visible")) {
$toggleContent.hide();
$toggleButton.html(`<i class="tb-icons">${TBui.icons.dotMenu}</i> Show AutoModerator rule`);
} else {
$toggleContent.show();
$toggleButton.html(`<i class="tb-icons">${TBui.icons.close}</i> Hide AutoModerator rule`);
}
});

$removalReasonsList.append($el);
}
}

// Mod macros are also nice to have!
function modMacrosContent () {
if (config.modMacros && config.modMacros.length > 0) {
Expand Down Expand Up @@ -1388,6 +1520,36 @@ function tbconfig () {
$body.find('.tb-window-tabs .edit_removal_reasons').removeClass('content-populated');
});

// magic removal reasons clicked
$body.on('click', '.tb-window-tabs .edit_suggested_removal_reasons', async function () {
const $this = $(this);
$body.find('#tb-magic-removal-reasons-list').empty().text("Loading...");

// load the automoderator contents so we can determine removal reasons
let automodContents = await TBApi.readFromWiki(subreddit, "config/automoderator");
if (automodContents === TBCore.WIKI_PAGE_UNKNOWN || automodContents === TBCore.NO_WIKI_PAGE) {
automodContents = "";
}

// update config if needed
if ($body.hasClass('toolbox-wiki-edited')) {
await TBApi.readFromWiki(subreddit, 'toolbox', true).then(resp => {
if (!resp || resp === TBCore.WIKI_PAGE_UNKNOWN || resp === TBCore.NO_WIKI_PAGE) {
return;
}

config = resp;
});
}

// load removal reasons
const reasons = await TBHelpers.getRemovalReasonsSettings(subreddit);

magicRemovalReasonsEditContent(TBHelpers.parseAutomodReasons(automodContents), reasons);

$this.addClass('content-populated');
});

// Mod macros tab is clicked.
$body.on('click', '.tb-window-tabs .edit_mod_macros', function () {
const $this = $(this);
Expand Down
Loading