Merge pull request #202 from owncloud/issue/201-extension-emails

Add email activities for activities from the manager and test the consumer
This commit is contained in:
Joas Schilling 2014-12-22 11:51:44 +01:00
Родитель 5cf18de914 9c42477532
Коммит 22fc9604ab
6 изменённых файлов: 200 добавлений и 11 удалений

Просмотреть файл

@ -34,10 +34,11 @@ $navigationEntry = array(
'href' => $c->query('URLGenerator')->linkToRoute('activity.Activities.showList'),
'icon' => $c->query('URLGenerator')->imagePath('activity', 'activity.svg'),
);
$c->query('ServerContainer')->getNavigationManager()->add($navigationEntry);
$c->getServer()->getNavigationManager()->add($navigationEntry);
// register the hooks for filesystem operations. All other events from other apps has to be send via the public api
\OCA\Activity\HooksStatic::register();
\OCA\Activity\Consumer::register($c->getServer()->getActivityManager(), $c);
// Personal settings for notifications and emails
\OCP\App::registerPersonal($c->getAppName(), 'personal');

Просмотреть файл

@ -23,6 +23,7 @@
namespace OCA\Activity\AppInfo;
use OC\Files\View;
use OCA\Activity\Consumer;
use OCA\Activity\Controller\Activities;
use OCA\Activity\Controller\Settings;
use OCA\Activity\Data;
@ -53,6 +54,13 @@ class Application extends App {
return $c->query('ServerContainer')->getL10N('activity');
});
$container->registerService('Consumer', function(IContainer $c) {
return new Consumer(
$c->query('UserSettings')
);
});
$container->registerService('DataHelper', function(IContainer $c) {
/** @var \OC\Server $server */
$server = $c->query('ServerContainer');

Просмотреть файл

@ -23,9 +23,34 @@
namespace OCA\Activity;
use OCP\Activity\IConsumer;
use OCP\Activity\IManager;
use OCP\AppFramework\IAppContainer;
class Consumer implements IConsumer {
/**
* Registers the consumer to the Activity Manager
*
* @param IManager $am
* @param IAppContainer $container
*/
public static function register(IManager $am, IAppContainer $container) {
$am->registerConsumer(function() use ($am, $container) {
return $container->query('Consumer');
});
}
/** @var UserSettings */
protected $userSettings;
/**
* Constructor
*
* @param UserSettings $userSettings
*/
public function __construct(UserSettings $userSettings) {
$this->userSettings = $userSettings;
}
class Consumer implements IConsumer
{
/**
* Send an event into the activity stream of a user
*
@ -41,7 +66,21 @@ class Consumer implements IConsumer
* @param int $priority Priority of the notification
* @return null
*/
function receive($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
Data::send($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority);
public function receive($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
$selfAction = substr($subject, -5) === '_self';
$streamSetting = $this->userSettings->getUserSetting($affectedUser, 'stream', $type);
$emailSetting = $this->userSettings->getUserSetting($affectedUser, 'email', $type);
$emailSetting = ($emailSetting) ? $this->userSettings->getUserSetting($affectedUser, 'setting', 'batchtime') : false;
// Add activity to stream
if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($affectedUser, 'setting', 'self'))) {
Data::send($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority);
}
// Add activity to mail queue
if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($affectedUser, 'setting', 'selfemail'))) {
$latestSend = time() + $emailSetting;
Data::storeMail($app, $subject, $subjectParams, $affectedUser, $type, $latestSend);
}
}
}

Просмотреть файл

