cloud_bbb/lib/Activity/Provider.php

261 lines
7.0 KiB
PHP
Raw Normal View History

2020-09-07 22:18:23 +02:00
<?php
namespace OCA\BigBlueButton\Activity;
use OCA\BigBlueButton\AppInfo\Application;
2020-09-18 17:13:46 +02:00
use OCA\BigBlueButton\Db\RoomShare;
2020-09-07 22:18:23 +02:00
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
2020-09-23 12:33:09 +02:00
use OCP\Activity\IProvider;
2020-09-18 17:13:46 +02:00
use OCP\IGroupManager;
2020-09-23 12:33:09 +02:00
use OCP\IL10N;
2020-09-18 14:36:31 +02:00
use OCP\IURLGenerator;
use OCP\IUser;
2020-09-23 12:33:09 +02:00
use OCP\IUserManager;
use OCP\L10N\IFactory;
2020-09-07 22:18:23 +02:00
class Provider implements IProvider {
/** @var string */
public const ROOM_CREATED = 'room_created';
/** @var string */
public const ROOM_DELETED = 'room_deleted';
2020-09-18 17:13:46 +02:00
/** @var string */
public const SHARE_CREATED = 'share_created';
/** @var string */
public const SHARE_DELETED = 'share_deleted';
2020-09-22 16:02:18 +02:00
/** @var string */
public const MEETING_STARTED = 'meeting_started';
/** @var string */
public const MEETING_ENDED = 'meeting_ended';
/** @var string */
public const RECORDING_READY = 'recording_ready';
2020-09-18 14:36:31 +02:00
/** @var IL10N */
protected $l;
2020-09-07 22:18:23 +02:00
/** @var IManager */
protected $activityManager;
2020-09-18 14:36:31 +02:00
/** @var IUserManager */
protected $userManager;
2020-09-18 17:13:46 +02:00
/** @var IGroupManager */
protected $groupManager;
2020-09-18 14:36:31 +02:00
/** @var IURLGenerator */
protected $url;
/** @var IFactory */
protected $languageFactory;
2020-09-07 22:18:23 +02:00
public function __construct(
2020-09-18 14:36:31 +02:00
IManager $manager,
IUserManager $userManager,
2020-09-18 17:13:46 +02:00
IGroupManager $groupManager,
2020-09-18 14:36:31 +02:00
IURLGenerator $url,
IFactory $languageFactory
2020-09-07 22:18:23 +02:00
) {
$this->activityManager = $manager;
2020-09-18 14:36:31 +02:00
$this->userManager = $userManager;
2020-09-18 17:13:46 +02:00
$this->groupManager = $groupManager;
2020-09-18 14:36:31 +02:00
$this->url = $url;
$this->languageFactory = $languageFactory;
2020-09-07 22:18:23 +02:00
}
public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== Application::ID) {
throw new \InvalidArgumentException();
}
2020-09-18 14:36:31 +02:00
$this->l = $this->languageFactory->get(Application::ID, $language);
2020-09-07 22:18:23 +02:00
$subject = $event->getSubject();
if ($subject === self::ROOM_CREATED) {
2020-09-18 14:36:31 +02:00
$this->parseRoomCreated($event);
2020-09-07 22:18:23 +02:00
} elseif ($subject === self::ROOM_DELETED) {
2020-09-18 14:36:31 +02:00
$this->parseRoomDeleted($event);
2020-09-18 17:13:46 +02:00
} elseif ($subject === self::SHARE_CREATED) {
$this->parseShareCreated($event);
} elseif ($subject === self::SHARE_DELETED) {
$this->parseShareDeleted($event);
2020-09-22 16:02:18 +02:00
} elseif ($subject === self::MEETING_STARTED) {
$this->parseMeetingStarted($event);
} elseif ($subject === self::MEETING_ENDED) {
$this->parseMeetingEnded($event);
} elseif ($subject === self::RECORDING_READY) {
$this->parseRecordingReady($event);
2020-09-18 14:36:31 +02:00
}
return $event;
}
private function parseRoomCreated(IEvent $event): void {
2020-09-18 14:36:31 +02:00
$params = $event->getSubjectParameters();
$event->setParsedSubject($this->l->t('You created the room %s.', [$params['name']]));
$this->setIcon($event, 'room-created');
}
private function parseRoomDeleted(IEvent $event): void {
2020-09-18 14:36:31 +02:00
$params = $event->getSubjectParameters();
if ($this->activityManager->getCurrentUserId() === $event->getAuthor()) {
$event->setParsedSubject($this->l->t('You deleted the room %s.', [$params['name']]));
} else {
2020-09-23 14:59:18 +02:00
$subject = $this->l->t('{user} deleted the room %s.', [$params['name']]);
2020-09-18 14:36:31 +02:00
$this->setSubjects($event, $subject, [
'user' => $this->getUser($event->getAuthor()),
]);
}
$this->setIcon($event, 'room-deleted');
}
private function parseShareCreated(IEvent $event): void {
2020-09-18 17:13:46 +02:00
$params = $event->getSubjectParameters();
if ($this->activityManager->getCurrentUserId() === $event->getAuthor()) {
$subject = $this->l->t('You shared the room %s with {shareWith}.', [$params['name']]);
} else {
$subject = $this->l->t('{user} shared the room %s with you.', [$params['name']]);
}
$this->setSubjects($event, $subject, [
'user' => $this->getUser($event->getAuthor()),
'shareWith' => $params['shareType'] === RoomShare::SHARE_TYPE_USER ? $this->getUser($params['shareWith']) : $this->getGroup($params['shareWith']),
]);
$this->setIcon($event, 'share-created');
}
private function parseShareDeleted(IEvent $event): void {
2020-09-18 17:13:46 +02:00
$params = $event->getSubjectParameters();
if ($this->activityManager->getCurrentUserId() === $event->getAuthor()) {
$subject = $this->l->t('You unshared the room %s with {shareWith}.', [$params['name']]);
} else {
$subject = $this->l->t('{user} unshared the room %s with you.', [$params['name']]);
}
$this->setSubjects($event, $subject, [
'user' => $this->getUser($event->getAuthor()),
'shareWith' => $params['shareType'] === RoomShare::SHARE_TYPE_USER ? $this->getUser($params['shareWith']) : $this->getGroup($params['shareWith']),
]);
$this->setIcon($event, 'share-deleted');
}
private function parseMeetingStarted(IEvent $event): void {
2020-09-22 16:02:18 +02:00
$params = $event->getSubjectParameters();
if ($this->activityManager->getCurrentUserId() === $event->getAuthor()) {
$subject = $this->l->t('You started a meeting in the "%s" room.', [$params['name']]);
} else {
$subject = $this->l->t('{user} started a meeting in the "%s" room.', [$params['name']]);
}
$this->setSubjects($event, $subject, [
'user' => $this->getUser($event->getAuthor()),
]);
$this->setIcon($event, 'meeting-started');
}
private function parseMeetingEnded(IEvent $event): void {
2020-09-22 16:02:18 +02:00
$params = $event->getSubjectParameters();
$event->setParsedSubject($this->l->t('The meeting in room "%s" has ended.', [$params['name']]));
$this->setIcon($event, 'meeting-ended');
}
private function parseRecordingReady(IEvent $event): void {
2020-09-22 16:02:18 +02:00
$params = $event->getSubjectParameters();
$event->setParsedSubject($this->l->t('Recording for room "%s" is ready.', [$params['name']]));
$this->setIcon($event, 'recording-ready');
}
private function setIcon(IEvent $event, string $baseName): void {
2020-09-18 14:36:31 +02:00
if ($this->activityManager->getRequirePNG()) {
$imagePath = $this->url->imagePath(Application::ID, 'actions/'.$baseName.'.png');
} else {
$imagePath = $this->url->imagePath(Application::ID, 'actions/'.$baseName.'.svg');
}
$event->setIcon($this->url->getAbsoluteURL($imagePath));
}
private function setSubjects(IEvent $event, string $subject, array $parameters): void {
2020-09-18 14:36:31 +02:00
$placeholders = $replacements = [];
foreach ($parameters as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
if ($parameter['type'] === 'file') {
$replacements[] = $parameter['path'];
2020-09-07 22:18:23 +02:00
} else {
2020-09-18 14:36:31 +02:00
$replacements[] = $parameter['name'];
2020-09-07 22:18:23 +02:00
}
}
2020-09-18 14:36:31 +02:00
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
->setRichSubject($subject, $parameters);
}
/**
* @return string[]
*
* @psalm-return array{type: string, id: string, name: string}
*/
protected function getUser(string $uid): array {
2020-09-18 14:36:31 +02:00
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
return [
'type' => 'user',
'id' => $user->getUID(),
'name' => $user->getDisplayName(),
];
}
return [
'type' => 'user',
'id' => $uid,
'name' => $uid,
];
2020-09-07 22:18:23 +02:00
}
2020-09-18 17:13:46 +02:00
/**
* @return (mixed|string)[]
*
* @psalm-return array{type: string, id: mixed|string, name: mixed|string}
*/
protected function getGroup($uid): array {
2020-09-18 17:13:46 +02:00
$group = $this->groupManager->get($uid);
if ($group !== null) {
return [
'type' => 'user-group',
'id' => $group->getGID(),
'name' => $group->getDisplayName(),
];
}
return [
'type' => 'user-group',
'id' => $uid,
'name' => $uid,
];
}
2020-09-07 22:18:23 +02:00
}