Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

Commit

Permalink
commentLock implemented in betterbuttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
creesch committed May 30, 2019
1 parent eb4d122 commit ba627b2
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions extension/data/modules/betterbuttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function betterbuttons() {
'advanced': true,
'title': 'Add unsticky button to stickied posts.'
});
self.register_setting('addCommentLockbutton', {
type: 'boolean',
default: true,
advanced: false,
title: 'Add comment lock button to comments.',
});

// Bread and buttons
var $body = $('body');
Expand Down Expand Up @@ -378,6 +384,71 @@ function betterbuttons() {
});
};

self.initCommentLock = function () {
if (TBUtils.isModmail) {
return;
}
if (!TB.utils.isMod) {
return;
}

function commentLockRun () {
const $comments = $('div.comment:not(.tb-lock-button)');
TBUtils.forEachChunkedDynamic($comments, processComment);
}

function processComment (comment) {
const $comment = $(comment);
if (!$comment.hasClass('tb-lock-button')) {
// Add the class so we don't add buttons twice.
$comment.addClass('tb-lock-button');
let action = 'lock';
if ($comment.find('.locked-tagline').length > 0) {
action = 'unlock';
}

$comment.find('ul.buttons li.toggle:last')
.after(`<li><a href="javascript:;" tb-action="${action}" class="tb-comment-lock-button">${action}</a></li>`);
}
}

$('body').on('click', '.tb-comment-lock-button', async function (event) {
const $lockButton = $(event.target);

const action = $lockButton.attr('tb-action');
const info = TB.utils.getThingInfo(this, true);
const data = {
id: info.id,
};

try {
await TBUtils.apiOauthPOST(`api/${action}`, data);
let newAction;
if (action === 'lock') {
newAction = 'unlock';
const lockedTaglineHTML = '<span class="locked-tagline" title="locked by this subreddit\'s moderators">locked comment</span>';
$lockButton.closest('.entry').find('.tagline').append(lockedTaglineHTML);
} else {
newAction = 'lock';
$lockButton.closest('.entry').find('.locked-tagline').remove();
}

$lockButton.attr('tb-action', newAction);
$lockButton.text(newAction);
} catch (error) {
self.log(`Error toggling lock on comment: ${error}`);
}
});

// NER support.
window.addEventListener('TBNewThings', () => {
commentLockRun();
});

commentLockRun();
};


// Module init

self.init = function() {
Expand All @@ -404,6 +475,9 @@ function betterbuttons() {
if (self.setting('addStickyButton')) {
self.initStickyButtons();
}
if (self.setting('addCommentLockbutton')) {
self.initCommentLock();
}
};

TB.register_module(self);
Expand Down

0 comments on commit ba627b2

Please sign in to comment.