diff options
author | Joas Schilling <coding@schilljs.com> | 2017-10-10 18:09:37 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2017-10-10 18:10:30 +0300 |
commit | 8ef70af937ae52f64842958bed4584eee3b3a5fd (patch) | |
tree | 63dce9169c22d0c4664de82b439d64f2c307bd7a /lib/Activity | |
parent | 0cdb0cc21192d00bfe9cc463140e7478e7aec85d (diff) |
Activities for calls
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/Activity')
-rw-r--r-- | lib/Activity/Hooks.php | 86 | ||||
-rw-r--r-- | lib/Activity/Provider/Base.php (renamed from lib/Activity/Provider.php) | 58 | ||||
-rw-r--r-- | lib/Activity/Provider/Call.php | 163 | ||||
-rw-r--r-- | lib/Activity/Provider/Invitation.php | 100 |
4 files changed, 354 insertions, 53 deletions
diff --git a/lib/Activity/Hooks.php b/lib/Activity/Hooks.php new file mode 100644 index 000000000..476371817 --- /dev/null +++ b/lib/Activity/Hooks.php @@ -0,0 +1,86 @@ +<?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\Room; +use OCP\Activity\IManager; +use OCP\AppFramework\Utility\ITimeFactory; + +class Hooks { + + /** @var IManager */ + protected $activityManager; + + /** @var ITimeFactory */ + protected $timeFactory; + + /** + * @param IManager $activityManager + * @param ITimeFactory $timeFactory + */ + public function __construct(IManager $activityManager, ITimeFactory $timeFactory) { + $this->activityManager = $activityManager; + $this->timeFactory = $timeFactory; + } + + public function setActive(Room $room, $isGuest) { + $room->setActiveSince(new \DateTime(), $isGuest); + } + + public function generateActivity(Room $room) { + $activeSince = $room->getActiveSince(); + if (!$activeSince instanceof \DateTime || $room->hasActiveSessions()) { + return false; + } + + $duration = $this->timeFactory->getTime() - $activeSince->getTimestamp(); + $participants = $room->getParticipants($activeSince->getTimestamp()); + $userIds = array_keys($participants['users']); + + if (empty($userIds) || (count($userIds) === 1 && $room->getActiveGuests() === 0)) { + // Single user pinged or guests only => no activity + $room->resetActiveSince(); + return false; + } + + $event = $this->activityManager->generateEvent(); + $event->setApp('spreed') + ->setType('spreed') + ->setAuthor('') + ->setObject('room', $room->getId(), $room->getName()) + ->setTimestamp(time()) + ->setSubject('call', [ + 'room' => $room->getId(), + 'users' => $userIds, + 'guests' => $room->getActiveGuests(), + 'duration' => $duration, + ]); + + foreach ($userIds as $userId) { + $event->setAffectedUser($userId); + $this->activityManager->publish($event); + } + + $room->resetActiveSince(); + } + +} diff --git a/lib/Activity/Provider.php b/lib/Activity/Provider/Base.php index 215c93d33..02eddf75d 100644 --- a/lib/Activity/Provider.php +++ b/lib/Activity/Provider/Base.php @@ -19,7 +19,7 @@ * */ -namespace OCA\Spreed\Activity; +namespace OCA\Spreed\Activity\Provider; use OCA\Spreed\Exceptions\RoomNotFoundException; use OCA\Spreed\Manager; @@ -32,7 +32,7 @@ use OCP\IUser; use OCP\IUserManager; use OCP\L10N\IFactory; -class Provider implements IProvider { +abstract class Base implements IProvider { /** @var IFactory */ protected $languageFactory; @@ -68,49 +68,20 @@ class Provider implements IProvider { } /** - * @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) { + public function preParse(IEvent $event) { if ($event->getApp() !== 'spreed') { throw new \InvalidArgumentException(); } - - $l = $this->languageFactory->get('spreed', $language); - - if (method_exists($this->activityManager, 'getRequirePNG') && $this->activityManager->getRequirePNG()) { + if ($this->activityManager->getRequirePNG()) { $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('spreed', 'app-dark.png'))); } else { $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('spreed', 'app-dark.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; } @@ -132,24 +103,6 @@ class Provider implements IProvider { } /** - * @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 */ @@ -199,8 +152,7 @@ class Provider implements IProvider { $user = $this->userManager->get($uid); if ($user instanceof IUser) { return $user->getDisplayName(); - } else { - return $uid; } + return $uid; } } diff --git a/lib/Activity/Provider/Call.php b/lib/Activity/Provider/Call.php new file mode 100644 index 000000000..47526d948 --- /dev/null +++ b/lib/Activity/Provider/Call.php @@ -0,0 +1,163 @@ +<?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\Provider; + +use OCA\Spreed\Exceptions\RoomNotFoundException; +use OCA\Spreed\Room; +use OCP\Activity\IEvent; +use OCP\IL10N; + +class Call extends Base { + + /** + * @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) { + $event = parent::preParse($event); + $l = $this->languageFactory->get('spreed', $language); + + try { + $parameters = $event->getSubjectParameters(); + $room = $this->manager->getRoomById((int) $parameters['room']); + + if ($event->getSubject() === 'call') { + $result = $this->parseCall($event, $l, $room); + $result['subject'] .= ' ' . $this->getDuration($l, $parameters['duration']); + $this->setSubjects($event, $result['subject'], $result['params']); + } else { + throw new \InvalidArgumentException(); + } + } catch (RoomNotFoundException $e) { + throw new \InvalidArgumentException(); + } + + return $event; + } + + protected function getDuration(IL10N $l, $seconds) { + $hours = floor($seconds / 3600); + $seconds %= 3600; + $minutes = floor($seconds / 60); + $seconds %= 60; + + if ($hours > 0) { + $duration = sprintf('%1$d:%2$02d:%3$02d', $hours, $minutes, $seconds); + } else { + $duration = sprintf('%1$d:%2$02d', $minutes, $seconds); + } + + return $l->t('(Duration %s)', $duration); + } + + protected function parseCall(IEvent $event, IL10N $l, Room $room) { + $parameters = $event->getSubjectParameters(); + + $currentUser = array_search($this->activityManager->getCurrentUserId(), $parameters['users'], true); + if ($currentUser === false) { + throw new \InvalidArgumentException('Unknown case'); + } + unset($parameters['users'][$currentUser]); + sort($parameters['users']); + + if ($room->getType() === Room::ONE_TO_ONE_CALL) { + $otherUser = array_pop($parameters['users']); + + if ($otherUser === '') { + throw new \InvalidArgumentException('Unknown case'); + } + + return [ + 'subject' => $l->t('You had a private call with {user}'), + 'params' => [ + 'user' => $this->getUser($otherUser), + ], + ]; + } + + $numUsers = count($parameters['users']); + $displayedUsers = $numUsers; + switch ($numUsers) { + case 0: + $subject = $l->t('You had a call with {user1}'); + $subject = str_replace('{user1}', $l->n('%n guest', '%n guests', $parameters['guests']), $subject); + break; + case 1: + if ($parameters['guests'] === 0) { + $subject = $l->t('You had a call with {user1}'); + } else { + $subject = $l->t('You had a call with {user1} and {user2}'); + $subject = str_replace('{user2}', $l->n('%n guest', '%n guests', $parameters['guests']), $subject); + } + break; + case 2: + if ($parameters['guests'] === 0) { + $subject = $l->t('You had a call with {user1} and {user2}'); + } else { + $subject = $l->t('You had a call with {user1}, {user2} and {user3}'); + $subject = str_replace('{user3}', $l->n('%n guest', '%n guests', $parameters['guests']), $subject); + } + break; + case 3: + if ($parameters['guests'] === 0) { + $subject = $l->t('You had a call with {user1}, {user2} and {user3}'); + } else { + $subject = $l->t('You had a call with {user1}, {user2}, {user3} and {user4}'); + $subject = str_replace('{user4}', $l->n('%n guest', '%n guests', $parameters['guests']), $subject); + } + break; + case 4: + if ($parameters['guests'] === 0) { + $subject = $l->t('You had a call with {user1}, {user2}, {user3} and {user4}'); + } else { + $subject = $l->t('You had a call with {user1}, {user2}, {user3}, {user4} and {user5}'); + $subject = str_replace('{user5}', $l->n('%n guest', '%n guests', $parameters['guests']), $subject); + } + break; + case 5: + default: + $subject = $l->t('You had a call with {user1}, {user2}, {user3}, {user4} and {user5}'); + if ($numUsers === 5 && $parameters['guests'] === 0) { + $displayedUsers = 5; + } else { + $displayedUsers = 4; + $numOthers = $parameters['guests'] + $numUsers - $displayedUsers; + $subject = str_replace('{user5}', $l->n('%n other', '%n others', $numOthers), $subject); + } + } + + $params = []; + for ($i = 1; $i <= $displayedUsers; $i++) { + $params['user' . $i] = $this->getUser($parameters['users'][$i - 1]); + } + + return [ + 'subject' => $subject, + 'params' => $params, + ]; + } + +} diff --git a/lib/Activity/Provider/Invitation.php b/lib/Activity/Provider/Invitation.php new file mode 100644 index 000000000..866d474c5 --- /dev/null +++ b/lib/Activity/Provider/Invitation.php @@ -0,0 +1,100 @@ +<?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\Provider; + +use OCA\Spreed\Exceptions\RoomNotFoundException; +use OCA\Spreed\Room; +use OCP\Activity\IEvent; +use OCP\IL10N; + +class Invitation extends Base { + + /** + * @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) { + $event = parent::preParse($event); + $l = $this->languageFactory->get('spreed', $language); + + try { + $parameters = $event->getSubjectParameters(); + $room = $this->manager->getRoomById((int) $parameters['room']); + + if ($event->getSubject() === 'invitation') { + $result = $this->parseInvitation($event, $l, $room); + $this->setSubjects($event, $result['subject'], $result['params']); + } else { + throw new \InvalidArgumentException(); + } + } catch (RoomNotFoundException $e) { + throw new \InvalidArgumentException(); + } + + return $event; + } + + protected function parseInvitation(IEvent $event, IL10N $l, Room $room) { + $parameters = $event->getSubjectParameters(); + + if ($room->getName() === '') { + if ($room->getType() === Room::ONE_TO_ONE_CALL) { + $subject = $l->t('{actor} invited you to a private call'); + } else { + $subject = $l->t('{actor} invited you to a group call'); + } + + return [ + 'subject' => $subject, + 'params' => $this->getParameters($parameters), + ]; + } + + return [ + 'subject' => $l->t('{actor} invited you to the call {call}'), + 'params' => $this->getParameters($parameters, $room), + ]; + } + + /** + * @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']), + ]; + } + + return [ + 'actor' => $this->getUser($parameters['user']), + 'call' => $this->getRoom($room), + ]; + } + +} |