Skip to content

Commit

Permalink
Final fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
  • Loading branch information
georgehrke authored and rullzer committed Aug 15, 2019
1 parent 2d6473e commit 4d28a45
Show file tree
Hide file tree
Showing 19 changed files with 1,660 additions and 869 deletions.
2 changes: 1 addition & 1 deletion 3rdparty
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
namespace OCA\DAV\BackgroundJob;

use OC\BackgroundJob\QueuedJob;
use OCP\BackgroundJob\QueuedJob;
use OCA\DAV\CalDAV\Reminder\ReminderService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/CalDAV/Reminder/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getRemindersToProcess():array {
$query = $this->db->getQueryBuilder();
$query->select(['cr.*', 'co.calendardata', 'c.displayname', 'c.principaluri'])
->from('calendar_reminders', 'cr')
// ->where($query->expr()->lte('cr.notification_date', $query->createNamedParameter($this->timeFactory->getTime())))
->where($query->expr()->lte('cr.notification_date', $query->createNamedParameter($this->timeFactory->getTime())))
->leftJoin('cr', 'calendarobjects', 'co', $query->expr()->eq('cr.object_id', 'co.id'))
->leftJoin('cr', 'calendars', 'c', $query->expr()->eq('cr.calendar_id', 'c.id'));
$stmt = $query->execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;

use \DateTime;
use \DateTimeImmutable;
use OCA\DAV\CalDAV\Reminder\INotificationProvider;
use OCP\IConfig;
use OCP\IL10N;
Expand All @@ -36,7 +34,6 @@
use OCP\IUser;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\Parameter;
use Sabre\VObject\Property;

/**
Expand All @@ -49,22 +46,22 @@ abstract class AbstractProvider implements INotificationProvider {
/** @var string */
public const NOTIFICATION_TYPE = '';

/** @var ILogger */
protected $logger;
/** @var ILogger */
protected $logger;

/** @var L10NFactory */
private $l10nFactory;
/** @var L10NFactory */
private $l10nFactory;

/** @var IL10N[] */
/** @var IL10N[] */
private $l10ns;

/** @var string */
private $fallbackLanguage;

/** @var IURLGenerator */
protected $urlGenerator;
/** @var IURLGenerator */
protected $urlGenerator;

/** @var IConfig */
/** @var IConfig */
protected $config;

/**
Expand All @@ -79,9 +76,9 @@ public function __construct(ILogger $logger,
IConfig $config) {
$this->logger = $logger;
$this->l10nFactory = $l10nFactory;
$this->urlGenerator = $urlGenerator;
$this->urlGenerator = $urlGenerator;
$this->config = $config;
}
}

/**
* Send notification
Expand All @@ -91,30 +88,30 @@ public function __construct(ILogger $logger,
* @param IUser[] $users
* @return void
*/
abstract public function send(VEvent $vevent,
abstract public function send(VEvent $vevent,
string $calendarDisplayName,
array $users=[]): void;

/**
* @return string
*/
protected function getFallbackLanguage():string {
if ($this->fallbackLanguage) {
return $this->fallbackLanguage;
protected function getFallbackLanguage():string {
if ($this->fallbackLanguage) {
return $this->fallbackLanguage;
}

$fallbackLanguage = $this->l10nFactory->findLanguage();
$this->fallbackLanguage = $fallbackLanguage;
$fallbackLanguage = $this->l10nFactory->findLanguage();
$this->fallbackLanguage = $fallbackLanguage;

return $fallbackLanguage;
return $fallbackLanguage;
}

/**
* @param string $lang
* @return bool
*/
protected function hasL10NForLang(string $lang):bool {
return $this->l10nFactory->languageExists('dav', $lang);
return $this->l10nFactory->languageExists('dav', $lang);
}

/**
Expand Down
77 changes: 25 additions & 52 deletions apps/dav/lib/CalDAV/Reminder/NotificationProvider/EmailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
*/
namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;

use DateTime;
use DateTimeImmutable;
use \DateTime;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger;
Expand Down Expand Up @@ -97,36 +96,35 @@ public function send(VEvent $vevent,
$organizer = $this->getOrganizerEMailAndNameFromEvent($vevent);

foreach($sortedByLanguage as $lang => $emailAddresses) {
if ($this->hasL10NForLang($lang)) {
if (!$this->hasL10NForLang($lang)) {
$lang = $fallbackLanguage;
}
$l10n = $this->getL10NForLang($lang);
$fromEMail = \OCP\Util::getDefaultEmailAddress('reminders-noreply');

$message = $this->mailer->createMessage();
$message->setFrom([$fromEMail]);
if ($organizer) {
$message->setReplyTo($organizer);
}
$message->setTo([])
->setBcc($emailAddresses);

$template = $this->mailer->createEMailTemplate('dav.calendarReminder');
$template->addHeader();

$this->addSubjectAndHeading($template, $l10n, $vevent);
$this->addBulletList($template, $l10n, $calendarDisplayName, $vevent);

$template->addFooter();
$message->useTemplate($template);

try {
$failed = $this->mailer->send($message);
if ($failed) {
$this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]);
foreach ($emailAddresses as $emailAddress) {
$message = $this->mailer->createMessage();
$message->setFrom([$fromEMail]);
if ($organizer) {
$message->setReplyTo($organizer);
}
$message->setTo([$emailAddress]);
$message->useTemplate($template);

try {
$failed = $this->mailer->send($message);
if ($failed) {
$this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]);
}
} catch (\Exception $ex) {
$this->logger->logException($ex, ['app' => 'dav']);
}
} catch (\Exception $ex) {
$this->logger->logException($ex, ['app' => 'dav']);
}
}
}
Expand Down Expand Up @@ -165,9 +163,9 @@ private function addBulletList(IEMailTemplate $template,
$template->addBodyListItem((string) $vevent->DESCRIPTION, $l10n->t('Description:'),
$this->getAbsoluteImagePath('actions/more.svg'));
}
}
}

/**
/**
* @param string $path
* @return string
*/
Expand Down Expand Up @@ -201,35 +199,6 @@ private function getOrganizerEMailAndNameFromEvent(VEvent $vevent):?array {
return [$organizerEMail];
}

/**
* @param array $sortedByLanguage
* @param IUser[] $users
* @param string $defaultLanguage
*/
private function sortUsersByLanguage(array &$sortedByLanguage,
array $users,
string $defaultLanguage):void {
/**
* @var array $sortedByLanguage
* [
* 'de' => ['a@b.com', 'c@d.com'],
* ...
* ]
*/
foreach($users as $user) {
/** @var IUser $user */
$emailAddress = $user->getEMailAddress();
$lang = $this->config->getUserValue($user->getUID(),
'core', 'lang', $defaultLanguage);

if (!isset($sortedByLanguage[$lang])) {
$sortedByLanguage[$lang] = [];
}

$sortedByLanguage[$lang][] = $emailAddress;
}
}

/**
* @param array $emails
* @param string $defaultLanguage
Expand Down Expand Up @@ -386,7 +355,7 @@ private function getEMailAddressesOfAllUsersWithWriteAccessToCalendar(array $use
}
}

return array_unique($emailAddresses);
return $emailAddresses;
}

/**
Expand Down Expand Up @@ -415,7 +384,9 @@ private function generateDateString(IL10N $l10n, VEvent $vevent):string {

$diff = $dtstartDt->diff($dtendDt);

/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$dtstartDt = new \DateTime($dtstartDt->format(\DateTime::ATOM));
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$dtendDt = new \DateTime($dtendDt->format(\DateTime::ATOM));

if ($isAllDay) {
Expand All @@ -432,7 +403,9 @@ private function generateDateString(IL10N $l10n, VEvent $vevent):string {

$startTimezone = $endTimezone = null;
if (!$vevent->DTSTART->isFloating()) {
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$startTimezone = $vevent->DTSTART->getDateTime()->getTimezone()->getName();
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
$endTimezone = $this->getDTEndFromEvent($vevent)->getDateTime()->getTimezone()->getName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PushProvider extends AbstractProvider {
/** @var string */
public const NOTIFICATION_TYPE = 'DISPLAY';

/** @var IManager */
/** @var IManager */
private $manager;

/** @var ITimeFactory */
Expand All @@ -70,7 +70,7 @@ public function __construct(IConfig $config,
parent::__construct($logger, $l10nFactory, $urlGenerator, $config);
$this->manager = $manager;
$this->timeFactory = $timeFactory;
}
}

