123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- <?php
- namespace OC\Activity;
- use OCP\Activity\ActivitySettings;
- use OCP\Activity\IConsumer;
- use OCP\Activity\IEvent;
- use OCP\Activity\IFilter;
- use OCP\Activity\IManager;
- use OCP\Activity\IProvider;
- use OCP\Activity\ISetting;
- use OCP\IConfig;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\IUser;
- use OCP\IUserSession;
- use OCP\RichObjectStrings\IValidator;
- class Manager implements IManager {
-
- protected $request;
-
- protected $session;
-
- protected $config;
-
- protected $validator;
-
- protected $formattingObjectType;
-
- protected $formattingObjectId;
-
- protected $requirePNG = false;
-
- protected $currentUserId;
- protected $l10n;
- public function __construct(
- IRequest $request,
- IUserSession $session,
- IConfig $config,
- IValidator $validator,
- IL10N $l10n
- ) {
- $this->request = $request;
- $this->session = $session;
- $this->config = $config;
- $this->validator = $validator;
- $this->l10n = $l10n;
- }
-
- private $consumersClosures = [];
-
- private $consumers = [];
-
- protected function getConsumers(): array {
- if (!empty($this->consumers)) {
- return $this->consumers;
- }
- $this->consumers = [];
- foreach ($this->consumersClosures as $consumer) {
- $c = $consumer();
- if ($c instanceof IConsumer) {
- $this->consumers[] = $c;
- } else {
- throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
- }
- }
- return $this->consumers;
- }
-
- public function generateEvent(): IEvent {
- return new Event($this->validator);
- }
-
- public function publish(IEvent $event): void {
- if ($event->getAuthor() === '') {
- if ($this->session->getUser() instanceof IUser) {
- $event->setAuthor($this->session->getUser()->getUID());
- }
- }
- if (!$event->getTimestamp()) {
- $event->setTimestamp(time());
- }
- if (!$event->isValid()) {
- throw new \BadMethodCallException('The given event is invalid');
- }
- foreach ($this->getConsumers() as $c) {
- $c->receive($event);
- }
- }
-
- public function registerConsumer(\Closure $callable): void {
- $this->consumersClosures[] = $callable;
- $this->consumers = [];
- }
-
- protected $filterClasses = [];
-
- protected $filters = [];
-
- public function registerFilter(string $filter): void {
- $this->filterClasses[$filter] = false;
- }
-
- public function getFilters(): array {
- foreach ($this->filterClasses as $class => $false) {
-
- $filter = \OC::$server->query($class);
- if (!$filter instanceof IFilter) {
- throw new \InvalidArgumentException('Invalid activity filter registered');
- }
- $this->filters[$filter->getIdentifier()] = $filter;
- unset($this->filterClasses[$class]);
- }
- return $this->filters;
- }
-
- public function getFilterById(string $id): IFilter {
- $filters = $this->getFilters();
- if (isset($filters[$id])) {
- return $filters[$id];
- }
- throw new \InvalidArgumentException('Requested filter does not exist');
- }
-
- protected $providerClasses = [];
-
- protected $providers = [];
-
- public function registerProvider(string $provider): void {
- $this->providerClasses[$provider] = false;
- }
-
- public function getProviders(): array {
- foreach ($this->providerClasses as $class => $false) {
-
- $provider = \OC::$server->query($class);
- if (!$provider instanceof IProvider) {
- throw new \InvalidArgumentException('Invalid activity provider registered');
- }
- $this->providers[] = $provider;
- unset($this->providerClasses[$class]);
- }
- return $this->providers;
- }
-
- protected $settingsClasses = [];
-
- protected $settings = [];
-
- public function registerSetting(string $setting): void {
- $this->settingsClasses[$setting] = false;
- }
-
- public function getSettings(): array {
- foreach ($this->settingsClasses as $class => $false) {
-
- $setting = \OC::$server->query($class);
- if ($setting instanceof ISetting) {
- if (!$setting instanceof ActivitySettings) {
- $setting = new ActivitySettingsAdapter($setting, $this->l10n);
- }
- } else {
- throw new \InvalidArgumentException('Invalid activity filter registered');
- }
- $this->settings[$setting->getIdentifier()] = $setting;
- unset($this->settingsClasses[$class]);
- }
- return $this->settings;
- }
-
- public function getSettingById(string $id): ActivitySettings {
- $settings = $this->getSettings();
- if (isset($settings[$id])) {
- return $settings[$id];
- }
- throw new \InvalidArgumentException('Requested setting does not exist');
- }
-
- public function setFormattingObject(string $type, int $id): void {
- $this->formattingObjectType = $type;
- $this->formattingObjectId = $id;
- }
-
- public function isFormattingFilteredObject(): bool {
- return $this->formattingObjectType !== null && $this->formattingObjectId !== null
- && $this->formattingObjectType === $this->request->getParam('object_type')
- && $this->formattingObjectId === (int) $this->request->getParam('object_id');
- }
-
- public function setRequirePNG(bool $status): void {
- $this->requirePNG = $status;
- }
-
- public function getRequirePNG(): bool {
- return $this->requirePNG;
- }
-
- public function setCurrentUserId(string $currentUserId = null): void {
- if (!is_string($currentUserId) && $currentUserId !== null) {
- throw new \UnexpectedValueException('The given current user is invalid');
- }
- $this->currentUserId = $currentUserId;
- }
-
- public function getCurrentUserId(): string {
- if ($this->currentUserId !== null) {
- return $this->currentUserId;
- }
- if (!$this->session->isLoggedIn()) {
- return $this->getUserFromToken();
- }
- return $this->session->getUser()->getUID();
- }
-
- protected function getUserFromToken(): string {
- $token = (string) $this->request->getParam('token', '');
- if (strlen($token) !== 30) {
- throw new \UnexpectedValueException('The token is invalid');
- }
- $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
- if (count($users) !== 1) {
-
- throw new \UnexpectedValueException('The token is invalid');
- }
-
- return array_shift($users);
- }
- }
|