From 23dcf93a30f948d30c678a96681f7fdefeba5171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ca=C5=82ka?= Date: Sun, 31 Jul 2022 22:27:43 +0200 Subject: [PATCH] fix: prevent starting a PD with users blocking them (#175) --- js/src/forum/extend/User.js | 8 ++------ js/src/forum/helpers/canStartPrivateDiscussion.js | 10 ++++++++++ js/src/forum/pages/PrivateDiscussionsUserPage.js | 3 ++- src/Discussion/Screener.php | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 js/src/forum/helpers/canStartPrivateDiscussion.js diff --git a/js/src/forum/extend/User.js b/js/src/forum/extend/User.js index 5efecac..9d03786 100644 --- a/js/src/forum/extend/User.js +++ b/js/src/forum/extend/User.js @@ -8,6 +8,7 @@ import Button from 'flarum/common/components/Button'; import ItemList from 'flarum/common/utils/ItemList'; import UserPage from 'flarum/forum/components/UserPage'; import LinkButton from 'flarum/common/components/LinkButton'; +import canStartPrivateDiscussion from '../helpers/canStartPrivateDiscussion'; export default () => { attributes(); @@ -17,12 +18,7 @@ export default () => { function message() { extend(UserControls, 'userControls', function (items, user) { - if ( - app.session.user && - app.session.user.id() !== user.id() && - app.forum.attribute('canStartPrivateDiscussion') && - (user.blocksPd() === false || (app.forum.attribute('canStartPrivateDiscussionWithBlockers') && user.cannotBeDirectMessaged())) - ) { + if (canStartPrivateDiscussion(user)) { items.add( 'private-discussion', Button.component( diff --git a/js/src/forum/helpers/canStartPrivateDiscussion.js b/js/src/forum/helpers/canStartPrivateDiscussion.js new file mode 100644 index 0000000..97c697f --- /dev/null +++ b/js/src/forum/helpers/canStartPrivateDiscussion.js @@ -0,0 +1,10 @@ +import app from 'flarum/forum/app'; + +export default function canStartPrivateDiscussion(recipient) { + return ( + app.session.user && + app.session.user.id() !== recipient.id() && + app.forum.attribute('canStartPrivateDiscussion') && + (!recipient.blocksPd() || (app.forum.attribute('canStartPrivateDiscussionWithBlockers') && recipient.cannotBeDirectMessaged())) + ); +} diff --git a/js/src/forum/pages/PrivateDiscussionsUserPage.js b/js/src/forum/pages/PrivateDiscussionsUserPage.js index 6ca382c..2d565b1 100644 --- a/js/src/forum/pages/PrivateDiscussionsUserPage.js +++ b/js/src/forum/pages/PrivateDiscussionsUserPage.js @@ -7,6 +7,7 @@ import ItemList from 'flarum/common/utils/ItemList'; import listItems from 'flarum/common/helpers/listItems'; import PrivateDiscussionList from './discussions/PrivateDiscussionList'; import PrivateComposing from './PrivateComposing'; +import canStartPrivateDiscussion from '../helpers/canStartPrivateDiscussion'; export default class PrivateDiscussionsUserPage extends UserPage { oninit(vnode) { @@ -57,7 +58,7 @@ export default class PrivateDiscussionsUserPage extends UserPage { const items = new ItemList(); - if (app.session.user && app.forum.attribute('canStartPrivateDiscussion')) { + if (canStartPrivateDiscussion(this.user)) { items.add('start_private', composing.component()); } diff --git a/src/Discussion/Screener.php b/src/Discussion/Screener.php index fc11d8d..26401e4 100644 --- a/src/Discussion/Screener.php +++ b/src/Discussion/Screener.php @@ -107,7 +107,7 @@ public function hasBlockingUsers(): bool { return $this->users ->first(function (User $user) { - return $user->getPreference('blocksPd', false); + return boolval($user->blocks_byobu_pd); }) !== null; }