@ -40,12 +40,6 @@ class HooksStatic {
Util::connectHook('OCP\Share', 'post_shared', 'OCA\Activity\HooksStatic', 'share');
Util::connectHook('OC_User', 'post_deleteUser', 'OCA\Activity\HooksStatic', 'deleteUser');
// hooking up the activity manager
$am = \OC::$server->getActivityManager();
$am->registerConsumer(function() {
return new Consumer();
});
}
/**

Просмотреть файл

@ -46,6 +46,8 @@ class ApplicationTest extends TestCase {
return array(
array('ActivityData', 'OCA\Activity\Data'),
array('ActivityL10N', 'OCP\IL10N'),
array('Consumer', 'OCA\Activity\Consumer'),
array('Consumer', 'OCP\Activity\IConsumer'),
array('DataHelper', 'OCA\Activity\DataHelper'),
array('GroupHelper', 'OCA\Activity\GroupHelper'),
array('Hooks', 'OCA\Activity\Hooks'),

145
tests/consumertest.php Normal file
Просмотреть файл

@ -0,0 +1,145 @@
<?php
/**
* ownCloud - Activity App
*
* @author Joas Schilling
* @copyright 2014 Joas Schilling nickvergessen@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\Activity\Tests;
use OC\ActivityManager;
use OCA\Activity\Consumer;
use OCP\Activity\IConsumer;
use OCP\DB;
class ConsumerTest extends TestCase {
/** @var \OCA\Activity\Consumer */
protected $consumer;
/** @var \OCA\Activity\UserSettings */
protected $userSettings;
protected function setUp() {
parent::setUp();
$this->deleteTestActivities();
$this->userSettings = $this->getMockBuilder('OCA\Activity\UserSettings')
->setMethods(array('getUserSetting'))
->disableOriginalConstructor()
->getMock();
$this->userSettings->expects($this->any())
->method('getUserSetting')
->with($this->stringContains('affectedUser'), $this->anything(), $this->anything())
->will($this->returnValueMap(array(
array('affectedUser', 'stream', 'type', true),
array('affectedUser', 'setting', 'self', true),
array('affectedUser2', 'setting', 'self', false),
array('affectedUser', 'email', 'type', true),
array('affectedUser', 'setting', 'selfemail', true),
array('affectedUser2', 'setting', 'selfemail', false),
array('affectedUser', 'setting', 'batchtime', 10),
array('affectedUser2', 'setting', 'batchtime', 10),
)));
$this->consumer = new Consumer($this->userSettings);
$this->assertTrue($this->consumer instanceof IConsumer, 'Consumer does not implement \OCP\Activity\IConsumer');
}
protected function tearDown() {
$this->deleteTestActivities();
parent::tearDown();
}
protected function deleteTestActivities() {
$query = DB::prepare('DELETE FROM `*PREFIX*activity` WHERE `app` = ?');
$query->execute(array('test'));
$query = DB::prepare('DELETE FROM `*PREFIX*activity_mq` WHERE `amq_appid` = ?');
$query->execute(array('test'));
}
public function receiveData() {
return [
['type', 'affectedUser', 'subject', 'affectedUser'],
['type2', 'affectedUser', 'subject', false],
['type', 'affectedUser', 'subject_self', 'affectedUser'],
['type', 'affectedUser2', 'subject_self', false],
];
}
/**
* @dataProvider receiveData
*
* @param string $type
* @param string $affectedUser
* @param string $subject
* @param array|false $expected
*/
public function testReceiveStream($type, $affectedUser, $subject, $expected) {
$this->consumer->receive('test', $subject, ['subjectParam1', 'subjectParam2'], 'message', ['messageParam1', 'messageParam2'], 'file', 'link', $affectedUser, $type, \OCP\Activity\IExtension::PRIORITY_HIGH);
$query = DB::prepare("SELECT `affecteduser` FROM `*PREFIX*activity` WHERE `app` = 'test'");
$result = $query->execute();
$this->assertEquals($expected, $result->fetchOne(0));
$this->assertEquals(false, $result->fetchRow());
}
/**
* @dataProvider receiveData
*
* @param string $type
* @param string $affectedUser
* @param string $subject
* @param array|false $expected
*/
public function testReceiveEmail($type, $affectedUser, $subject, $expected) {
$this->consumer->receive('test', $subject, ['subjectParam1', 'subjectParam2'], 'message', ['messageParam1', 'messageParam2'], 'file', 'link', $affectedUser, $type, \OCP\Activity\IExtension::PRIORITY_HIGH);
$query = DB::prepare("SELECT `amq_affecteduser` FROM `*PREFIX*activity_mq` WHERE `amq_appid` = 'test'");
$result = $query->execute();
$this->assertEquals($expected, $result->fetchOne(0));
$this->assertEquals(false, $result->fetchRow());
}
/**
* @dataProvider receiveData
*
* @param string $type
* @param string $affectedUser
* @param string $subject
* @param array|false $expected
*/
public function testRegister($type, $affectedUser, $subject, $expected) {
$activityManager = new ActivityManager();
$container = $this->getMock('\OCP\AppFramework\IAppContainer');
$container->expects($this->any())
->method('query')
->with($this->stringContains('Consumer'))
->will($this->returnValueMap(array(
array('Consumer', $this->consumer),
)));
Consumer::register($activityManager, $container);
$activityManager->publishActivity('test', $subject, ['subjectParam1', 'subjectParam2'], 'message', ['messageParam1', 'messageParam2'], 'file', 'link', $affectedUser, $type, \OCP\Activity\IExtension::PRIORITY_HIGH);
$query = DB::prepare("SELECT `affecteduser` FROM `*PREFIX*activity` WHERE `app` = 'test'");
$result = $query->execute();
$this->assertEquals($expected, $result->fetchOne(0));
$this->assertEquals(false, $result->fetchRow());
}
}