/** * Creates a new channel in the postbox. * * @param string $Title The title of the new channel. * @param string $MessageText The text for the first message in the channel. * @param GenericORMapperDataObject[] $Readers An array of users which will be recipients. * * @return MessageChannel The new created channel * @throws InvalidArgumentException */ public function createChannel($Title, $MessageText, array $Readers) { $Message = new Message(); $Message->setText($MessageText); $Message->setAuthor($this->User); $Channel = new MessageChannel(); $Channel->setTitle($Title); $Channel->addRelatedObject('MessageChannel2Message', $Message); $Channel->addRelatedObject('User2MessageChannel', $this->User); // we do not use the channel's addReaders() function here, because this would only work for an existing channel! $RealReadersPresent = false; foreach ($Readers as &$Reader) { // author could have been added as reader as well, let's filter this possibility // and check if reader is blocking the current user if ($Reader->getObjectId() !== $this->User->getObjectId() && !$this->isOnUsersBlacklist($Reader)) { $RealReadersPresent = true; $Channel->addRelatedObject('User2MessageChannel', $Reader); $Message->addRelatedObject('User2UnreadMessage', $Reader); $Channel->addRelatedObject('User2UnreadMessageChannel', $Reader); } } if (!$RealReadersPresent) { throw new InvalidArgumentException('[Postbox::createChannel()] There are no recipients, maybe you tried to send a message to yourself, or the recipient(s) are blocking you!'); } $Channel->setDataComponent($this->ORM); return $Channel->save(); }