diff --git a/extension/data/modules/config.js b/extension/data/modules/config.js index 1cd603e31..fb8cbfbe1 100644 --- a/extension/data/modules/config.js +++ b/extension/data/modules/config.js @@ -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. @@ -265,6 +266,14 @@ const self = new Module({ +
+

User flair

+ + +

Which context

@@ -384,6 +393,7 @@ const self = new Module({ sortReasons = []; subreddit = null; postFlairTemplates = null; + userFlairTemplates = null; }); // now we can play around! @@ -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 ``); + }); + } + // With this function we'll fetch the removal reasons for editing function removalReasonsContent () { if (config.removalReasons && config.removalReasons.reasons.length > 0) { @@ -802,6 +833,14 @@ const self = new Module({
+
+

User flair

+ + +

Which context

@@ -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); @@ -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(); @@ -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(); }); @@ -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 @@ -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'), @@ -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 @@ -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(); }); @@ -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'), @@ -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; @@ -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); @@ -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); diff --git a/extension/data/modules/macros.js b/extension/data/modules/macros.js index c43a230bf..99f3a9db3 100644 --- a/extension/data/modules/macros.js +++ b/extension/data/modules/macros.js @@ -248,6 +248,8 @@ export default new Module({ ban, unban, mute, + userflair, + userflairtext, lockthread: lockitem, // saved as lockthread for legacy reasons lockreply, sticky, @@ -313,6 +315,10 @@ export default new Module({ actionList += '
- This user will be unbanned'; } + if (userflair) { + actionList += `
- This user will be flaired with [ ${userflairtext} ]`; + } + if (mute) { actionList += '
- This user will be muted'; } @@ -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(); } @@ -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); + }); + } } } });