cloud_bbb/lib/Activity/Provider.php

205 lines
5.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;
2020-09-18 17:13:46 +02:00
use OCA\BigBlueButton\Db\RoomShare;
2020-09-07 22:18:23 +02:00
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;
2020-09-18 17:13:46 +02:00
use OCP\IGroupManager;
2020-09-18 14:36:31 +02:00
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 17:13:46 +02:00
/** @var string */
public const SHARE_CREATED = 'share_created';
/** @var string */
public const SHARE_DELETED = 'share_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;
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-18 14:36:31 +02:00
}
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');
}
2020-09-18 17:13:46 +02:00
private function parseShareCreated(IEvent $event) {
$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) {
$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');
}
2020-09-18 14:36:31 +02:00
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
}
2020-09-18 17:13:46 +02:00
protected function getGroup($uid) {
$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
}