Skip to content

Commit ad99ab0

Browse files
rysieeritbh
andauthored
User flair in macros (#680)
* 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]>
1 parent d519a21 commit ad99ab0

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

extension/data/modules/config.js

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const self = new Module({
2323
let config = TBCore.config,
2424
sortReasons = [],
2525
subreddit,
26-
postFlairTemplates;
26+
postFlairTemplates,
27+
userFlairTemplates;
2728

2829
// With the following function we will create the UI when we need it.
2930
// Create the window overlay.
@@ -265,6 +266,14 @@ const self = new Module({
265266
<label><input type="checkbox" id="unbanuser">unban user</label>
266267
<label><input type="checkbox" id="muteuser">mute user</label>
267268
</div>
269+
<div class="tb-macro-actions-row">
270+
<h2 style="padding-right: 8px">User flair</h2>
271+
<input type="hidden" class="tb-input" name="user-flair-text" id="userflair-text" value="">
272+
<select name="user-flair-id" id="userflair-id-select" class="tb-action-button inline-button user-flair-picker">
273+
<option value="Select user flair" disabled>Select user flair template</option>
274+
<option value="">Don't touch</option>
275+
</select>
276+
</div>
268277
</div>
269278
<div class="tb-macro-context">
270279
<h2>Which context</h2>
@@ -384,6 +393,7 @@ const self = new Module({
384393
sortReasons = [];
385394
subreddit = null;
386395
postFlairTemplates = null;
396+
userFlairTemplates = null;
387397
});
388398

389399
// now we can play around!
@@ -651,6 +661,27 @@ const self = new Module({
651661
});
652662
}
653663

664+
async function addUserFlairTemplatesToDropdown ($dropdown, macroNum) {
665+
// Fetching the flair templates if not fetched already
666+
if (!userFlairTemplates) {
667+
userFlairTemplates = await TBApi.apiOauthGET(`/r/${subreddit}/api/user_flair_v2`).then(r => r.json());
668+
}
669+
670+
// We should only append user flair templates to the dropdown if they're not
671+
// already there, otherwise they'll duplicate with every click of the edit icon.
672+
if ($dropdown[0].childElementCount > 2) {
673+
return;
674+
}
675+
// Getting the current flair template for macro so we can set the `selected` attribute
676+
// on one of the `<option>`s. When adding a new macro we don't have one
677+
// selected yet, so this argument won't be provided.
678+
const defaultOption = macroNum ? config.modMacros[macroNum].userflair : '';
679+
680+
userFlairTemplates.forEach(flair => {
681+
$dropdown.append(`<option value="${flair.id}" ${flair.id === defaultOption ? 'selected' : ''}>${flair.text}</option>`);
682+
});
683+
}
684+
654685
// With this function we'll fetch the removal reasons for editing
655686
function removalReasonsContent () {
656687
if (config.removalReasons && config.removalReasons.reasons.length > 0) {
@@ -802,6 +833,14 @@ const self = new Module({
802833
<label><input type="checkbox" class="{{i}}-unbanuser" id="unbanuser">unban user</label>
803834
<label><input type="checkbox" class="{{i}}-muteuser" id="muteuser">mute user</label>
804835
</div>
836+
<div class="tb-macro-actions-row">
837+
<h2 style="padding-right: 8px;">User flair</h2>
838+
<input type="hidden" name="user-flair-text" class="tb-input {{i}}-user-flair-text" id="userflair-text" value="">
839+
<select name="user-flair-id" id="userflair-id-select" class="tb-action-button inline-button {{i}}-user-flair-picker">
840+
<option value="Select user flair" disabled>Select user flair template</option>
841+
<option value="">Don't touch</option>
842+
</select>
843+
</div>
805844
</div>
806845
<div class="tb-macro-context">
807846
<h2>Which context</h2>
@@ -830,6 +869,8 @@ const self = new Module({
830869
$(`.${i}-banuser`).prop('checked', macro.ban);
831870
$(`.${i}-unbanuser`).prop('checked', macro.unban);
832871
$(`.${i}-muteuser`).prop('checked', macro.mute);
872+
$(`.${i}-user-flair-picker`).val(macro.userflair);
873+
$(`.${i}-user-flair-text`).val(macro.userflairtext);
833874
$(`.${i}-removeitem`).prop('checked', macro.remove);
834875
$(`.${i}-spamitem`).prop('checked', macro.spam);
835876
$(`.${i}-approveitem`).prop('checked', macro.approve);
@@ -1269,6 +1310,18 @@ const self = new Module({
12691310
$flairCSS.val(flairTemplate.css_class);
12701311
});
12711312

1313+
// Watching for changes in the flair template dropdown and assigning the flair text and class
1314+
$body.on('change', '#userflair-id-select', function () {
1315+
const $this = $(this);
1316+
const selectedFlairID = $this.val();
1317+
1318+
const $flairText = $this.parents('.tb-macro-actions').find('input.tb-input[name="user-flair-text"]');
1319+
1320+
const flairTemplate = userFlairTemplates.find(flair => flair.id === selectedFlairID);
1321+
1322+
$flairText.val(flairTemplate?.text ?? '');
1323+
});
1324+
12721325
const resetForm = () => {
12731326
$body.find('#tb-add-removal-reason').show();
12741327
$body.find('#tb-add-removal-reason-form').hide();
@@ -1439,6 +1492,13 @@ const self = new Module({
14391492
const $this = $(this);
14401493

14411494
$this.closest('tr.mod-macro').find('.mod-macro-label').hide();
1495+
1496+
// Getting the flair dropdown
1497+
const macroNum = $this.attr('data-macro');
1498+
const $flairDropdown = $this.closest('.mod-macro').find('#userflair-id-select');
1499+
1500+
addUserFlairTemplatesToDropdown($flairDropdown, macroNum);
1501+
14421502
$this.closest('tr.mod-macro').find('.mod-macro-edit').show();
14431503
});
14441504

@@ -1455,6 +1515,8 @@ const self = new Module({
14551515
$macroContent.find('#banuser').prop('checked', macro.ban);
14561516
$macroContent.find('#unbanuser').prop('checked', macro.unban);
14571517
$macroContent.find('#muteuser').prop('checked', macro.mute);
1518+
$macroContent.find('#userflair-id-select').val(macro.userflair);
1519+
$macroContent.find('#userflair-text').val(macro.userflairtext);
14581520
$macroContent.find('#removeitem').prop('checked', macro.remove);
14591521
$macroContent.find('#approveitem').prop('checked', macro.approve);
14601522
// saved as lockthread for legacy reasons
@@ -1480,6 +1542,8 @@ const self = new Module({
14801542
banuser = $macroContent.find('#banuser').prop('checked'),
14811543
unbanuser = $macroContent.find('#unbanuser').prop('checked'),
14821544
muteuser = $macroContent.find('#muteuser').prop('checked'),
1545+
flairuser = $macroContent.find('#userflair-id-select').val(),
1546+
flairusertext = $macroContent.find('#userflair-text').val(),
14831547
removeitem = $macroContent.find('#removeitem').prop('checked'),
14841548
approveitem = $macroContent.find('#approveitem').prop('checked'),
14851549
lockitem = $macroContent.find('#lockitem').prop('checked'),
@@ -1510,6 +1574,8 @@ const self = new Module({
15101574
macro.ban = banuser;
15111575
macro.unban = unbanuser;
15121576
macro.mute = muteuser;
1577+
macro.userflair = flairuser;
1578+
macro.userflairtext = flairusertext;
15131579
macro.remove = removeitem;
15141580
macro.approve = approveitem;
15151581
// saved as lockthread for legacy reasons
@@ -1580,7 +1646,16 @@ const self = new Module({
15801646

15811647
// Adding a new macro
15821648
$body.on('click', '#tb-add-mod-macro', function () {
1583-
$(this).hide();
1649+
const $this = $(this);
1650+
1651+
$this.hide();
1652+
1653+
// Getting the flair dropdown
1654+
const $addMacroForm = $('#tb-add-mod-macro-form');
1655+
const $flairDropdown = $addMacroForm.find('select#userflair-id-select');
1656+
1657+
addUserFlairTemplatesToDropdown($flairDropdown);
1658+
15841659
$body.find('#tb-add-mod-macro-form').show();
15851660
});
15861661

@@ -1592,6 +1667,8 @@ const self = new Module({
15921667
banuser = $body.find('#banuser').prop('checked'),
15931668
unbanuser = $body.find('#unbanuser').prop('checked'),
15941669
muteuser = $body.find('#muteuser').prop('checked'),
1670+
flairuser = $body.find('#userflair-id-select').val(),
1671+
flairusertext = $body.find('#userflair-text').val(),
15951672
removeitem = $body.find('#removeitem').prop('checked'),
15961673
approveitem = $body.find('#approveitem').prop('checked'),
15971674
spamitem = $body.find('#spamitem').prop('checked'),
@@ -1621,6 +1698,8 @@ const self = new Module({
16211698
macro.ban = banuser;
16221699
macro.unban = unbanuser;
16231700
macro.mute = muteuser;
1701+
macro.userflair = flairuser;
1702+
macro.userflairtext = flairusertext;
16241703
macro.remove = removeitem;
16251704
macro.approve = approveitem;
16261705
macro.spam = spamitem;
@@ -1654,6 +1733,8 @@ const self = new Module({
16541733
$body.find('#banuser').prop('checked', false);
16551734
$body.find('#unbanuser').prop('checked', false);
16561735
$body.find('#muteuser').prop('checked', false);
1736+
$body.find('#userflair-id-select').val('');
1737+
$body.find('#userflair-text').val('');
16571738
$body.find('#removeitem').prop('checked', false);
16581739
$body.find('#approveitem').prop('checked', false);
16591740
$body.find('#spamitem').prop('checked', false);
@@ -1678,6 +1759,8 @@ const self = new Module({
16781759
$body.find('#banuser').prop('checked', false);
16791760
$body.find('#unbanuser').prop('checked', false);
16801761
$body.find('#muteuser').prop('checked', false);
1762+
$body.find('#userflair-id-select').val('');
1763+
$body.find('#userflair-text').val('');
16811764
$body.find('#removeitem').prop('checked', false);
16821765
$body.find('#approveitem').prop('checked', false);
16831766
$body.find('#lockitem').prop('checked', false);

extension/data/modules/macros.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ export default new Module({
248248
ban,
249249
unban,
250250
mute,
251+
userflair,
252+
userflairtext,
251253
lockthread: lockitem, // saved as lockthread for legacy reasons
252254
lockreply,
253255
sticky,
@@ -313,6 +315,10 @@ export default new Module({
313315
actionList += '<br>- This user will be unbanned';
314316
}
315317

318+
if (userflair) {
319+
actionList += `<br>- This user will be flaired with [ ${userflairtext} ]`;
320+
}
321+
316322
if (mute) {
317323
actionList += '<br>- This user will be muted';
318324
}
@@ -441,6 +447,12 @@ export default new Module({
441447
$body.find('.ThreadViewer .InfoBar__control:not(.m-on) .icon-mute').click();
442448
}
443449

450+
if (userflair) {
451+
TBApi.flairUser(info.author, info.subreddit, null, null, userflair).catch(() => {
452+
TBui.textFeedback(`error, failed to flair user (${userflair})`, TBui.FEEDBACK_NEGATIVE);
453+
});
454+
}
455+
444456
if (highlightmodmail) {
445457
$body.find('.ThreadViewer .ThreadViewerHeader__control:not(.m-selected) .icon-flair').click();
446458
}
@@ -537,6 +549,12 @@ export default new Module({
537549
banReason: `Muted from: ${info.permalink}`,
538550
});
539551
}
552+
553+
if (userflair) {
554+
TBApi.flairUser(info.author, info.subreddit, null, null, userflair).catch(() => {
555+
TBui.textFeedback(`error, failed to flair user (${userflair})`, TBui.FEEDBACK_NEGATIVE);
556+
});
557+
}
540558
}
541559
}
542560
});

0 commit comments

Comments
 (0)