/**
* Send push notification to all users.
Expand All @@ -80,13 +80,13 @@ public function __construct(IConfig $config,
* @param IUser[] $users
* @throws \Exception
*/
public function send(VEvent $vevent,
public function send(VEvent $vevent,
string $calendarDisplayName=null,
array $users=[]):void {
$eventDetails = $this->extractEventDetails($vevent);
$eventDetails['calendar_displayname'] = $calendarDisplayName;

foreach($users as $user) {
foreach($users as $user) {
/** @var INotification $notification */
$notification = $this->manager->createNotification();
$notification->setApp(Application::APP_ID)
Expand All @@ -101,7 +101,7 @@ public function send(VEvent $vevent,

$this->manager->notify($notification);
}
}
}

/**
* @var VEvent $vevent
Expand All @@ -124,11 +124,15 @@ protected function extractEventDetails(VEvent $vevent):array {
? ((string) $vevent->LOCATION)
: null,
'all_day' => $start instanceof Property\ICalendar\Date,
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
'start_atom' => $start->getDateTime()->format(\DateTime::ATOM),
'start_is_floating' => $start->isFloating(),
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
'start_timezone' => $start->getDateTime()->getTimezone()->getName(),
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
'end_atom' => $end->getDateTime()->format(\DateTime::ATOM),
'end_is_floating' => $end->isFloating(),
/** @phan-suppress-next-line PhanUndeclaredClassMethod */
'end_timezone' => $end->getDateTime()->getTimezone()->getName(),
];
}
Expand Down
36 changes: 18 additions & 18 deletions apps/dav/lib/CalDAV/Reminder/NotificationProviderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
*/
class NotificationProviderManager {

/** @var INotificationProvider[] */
private $providers = [];
/** @var INotificationProvider[] */
private $providers = [];

/**
* Checks whether a provider for a given ACTION exists
Expand All @@ -45,31 +45,31 @@ public function hasProvider(string $type):bool {
&& isset($this->providers[$type]));
}

/**
/**
* Get provider for a given ACTION
*
* @param string $type
* @return INotificationProvider
* @throws NotificationProvider\ProviderNotAvailableException
* @throws NotificationTypeDoesNotExistException
*/
public function getProvider(string $type):INotificationProvider {
if (in_array($type, ReminderService::REMINDER_TYPES, true)) {
if (isset($this->providers[$type])) {
return $this->providers[$type];
}
throw new NotificationProvider\ProviderNotAvailableException($type);
}
throw new NotificationTypeDoesNotExistException($type);
}
* @param string $type
* @return INotificationProvider
* @throws NotificationProvider\ProviderNotAvailableException
* @throws NotificationTypeDoesNotExistException
*/
public function getProvider(string $type):INotificationProvider {
if (in_array($type, ReminderService::REMINDER_TYPES, true)) {
if (isset($this->providers[$type])) {
return $this->providers[$type];
}
throw new NotificationProvider\ProviderNotAvailableException($type);
}
throw new NotificationTypeDoesNotExistException($type);
}

/**
* Registers a new provider
*
* @param string $providerClassName
* @throws \OCP\AppFramework\QueryException
*/
public function registerProvider(string $providerClassName):void {
public function registerProvider(string $providerClassName):void {
$provider = \OC::$server->query($providerClassName);

if (!$provider instanceof INotificationProvider) {
Expand Down
Loading

0 comments on commit 4d28a45

Please sign in to comment.