Move ProjectActivitySubscriber to a new notification type

This commit is contained in:
Frederic Guillot 2015-10-17 12:53:11 -04:00
parent 472f94efee
commit 9153c6ff0d
7 changed files with 106 additions and 78 deletions

View File

@ -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 '';
}

View File

@ -0,0 +1,58 @@
<?php
namespace Kanboard\Notification;
use Kanboard\Core\Base;
/**
* Activity Stream Notification
*
* @package notification
* @author Frederic Guillot
*/
class ActivityStream extends Base implements NotificationInterface
{
/**
* Send notification to a user
*
* @access public
* @param array $user
* @param string $event_name
* @param array $event_data
*/
public function notifyUser(array $user, $event_name, array $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)
{
if ($this->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
);
}
}
}
}

View File

@ -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);
}
}
}

View File

@ -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;
};

View File

@ -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));

View File

@ -1,74 +0,0 @@
<?php
namespace Kanboard\Subscriber;
use Kanboard\Event\GenericEvent;
use Kanboard\Model\Task;
use Kanboard\Model\Comment;
use Kanboard\Model\Subtask;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProjectActivitySubscriber extends \Kanboard\Core\Base implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
Task::EVENT_ASSIGNEE_CHANGE => 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;
}
}

View File

@ -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);