Merge pull request #243 from nextcloud/activities

Add activities when you were invited to a call
This commit is contained in:
Ivan Sein 2017-02-28 15:47:38 +01:00 коммит произвёл GitHub
Родитель 9ec8d4ea6e c1b861d6ac
Коммит 1404261711
6 изменённых файлов: 742 добавлений и 5 удалений

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

@ -56,6 +56,16 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
<admin>OCA\Spreed\Settings\Admin</admin>
</settings>
<activity>
<settings>
<setting>OCA\Spreed\Activity\Setting</setting>
</settings>
<providers>
<provider>OCA\Spreed\Activity\Provider</provider>
</providers>
</activity>
<repair-steps>
<post-migration>
<step>OCA\Spreed\Migration\EmptyNameInsteadOfRandom</step>

202
lib/Activity/Provider.php Normal file
Просмотреть файл

@ -0,0 +1,202 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Spreed\Activity;
use OCA\Spreed\Exceptions\RoomNotFoundException;
use OCA\Spreed\Manager;
use OCA\Spreed\Room;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Activity\IProvider;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
class Provider implements IProvider {
/** @var IFactory */
protected $languageFactory;
/** @var IURLGenerator */
protected $url;
/** @var IManager */
protected $activityManager;
/** @var IUserManager */
protected $userManager;
/** @var Manager */
protected $manager;
/** @var string[] */
protected $displayNames = [];
/**
* @param IFactory $languageFactory
* @param IURLGenerator $url
* @param IManager $activityManager
* @param IUserManager $userManager
* @param Manager $manager
*/
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, Manager $manager) {
$this->languageFactory = $languageFactory;
$this->url = $url;
$this->activityManager = $activityManager;
$this->userManager = $userManager;
$this->manager = $manager;
}
/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws \InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== 'spreed') {
throw new \InvalidArgumentException();
}
$l = $this->languageFactory->get('spreed', $language);
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('spreed', 'app.svg')));
try {
$parameters = $event->getSubjectParameters();
$room = $this->manager->getRoomById((int) $parameters['room']);
if ($room->getName() === '') {
if ($room->getType() === Room::ONE_TO_ONE_CALL) {
$parsedParameters = $this->getParameters($parameters);
$subject = $l->t('{actor} invited you to a private call');
} else {
$parsedParameters = $this->getParameters($parameters);
$subject = $l->t('{actor} invited you to a group call');
}
} else {
$parsedParameters = $this->getParameters($parameters, $room);
$subject = $l->t('{actor} invited you to the call {call}');
}
} catch (RoomNotFoundException $e) {
throw new \InvalidArgumentException();
}
$this->setSubjects($event, $subject, $parsedParameters);
return $event;
}
/**
* @param IEvent $event
* @param string $subject
* @param array $parameters
* @throws \InvalidArgumentException
*/
protected function setSubjects(IEvent $event, $subject, array $parameters) {
$placeholders = $replacements = [];
foreach ($parameters as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
$replacements[] = $parameter['name'];
}
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
->setRichSubject($subject, $parameters);
}
/**
* @param array $parameters
* @param Room $room
* @return array
*/
protected function getParameters(array $parameters, Room $room = null) {
if ($room === null) {
return [
'actor' => $this->getUser($parameters['user']),
];
} else {
return [
'actor' => $this->getUser($parameters['user']),
'call' => $this->getRoom($room),
];
}
}
/**
* @param Room $room
* @return array
*/
protected function getRoom(Room $room) {
switch ($room->getType()) {
case Room::ONE_TO_ONE_CALL:
$stringType = 'one2one';
break;
case Room::GROUP_CALL:
$stringType = 'group';
break;
case Room::PUBLIC_CALL:
default:
$stringType = 'public';
break;
}
return [
'type' => 'call',
'id' => $room->getId(),
'name' => $room->getName(),
'call-type' => $stringType,
];
}
/**
* @param string $uid
* @return array
*/
protected function getUser($uid) {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);
}
return [
'type' => 'user',
'id' => $uid,
'name' => $this->displayNames[$uid],
];
}
/**
* @param string $uid
* @return string
*/
protected function getDisplayName($uid) {
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
return $user->getDisplayName();
} else {
return $uid;
}
}
}

