/** * @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(); } }
/** * Send an event to the notifications of a user * * @param IEvent $event * @return null */ public function receive(IEvent $event) { $selfAction = $event->getAffectedUser() === $event->getAuthor(); $streamSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'stream', $event->getType()); $emailSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'email', $event->getType()); $emailSetting = $emailSetting ? $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'batchtime') : false; $types = $this->data->getNotificationTypes($this->l10nFactory->get('activity')); $typeData = $types[$event->getType()]; // User is not the author or wants to see their own actions $createStream = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'self'); // User can not control the setting $createStream = $createStream || is_array($typeData) && isset($typeData['methods']) && !in_array(IExtension::METHOD_STREAM, $typeData['methods']); // Add activity to stream if ($streamSetting && $createStream) { $this->data->send($event); } // User is not the author or wants to see their own actions $createEmail = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'selfemail'); // User can not control the setting $createEmail = $createEmail || is_array($typeData) && isset($typeData['methods']) && !in_array(IExtension::METHOD_MAIL, $typeData['methods']); // Add activity to mail queue if ($emailSetting && $createEmail) { $latestSend = $event->getTimestamp() + $emailSetting; $this->data->storeMail($event, $latestSend); } }
/** * @PublicPage * @NoCSRFRequired * * @return TemplateResponse */ public function show() { try { $user = $this->activityManager->getCurrentUserId(); $userLang = $this->config->getUserValue($user, 'core', 'lang'); // Overwrite user and language in the helper $l = $this->l10nFactory->get('activity', $userLang); $this->helper->setL10n($l); $this->helper->setUser($user); $description = (string) $l->t('Personal activity feed for %s', $user); $response = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', 'all'); $activities = $response['data']; } catch (\UnexpectedValueException $e) { $l = $this->l10nFactory->get('activity'); $description = (string) $l->t('Your feed URL is invalid'); $activities = [['activity_id' => -1, 'timestamp' => time(), 'subject' => true, 'subjectformatted' => ['full' => $description]]]; } $response = new TemplateResponse('activity', 'rss', ['rssLang' => $l->getLanguageCode(), 'rssLink' => $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show'), 'rssPubDate' => date('r'), 'description' => $description, 'activities' => $activities], ''); if ($this->request->getHeader('accept') !== null && stristr($this->request->getHeader('accept'), 'application/rss+xml')) { $response->addHeader('Content-Type', 'application/rss+xml'); } else { $response->addHeader('Content-Type', 'text/xml; charset=UTF-8'); } return $response; }
protected function setUp() { parent::setUp(); $this->manager = $this->getMockBuilder('OCA\\AnnouncementCenter\\Manager')->disableOriginalConstructor()->getMock(); $this->l = $this->getMockBuilder('OCP\\IL10N')->disableOriginalConstructor()->getMock(); $this->l->expects($this->any())->method('t')->willReturnCallback(function ($string, $args) { return vsprintf($string, $args); }); $this->factory = $this->getMockBuilder('OCP\\L10N\\IFactory')->disableOriginalConstructor()->getMock(); $this->factory->expects($this->any())->method('get')->willReturn($this->l); $this->notifier = new NotificationsNotifier($this->manager, $this->factory); }
public function testAppNavigation() { $this->navigationManager->expects($this->once())->method('add')->willReturnCallback(function ($closure) { $this->assertInstanceOf('\\Closure', $closure); $navigation = $closure(); $this->assertInternalType('array', $navigation); $this->assertCount(5, $navigation); $this->assertSame(['id' => 'announcementcenter', 'order' => 10, 'href' => '/apps/announcementcenter/announcement', 'icon' => '/apps/announcementcenter/img/announcementcenter.svg', 'name' => 'Announcements'], $navigation); }); $this->urlGenerator->expects($this->once())->method('linkToRoute')->with('announcementcenter.page.index')->willReturn('/apps/announcementcenter/announcement'); $this->urlGenerator->expects($this->once())->method('imagePath')->with('announcementcenter', 'announcementcenter.svg')->willReturn('/apps/announcementcenter/img/announcementcenter.svg'); $this->languageFactory->expects($this->once())->method('get')->with('announcementcenter')->willReturn($this->language); include __DIR__ . '/../../appinfo/app.php'; }
/** * Returnsed function accepts the argument $n * * Called by \OC_L10N_String * @return string the plural form function */ public function getPluralFormFunction() { if (is_null($this->pluralFormFunction)) { $this->pluralFormFunction = $this->factory->createPluralFunction($this->pluralFormString); } return $this->pluralFormFunction; }
/** * The extension can translate a given message to the requested languages. * If no translation is available false is to be returned. * * @param string $app * @param string $text * @param array $params * @param boolean $stripPath * @param boolean $highlightParams * @param string $languageCode * @return string|false */ public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { if ($app === 'announcementcenter') { $l = $this->languageFactory->get('announcementcenter', $languageCode); list(, $id) = explode('#', $text); try { $announcement = $this->manager->getAnnouncement($id, $highlightParams); } catch (\InvalidArgumentException $e) { return (string) $l->t('Announcement does not exist anymore', $params); } if (strpos($text, 'announcementmessage#') === 0) { return $announcement['message']; } if ($highlightParams) { $params[] = '<strong>' . $announcement['subject'] . '</strong>'; } else { $params[] = $announcement['subject']; } if ($announcement['author'] === $this->activityManager->getCurrentUserId()) { array_shift($params); return (string) $l->t('You announced %s', $params); } return (string) $l->t('%s announced %s', $params); } return false; }
protected function setUp() { parent::setUp(); $this->request = $this->getMockBuilder('OCP\\IRequest')->disableOriginalConstructor()->getMock(); $this->session = $this->getMockBuilder('OCP\\IUserSession')->disableOriginalConstructor()->getMock(); $this->config = $this->getMockBuilder('OCP\\IConfig')->disableOriginalConstructor()->getMock(); $this->activityHelper = $this->getMockBuilder('OCA\\Files\\ActivityHelper')->disableOriginalConstructor()->getMock(); $this->activityManager = new \OC\ActivityManager($this->request, $this->session, $this->config); $this->l10nFactory = $this->getMockBuilder('OCP\\L10N\\IFactory')->disableOriginalConstructor()->getMock(); $deL10n = $this->getMockBuilder('OC_L10N')->disableOriginalConstructor()->getMock(); $deL10n->expects($this->any())->method('t')->willReturnCallback(function ($argument) { return 'translate(' . $argument . ')'; }); $this->l10nFactory->expects($this->any())->method('get')->willReturnMap([['files', null, new \OC_L10N('files', 'en')], ['files', 'en', new \OC_L10N('files', 'en')], ['files', 'de', $deL10n]]); $this->activityExtension = $activityExtension = new Activity($this->l10nFactory, $this->getMockBuilder('OCP\\IURLGenerator')->disableOriginalConstructor()->getMock(), $this->activityManager, $this->activityHelper, \OC::$server->getDatabaseConnection(), $this->config); $this->activityManager->registerExtension(function () use($activityExtension) { return $activityExtension; }); }
/** * @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 * @since 9.0.0 */ public function prepare(INotification $notification, $languageCode) { if ($notification->getApp() !== 'updatenotification') { throw new \InvalidArgumentException(); } $l = $this->l10NFactory->get('updatenotification', $languageCode); if ($notification->getObjectType() === 'core') { $appName = $l->t('ownCloud core'); $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions()); } else { $appInfo = $this->getAppInfo($notification->getObjectType()); $appName = $appInfo === null ? $notification->getObjectType() : $appInfo['name']; if (isset($this->appVersions[$notification->getObjectType()])) { $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]); } } $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()])); return $notification; }
/** * @dataProvider dataTranslation * * @param string $app * @param string $text * @param array $params * @param array $prepared * @param bool $stripPath * @param bool $highlightParams * @param bool|string $managerReturn * @param string $expected */ public function testTranslation($app, $text, array $params, array $prepared, $stripPath, $highlightParams, $managerReturn, $expected) { $this->activityManager->expects($this->once())->method('translate')->with($app, $text, $prepared, $stripPath, $highlightParams)->willReturn($managerReturn); if ($managerReturn === false) { $l = $this->getMockBuilder('OCP\\IL10N')->disableOriginalConstructor()->getMock(); $l->expects($this->once())->method('t')->with($text, $prepared)->willReturn('lang'); $this->l10Nfactory->expects($this->once())->method('get')->with($app, $this->anything())->willReturn($l); } $helper = $this->getHelper(); $this->assertSame($expected, (string) $helper->translation($app, $text, $params, $stripPath, $highlightParams)); }
/** * @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 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(); } }
/** * @dataProvider dataTranslate * * @param string $app * @param string $text * @param array $params * @param bool $stripPath * @param bool $highlightParams * @param string $languageCode * @param mixed $managerReturn * @param string $currentUser * @param mixed $expected */ public function testTranslate($app, $text, $params, $stripPath, $highlightParams, $languageCode, $managerReturn, $currentUser, $expected) { if ($managerReturn === null) { $this->manager->expects($this->never())->method('getAnnouncement'); } else { $this->factory->expects($this->any())->method('get')->with('announcementcenter', $languageCode)->willReturn($this->l); $this->activity->expects($this->any())->method('getCurrentUserId')->willReturn($currentUser); if ($managerReturn === false) { $this->manager->expects($this->once())->method('getAnnouncement')->with(10, $highlightParams)->willThrowException(new \InvalidArgumentException()); } else { $this->manager->expects($this->once())->method('getAnnouncement')->with(10, $highlightParams)->willReturn($managerReturn); } } $this->assertSame($expected, $this->extension->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode)); }
/** * @brief Translate an event string with the translations from the app where it was send from * @param string $app The app where this event comes from * @param string $text The text including placeholders * @param IParameter[] $params The parameter for the placeholder * @param bool $stripPath Shall we strip the path from file names? * @param bool $highlightParams Shall we highlight the parameters in the string? * They will be highlighted with `<strong>`, all data will be passed through * \OCP\Util::sanitizeHTML() before, so no XSS is possible. * @return string translated */ public function translation($app, $text, array $params, $stripPath = false, $highlightParams = false) { if (!$text) { return ''; } $preparedParams = []; foreach ($params as $parameter) { $preparedParams[] = $parameter->format($highlightParams, !$stripPath); } // Allow apps to correctly translate their activities $translation = $this->activityManager->translate($app, $text, $preparedParams, $stripPath, $highlightParams, $this->l->getLanguageCode()); if ($translation !== false) { return $translation; } $l = $this->l10Nfactory->get($app, $this->l->getLanguageCode()); return $l->t($text, $preparedParams); }
protected function getL10N($languageCode = null) { return $this->languageFactory->get(self::APP_NAME, $languageCode); }
protected function getL10N($languageCode = null) { return $this->languageFactory->get(self::FILES_SHARING_APP, $languageCode); }