From 9bcc1d5618679a374c0078ce9a02f4cc12f7842e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 10 Dec 2014 15:26:31 +0100 Subject: [PATCH] Use OCP\DateTimeFormatter from core --- appinfo/application.php | 37 +++++++----- lib/backgroundjob/emailnotification.php | 28 +++++---- lib/mailqueuehandler.php | 61 ++++++++------------ tests/mailqueuehandlertest.php | 76 ++----------------------- 4 files changed, 68 insertions(+), 134 deletions(-) diff --git a/appinfo/application.php b/appinfo/application.php index 60de7070..bf2e3fd5 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -39,24 +39,17 @@ class Application extends App { parent::__construct('activity', $urlParams); $container = $this->getContainer(); + /** + * Activity Services + */ $container->registerService('ActivityData', function(IContainer $c) { return new Data( $c->query('ServerContainer')->query('ActivityManager') ); }); - $container->registerService('UserSettings', function(IContainer $c) { - return new UserSettings( - $c->query('ServerContainer')->query('ActivityManager') - ); - }); - - $container->registerService('GroupHelper', function(IContainer $c) { - return new GroupHelper( - $c->query('ServerContainer')->query('ActivityManager'), - $c->query('DataHelper'), - true - ); + $container->registerService('ActivityL10N', function(IContainer $c) { + return $c->query('ServerContainer')->getL10N('activity'); }); $container->registerService('DataHelper', function(IContainer $c) { @@ -73,6 +66,14 @@ class Application extends App { ); }); + $container->registerService('GroupHelper', function(IContainer $c) { + return new GroupHelper( + $c->query('ServerContainer')->query('ActivityManager'), + $c->query('DataHelper'), + true + ); + }); + $container->registerService('Navigation', function(IContainer $c) { return new Navigation( $c->query('ActivityL10N'), @@ -81,14 +82,22 @@ class Application extends App { ); }); - $container->registerService('ActivityL10N', function(IContainer $c) { - return $c->query('ServerContainer')->getL10N('activity'); + $container->registerService('UserSettings', function(IContainer $c) { + return new UserSettings( + $c->query('ServerContainer')->query('ActivityManager') + ); }); + /** + * Core Services + */ $container->registerService('URLGenerator', function(IContainer $c) { return $c->query('ServerContainer')->getURLGenerator(); }); + /** + * Controller + */ $container->registerService('SettingsController', function(IContainer $c) { /** @var \OC\Server $server */ $server = $c->query('ServerContainer'); diff --git a/lib/backgroundjob/emailnotification.php b/lib/backgroundjob/emailnotification.php index 2a639011..b4d248dc 100644 --- a/lib/backgroundjob/emailnotification.php +++ b/lib/backgroundjob/emailnotification.php @@ -35,10 +35,16 @@ class EmailNotification extends \OC\BackgroundJob\TimedJob { /** @var \OCA\Activity\MailQueueHandler */ protected $mqHandler; + /** @var \OCP\IConfig */ + protected $config; + public function __construct() { // Run all 15 Minutes $this->setInterval(15 * 60); - $this->mqHandler = new \OCA\Activity\MailQueueHandler(); + $this->mqHandler = new \OCA\Activity\MailQueueHandler( + \OC::$server->query('DateTimeFormatter') + ); + $this->config = \OC::$server->getConfig(); } protected function run($argument) { @@ -61,27 +67,27 @@ class EmailNotification extends \OC\BackgroundJob\TimedJob { * @return int Number of users we sent an email to */ protected function runStep($limit) { + // We don't use time() but "time() - 1" here, so we don't run into + // runtime issues later and delete emails, which were created in the + // same second, but were not collected for the emails. + $sendTime = time() - 1; + // Get all users which should receive an email - $affectedUsers = $this->mqHandler->getAffectedUsers($limit); + $affectedUsers = $this->mqHandler->getAffectedUsers($limit, $sendTime); if (empty($affectedUsers)) { // No users found to notify, mission abort return 0; } - $preferences = new \OC\Preferences(\OC_DB::getConnection()); - $userLanguages = $preferences->getValueForUsers('core', 'lang', $affectedUsers); - $userTimezones = $preferences->getValueForUsers('core', 'timezone', $affectedUsers); - $userEmails = $preferences->getValueForUsers('settings', 'email', $affectedUsers); + $userLanguages = $this->config->getUserValueForUsers('core', 'lang', $affectedUsers); + $userTimezones = $this->config->getUserValueForUsers('core', 'timezone', $affectedUsers); + $userEmails = $this->config->getUserValueForUsers('settings', 'email', $affectedUsers); // Get all items for these users - // We don't use time() but "time() - 1" here, so we don't run into - // runtime issues and delete emails later, which were created in the - // same second, but were not collected for the emails. - $sendTime = time() - 1; $mailData = $this->mqHandler->getItemsForUsers($affectedUsers, $sendTime); // Send Email - $default_lang = \OCP\Config::getSystemValue('default_language', 'en'); + $default_lang = $this->config->getSystemValue('default_language', 'en'); foreach ($mailData as $user => $data) { if (!isset($userEmails[$user])) { // The user did not setup an email address diff --git a/lib/mailqueuehandler.php b/lib/mailqueuehandler.php index 5e3fd7da..1c67f479 100644 --- a/lib/mailqueuehandler.php +++ b/lib/mailqueuehandler.php @@ -23,6 +23,8 @@ namespace OCA\Activity; +use \OCP\IDateTimeFormatter; + /** * Class MailQueueHandler * Gets the users from the database and @@ -39,13 +41,26 @@ class MailQueueHandler { /** @var string */ protected $senderName; + /** @var IDateTimeFormatter */ + protected $dateFormatter; + + /** + * Constructor + * + * @param IDateTimeFormatter $dateFormatter + */ + public function __construct(IDateTimeFormatter $dateFormatter) { + $this->dateFormatter = $dateFormatter; + } + /** * Get the users we want to send an email to * * @param int|null $limit + * @param int $latestSend * @return array */ - public function getAffectedUsers($limit) { + public function getAffectedUsers($limit, $latestSend) { $limit = (!$limit) ? null : (int) $limit; $query = \OCP\DB::prepare( @@ -55,7 +70,7 @@ class MailQueueHandler { . ' GROUP BY `amq_affecteduser` ' . ' ORDER BY `amq_trigger_time` ASC', $limit); - $result = $query->execute(array(time())); + $result = $query->execute(array($latestSend)); $affectedUsers = array(); if (\OCP\DB::isError($result)) { @@ -177,11 +192,17 @@ class MailQueueHandler { $activityList = array(); foreach ($mailData as $activity) { + $relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay( + $activity['amq_timestamp'], + 'long', 'medium', + new \DateTimeZone($timezone), $l + ); + $activityList[] = array( $dataHelper->translation( $activity['amq_appid'], $activity['amq_subject'], unserialize($activity['amq_subjectparams']) ), - $this->generateRelativeDatetime($l, $activity['amq_timestamp'], $timezone), + $relativeDateTime, ); } @@ -204,40 +225,6 @@ class MailQueueHandler { } } - /** - * Creates a relative datetime string (with today, yesterday) or the normal date - * - * @param \OC_L10N $l - * @param int $timestamp - * @param string $timeZone - * @return string - */ - protected function generateRelativeDatetime(\OC_L10N $l, $timestamp, $timeZone) { - $offset = 0; - if ($timeZone) { - if (!$timeZone instanceof \DateTimeZone) { - $timeZone = new \DateTimeZone($timeZone); - } - $dt = new \DateTime("@$timestamp"); - $offset = $timeZone->getOffset($dt); - } - - $timestamp = $timestamp + $offset; - $dateOfTimestamp = $l->l('date', $timestamp); - $dateOfToday = $l->l('date', time() + $offset); - $dateOfYesterday = $l->l('date', time() - 3600 * 24 + $offset); - - if ($dateOfTimestamp === $dateOfToday) { - return (string) $l->t('Today %s', $l->l('time', $timestamp)); - } - - if ($dateOfTimestamp === $dateOfYesterday) { - return (string) $l->t('Yesterday %s', $l->l('time', $timestamp)); - } - - return (string) $l->l('datetime', $timestamp); - } - /** * Delete all entries we dealt with * diff --git a/tests/mailqueuehandlertest.php b/tests/mailqueuehandlertest.php index ed54e7cc..93e624d5 100644 --- a/tests/mailqueuehandlertest.php +++ b/tests/mailqueuehandlertest.php @@ -64,79 +64,11 @@ class MailQueueHandlerTest extends TestCase { * @dataProvider getAffectedUsersData */ public function testGetAffectedUsers($limit, $expected) { - $mq = new MailQueueHandler(); - $users = $mq->getAffectedUsers($limit); + $mq = new MailQueueHandler( + $this->getMock('\OCP\IDateTimeFormatter') + ); + $users = $mq->getAffectedUsers($limit, time()); $this->assertEquals($expected, $users); } - - public function generateRelativeDatetimeDataToday() - { - return array( - array(time(), ''), - array(time(), 'Europe/Berlin'), - ); - } - - /** - * @dataProvider generateRelativeDatetimeDataToday - */ - public function testGenerateRelativeDatetimeToday($time, $timezone) { - $mq = new MailQueueHandler(); - $l = \OC_L10N::get('activity'); - - $this->assertStringStartsWith((string) $l->t('Today %s', ''), \Test_Helper::invokePrivate($mq, 'generateRelativeDatetime', array( - $l, - $time, - $timezone - ))); - } - - public function generateRelativeDatetimeDataYesterday() - { - return array( - array(time() - 3600 * 24, ''), - array(time() - 3600 * 24, 'Europe/Berlin'), - ); - } - - /** - * @dataProvider generateRelativeDatetimeDataYesterday - */ - public function testGenerateRelativeDatetimeYesterday($time, $timezone) { - $mq = new MailQueueHandler(); - $l = \OC_L10N::get('activity'); - - $this->assertStringStartsWith((string) $l->t('Yesterday %s', ''), \Test_Helper::invokePrivate($mq, 'generateRelativeDatetime', array( - $l, - $time, - $timezone - ))); - } - - public function generateRelativeDatetimeDataOthers() - { - return array( - array(time() - 3600 * 48, ''), - array(time() - 3600 * 48, 'Europe/Berlin'), - array(time() + 3600 * 24, ''), - array(time() + 3600 * 24, 'Europe/Berlin'), - ); - } - - /** - * @dataProvider generateRelativeDatetimeDataOthers - */ - public function testGenerateRelativeDatetimeOthers($time, $timezone) { - $mq = new MailQueueHandler(); - $l = \OC_L10N::get('activity'); - $result = \Test_Helper::invokePrivate($mq, 'generateRelativeDatetime', array( - $l, - $time, - $timezone - )); - - $this->assertStringStartsNotWith((string) $l->t('Today %s', ''), $result); - $this->assertStringStartsNotWith((string) $l->t('Yesterday %s', ''), $result); - } }