Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion files/lib/form/ConversationAddForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use wcf\data\conversation\ConversationAction;
use wcf\data\IStorableObject;
use wcf\data\user\group\UserGroup;
use wcf\data\user\UserProfile;
use wcf\system\cache\builder\UserGroupCacheBuilder;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\conversation\TConversationForm;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\NamedUserException;
use wcf\system\flood\FloodControl;
use wcf\system\form\builder\container\FormContainer;
use wcf\system\form\builder\container\wysiwyg\WysiwygFormContainer;
Expand Down Expand Up @@ -59,6 +62,8 @@ class ConversationAddForm extends AbstractFormBuilderForm
*/
public $objectActionClass = ConversationAction::class;

protected ?UserProfile $user = null;

/**
* @inheritDoc
*/
Expand All @@ -70,6 +75,26 @@ public function readData()
PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.conversation.ConversationList');
}

#[\Override]
public function readParameters()
{
parent::readParameters();

if (isset($_REQUEST['userID'])) {
$userID = \intval($_REQUEST['userID']);
$this->user = UserProfileRuntimeCache::getInstance()->getObject($userID);
if ($this->user === null || $this->user->userID === WCF::getUser()->userID) {
throw new IllegalLinkException();
}

$error = $this->isValidParticipant($this->user);
if ($error !== null) {
throw new NamedUserException($error->getMessage());
}
}
}


#[\Override]
public function createForm()
{
Expand Down Expand Up @@ -102,7 +127,8 @@ static function (UserGroup $group) {
->description('wcf.conversation.participants.description')
->maximumMultiples(WCF::getSession()->getPermission('user.conversation.maxParticipants'))
->addValidator($this->getParticipantsValidator())
->addValidator($this->getMaximumParticipantsValidator()),
->addValidator($this->getMaximumParticipantsValidator())
->value($this->user ? [$this->user->userID] : []),
BooleanFormField::create('addGroupParticipants')
->label('wcf.conversation.addGroupParticipants')
->available(\count($groupParticipants) > 0),
Expand Down
165 changes: 76 additions & 89 deletions files/lib/system/conversation/TConversationForm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace wcf\system\conversation;

use wcf\data\user\ignore\UserIgnore;
use wcf\data\user\UserProfile;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\form\builder\field\BooleanFormField;
use wcf\system\form\builder\field\MultipleSelectionFormField;
Expand Down Expand Up @@ -56,106 +57,92 @@ protected function getUserByGroups(array $groupIDs): array
*/
protected function getParticipantsValidator(): FormFieldValidator
{
return new FormFieldValidator('participantsValidator', static function (UserFormField $formField) {
return new FormFieldValidator('participantsValidator', function (UserFormField $formField) {
$users = $formField->getUsers();
$userIDs = \array_column($users, 'userID');

UserStorageHandler::getInstance()->loadStorage($userIDs);

foreach ($users as $user) {
if ($user->userID === WCF::getUser()->userID) {
$formField->addValidationError(
new FormFieldValidationError(
'isAuthor',
'wcf.conversation.participants.error.isAuthor'
)
);

continue;
$error = $this->isValidParticipant($user);
if ($error !== null) {
$formField->addValidationError($error);
}
}
});
}

// check participant's settings and permissions
if (!$user->getPermission('user.conversation.canUseConversation')) {
$formField->addValidationError(
new FormFieldValidationError(
'canNotUseConversation',
'wcf.conversation.participants.error.canNotUseConversation',
[
'username' => $user->username,
]
)
);
protected function isValidParticipant(UserProfile $user): ?FormFieldValidationError
{
if ($user->userID === WCF::getUser()->userID) {
return new FormFieldValidationError(
'isAuthor',
'wcf.conversation.participants.error.isAuthor'
);
}

continue;
}
// check participant's settings and permissions
if (!$user->getPermission('user.conversation.canUseConversation')) {
return new FormFieldValidationError(
'canNotUseConversation',
'wcf.conversation.participants.error.canNotUseConversation',
[
'username' => $user->username,
]
);
}

if (!WCF::getSession()->getPermission('user.profile.cannotBeIgnored')) {
// check if user wants to receive any conversations
/** @noinspection PhpUndefinedFieldInspection */
if ($user->canSendConversation == 2) {
$formField->addValidationError(
new FormFieldValidationError(
'doesNotAcceptConversation',
'wcf.conversation.participants.error.doesNotAcceptConversation',
[
'username' => $user->username,
]
)
);

continue;
}

// check if user only wants to receive conversations by
// users they are following and if the active user is followed
// by the relevant user
/** @noinspection PhpUndefinedFieldInspection */
if ($user->canSendConversation == 1 && !$user->isFollowing(WCF::getUser()->userID)) {
$formField->addValidationError(
new FormFieldValidationError(
'doesNotAcceptConversation',
'wcf.conversation.participants.error.doesNotAcceptConversation',
[
'username' => $user->username,
]
)
);

continue;
}

// active user is ignored by participant
if ($user->isIgnoredUser(WCF::getUser()->userID, UserIgnore::TYPE_BLOCK_DIRECT_CONTACT)) {
$formField->addValidationError(
new FormFieldValidationError(
'ignoresYou',
'wcf.conversation.participants.error.ignoresYou',
[
'username' => $user->username,
]
)
);

continue;
}

// check participant's mailbox quota
if (ConversationHandler::getInstance()->getConversationCount($user->userID) >= $user->getPermission('user.conversation.maxConversations')) {
$formField->addValidationError(
new FormFieldValidationError(
'mailboxIsFull',
'wcf.conversation.participants.error.mailboxIsFull',
[
'username' => $user->username,
]
)
);

continue;
}
}
if (!WCF::getSession()->getPermission('user.profile.cannotBeIgnored')) {
// check if user wants to receive any conversations
/** @noinspection PhpUndefinedFieldInspection */
if ($user->canSendConversation == 2) {
return new FormFieldValidationError(
'doesNotAcceptConversation',
'wcf.conversation.participants.error.doesNotAcceptConversation',
[
'username' => $user->username,
]
);
}
});

// check if user only wants to receive conversations by
// users they are following and if the active user is followed
// by the relevant user
/** @noinspection PhpUndefinedFieldInspection */
if ($user->canSendConversation == 1 && !$user->isFollowing(WCF::getUser()->userID)) {
return new FormFieldValidationError(
'doesNotAcceptConversation',
'wcf.conversation.participants.error.doesNotAcceptConversation',
[
'username' => $user->username,
]
);
}

// active user is ignored by participant
if ($user->isIgnoredUser(WCF::getUser()->userID, UserIgnore::TYPE_BLOCK_DIRECT_CONTACT)) {
return new FormFieldValidationError(
'ignoresYou',
'wcf.conversation.participants.error.ignoresYou',
[
'username' => $user->username,
]
);
}

// check participant's mailbox quota
if (ConversationHandler::getInstance()->getConversationCount($user->userID) >= $user->getPermission('user.conversation.maxConversations')) {
return new FormFieldValidationError(
'mailboxIsFull',
'wcf.conversation.participants.error.mailboxIsFull',
[
'username' => $user->username,
]
);
}
}

return null;
}

/**
Expand Down