From 9153c6ff0ddb3170928d33599d9178e67ca466b6 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 17 Oct 2015 12:53:11 -0400 Subject: Move ProjectActivitySubscriber to a new notification type --- app/Model/ProjectActivity.php | 2 + app/Notification/ActivityStream.php | 58 +++++++++++++++++++ app/Notification/Webhook.php | 2 +- app/ServiceProvider/ClassProvider.php | 3 +- app/ServiceProvider/EventDispatcherProvider.php | 2 - app/Subscriber/ProjectActivitySubscriber.php | 74 ------------------------- tests/units/Model/ProjectActivityTest.php | 43 ++++++++++++++ 7 files changed, 106 insertions(+), 78 deletions(-) create mode 100644 app/Notification/ActivityStream.php delete mode 100644 app/Subscriber/ProjectActivitySubscriber.php diff --git a/app/Model/ProjectActivity.php b/app/Model/ProjectActivity.php index 2cc1c975..032c1fa6 100644 --- a/app/Model/ProjectActivity.php +++ b/app/Model/ProjectActivity.php @@ -238,6 +238,8 @@ class ProjectActivity extends Base return t('%s updated a comment on the task #%d', $event['author'], $event['task']['id']); case Comment::EVENT_CREATE: return t('%s commented on the task #%d', $event['author'], $event['task']['id']); + case File::EVENT_CREATE: + return t('%s attached a file to the task #%d', $event['author'], $event['task']['id']); default: return ''; } diff --git a/app/Notification/ActivityStream.php b/app/Notification/ActivityStream.php new file mode 100644 index 00000000..65ae64b1 --- /dev/null +++ b/app/Notification/ActivityStream.php @@ -0,0 +1,58 @@ +userSession->isLogged()) { + + $this->projectActivity->createEvent( + $project['id'], + $event_data['task']['id'], + $this->userSession->getId(), + $event_name, + $event_data + ); + + // TODO: need to be moved to external plugins + foreach (array('slackWebhook', 'hipchatWebhook', 'jabber') as $model) { + $this->$model->notify( + $project['id'], + $event_data['task']['id'], + $event_name, + $event_data + ); + } + } + } +} diff --git a/app/Notification/Webhook.php b/app/Notification/Webhook.php index feeee5d6..e187909f 100644 --- a/app/Notification/Webhook.php +++ b/app/Notification/Webhook.php @@ -49,7 +49,7 @@ class Webhook extends Base implements NotificationInterface 'event_data' => $event_data, ); - return $this->httpClient->postJson($url, $payload); + $this->httpClient->postJson($url, $payload); } } } diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index 182ecf26..b969c289 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -138,7 +138,8 @@ class ClassProvider implements ServiceProviderInterface $container['projectNotificationType'] = function ($container) { $type = new ProjectNotificationType($container); - $type->setType('webhook', t('Webhook'), '\Kanboard\Notification\Webhook', true); + $type->setType('webhook', 'Webhook', '\Kanboard\Notification\Webhook', true); + $type->setType('activity_stream', 'ActivityStream', '\Kanboard\Notification\ActivityStream', true); return $type; }; diff --git a/app/ServiceProvider/EventDispatcherProvider.php b/app/ServiceProvider/EventDispatcherProvider.php index 3c9404d0..1711919e 100644 --- a/app/ServiceProvider/EventDispatcherProvider.php +++ b/app/ServiceProvider/EventDispatcherProvider.php @@ -8,7 +8,6 @@ use Symfony\Component\EventDispatcher\EventDispatcher; use Kanboard\Subscriber\AuthSubscriber; use Kanboard\Subscriber\BootstrapSubscriber; use Kanboard\Subscriber\NotificationSubscriber; -use Kanboard\Subscriber\ProjectActivitySubscriber; use Kanboard\Subscriber\ProjectDailySummarySubscriber; use Kanboard\Subscriber\ProjectModificationDateSubscriber; use Kanboard\Subscriber\SubtaskTimeTrackingSubscriber; @@ -23,7 +22,6 @@ class EventDispatcherProvider implements ServiceProviderInterface $container['dispatcher'] = new EventDispatcher; $container['dispatcher']->addSubscriber(new BootstrapSubscriber($container)); $container['dispatcher']->addSubscriber(new AuthSubscriber($container)); - $container['dispatcher']->addSubscriber(new ProjectActivitySubscriber($container)); $container['dispatcher']->addSubscriber(new ProjectDailySummarySubscriber($container)); $container['dispatcher']->addSubscriber(new ProjectModificationDateSubscriber($container)); $container['dispatcher']->addSubscriber(new NotificationSubscriber($container)); diff --git a/app/Subscriber/ProjectActivitySubscriber.php b/app/Subscriber/ProjectActivitySubscriber.php deleted file mode 100644 index a49a00ed..00000000 --- a/app/Subscriber/ProjectActivitySubscriber.php +++ /dev/null @@ -1,74 +0,0 @@ - array('execute', 0), - Task::EVENT_UPDATE => array('execute', 0), - Task::EVENT_CREATE => array('execute', 0), - Task::EVENT_CLOSE => array('execute', 0), - Task::EVENT_OPEN => array('execute', 0), - Task::EVENT_MOVE_COLUMN => array('execute', 0), - Task::EVENT_MOVE_POSITION => array('execute', 0), - Task::EVENT_MOVE_SWIMLANE => array('execute', 0), - Comment::EVENT_UPDATE => array('execute', 0), - Comment::EVENT_CREATE => array('execute', 0), - Subtask::EVENT_UPDATE => array('execute', 0), - Subtask::EVENT_CREATE => array('execute', 0), - ); - } - - public function execute(GenericEvent $event, $event_name) - { - // Executed only when someone is logged - if ($this->userSession->isLogged() && isset($event['task_id'])) { - $values = $this->getValues($event); - - $this->projectActivity->createEvent( - $values['task']['project_id'], - $values['task']['id'], - $this->userSession->getId(), - $event_name, - $values - ); - - // Send notifications to third-party services - foreach (array('slackWebhook', 'hipchatWebhook', 'jabber') as $model) { - $this->$model->notify( - $values['task']['project_id'], - $values['task']['id'], - $event_name, - $values - ); - } - } - } - - private function getValues(GenericEvent $event) - { - $values = array(); - $values['task'] = $this->taskFinder->getDetails($event['task_id']); - $values['changes'] = isset($event['changes']) ? $event['changes'] : array(); - - switch (get_class($event)) { - case 'Kanboard\Event\SubtaskEvent': - $values['subtask'] = $this->subtask->getById($event['id'], true); - break; - case 'Kanboard\Event\CommentEvent': - $values['comment'] = $this->comment->getById($event['id']); - break; - } - - return $values; - } -} diff --git a/tests/units/Model/ProjectActivityTest.php b/tests/units/Model/ProjectActivityTest.php index e8adc10d..ead74e08 100644 --- a/tests/units/Model/ProjectActivityTest.php +++ b/tests/units/Model/ProjectActivityTest.php @@ -7,9 +7,52 @@ use Kanboard\Model\TaskFinder; use Kanboard\Model\TaskCreation; use Kanboard\Model\ProjectActivity; use Kanboard\Model\Project; +use Kanboard\Model\Subtask; +use Kanboard\Model\Comment; +use Kanboard\Model\File; +use Kanboard\Subscriber\NotificationSubscriber; class ProjectActivityTest extends Base { + public function testGetTitle() + { + $pa = new ProjectActivity($this->container); + $p = new Project($this->container); + $tf = new TaskFinder($this->container); + $tc = new TaskCreation($this->container); + $s = new Subtask($this->container); + $c = new Comment($this->container); + $f = new File($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); + $this->assertEquals(1, $s->create(array('title' => 'test', 'task_id' => 1))); + $this->assertEquals(1, $c->create(array('comment' => 'test', 'task_id' => 1, 'user_id' => 1))); + $this->assertEquals(1, $f->create(1, 'test', 'blah', 123)); + + $task = $tf->getDetails(1); + $subtask = $s->getById(1, true); + $comment = $c->getById(1); + $file = $c->getById(1); + + $this->assertNotEmpty($task); + $this->assertNotEmpty($subtask); + $this->assertNotEmpty($comment); + $this->assertNotEmpty($file); + + foreach (NotificationSubscriber::getSubscribedEvents() as $event_name => $listeners) { + $this->assertNotEmpty($pa->getTitle(array( + 'event_name' => $event_name, + 'task' => $task, + 'comment' => $comment, + 'subtask' => $subtask, + 'file' => $file, + 'author' => 'bob', + 'changes' => array()) + )); + } + } + public function testDecode() { $e = new ProjectActivity($this->container); -- cgit v1.2.3