docbrown/app/javascript/packs/articleModerationTools.js
Jeremy Friesen bcfbd523d4
Moving from user.trusted tests to policy tests (#17619)
The [`ArticlePolicy#moderate?`][1] method answers the questions around article
moderation.

At present this change does not introduce any significant changes (aside
from the JS logic loop).  The next "step" is to adjust the
`ArticlePolicy#moderate?` method to test if the user has a moderator
role.

Further, we'd need to see what is available on the action panel.

Related to forem/forem#17606

[1]:5ac3216a5a/app/policies/article_policy.rb (L160-L174)
2022-05-11 08:18:13 -04:00

44 lines
1.6 KiB
JavaScript

/* global userData */
import { isModerationPage } from '@utilities/moderation';
getCsrfToken().then(() => {
const articleContainer = document.getElementById('article-show-container');
if (articleContainer) {
const user = userData();
const { authorId: articleAuthorId, path } = articleContainer.dataset;
const initializeModerationsTools = async () => {
const { initializeActionsPanel } = await import(
'../actionsPanel/initializeActionsPanelToggle'
);
const { initializeFlagUserModal } = await import('./flagUserModal');
// If the user can moderate an article give them access to this panel.
// Note: this assumes we're within the article context (which is the case
// given the articleContainer)
const canModerateArticles = user?.policies?.find(
(o) =>
o.dom_class === 'js-policy-article-moderate' && o.visible === true,
);
// article show page
if (canModerateArticles) {
// <2022-05-09 Mon> [@jeremyf] the user.id is an integer and
// articleAuthorId is a string so our logic is such that we always
// initializeActionsPanel and initializeFlagUserModal; I'm asking
// product to clarify if we want mods to boost their own posts.
if (user?.id !== articleAuthorId && !isModerationPage()) {
initializeActionsPanel(user, path);
initializeFlagUserModal(articleAuthorId);
// "/mod" page
} else if (isModerationPage()) {
initializeActionsPanel(user, path);
initializeFlagUserModal(articleAuthorId);
}
}
};
initializeModerationsTools();
}
});