cloud_bbb/lib/Activity/Provider.php

138 lines
3.3 KiB
PHP
Raw Normal View History

2020-09-07 22:18:23 +02:00
<?php
namespace OCA\BigBlueButton\Activity;
use OCA\BigBlueButton\AppInfo\Application;
use OCP\Activity\IProvider;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
2020-09-18 14:36:31 +02:00
use OCP\IL10N;
use OCP\IUserManager;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\IUser;
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 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;
/** @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,
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;
$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);
} else {
$event->setParsedSubject('Unknown subject: ' . $subject);
}
return $event;
}
private function parseRoomCreated(IEvent $event) {
$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) {
$params = $event->getSubjectParameters();
if ($this->activityManager->getCurrentUserId() === $event->getAuthor()) {
$event->setParsedSubject($this->l->t('You deleted the room %s.', [$params['name']]));
} else {
$subject = $this->l->t('{user} deleted the room %s', [$params['name']]);
$this->setSubjects($event, $subject, [
'user' => $this->getUser($event->getAuthor()),
]);
}
$this->setIcon($event, 'room-deleted');
}
private function setIcon(IEvent $event, string $baseName) {
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, $subject, array $parameters) {
$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);
}
protected function getUser($uid) {
$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
}
}