/** * @param INotification $notification * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification */ public function prepare(INotification $notification, $languageCode) { if ($notification->getApp() !== 'files_sharing') { // Not my app => throw throw new \InvalidArgumentException(); } // Read the language from the notification $l = $this->factory->get('files_sharing', $languageCode); switch ($notification->getSubject()) { // Deal with known subjects case 'remote_share': $params = $notification->getSubjectParameters(); $notification->setParsedSubject((string) $l->t('You received %s as a remote share from %s', $params)); // Deal with the actions for a known subject foreach ($notification->getActions() as $action) { switch ($action->getLabel()) { case 'accept': $action->setParsedLabel((string) $l->t('Accept')); break; case 'decline': $action->setParsedLabel((string) $l->t('Decline')); break; } $notification->addParsedAction($action); } return $notification; default: // Unknown subject => Unknown notification => throw throw new \InvalidArgumentException(); } }
/** * @expectedException \InvalidArgumentException */ public function testAddParsedActionInvalid() { /** @var \OC\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */ $action = $this->getMockBuilder('OC\\Notification\\IAction')->disableOriginalConstructor()->getMock(); $action->expects($this->once())->method('isValidParsed')->willReturn(false); $action->expects($this->never())->method('isValid'); $this->notification->addParsedAction($action); }
public function testAddActionSecondParsedPrimary() { /** @var \OC\Notification\IAction|\PHPUnit_Framework_MockObject_MockObject $action */ $action = $this->getMockBuilder('OC\\Notification\\IAction')->disableOriginalConstructor()->getMock(); $action->expects($this->exactly(2))->method('isValidParsed')->willReturn(true); $action->expects($this->exactly(2))->method('isPrimary')->willReturn(true); $this->assertSame($this->notification, $this->notification->addParsedAction($action)); $this->setExpectedException('\\InvalidArgumentException'); $this->notification->addParsedAction($action); }
/** * @param INotification $notification * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification * @throws \InvalidArgumentException When the notification was not prepared by a notifier */ public function prepare(INotification $notification, $languageCode) { if ($notification->getApp() !== 'announcementcenter') { // Not my app => throw throw new \InvalidArgumentException(); } // Read the language from the notification $l = $this->l10nFactory->get('announcementcenter', $languageCode); switch ($notification->getSubject()) { // Deal with known subjects case 'announced': $params = $notification->getSubjectParameters(); $announcement = $this->manager->getAnnouncement($notification->getObjectId(), false); $params[] = $this->prepareMessage($announcement['subject']); $notification->setParsedMessage($this->prepareMessage($announcement['message']))->setParsedSubject((string) $l->t('%1$s announced “%2$s”', $params)); return $notification; default: // Unknown subject => Unknown notification => throw throw new \InvalidArgumentException(); } }
/** * @param INotification $notification * @param string $languageCode The code of the language that should be used to prepare the notification * @return INotification * @throws \InvalidArgumentException When the notification was not prepared by a notifier */ public function prepare(INotification $notification, $languageCode) { if ($notification->getApp() !== 'popularitycontestclient') { // Not my app => throw throw new \InvalidArgumentException(); } // Read the language from the notification $l = $this->l10nFactory->get('popularitycontestclient', $languageCode); $notification->setParsedSubject((string) $l->t('Do you want to send monthly usage statistics to ownCloud?')); foreach ($notification->getActions() as $action) { if ($action->getLabel() === 'enable') { $action->setParsedLabel((string) $l->t('Yes')); } else { if ($action->getLabel() === 'disable') { $action->setParsedLabel((string) $l->t('Not now')); } } $notification->addParsedAction($action); } return $notification; }
/** * @param int $notificationId * @param INotification $notification * @return array */ protected function notificationToArray($notificationId, INotification $notification) { $data = ['notification_id' => $notificationId, 'app' => $notification->getApp(), 'user' => $notification->getUser(), 'timestamp' => $notification->getTimestamp(), 'object_type' => $notification->getObjectType(), 'object_id' => $notification->getObjectId(), 'subject' => $notification->getParsedSubject(), 'message' => $notification->getParsedMessage(), 'link' => $notification->getLink(), 'icon' => $notification->getIcon(), 'actions' => []]; foreach ($notification->getParsedActions() as $action) { $data['actions'][] = $this->actionToArray($action); } return $data; }
/** * Add where statements to a query builder matching the given notification * * @param IQueryBuilder $sql * @param INotification $notification */ protected function sqlInsert(IQueryBuilder $sql, INotification $notification) { $sql->setValue('app', $sql->createParameter('app'))->setParameter('app', $notification->getApp()); $sql->setValue('user', $sql->createParameter('user'))->setParameter('user', $notification->getUser()); $sql->setValue('timestamp', $sql->createParameter('timestamp'))->setParameter('timestamp', $notification->getTimestamp()); $sql->setValue('object_type', $sql->createParameter('objectType'))->setParameter('objectType', $notification->getObjectType()); $sql->setValue('object_id', $sql->createParameter('objectId'))->setParameter('objectId', $notification->getObjectId()); $sql->setValue('subject', $sql->createParameter('subject'))->setParameter('subject', $notification->getSubject()); $sql->setValue('subject_parameters', $sql->createParameter('subject_parameters'))->setParameter('subject_parameters', json_encode($notification->getSubjectParameters())); $sql->setValue('message', $sql->createParameter('message'))->setParameter('message', $notification->getMessage()); $sql->setValue('message_parameters', $sql->createParameter('message_parameters'))->setParameter('message_parameters', json_encode($notification->getMessageParameters())); $sql->setValue('link', $sql->createParameter('link'))->setParameter('link', $notification->getLink()); $sql->setValue('icon', $sql->createParameter('icon'))->setParameter('icon', $notification->getIcon()); $actions = []; foreach ($notification->getActions() as $action) { /** @var IAction $action */ $actions[] = ['label' => $action->getLabel(), 'icon' => $action->getIcon(), 'link' => $action->getLink(), 'type' => $action->getRequestType()]; } $sql->setValue('actions', $sql->createParameter('actions'))->setParameter('actions', json_encode($actions)); }