2013-09-29 22:31:12 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-11-16 14:19:42 +03:00
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
2016-07-21 18:07:57 +03:00
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-09-29 22:31:12 +04:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-05-02 12:57:24 +03:00
|
|
|
namespace OC\Activity;
|
2013-09-29 22:31:12 +04:00
|
|
|
|
2020-06-15 16:01:52 +03:00
|
|
|
use OCP\Activity\ActivitySettings;
|
2013-09-29 22:31:12 +04:00
|
|
|
use OCP\Activity\IConsumer;
|
2015-08-19 18:37:55 +03:00
|
|
|
use OCP\Activity\IEvent;
|
2016-10-20 18:57:44 +03:00
|
|
|
use OCP\Activity\IFilter;
|
2013-10-09 20:06:21 +04:00
|
|
|
use OCP\Activity\IManager;
|
2016-11-04 13:33:33 +03:00
|
|
|
use OCP\Activity\IProvider;
|
2016-10-25 19:00:25 +03:00
|
|
|
use OCP\Activity\ISetting;
|
2015-03-25 17:18:11 +03:00
|
|
|
use OCP\IConfig;
|
2020-08-05 16:57:51 +03:00
|
|
|
use OCP\IL10N;
|
2015-03-25 17:18:11 +03:00
|
|
|
use OCP\IRequest;
|
2015-08-19 18:37:55 +03:00
|
|
|
use OCP\IUser;
|
2015-03-25 17:18:11 +03:00
|
|
|
use OCP\IUserSession;
|
2016-11-10 17:33:00 +03:00
|
|
|
use OCP\RichObjectStrings\IValidator;
|
2013-09-29 22:31:12 +04:00
|
|
|
|
2016-05-02 12:57:24 +03:00
|
|
|
class Manager implements IManager {
|
2015-03-25 17:18:11 +03:00
|
|
|
/** @var IRequest */
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/** @var IUserSession */
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2016-11-10 17:33:00 +03:00
|
|
|
/** @var IValidator */
|
|
|
|
protected $validator;
|
|
|
|
|
2015-10-02 10:53:39 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $formattingObjectType;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $formattingObjectId;
|
|
|
|
|
2017-06-20 13:40:45 +03:00
|
|
|
/** @var bool */
|
2019-02-18 02:13:02 +03:00
|
|
|
protected $requirePNG = false;
|
2017-06-20 13:40:45 +03:00
|
|
|
|
2016-03-24 11:33:00 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $currentUserId;
|
|
|
|
|
2020-08-05 16:57:51 +03:00
|
|
|
protected $l10n;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
IRequest $request,
|
|
|
|
IUserSession $session,
|
|
|
|
IConfig $config,
|
|
|
|
IValidator $validator,
|
|
|
|
IL10N $l10n
|
|
|
|
) {
|
2015-03-25 17:18:11 +03:00
|
|
|
$this->request = $request;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->config = $config;
|
2016-11-10 17:33:00 +03:00
|
|
|
$this->validator = $validator;
|
2020-08-05 16:57:51 +03:00
|
|
|
$this->l10n = $l10n;
|
2015-03-25 17:18:11 +03:00
|
|
|
}
|
2013-09-29 22:31:12 +04:00
|
|
|
|
2015-07-23 12:00:29 +03:00
|
|
|
/** @var \Closure[] */
|
2020-03-26 11:30:18 +03:00
|
|
|
private $consumersClosures = [];
|
2015-07-23 12:00:29 +03:00
|
|
|
|
|
|
|
/** @var IConsumer[] */
|
2020-03-26 11:30:18 +03:00
|
|
|
private $consumers = [];
|
2013-09-29 22:31:12 +04:00
|
|
|
|
2015-07-23 12:00:29 +03:00
|
|
|
/**
|
|
|
|
* @return \OCP\Activity\IConsumer[]
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
protected function getConsumers(): array {
|
2015-07-23 12:00:29 +03:00
|
|
|
if (!empty($this->consumers)) {
|
|
|
|
return $this->consumers;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->consumers = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($this->consumersClosures as $consumer) {
|
2015-07-23 12:00:29 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:37:55 +03:00
|
|
|
/**
|
2015-08-20 16:35:24 +03:00
|
|
|
* Generates a new IEvent object
|
|
|
|
*
|
|
|
|
* Make sure to call at least the following methods before sending it to the
|
|
|
|
* app with via the publish() method:
|
|
|
|
* - setApp()
|
|
|
|
* - setType()
|
|
|
|
* - setAffectedUser()
|
|
|
|
* - setSubject()
|
|
|
|
*
|
2015-08-19 18:37:55 +03:00
|
|
|
* @return IEvent
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function generateEvent(): IEvent {
|
2016-11-10 17:33:00 +03:00
|
|
|
return new Event($this->validator);
|
2015-08-19 18:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish an event to the activity consumers
|
|
|
|
*
|
2015-08-20 16:35:24 +03:00
|
|
|
* Make sure to call at least the following methods before sending an Event:
|
|
|
|
* - setApp()
|
|
|
|
* - setType()
|
|
|
|
* - setAffectedUser()
|
|
|
|
* - setSubject()
|
|
|
|
*
|
2015-08-19 18:37:55 +03:00
|
|
|
* @param IEvent $event
|
|
|
|
* @throws \BadMethodCallException if required values have not been set
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function publish(IEvent $event): void {
|
2016-11-10 17:33:00 +03:00
|
|
|
if ($event->getAuthor() === '') {
|
2015-08-19 18:37:55 +03:00
|
|
|
if ($this->session->getUser() instanceof IUser) {
|
|
|
|
$event->setAuthor($this->session->getUser()->getUID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$event->getTimestamp()) {
|
|
|
|
$event->setTimestamp(time());
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:44:57 +03:00
|
|
|
if (!$event->isValid()) {
|
2016-10-28 15:55:53 +03:00
|
|
|
throw new \BadMethodCallException('The given event is invalid');
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:37:55 +03:00
|
|
|
foreach ($this->getConsumers() as $c) {
|
|
|
|
$c->receive($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-29 22:31:12 +04:00
|
|
|
/**
|
|
|
|
* In order to improve lazy loading a closure can be registered which will be called in case
|
|
|
|
* activity consumers are actually requested
|
|
|
|
*
|
|
|
|
* $callable has to return an instance of OCA\Activity\IConsumer
|
|
|
|
*
|
|
|
|
* @param \Closure $callable
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function registerConsumer(\Closure $callable): void {
|
2018-01-26 00:36:03 +03:00
|
|
|
$this->consumersClosures[] = $callable;
|
2015-07-23 12:00:29 +03:00
|
|
|
$this->consumers = [];
|
2013-09-29 22:31:12 +04:00
|
|
|
}
|
2013-10-09 20:06:21 +04:00
|
|
|
|
2016-10-25 19:00:25 +03:00
|
|
|
/** @var string[] */
|
2016-11-11 13:27:39 +03:00
|
|
|
protected $filterClasses = [];
|
2016-10-20 18:57:44 +03:00
|
|
|
|
|
|
|
/** @var IFilter[] */
|
2016-11-11 13:27:39 +03:00
|
|
|
protected $filters = [];
|
2016-10-20 18:57:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filter Class must implement OCA\Activity\IFilter
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function registerFilter(string $filter): void {
|
2016-10-20 18:57:44 +03:00
|
|
|
$this->filterClasses[$filter] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IFilter[]
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function getFilters(): array {
|
2016-10-20 18:57:44 +03:00
|
|
|
foreach ($this->filterClasses as $class => $false) {
|
|
|
|
/** @var IFilter $filter */
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $id
|
|
|
|
* @return IFilter
|
|
|
|
* @throws \InvalidArgumentException when the filter was not found
|
2016-11-16 11:29:27 +03:00
|
|
|
* @since 11.0.0
|
2016-10-20 18:57:44 +03:00
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function getFilterById(string $id): IFilter {
|
2016-10-20 18:57:44 +03:00
|
|
|
$filters = $this->getFilters();
|
|
|
|
|
|
|
|
if (isset($filters[$id])) {
|
|
|
|
return $filters[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException('Requested filter does not exist');
|
|
|
|
}
|
|
|
|
|
2016-11-04 13:33:33 +03:00
|
|
|
/** @var string[] */
|
2016-11-11 13:27:39 +03:00
|
|
|
protected $providerClasses = [];
|
2016-11-04 13:33:33 +03:00
|
|
|
|
|
|
|
/** @var IProvider[] */
|
2016-11-11 13:27:39 +03:00
|
|
|
protected $providers = [];
|
2016-11-04 13:33:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $provider Class must implement OCA\Activity\IProvider
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function registerProvider(string $provider): void {
|
2016-11-04 13:33:33 +03:00
|
|
|
$this->providerClasses[$provider] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IProvider[]
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function getProviders(): array {
|
2016-11-04 13:33:33 +03:00
|
|
|
foreach ($this->providerClasses as $class => $false) {
|
|
|
|
/** @var IProvider $provider */
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2016-10-25 19:00:25 +03:00
|
|
|
/** @var string[] */
|
2016-11-11 13:27:39 +03:00
|
|
|
protected $settingsClasses = [];
|
2016-10-25 19:00:25 +03:00
|
|
|
|
|
|
|
/** @var ISetting[] */
|
2016-11-11 13:27:39 +03:00
|
|
|
protected $settings = [];
|
2016-10-25 19:00:25 +03:00
|
|
|
|
2014-07-03 15:48:15 +04:00
|
|
|
/**
|
2016-10-25 19:00:25 +03:00
|
|
|
* @param string $setting Class must implement OCA\Activity\ISetting
|
|
|
|
* @return void
|
2014-07-03 15:48:15 +04:00
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function registerSetting(string $setting): void {
|
2016-10-25 19:00:25 +03:00
|
|
|
$this->settingsClasses[$setting] = false;
|
|
|
|
}
|
2015-08-28 19:40:50 +03:00
|
|
|
|
2016-10-25 19:00:25 +03:00
|
|
|
/**
|
2020-06-15 16:01:52 +03:00
|
|
|
* @return ActivitySettings[]
|
2016-10-25 19:00:25 +03:00
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function getSettings(): array {
|
2016-10-25 19:00:25 +03:00
|
|
|
foreach ($this->settingsClasses as $class => $false) {
|
|
|
|
/** @var ISetting $setting */
|
|
|
|
$setting = \OC::$server->query($class);
|
|
|
|
|
2020-07-30 18:52:23 +03:00
|
|
|
if ($setting instanceof ISetting) {
|
2020-07-23 17:39:27 +03:00
|
|
|
if (!$setting instanceof ActivitySettings) {
|
2020-08-05 16:57:51 +03:00
|
|
|
$setting = new ActivitySettingsAdapter($setting, $this->l10n);
|
2020-07-23 17:39:27 +03:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-25 19:00:25 +03:00
|
|
|
throw new \InvalidArgumentException('Invalid activity filter registered');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->settings[$setting->getIdentifier()] = $setting;
|
|
|
|
|
|
|
|
unset($this->settingsClasses[$class]);
|
|
|
|
}
|
|
|
|
return $this->settings;
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-25 19:00:25 +03:00
|
|
|
* @param string $id
|
2020-06-15 16:01:52 +03:00
|
|
|
* @return ActivitySettings
|
2016-10-25 19:00:25 +03:00
|
|
|
* @throws \InvalidArgumentException when the setting was not found
|
2016-11-16 11:29:27 +03:00
|
|
|
* @since 11.0.0
|
2014-07-03 15:48:15 +04:00
|
|
|
*/
|
2020-06-15 16:01:52 +03:00
|
|
|
public function getSettingById(string $id): ActivitySettings {
|
2016-10-25 19:00:25 +03:00
|
|
|
$settings = $this->getSettings();
|
|
|
|
|
|
|
|
if (isset($settings[$id])) {
|
|
|
|
return $settings[$id];
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
2016-10-25 19:00:25 +03:00
|
|
|
|
|
|
|
throw new \InvalidArgumentException('Requested setting does not exist');
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-02 10:53:39 +03:00
|
|
|
/**
|
|
|
|
* @param string $type
|
2019-01-24 18:52:38 +03:00
|
|
|
* @param int $id
|
2015-10-02 10:53:39 +03:00
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function setFormattingObject(string $type, int $id): void {
|
2015-10-02 10:53:39 +03:00
|
|
|
$this->formattingObjectType = $type;
|
2019-01-24 18:52:38 +03:00
|
|
|
$this->formattingObjectId = $id;
|
2015-10-02 10:53:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function isFormattingFilteredObject(): bool {
|
2016-03-24 11:32:11 +03:00
|
|
|
return $this->formattingObjectType !== null && $this->formattingObjectId !== null
|
|
|
|
&& $this->formattingObjectType === $this->request->getParam('object_type')
|
2019-01-24 18:52:38 +03:00
|
|
|
&& $this->formattingObjectId === (int) $this->request->getParam('object_id');
|
2015-10-02 10:53:39 +03:00
|
|
|
}
|
|
|
|
|
2017-06-20 13:40:45 +03:00
|
|
|
/**
|
|
|
|
* @param bool $status Set to true, when parsing events should not use SVG icons
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function setRequirePNG(bool $status): void {
|
2017-06-20 13:40:45 +03:00
|
|
|
$this->requirePNG = $status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function getRequirePNG(): bool {
|
2017-06-20 13:40:45 +03:00
|
|
|
return $this->requirePNG;
|
|
|
|
}
|
|
|
|
|
2016-10-20 18:57:44 +03:00
|
|
|
/**
|
|
|
|
* Set the user we need to use
|
|
|
|
*
|
|
|
|
* @param string|null $currentUserId
|
|
|
|
* @throws \UnexpectedValueException If the user is invalid
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function setCurrentUserId(string $currentUserId = null): void {
|
2016-10-20 18:57:44 +03:00
|
|
|
if (!is_string($currentUserId) && $currentUserId !== null) {
|
|
|
|
throw new \UnexpectedValueException('The given current user is invalid');
|
|
|
|
}
|
|
|
|
$this->currentUserId = $currentUserId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user we need to use
|
|
|
|
*
|
|
|
|
* Either the user is logged in, or we try to get it from the token
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
public function getCurrentUserId(): string {
|
2016-10-20 18:57:44 +03:00
|
|
|
if ($this->currentUserId !== null) {
|
|
|
|
return $this->currentUserId;
|
2019-01-24 18:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->session->isLoggedIn()) {
|
2016-10-20 18:57:44 +03:00
|
|
|
return $this->getUserFromToken();
|
|
|
|
}
|
2019-01-24 18:52:38 +03:00
|
|
|
|
|
|
|
return $this->session->getUser()->getUID();
|
2016-10-20 18:57:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user for the token
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
|
|
|
|
*/
|
2019-01-24 18:52:38 +03:00
|
|
|
protected function getUserFromToken(): string {
|
2016-10-20 18:57:44 +03:00
|
|
|
$token = (string) $this->request->getParam('token', '');
|
|
|
|
if (strlen($token) !== 30) {
|
|
|
|
throw new \UnexpectedValueException('The token is invalid');
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
|
|
|
|
|
2017-07-23 22:03:26 +03:00
|
|
|
if (count($users) !== 1) {
|
2016-10-20 18:57:44 +03:00
|
|
|
// No unique user found
|
|
|
|
throw new \UnexpectedValueException('The token is invalid');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token found login as that user
|
|
|
|
return array_shift($users);
|
|
|
|
}
|
2013-09-29 22:31:12 +04:00
|
|
|
}
|