/**
  * @param User $sender
  * @param array $usersByRestrictions
  * @return array
  */
 private function prepareRecipients(User $sender, array $usersByRestrictions)
 {
     if (empty($usersByRestrictions)) {
         return [];
     }
     unset($usersByRestrictions['suspendedUsers'][$sender->getId()], $usersByRestrictions['activeUsers'][$sender->getId()]);
     if (!$this->authorizator->isAllowed($sender, 'message', 'send_to_restricted_recipients')) {
         $recipients = array_diff_key($usersByRestrictions['activeUsers'], $usersByRestrictions['suspendedUsers'], $usersByRestrictions['usersBlockedByMe'], $usersByRestrictions['usersBlockingMe']);
     } else {
         $recipients = $usersByRestrictions['activeUsers'] + $usersByRestrictions['usersBlockedByMe'] + $usersByRestrictions['usersBlockingMe'];
     }
     return Arrays::associate($recipients, 'id=username');
 }
 /**
  * @param $id
  * @param string $messageType ..Domain\Entities\IMessage::SENT or ..Domain\Entities\IMessage:RECEIVED
  * @param User|null $recipient
  * @return IMessage|null
  * @throws MessageTypeException
  */
 public function readMessage($id, $messageType, User $recipient = null)
 {
     $message = null;
     if ($messageType === IMessage::SENT) {
         $message = $this->messagesReader->getSentMessage($id);
     } elseif ($messageType === IMessage::RECEIVED) {
         $message = $this->messagesReader->getReceivedMessage($id);
         if (isset($recipient) and $this->authorizator->isAllowed($recipient, $message, 'mark_as_read')) {
             $message->markMessageAsRead();
             $this->messagesWriter->saveMessageReference($message);
         }
     } else {
         throw new MessageTypeException();
     }
     return $message;
 }