Add activities when you were invited to a call

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2017-02-09 17:22:52 +01:00
Родитель 9816ad1cd8
Коммит 680a018959
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E166FD8976B3BAC8
4 изменённых файлов: 343 добавлений и 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>

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

@ -0,0 +1,201 @@
<?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 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 IEvent $event
* @param string $subject
* @param array $parameters
*/
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 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']);
}
}
}