98
lib/Activity/Setting.php Normal file
Просмотреть файл

@ -0,0 +1,98 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Spreed\Activity;
use OCP\Activity\ISetting;
use OCP\IL10N;
class Setting implements ISetting {
/** @var IL10N */
protected $l;
/**
* @param IL10N $l
*/
public function __construct(IL10N $l) {
$this->l = $l;
}
/**
* @return string Lowercase a-z and underscore only identifier
* @since 11.0.0
*/
public function getIdentifier() {
return 'spreed';
}
/**
* @return string A translated string
* @since 11.0.0
*/
public function getName() {
return $this->l->t('You were invited to a <strong>video call</strong>');
}
/**
* @return int whether the filter should be rather on the top or bottom of
* the admin section. The filters are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
* @since 11.0.0
*/
public function getPriority() {
return 51;
}
/**
* @return bool True when the option can be changed for the stream
* @since 11.0.0
*/
public function canChangeStream() {
return true;
}
/**
* @return bool True when the option can be changed for the stream
* @since 11.0.0
*/
public function isDefaultEnabledStream() {
return true;
}
/**
* @return bool True when the option can be changed for the mail
* @since 11.0.0
*/
public function canChangeMail() {
return true;
}
/**
* @return bool True when the option can be changed for the stream
* @since 11.0.0
*/
public function isDefaultEnabledMail() {
return false;
}
}

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

