Skip to content

Commit

Permalink
User flair in macros (#680)
Browse files Browse the repository at this point in the history
* Mod Macro add-on: flair user

* Mod Macro add-on: flair user

* Update extension/data/modules/macros.js

Co-authored-by: Erin <[email protected]>

* Mod Macro add-on: flair user, non-modmail action

Co-authored-by: Erin <[email protected]>
  • Loading branch information
rysie and eritbh authored Dec 4, 2022
1 parent 73f38ac commit 792b189
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
87 changes: 85 additions & 2 deletions extension/data/modules/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const self = new Module({
let config = TBCore.config,
sortReasons = [],
subreddit,
postFlairTemplates;
postFlairTemplates,
userFlairTemplates;

// With the following function we will create the UI when we need it.
// Create the window overlay.
Expand Down Expand Up @@ -265,6 +266,14 @@ const self = new Module({
<label><input type="checkbox" id="unbanuser">unban user</label>
<label><input type="checkbox" id="muteuser">mute user</label>
</div>
<div class="tb-macro-actions-row">
<h2 style="padding-right: 8px">User flair</h2>
<input type="hidden" class="tb-input" name="user-flair-text" id="userflair-text" value="">
<select name="user-flair-id" id="userflair-id-select" class="tb-action-button inline-button user-flair-picker">
<option value="Select user flair" disabled>Select user flair template</option>
<option value="">Don't touch</option>
</select>
</div>
</div>
<div class="tb-macro-context">
<h2>Which context</h2>
Expand Down Expand Up @@ -384,6 +393,7 @@ const self = new Module({
sortReasons = [];
subreddit = null;
postFlairTemplates = null;
userFlairTemplates = null;
});

// now we can play around!
Expand Down Expand Up @@ -651,6 +661,27 @@ const self = new Module({
});
}

async function addUserFlairTemplatesToDropdown ($dropdown, macroNum) {
// Fetching the flair templates if not fetched already
if (!userFlairTemplates) {
userFlairTemplates = await TBApi.apiOauthGET(`/r/${subreddit}/api/user_flair_v2`).then(r => r.json());
}

// We should only append user flair templates to the dropdown if they're not
// already there, otherwise they'll duplicate with every click of the edit icon.
if ($dropdown[0].childElementCount > 2) {
return;
}
// Getting the current flair template for macro so we can set the `selected` attribute
// on one of the `<option>`s. When adding a new macro we don't have one
// selected yet, so this argument won't be provided.
const defaultOption = macroNum ? config.modMacros[macroNum].userflair : '';

userFlairTemplates.forEach(flair => {
$dropdown.append(`<option value="${flair.id}" ${flair.id === defaultOption ? 'selected' : ''}>${flair.text}</option>`);
});
}

// With this function we'll fetch the removal reasons for editing
function removalReasonsContent () {
if (config.removalReasons && config.removalReasons.reasons.length > 0) {
Expand Down Expand Up @@ -802,6 +833,14 @@ const self = new Module({
<label><input type="checkbox" class="{{i}}-unbanuser" id="unbanuser">unban user</label>
<label><input type="checkbox" class="{{i}}-muteuser" id="muteuser">mute user</label>
</div>
<div class="tb-macro-actions-row">
<h2 style="padding-right: 8px;">User flair</h2>
<input type="hidden" name="user-flair-text" class="tb-input {{i}}-user-flair-text" id="userflair-text" value="">
<select name="user-flair-id" id="userflair-id-select" class="tb-action-button inline-button {{i}}-user-flair-picker">
<option value="Select user flair" disabled>Select user flair template</option>
<option value="">Don't touch</option>
</select>
</div>
</div>
<div class="tb-macro-context">
<h2>Which context</h2>
Expand Down Expand Up @@ -830,6 +869,8 @@ const self = new Module({
$(`.${i}-banuser`).prop('checked', macro.ban);
$(`.${i}-unbanuser`).prop('checked', macro.unban);
$(`.${i}-muteuser`).prop('checked', macro.mute);
$(`.${i}-user-flair-picker`).val(macro.userflair);
$(`.${i}-user-flair-text`).val(macro.userflairtext);
$(`.${i}-removeitem`).prop('checked', macro.remove);
$(`.${i}-spamitem`).prop('checked', macro.spam);
$(`.${i}-approveitem`).prop('checked', macro.approve);
Expand Down Expand Up @@ -1269,6 +1310,18 @@ const self = new Module({
$flairCSS.val(flairTemplate.css_class);
});

// Watching for changes in the flair template dropdown and assigning the flair text and class
$body.on('change', '#userflair-id-select', function () {
const $this = $(this);
const selectedFlairID = $this.val();

const $flairText = $this.parents('.tb-macro-actions').find('input.tb-input[name="user-flair-text"]');

const flairTemplate = userFlairTemplates.find(flair => flair.id === selectedFlairID);

$flairText.val(flairTemplate?.text ?? '');
});

const resetForm = () => {
$body.find('#tb-add-removal-reason').show();
$body.find('#tb-add-removal-reason-form').hide();
Expand Down Expand Up @@ -1439,6 +1492,13 @@ const self = new Module({
const $this = $(this);

$this.closest('tr.mod-macro').find('.mod-macro-label').hide();

// Getting the flair dropdown
const macroNum = $this.attr('data-macro');
const $flairDropdown = $this.closest('.mod-macro').find('#userflair-id-select');

addUserFlairTemplatesToDropdown($flairDropdown, macroNum);

$this.closest('tr.mod-macro').find('.mod-macro-edit').show();
});

Expand All @@ -1455,6 +1515,8 @@ const self = new Module({
$macroContent.find('#banuser').prop('checked', macro.ban);
$macroContent.find('#unbanuser').prop('checked', macro.unban);
$macroContent.find('#muteuser').prop('checked', macro.mute);
$macroContent.find('#userflair-id-select').val(macro.userflair);
$macroContent.find('#userflair-text').val(macro.userflairtext);
$macroContent.find('#removeitem').prop('checked', macro.remove);
$macroContent.find('#approveitem').prop('checked', macro.approve);
// saved as lockthread for legacy reasons
Expand All @@ -1480,6 +1542,8 @@ const self = new Module({
banuser = $macroContent.find('#banuser').prop('checked'),
unbanuser = $macroContent.find('#unbanuser').prop('checked'),
muteuser = $macroContent.find('#muteuser').prop('checked'),
flairuser = $macroContent.find('#userflair-id-select').val(),
flairusertext = $macroContent.find('#userflair-text').val(),
removeitem = $macroContent.find('#removeitem').prop('checked'),
approveitem = $macroContent.find('#approveitem').prop('checked'),
lockitem = $macroContent.find('#lockitem').prop('checked'),
Expand Down Expand Up @@ -1510,6 +1574,8 @@ const self = new Module({
macro.ban = banuser;
macro.unban = unbanuser;
macro.mute = muteuser;
macro.userflair = flairuser;
macro.userflairtext = flairusertext;
macro.remove = removeitem;
macro.approve = approveitem;
// saved as lockthread for legacy reasons
Expand Down Expand Up @@ -1580,7 +1646,16 @@ const self = new Module({

// Adding a new macro
$body.on('click', '#tb-add-mod-macro', function () {
$(this).hide();
const $this = $(this);

$this.hide();

// Getting the flair dropdown
const $addMacroForm = $('#tb-add-mod-macro-form');
const $flairDropdown = $addMacroForm.find('select#userflair-id-select');

addUserFlairTemplatesToDropdown($flairDropdown);

$body.find('#tb-add-mod-macro-form').show();
});

Expand All @@ -1592,6 +1667,8 @@ const self = new Module({
banuser = $body.find('#banuser').prop('checked'),
unbanuser = $body.find('#unbanuser').prop('checked'),
muteuser = $body.find('#muteuser').prop('checked'),
flairuser = $body.find('#userflair-id-select').val(),
flairusertext = $body.find('#userflair-text').val(),
removeitem = $body.find('#removeitem').prop('checked'),
approveitem = $body.find('#approveitem').prop('checked'),
spamitem = $body.find('#spamitem').prop('checked'),
Expand Down Expand Up @@ -1621,6 +1698,8 @@ const self = new Module({
macro.ban = banuser;
macro.unban = unbanuser;
macro.mute = muteuser;
macro.userflair = flairuser;
macro.userflairtext = flairusertext;
macro.remove = removeitem;
macro.approve = approveitem;
macro.spam = spamitem;
Expand Down Expand Up @@ -1654,6 +1733,8 @@ const self = new Module({
$body.find('#banuser').prop('checked', false);
$body.find('#unbanuser').prop('checked', false);
$body.find('#muteuser').prop('checked', false);
$body.find('#userflair-id-select').val('');
$body.find('#userflair-text').val('');
$body.find('#removeitem').prop('checked', false);
$body.find('#approveitem').prop('checked', false);
$body.find('#spamitem').prop('checked', false);
Expand All @@ -1678,6 +1759,8 @@ const self = new Module({
$body.find('#banuser').prop('checked', false);
$body.find('#unbanuser').prop('checked', false);
$body.find('#muteuser').prop('checked', false);
$body.find('#userflair-id-select').val('');
$body.find('#userflair-text').val('');
$body.find('#removeitem').prop('checked', false);
$body.find('#approveitem').prop('checked', false);
$body.find('#lockitem').prop('checked', false);
Expand Down
18 changes: 18 additions & 0 deletions extension/data/modules/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ export default new Module({
ban,
unban,
mute,
userflair,
userflairtext,
lockthread: lockitem, // saved as lockthread for legacy reasons
lockreply,
sticky,
Expand Down Expand Up @@ -313,6 +315,10 @@ export default new Module({
actionList += '<br>- This user will be unbanned';
}

if (userflair) {
actionList += `<br>- This user will be flaired with [ ${userflairtext} ]`;
}

if (mute) {
actionList += '<br>- This user will be muted';
}
Expand Down Expand Up @@ -441,6 +447,12 @@ export default new Module({
$body.find('.ThreadViewer .InfoBar__control:not(.m-on) .icon-mute').click();
}

if (userflair) {
TBApi.flairUser(info.author, info.subreddit, null, null, userflair).catch(() => {
TBui.textFeedback(`error, failed to flair user (${userflair})`, TBui.FEEDBACK_NEGATIVE);
});
}

if (highlightmodmail) {
$body.find('.ThreadViewer .ThreadViewerHeader__control:not(.m-selected) .icon-flair').click();
}
Expand Down Expand Up @@ -537,6 +549,12 @@ export default new Module({
banReason: `Muted from: ${info.permalink}`,
});
}

if (userflair) {
TBApi.flairUser(info.author, info.subreddit, null, null, userflair).catch(() => {
TBui.textFeedback(`error, failed to flair user (${userflair})`, TBui.FEEDBACK_NEGATIVE);
});
}
}
}
});
Expand Down

0 comments on commit 792b189

Please sign in to comment.