From d6c1c1ea33de6386fabe7c9546bfae1c38d3b9e7 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 28 May 2016 17:36:55 -0400 Subject: Improve notification classes and move interface to core --- app/Controller/UserCreationController.php | 2 +- app/Core/Notification/NotificationInterface.php | 32 +++++ app/Notification/ActivityStream.php | 47 -------- app/Notification/ActivityStreamNotification.php | 48 ++++++++ app/Notification/Mail.php | 150 ----------------------- app/Notification/MailNotification.php | 151 ++++++++++++++++++++++++ app/Notification/NotificationInterface.php | 32 ----- app/Notification/Web.php | 46 -------- app/Notification/WebNotification.php | 47 ++++++++ app/Notification/Webhook.php | 55 --------- app/Notification/WebhookNotification.php | 56 +++++++++ app/ServiceProvider/NotificationProvider.php | 12 +- tests/units/Notification/MailTest.php | 8 +- 13 files changed, 345 insertions(+), 341 deletions(-) create mode 100644 app/Core/Notification/NotificationInterface.php delete mode 100644 app/Notification/ActivityStream.php create mode 100644 app/Notification/ActivityStreamNotification.php delete mode 100644 app/Notification/Mail.php create mode 100644 app/Notification/MailNotification.php delete mode 100644 app/Notification/NotificationInterface.php delete mode 100644 app/Notification/Web.php create mode 100644 app/Notification/WebNotification.php delete mode 100644 app/Notification/Webhook.php create mode 100644 app/Notification/WebhookNotification.php diff --git a/app/Controller/UserCreationController.php b/app/Controller/UserCreationController.php index 49f9db54..4ae170e0 100644 --- a/app/Controller/UserCreationController.php +++ b/app/Controller/UserCreationController.php @@ -3,7 +3,7 @@ namespace Kanboard\Controller; use Kanboard\Core\Security\Role; -use Kanboard\Notification\Mail as MailNotification; +use Kanboard\Notification\MailNotification; /** * Class UserCreationController diff --git a/app/Core/Notification/NotificationInterface.php b/app/Core/Notification/NotificationInterface.php new file mode 100644 index 00000000..d336983a --- /dev/null +++ b/app/Core/Notification/NotificationInterface.php @@ -0,0 +1,32 @@ +userSession->isLogged()) { - $this->projectActivity->createEvent( - $project['id'], - $event_data['task']['id'], - $this->userSession->getId(), - $event_name, - $event_data - ); - } - } -} diff --git a/app/Notification/ActivityStreamNotification.php b/app/Notification/ActivityStreamNotification.php new file mode 100644 index 00000000..8ac265d3 --- /dev/null +++ b/app/Notification/ActivityStreamNotification.php @@ -0,0 +1,48 @@ +userSession->isLogged()) { + $this->projectActivity->createEvent( + $project['id'], + $event_data['task']['id'], + $this->userSession->getId(), + $event_name, + $event_data + ); + } + } +} diff --git a/app/Notification/Mail.php b/app/Notification/Mail.php deleted file mode 100644 index c924fb50..00000000 --- a/app/Notification/Mail.php +++ /dev/null @@ -1,150 +0,0 @@ -emailClient->send( - $user['email'], - $user['name'] ?: $user['username'], - $this->getMailSubject($event_name, $event_data), - $this->getMailContent($event_name, $event_data) - ); - } - } - - /** - * Send notification to a project - * - * @access public - * @param array $project - * @param string $event_name - * @param array $event_data - */ - public function notifyProject(array $project, $event_name, array $event_data) - { - } - - /** - * Get the mail content for a given template name - * - * @access public - * @param string $event_name Event name - * @param array $event_data Event data - * @return string - */ - public function getMailContent($event_name, array $event_data) - { - return $this->template->render( - 'notification/'.str_replace('.', '_', $event_name), - $event_data + array('application_url' => $this->config->get('application_url')) - ); - } - - /** - * Get the mail subject for a given template name - * - * @access public - * @param string $event_name Event name - * @param array $event_data Event data - * @return string - */ - public function getMailSubject($event_name, array $event_data) - { - switch ($event_name) { - case TaskFile::EVENT_CREATE: - $subject = $this->getStandardMailSubject(e('New attachment'), $event_data); - break; - case Comment::EVENT_CREATE: - $subject = $this->getStandardMailSubject(e('New comment'), $event_data); - break; - case Comment::EVENT_UPDATE: - $subject = $this->getStandardMailSubject(e('Comment updated'), $event_data); - break; - case Subtask::EVENT_CREATE: - $subject = $this->getStandardMailSubject(e('New subtask'), $event_data); - break; - case Subtask::EVENT_UPDATE: - $subject = $this->getStandardMailSubject(e('Subtask updated'), $event_data); - break; - case Task::EVENT_CREATE: - $subject = $this->getStandardMailSubject(e('New task'), $event_data); - break; - case Task::EVENT_UPDATE: - $subject = $this->getStandardMailSubject(e('Task updated'), $event_data); - break; - case Task::EVENT_CLOSE: - $subject = $this->getStandardMailSubject(e('Task closed'), $event_data); - break; - case Task::EVENT_OPEN: - $subject = $this->getStandardMailSubject(e('Task opened'), $event_data); - break; - case Task::EVENT_MOVE_COLUMN: - $subject = $this->getStandardMailSubject(e('Column change'), $event_data); - break; - case Task::EVENT_MOVE_POSITION: - $subject = $this->getStandardMailSubject(e('Position change'), $event_data); - break; - case Task::EVENT_MOVE_SWIMLANE: - $subject = $this->getStandardMailSubject(e('Swimlane change'), $event_data); - break; - case Task::EVENT_ASSIGNEE_CHANGE: - $subject = $this->getStandardMailSubject(e('Assignee change'), $event_data); - break; - case Task::EVENT_USER_MENTION: - case Comment::EVENT_USER_MENTION: - $subject = $this->getStandardMailSubject(e('Mentioned'), $event_data); - break; - case Task::EVENT_OVERDUE: - $subject = e('[%s] Overdue tasks', $event_data['project_name']); - break; - default: - $subject = e('Notification'); - } - - return $subject; - } - - /** - * Get the mail subject for a given label - * - * @access private - * @param string $label Label - * @param array $data Template data - * @return string - */ - private function getStandardMailSubject($label, array $data) - { - return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']); - } -} diff --git a/app/Notification/MailNotification.php b/app/Notification/MailNotification.php new file mode 100644 index 00000000..0ba06715 --- /dev/null +++ b/app/Notification/MailNotification.php @@ -0,0 +1,151 @@ +emailClient->send( + $user['email'], + $user['name'] ?: $user['username'], + $this->getMailSubject($event_name, $event_data), + $this->getMailContent($event_name, $event_data) + ); + } + } + + /** + * Send notification to a project + * + * @access public + * @param array $project + * @param string $event_name + * @param array $event_data + */ + public function notifyProject(array $project, $event_name, array $event_data) + { + } + + /** + * Get the mail content for a given template name + * + * @access public + * @param string $event_name Event name + * @param array $event_data Event data + * @return string + */ + public function getMailContent($event_name, array $event_data) + { + return $this->template->render( + 'notification/'.str_replace('.', '_', $event_name), + $event_data + array('application_url' => $this->config->get('application_url')) + ); + } + + /** + * Get the mail subject for a given template name + * + * @access public + * @param string $event_name Event name + * @param array $event_data Event data + * @return string + */ + public function getMailSubject($event_name, array $event_data) + { + switch ($event_name) { + case TaskFile::EVENT_CREATE: + $subject = $this->getStandardMailSubject(e('New attachment'), $event_data); + break; + case Comment::EVENT_CREATE: + $subject = $this->getStandardMailSubject(e('New comment'), $event_data); + break; + case Comment::EVENT_UPDATE: + $subject = $this->getStandardMailSubject(e('Comment updated'), $event_data); + break; + case Subtask::EVENT_CREATE: + $subject = $this->getStandardMailSubject(e('New subtask'), $event_data); + break; + case Subtask::EVENT_UPDATE: + $subject = $this->getStandardMailSubject(e('Subtask updated'), $event_data); + break; + case Task::EVENT_CREATE: + $subject = $this->getStandardMailSubject(e('New task'), $event_data); + break; + case Task::EVENT_UPDATE: + $subject = $this->getStandardMailSubject(e('Task updated'), $event_data); + break; + case Task::EVENT_CLOSE: + $subject = $this->getStandardMailSubject(e('Task closed'), $event_data); + break; + case Task::EVENT_OPEN: + $subject = $this->getStandardMailSubject(e('Task opened'), $event_data); + break; + case Task::EVENT_MOVE_COLUMN: + $subject = $this->getStandardMailSubject(e('Column change'), $event_data); + break; + case Task::EVENT_MOVE_POSITION: + $subject = $this->getStandardMailSubject(e('Position change'), $event_data); + break; + case Task::EVENT_MOVE_SWIMLANE: + $subject = $this->getStandardMailSubject(e('Swimlane change'), $event_data); + break; + case Task::EVENT_ASSIGNEE_CHANGE: + $subject = $this->getStandardMailSubject(e('Assignee change'), $event_data); + break; + case Task::EVENT_USER_MENTION: + case Comment::EVENT_USER_MENTION: + $subject = $this->getStandardMailSubject(e('Mentioned'), $event_data); + break; + case Task::EVENT_OVERDUE: + $subject = e('[%s] Overdue tasks', $event_data['project_name']); + break; + default: + $subject = e('Notification'); + } + + return $subject; + } + + /** + * Get the mail subject for a given label + * + * @access private + * @param string $label Label + * @param array $data Template data + * @return string + */ + private function getStandardMailSubject($label, array $data) + { + return sprintf('[%s][%s] %s (#%d)', $data['task']['project_name'], $label, $data['task']['title'], $data['task']['id']); + } +} diff --git a/app/Notification/NotificationInterface.php b/app/Notification/NotificationInterface.php deleted file mode 100644 index 8431a77c..00000000 --- a/app/Notification/NotificationInterface.php +++ /dev/null @@ -1,32 +0,0 @@ -userUnreadNotification->create($user['id'], $event_name, $event_data); - } - - /** - * Send notification to a project - * - * @access public - * @param array $project - * @param string $event_name - * @param array $event_data - */ - public function notifyProject(array $project, $event_name, array $event_data) - { - } -} diff --git a/app/Notification/WebNotification.php b/app/Notification/WebNotification.php new file mode 100644 index 00000000..99c0c903 --- /dev/null +++ b/app/Notification/WebNotification.php @@ -0,0 +1,47 @@ +userUnreadNotification->create($user['id'], $event_name, $event_data); + } + + /** + * Send notification to a project + * + * @access public + * @param array $project + * @param string $event_name + * @param array $event_data + */ + public function notifyProject(array $project, $event_name, array $event_data) + { + } +} diff --git a/app/Notification/Webhook.php b/app/Notification/Webhook.php deleted file mode 100644 index e187909f..00000000 --- a/app/Notification/Webhook.php +++ /dev/null @@ -1,55 +0,0 @@ -config->get('webhook_url'); - $token = $this->config->get('webhook_token'); - - if (! empty($url)) { - if (strpos($url, '?') !== false) { - $url .= '&token='.$token; - } else { - $url .= '?token='.$token; - } - - $payload = array( - 'event_name' => $event_name, - 'event_data' => $event_data, - ); - - $this->httpClient->postJson($url, $payload); - } - } -} diff --git a/app/Notification/WebhookNotification.php b/app/Notification/WebhookNotification.php new file mode 100644 index 00000000..25d59251 --- /dev/null +++ b/app/Notification/WebhookNotification.php @@ -0,0 +1,56 @@ +config->get('webhook_url'); + $token = $this->config->get('webhook_token'); + + if (! empty($url)) { + if (strpos($url, '?') !== false) { + $url .= '&token='.$token; + } else { + $url .= '?token='.$token; + } + + $payload = array( + 'event_name' => $event_name, + 'event_data' => $event_data, + ); + + $this->httpClient->postJson($url, $payload); + } + } +} diff --git a/app/ServiceProvider/NotificationProvider.php b/app/ServiceProvider/NotificationProvider.php index 23d1d516..2cb01576 100644 --- a/app/ServiceProvider/NotificationProvider.php +++ b/app/ServiceProvider/NotificationProvider.php @@ -6,8 +6,8 @@ use Pimple\Container; use Pimple\ServiceProviderInterface; use Kanboard\Model\UserNotificationType; use Kanboard\Model\ProjectNotificationType; -use Kanboard\Notification\Mail as MailNotification; -use Kanboard\Notification\Web as WebNotification; +use Kanboard\Notification\MailNotification as MailNotification; +use Kanboard\Notification\WebNotification as WebNotification; /** * Notification Provider @@ -28,15 +28,15 @@ class NotificationProvider implements ServiceProviderInterface { $container['userNotificationType'] = function ($container) { $type = new UserNotificationType($container); - $type->setType(MailNotification::TYPE, t('Email'), '\Kanboard\Notification\Mail'); - $type->setType(WebNotification::TYPE, t('Web'), '\Kanboard\Notification\Web'); + $type->setType(MailNotification::TYPE, t('Email'), '\Kanboard\Notification\MailNotification'); + $type->setType(WebNotification::TYPE, t('Web'), '\Kanboard\Notification\WebNotification'); return $type; }; $container['projectNotificationType'] = function ($container) { $type = new ProjectNotificationType($container); - $type->setType('webhook', 'Webhook', '\Kanboard\Notification\Webhook', true); - $type->setType('activity_stream', 'ActivityStream', '\Kanboard\Notification\ActivityStream', true); + $type->setType('webhook', 'Webhook', '\Kanboard\Notification\WebhookNotification', true); + $type->setType('activity_stream', 'ActivityStream', '\Kanboard\Notification\ActivityStreamNotification', true); return $type; }; diff --git a/tests/units/Notification/MailTest.php b/tests/units/Notification/MailTest.php index 7dc6aaef..8d32b497 100644 --- a/tests/units/Notification/MailTest.php +++ b/tests/units/Notification/MailTest.php @@ -10,14 +10,14 @@ use Kanboard\Model\User; use Kanboard\Model\TaskFile; use Kanboard\Model\Project; use Kanboard\Model\Task; -use Kanboard\Notification\Mail; +use Kanboard\Notification\MailNotification; use Kanboard\Subscriber\NotificationSubscriber; class MailTest extends Base { public function testGetMailContent() { - $en = new Mail($this->container); + $en = new MailNotification($this->container); $p = new Project($this->container); $tf = new TaskFinder($this->container); $tc = new TaskCreation($this->container); @@ -62,7 +62,7 @@ class MailTest extends Base public function testSendWithEmailAddress() { - $en = new Mail($this->container); + $en = new MailNotification($this->container); $p = new Project($this->container); $tf = new TaskFinder($this->container); $tc = new TaskCreation($this->container); @@ -93,7 +93,7 @@ class MailTest extends Base public function testSendWithoutEmailAddress() { - $en = new Mail($this->container); + $en = new MailNotification($this->container); $p = new Project($this->container); $tf = new TaskFinder($this->container); $tc = new TaskCreation($this->container); -- cgit v1.2.3