@ -28,6 +28,7 @@ namespace OCA\Spreed\Controller;
use OCA\Spreed\Exceptions\RoomNotFoundException;
use OCA\Spreed\Manager;
use OCA\Spreed\Room;
use OCP\Activity\IManager as IActivityManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
@ -39,7 +40,7 @@ use OCP\IUser;
use OCP\IUserManager;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\Notification\IManager;
use OCP\Notification\IManager as INotificationManager;
class ApiController extends Controller {
/** @var string */
@ -56,8 +57,10 @@ class ApiController extends Controller {
private $logger;
/** @var Manager */
private $manager;
/** @var IManager */
/** @var INotificationManager */
private $notificationManager;
/** @var IActivityManager */
private $activityManager;
/**
* @param string $appName
@ -69,7 +72,8 @@ class ApiController extends Controller {
* @param ISession $session
* @param ILogger $logger
* @param Manager $manager
* @param IManager $notificationManager
* @param INotificationManager $notificationManager
* @param IActivityManager $activityManager
*/
public function __construct($appName,
$UserId,
@ -80,7 +84,8 @@ class ApiController extends Controller {
ISession $session,
ILogger $logger,
Manager $manager,
IManager $notificationManager) {
INotificationManager $notificationManager,
IActivityManager $activityManager) {
parent::__construct($appName, $request);
$this->userId = $UserId;
$this->l10n = $l10n;
@ -90,6 +95,7 @@ class ApiController extends Controller {
$this->logger = $logger;
$this->manager = $manager;
$this->notificationManager = $notificationManager;
$this->activityManager = $activityManager;
}
/**
@ -571,10 +577,11 @@ class ApiController extends Controller {
*/
protected function createNotification(IUser $actor, IUser $user, Room $room) {
$notification = $this->notificationManager->createNotification();
$dateTime = new \DateTime();
try {
$notification->setApp('spreed')
->setUser($user->getUID())
->setDateTime(new \DateTime())
->setDateTime($dateTime)
->setObject('room', $room->getId())
->setSubject('invitation', [$actor->getUID()]);
$this->notificationManager->notify($notification);
@ -582,5 +589,27 @@ class ApiController extends Controller {
// Error while creating the notification
$this->logger->logException($e, ['app' => 'spreed']);
}
$event = $this->activityManager->generateEvent();
try {
$event->setApp('spreed')
->setType('spreed')
->setAuthor($actor->getUID())
->setAffectedUser($user->getUID())
->setObject('room', $room->getId(), $room->getName())
->setTimestamp($dateTime->getTimestamp())
->setSubject('invitation', [
'user' => $actor->getUID(),
'room' => $room->getId(),
'name' => $room->getName(),
]);
$this->activityManager->publish($event);
} catch (\InvalidArgumentException $e) {
// Error while creating the activity
$this->logger->logException($e, ['app' => 'spreed']);
} catch (\BadMethodCallException $e) {
// Error while sending the activity
$this->logger->logException($e, ['app' => 'spreed']);
}
}
}

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

@ -0,0 +1,281 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Spreed\Tests\php\Activity;
use OCA\Spreed\Activity\Provider;
use OCA\Spreed\Manager;
use OCA\Spreed\Room;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use Test\TestCase;
/**
* Class ProviderTest
*
* @package OCA\Spreed\Tests\php\Activity
*/
class ProviderTest extends TestCase {
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $l10nFactory;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
protected $url;
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
protected $activityManager;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
protected $userManager;
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
protected $manager;
public function setUp() {
parent::setUp();
$this->l10nFactory = $this->createMock(IFactory::class);
$this->url = $this->createMock(IURLGenerator::class);
$this->activityManager = $this->createMock(IManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->manager = $this->createMock(Manager::class);
}
/**
* @param string[] $methods
* @return Provider|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getProvider(array $methods = []) {
if (!empty($methods)) {
return $this->getMockBuilder(Provider::class)
->setConstructorArgs([
$this->l10nFactory,
$this->url,
$this->activityManager,
$this->userManager,
$this->manager,
])
->setMethods($methods)
->getMock();
}
return new Provider(
$this->l10nFactory,
$this->url,
$this->activityManager,
$this->userManager,
$this->manager
);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testParseThrows() {
/** @var IEvent|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->createMock(IEvent::class);
$event->expects($this->once())
->method('getApp')
->willReturn('activity');
$provider = $this->getProvider();
$provider->parse('en', $event, null);
}
public function dataSetSubject() {
return [
['No placeholder', [], 'No placeholder'],
['This has one {placeholder}', ['placeholder' => ['name' => 'foobar']], 'This has one foobar'],
['This has {number} {placeholders}', ['number' => ['name' => 'two'], 'placeholders' => ['name' => 'foobars']], 'This has two foobars'],
];
}
/**
* @dataProvider dataSetSubject
*
* @param string $subject
* @param array $parameters
* @param string $parsedSubject
*/
public function testSetSubject($subject, array $parameters, $parsedSubject) {
$provider = $this->getProvider();
$event = $this->createMock(IEvent::class);
$event->expects($this->once())
->method('setParsedSubject')
->with($parsedSubject)
->willReturnSelf();
$event->expects($this->once())
->method('setRichSubject')
->with($subject, $parameters)
->willReturnSelf();
self::invokePrivate($provider, 'setSubjects', [$event, $subject, $parameters]);
}
public function dataGetParameters() {
return [
['test', true, ['actor' => 'array(user)', 'call' => 'array(room)']],
['admin', false, ['actor' => 'array(user)']],
];
}
/**
* @dataProvider dataGetParameters
*
* @param string $user
* @param bool $isRoom
* @param array $expected
*/
public function testGetParameters($user, $isRoom, array $expected) {
$provider = $this->getProvider(['getUser', 'getRoom']);
$provider->expects($this->once())
->method('getUser')
->with($user)
->willReturn('array(user)');
if ($isRoom) {
$room = $this->createMock(Room::class);
$provider->expects($this->once())
->method('getRoom')
->with($room)
->willReturn('array(room)');
} else {
$room = null;
$provider->expects($this->never())
->method('getRoom');
}
$this->assertEquals($expected, self::invokePrivate($provider, 'getParameters', [['user' => $user], $room]));
}
public function dataGetRoom() {
return [
[Room::ONE_TO_ONE_CALL, 23, 'private-call', 'one2one'],
[Room::GROUP_CALL, 42, 'group-call', 'group'],
[Room::PUBLIC_CALL, 128, 'public-call', 'public'],
];
}
/**
* @dataProvider dataGetRoom
*
* @param int $type
* @param int $id
* @param string $name
* @param string $expectedType
*/
public function testGetRoom($type, $id, $name, $expectedType) {
$provider = $this->getProvider();
$room = $this->createMock(Room::class);
$room->expects($this->once())
->method('getType')
->willReturn($type);
$room->expects($this->once())
->method('getId')
->willReturn($id);
$room->expects($this->once())
->method('getName')
->willReturn($name);
$this->assertEquals([
'type' => 'call',
'id' => $id,
'name' => $name,
'call-type' => $expectedType,
], self::invokePrivate($provider, 'getRoom', [$room]));
}
public function dataGetUser() {
return [
['test', [], false, 'Test'],
['foo', ['admin' => 'Admin'], false, 'Bar'],
['admin', ['admin' => 'Administrator'], true, 'Administrator'],
];
}
/**
* @dataProvider dataGetUser
*
* @param string $uid
* @param array $cache
* @param bool $cacheHit
* @param string $name
*/
public function testGetUser($uid, $cache, $cacheHit, $name) {
$provider = $this->getProvider(['getDisplayName']);
self::invokePrivate($provider, 'displayNames', [$cache]);
if (!$cacheHit) {
$provider->expects($this->once())
->method('getDisplayName')
->with($uid)
->willReturn($name);
} else {
$provider->expects($this->never())
->method('getDisplayName');
}
$result = self::invokePrivate($provider, 'getUser', [$uid]);
$this->assertSame('user', $result['type']);
$this->assertSame($uid, $result['id']);
$this->assertSame($name, $result['name']);
}
public function dataGetDisplayName() {
return [
['test', true, 'Test'],
['foo', false, 'foo'],
];
}
/**
* @dataProvider dataGetDisplayName
*
* @param string $uid
* @param bool $validUser
* @param string $name
*/
public function testGetDisplayName($uid, $validUser, $name) {
$provider = $this->getProvider();
if ($validUser) {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getDisplayName')
->willReturn($name);
$this->userManager->expects($this->once())
->method('get')
->with($uid)
->willReturn($user);
} else {
$this->userManager->expects($this->once())
->method('get')
->with($uid)
->willReturn(null);
}
$this->assertSame($name, self::invokePrivate($provider, 'getDisplayName', [$uid]));
}
}

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

@ -0,0 +1,117 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Spreed\Tests\php\Activity;
use OCA\Spreed\Activity\Setting;
use OCP\Activity\ISetting;
use Test\TestCase;
class SettingTest extends TestCase {
public function dataSettings() {
return [
[Setting::class],
];
}
/**
* @dataProvider dataSettings
* @param string $settingClass
*/
public function testImplementsInterface($settingClass) {
$setting = \OC::$server->query($settingClass);
$this->assertInstanceOf(ISetting::class, $setting);
}
/**
* @dataProvider dataSettings
* @param string $settingClass
*/
public function testGetIdentifier($settingClass) {
/** @var ISetting $setting */
$setting = \OC::$server->query($settingClass);
$this->assertInternalType('string', $setting->getIdentifier());
}
/**
* @dataProvider dataSettings
* @param string $settingClass
*/
public function testGetName($settingClass) {
/** @var ISetting $setting */
$setting = \OC::$server->query($settingClass);
$this->assertInternalType('string', $setting->getName());
}
/**
* @dataProvider dataSettings
* @param string $settingClass
*/
public function testGetPriority($settingClass) {
/** @var ISetting $setting */
$setting = \OC::$server->query($settingClass);
$priority = $setting->getPriority();
$this->assertInternalType('int', $setting->getPriority());
$this->assertGreaterThanOrEqual(0, $priority);
$this->assertLessThanOrEqual(100, $priority);
}
/**
* @dataProvider dataSettings
* @param string $settingClass
*/
public function testCanChangeStream($settingClass) {
/** @var ISetting $setting */
$setting = \OC::$server->query($settingClass);
$this->assertInternalType('bool', $setting->canChangeStream());
}
/**
* @dataProvider dataSettings
* @param string $settingClass
*/
public function testIsDefaultEnabledStream($settingClass) {
/** @var ISetting $setting */
$setting = \OC::$server->query($settingClass);
$this->assertInternalType('bool', $setting->isDefaultEnabledStream());
}
/**
* @dataProvider dataSettings
* @param string $settingClass
*/
public function testCanChangeMail($settingClass) {
/** @var ISetting $setting */
$setting = \OC::$server->query($settingClass);
$this->assertInternalType('bool', $setting->canChangeMail());
}
/**
* @dataProvider dataSettings
* @param string $settingClass
*/
public function testIsDefaultEnabledMail($settingClass) {
/** @var ISetting $setting */
$setting = \OC::$server->query($settingClass);
$this->assertInternalType('bool', $setting->isDefaultEnabledMail());
}
}