migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
* @author Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Activity;
use OCP\Activity\ActivitySettings;
use OCP\Activity\ISetting;
use OCP\IL10N;
/**
* Adapt the old interface based settings into the new abstract
* class based one
*/
class ActivitySettingsAdapter extends ActivitySettings {
private $oldSettings;
private $l10n;
public function __construct(ISetting $oldSettings, IL10N $l10n) {
$this->oldSettings = $oldSettings;
$this->l10n = $l10n;
}
public function getIdentifier() {
return $this->oldSettings->getIdentifier();
}
public function getName() {
return $this->oldSettings->getName();
}
public function getGroupIdentifier() {
return 'other';
}
public function getGroupName() {
return $this->l10n->t('Other activities');
}
public function getPriority() {
return $this->oldSettings->getPriority();
}
public function canChangeMail() {
return $this->oldSettings->canChangeMail();
}
public function isDefaultEnabledMail() {
return $this->oldSettings->isDefaultEnabledMail();
}
}
+577
View File
@@ -0,0 +1,577 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Phil Davis <phil.davis@inf.org>
* @author Robin Appelman <robin@icewind.nl>
*
* @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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Activity;
use OCP\Activity\IEvent;
use OCP\RichObjectStrings\InvalidObjectExeption;
use OCP\RichObjectStrings\IValidator;
class Event implements IEvent {
/** @var string */
protected $app = '';
/** @var string */
protected $type = '';
/** @var string */
protected $affectedUser = '';
/** @var string */
protected $author = '';
/** @var int */
protected $timestamp = 0;
/** @var string */
protected $subject = '';
/** @var array */
protected $subjectParameters = [];
/** @var string */
protected $subjectParsed = '';
/** @var string */
protected $subjectRich = '';
/** @var array */
protected $subjectRichParameters = [];
/** @var string */
protected $message = '';
/** @var array */
protected $messageParameters = [];
/** @var string */
protected $messageParsed = '';
/** @var string */
protected $messageRich = '';
/** @var array */
protected $messageRichParameters = [];
/** @var string */
protected $objectType = '';
/** @var int */
protected $objectId = 0;
/** @var string */
protected $objectName = '';
/** @var string */
protected $link = '';
/** @var string */
protected $icon = '';
/** @var bool */
protected $generateNotification = true;
/** @var IEvent|null */
protected $child;
/** @var IValidator */
protected $richValidator;
/**
* @param IValidator $richValidator
*/
public function __construct(IValidator $richValidator) {
$this->richValidator = $richValidator;
}
/**
* Set the app of the activity
*
* @param string $app
* @return IEvent
* @throws \InvalidArgumentException if the app id is invalid
* @since 8.2.0
*/
public function setApp(string $app): IEvent {
if ($app === '' || isset($app[32])) {
throw new \InvalidArgumentException('The given app is invalid');
}
$this->app = $app;
return $this;
}
/**
* @return string
*/
public function getApp(): string {
return $this->app;
}
/**
* Set the type of the activity
*
* @param string $type
* @return IEvent
* @throws \InvalidArgumentException if the type is invalid
* @since 8.2.0
*/
public function setType(string $type): IEvent {
if ($type === '' || isset($type[255])) {
throw new \InvalidArgumentException('The given type is invalid');
}
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getType(): string {
return $this->type;
}
/**
* Set the affected user of the activity
*
* @param string $affectedUser
* @return IEvent
* @throws \InvalidArgumentException if the affected user is invalid
* @since 8.2.0
*/
public function setAffectedUser(string $affectedUser): IEvent {
if ($affectedUser === '' || isset($affectedUser[64])) {
throw new \InvalidArgumentException('The given affected user is invalid');
}
$this->affectedUser = $affectedUser;
return $this;
}
/**
* @return string
*/
public function getAffectedUser(): string {
return $this->affectedUser;
}
/**
* Set the author of the activity
*
* @param string $author
* @return IEvent
* @throws \InvalidArgumentException if the author is invalid
* @since 8.2.0
*/
public function setAuthor(string $author): IEvent {
if (isset($author[64])) {
throw new \InvalidArgumentException('The given author user is invalid');
}
$this->author = $author;
return $this;
}
/**
* @return string
*/
public function getAuthor(): string {
return $this->author;
}
/**
* Set the timestamp of the activity
*
* @param int $timestamp
* @return IEvent
* @throws \InvalidArgumentException if the timestamp is invalid
* @since 8.2.0
*/
public function setTimestamp(int $timestamp): IEvent {
$this->timestamp = $timestamp;
return $this;
}
/**
* @return int
*/
public function getTimestamp(): int {
return $this->timestamp;
}
/**
* Set the subject of the activity
*
* @param string $subject
* @param array $parameters
* @return IEvent
* @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 8.2.0
*/
public function setSubject(string $subject, array $parameters = []): IEvent {
if (isset($subject[255])) {
throw new \InvalidArgumentException('The given subject is invalid');
}
$this->subject = $subject;
$this->subjectParameters = $parameters;
return $this;
}
/**
* @return string
*/
public function getSubject(): string {
return $this->subject;
}
/**
* @return array
*/
public function getSubjectParameters(): array {
return $this->subjectParameters;
}
/**
* @param string $subject
* @return $this
* @throws \InvalidArgumentException if the subject is invalid
* @since 11.0.0
*/
public function setParsedSubject(string $subject): IEvent {
if ($subject === '') {
throw new \InvalidArgumentException('The given parsed subject is invalid');
}
$this->subjectParsed = $subject;
return $this;
}
/**
* @return string
* @since 11.0.0
*/
public function getParsedSubject(): string {
return $this->subjectParsed;
}
/**
* @param string $subject
* @param array $parameters
* @return $this
* @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 11.0.0
*/
public function setRichSubject(string $subject, array $parameters = []): IEvent {
if ($subject === '') {
throw new \InvalidArgumentException('The given parsed subject is invalid');
}
$this->subjectRich = $subject;
$this->subjectRichParameters = $parameters;
if ($this->subjectParsed === '') {
$this->subjectParsed = $this->richToParsed($subject, $parameters);
}
return $this;
}
/**
* @throws \InvalidArgumentException if a parameter has no name or no type
*/
private function richToParsed(string $message, array $parameters): string {
$placeholders = [];
$replacements = [];
foreach ($parameters as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
foreach (['name','type'] as $requiredField) {
if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
}
}
if ($parameter['type'] === 'user') {
$replacements[] = '@' . $parameter['name'];
} elseif ($parameter['type'] === 'file') {
$replacements[] = $parameter['path'] ?? $parameter['name'];
} else {
$replacements[] = $parameter['name'];
}
}
return str_replace($placeholders, $replacements, $message);
}
/**
* @return string
* @since 11.0.0
*/
public function getRichSubject(): string {
return $this->subjectRich;
}
/**
* @return array[]
* @since 11.0.0
*/
public function getRichSubjectParameters(): array {
return $this->subjectRichParameters;
}
/**
* Set the message of the activity
*
* @param string $message
* @param array $parameters
* @return IEvent
* @throws \InvalidArgumentException if the message or parameters are invalid
* @since 8.2.0
*/
public function setMessage(string $message, array $parameters = []): IEvent {
if (isset($message[255])) {
throw new \InvalidArgumentException('The given message is invalid');
}
$this->message = $message;
$this->messageParameters = $parameters;
return $this;
}
/**
* @return string
*/
public function getMessage(): string {
return $this->message;
}
/**
* @return array
*/
public function getMessageParameters(): array {
return $this->messageParameters;
}
/**
* @param string $message
* @return $this
* @throws \InvalidArgumentException if the message is invalid
* @since 11.0.0
*/
public function setParsedMessage(string $message): IEvent {
$this->messageParsed = $message;
return $this;
}
/**
* @return string
* @since 11.0.0
*/
public function getParsedMessage(): string {
return $this->messageParsed;
}
/**
* @param string $message
* @param array $parameters
* @return $this
* @throws \InvalidArgumentException if the subject or parameters are invalid
* @since 11.0.0
*/
public function setRichMessage(string $message, array $parameters = []): IEvent {
$this->messageRich = $message;
$this->messageRichParameters = $parameters;
if ($this->messageParsed === '') {
$this->messageParsed = $this->richToParsed($message, $parameters);
}
return $this;
}
/**
* @return string
* @since 11.0.0
*/
public function getRichMessage(): string {
return $this->messageRich;
}
/**
* @return array[]
* @since 11.0.0
*/
public function getRichMessageParameters(): array {
return $this->messageRichParameters;
}
/**
* Set the object of the activity
*
* @param string $objectType
* @param int $objectId
* @param string $objectName
* @return IEvent
* @throws \InvalidArgumentException if the object is invalid
* @since 8.2.0
*/
public function setObject(string $objectType, int $objectId, string $objectName = ''): IEvent {
if (isset($objectType[255])) {
throw new \InvalidArgumentException('The given object type is invalid');
}
if (isset($objectName[4000])) {
throw new \InvalidArgumentException('The given object name is invalid');
}
$this->objectType = $objectType;
$this->objectId = $objectId;
$this->objectName = $objectName;
return $this;
}
/**
* @return string
*/
public function getObjectType(): string {
return $this->objectType;
}
/**
* @return int
*/
public function getObjectId(): int {
return $this->objectId;
}
/**
* @return string
*/
public function getObjectName(): string {
return $this->objectName;
}
/**
* Set the link of the activity
*
* @param string $link
* @return IEvent
* @throws \InvalidArgumentException if the link is invalid
* @since 8.2.0
*/
public function setLink(string $link): IEvent {
if (isset($link[4000])) {
throw new \InvalidArgumentException('The given link is invalid');
}
$this->link = $link;
return $this;
}
/**
* @return string
*/
public function getLink(): string {
return $this->link;
}
/**
* @param string $icon
* @return $this
* @throws \InvalidArgumentException if the icon is invalid
* @since 11.0.0
*/
public function setIcon(string $icon): IEvent {
if (isset($icon[4000])) {
throw new \InvalidArgumentException('The given icon is invalid');
}
$this->icon = $icon;
return $this;
}
/**
* @return string
* @since 11.0.0
*/
public function getIcon(): string {
return $this->icon;
}
/**
* @param IEvent $child
* @return $this
* @since 11.0.0 - Since 15.0.0 returns $this
*/
public function setChildEvent(IEvent $child): IEvent {
$this->child = $child;
return $this;
}
/**
* @return IEvent|null
* @since 11.0.0
*/
public function getChildEvent() {
return $this->child;
}
/**
* @return bool
* @since 8.2.0
*/
public function isValid(): bool {
return
$this->isValidCommon()
&&
$this->getSubject() !== ''
;
}
/**
* @return bool
* @since 8.2.0
*/
public function isValidParsed(): bool {
if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) {
try {
$this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters());
} catch (InvalidObjectExeption $e) {
return false;
}
}
if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) {
try {
$this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters());
} catch (InvalidObjectExeption $e) {
return false;
}
}
return
$this->isValidCommon()
&&
$this->getParsedSubject() !== ''
;
}
protected function isValidCommon(): bool {
return
$this->getApp() !== ''
&&
$this->getType() !== ''
&&
$this->getAffectedUser() !== ''
&&
$this->getTimestamp() !== 0
/**
* Disabled for BC with old activities
* &&
* $this->getObjectType() !== ''
* &&
* $this->getObjectId() !== 0
*/
;
}
public function setGenerateNotification(bool $generate): IEvent {
$this->generateNotification = $generate;
return $this;
}
public function getGenerateNotification(): bool {
return $this->generateNotification;
}
}
@@ -0,0 +1,259 @@
<?php
/**
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Joas Schilling <coding@schilljs.com>
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Activity;
use OCP\Activity\IEvent;
use OCP\Activity\IEventMerger;
use OCP\IL10N;
class EventMerger implements IEventMerger {
/** @var IL10N */
protected $l10n;
/**
* @param IL10N $l10n
*/
public function __construct(IL10N $l10n) {
$this->l10n = $l10n;
}
/**
* Combines two events when possible to have grouping:
*
* Example1: Two events with subject '{user} created {file}' and
* $mergeParameter file with different file and same user will be merged
* to '{user} created {file1} and {file2}' and the childEvent on the return
* will be set, if the events have been merged.
*
* Example2: Two events with subject '{user} created {file}' and
* $mergeParameter file with same file and same user will be merged to
* '{user} created {file1}' and the childEvent on the return will be set, if
* the events have been merged.
*
* The following requirements have to be met, in order to be merged:
* - Both events need to have the same `getApp()`
* - Both events must not have a message `getMessage()`
* - Both events need to have the same subject `getSubject()`
* - Both events need to have the same object type `getObjectType()`
* - The time difference between both events must not be bigger then 3 hours
* - Only up to 5 events can be merged.
* - All parameters apart from such starting with $mergeParameter must be
* the same for both events.
*
* @param string $mergeParameter
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
*/
public function mergeEvents($mergeParameter, IEvent $event, IEvent $previousEvent = null) {
// No second event => can not combine
if (!$previousEvent instanceof IEvent) {
return $event;
}
// Different app => can not combine
if ($event->getApp() !== $previousEvent->getApp()) {
return $event;
}
// Message is set => can not combine
if ($event->getMessage() !== '' || $previousEvent->getMessage() !== '') {
return $event;
}
// Different subject => can not combine
if ($event->getSubject() !== $previousEvent->getSubject()) {
return $event;
}
// Different object type => can not combine
if ($event->getObjectType() !== $previousEvent->getObjectType()) {
return $event;
}
// More than 3 hours difference => can not combine
if (abs($event->getTimestamp() - $previousEvent->getTimestamp()) > 3 * 60 * 60) {
return $event;
}
// Other parameters are not the same => can not combine
try {
[$combined, $parameters] = $this->combineParameters($mergeParameter, $event, $previousEvent);
} catch (\UnexpectedValueException $e) {
return $event;
}
try {
$newSubject = $this->getExtendedSubject($event->getRichSubject(), $mergeParameter, $combined);
$parsedSubject = $this->generateParsedSubject($newSubject, $parameters);
$event->setRichSubject($newSubject, $parameters)
->setParsedSubject($parsedSubject)
->setChildEvent($previousEvent)
->setTimestamp(max($event->getTimestamp(), $previousEvent->getTimestamp()));
} catch (\UnexpectedValueException $e) {
return $event;
}
return $event;
}
/**
* @param string $mergeParameter
* @param IEvent $event
* @param IEvent $previousEvent
* @return array
* @throws \UnexpectedValueException
*/
protected function combineParameters($mergeParameter, IEvent $event, IEvent $previousEvent) {
$params1 = $event->getRichSubjectParameters();
$params2 = $previousEvent->getRichSubjectParameters();
$params = [];
$combined = 0;
// Check that all parameters from $event exist in $previousEvent
foreach ($params1 as $key => $parameter) {
if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) {
if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) {
$combined++;
$params[$mergeParameter . $combined] = $parameter;
}
continue;
}
if (!isset($params2[$key]) || $params2[$key] !== $parameter) {
// Parameter missing on $previousEvent or different => can not combine
throw new \UnexpectedValueException();
}
$params[$key] = $parameter;
}
// Check that all parameters from $previousEvent exist in $event
foreach ($params2 as $key => $parameter) {
if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) {
if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) {
$combined++;
$params[$mergeParameter . $combined] = $parameter;
}
continue;
}
if (!isset($params1[$key]) || $params1[$key] !== $parameter) {
// Parameter missing on $event or different => can not combine
throw new \UnexpectedValueException();
}
$params[$key] = $parameter;
}
return [$combined, $params];
}
/**
* @param array[] $parameters
* @param string $mergeParameter
* @param array $parameter
* @return bool
*/
protected function checkParameterAlreadyExits($parameters, $mergeParameter, $parameter) {
foreach ($parameters as $key => $param) {
if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) {
if ($param === $parameter) {
return true;
}
}
}
return false;
}
/**
* @param string $subject
* @param string $parameter
* @param int $counter
* @return mixed
*/
protected function getExtendedSubject($subject, $parameter, $counter) {
switch ($counter) {
case 1:
$replacement = '{' . $parameter . '1}';
break;
case 2:
$replacement = $this->l10n->t(
'%1$s and %2$s',
['{' . $parameter . '2}', '{' . $parameter . '1}']
);
break;
case 3:
$replacement = $this->l10n->t(
'%1$s, %2$s and %3$s',
['{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}']
);
break;
case 4:
$replacement = $this->l10n->t(
'%1$s, %2$s, %3$s and %4$s',
['{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}']
);
break;
case 5:
$replacement = $this->l10n->t(
'%1$s, %2$s, %3$s, %4$s and %5$s',
['{' . $parameter . '5}', '{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}']
);
break;
default:
throw new \UnexpectedValueException();
}
return str_replace(
'{' . $parameter . '}',
$replacement,
$subject
);
}
/**
* @param string $subject
* @param array[] $parameters
* @return string
*/
protected function generateParsedSubject($subject, $parameters) {
$placeholders = $replacements = [];
foreach ($parameters as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
if ($parameter['type'] === 'file') {
$replacements[] = trim($parameter['path'], '/');
} elseif (isset($parameter['name'])) {
$replacements[] = $parameter['name'];
} else {
$replacements[] = $parameter['id'];
}
}
return str_replace($placeholders, $replacements, $subject);
}
}
@@ -0,0 +1,400 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Daniel Kesselberg <mail@danielkesselberg.de>
* @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
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 {
/** @var IRequest */
protected $request;
/** @var IUserSession */
protected $session;
/** @var IConfig */
protected $config;
/** @var IValidator */
protected $validator;
/** @var string */
protected $formattingObjectType;
/** @var int */
protected $formattingObjectId;
/** @var bool */
protected $requirePNG = false;
/** @var string */
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;
}
/** @var \Closure[] */
private $consumersClosures = [];
/** @var IConsumer[] */
private $consumers = [];
/**
* @return \OCP\Activity\IConsumer[]
*/
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;
}
/**
* 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()
*
* @return IEvent
*/
public function generateEvent(): IEvent {
return new Event($this->validator);
}
/**
* Publish an event to the activity consumers
*
* Make sure to call at least the following methods before sending an Event:
* - setApp()
* - setType()
* - setAffectedUser()
* - setSubject()
*
* @param IEvent $event
* @throws \BadMethodCallException if required values have not been set
*/
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);
}
}
/**
* 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
*/
public function registerConsumer(\Closure $callable): void {
$this->consumersClosures[] = $callable;
$this->consumers = [];
}
/** @var string[] */
protected $filterClasses = [];
/** @var IFilter[] */
protected $filters = [];
/**
* @param string $filter Class must implement OCA\Activity\IFilter
* @return void
*/
public function registerFilter(string $filter): void {
$this->filterClasses[$filter] = false;
}
/**
* @return IFilter[]
* @throws \InvalidArgumentException
*/
public function getFilters(): array {
foreach ($this->filterClasses as $class => $false) {
/** @var IFilter $filter */
$filter = \OCP\Server::get($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
* @since 11.0.0
*/
public function getFilterById(string $id): IFilter {
$filters = $this->getFilters();
if (isset($filters[$id])) {
return $filters[$id];
}
throw new \InvalidArgumentException('Requested filter does not exist');
}
/** @var string[] */
protected $providerClasses = [];
/** @var IProvider[] */
protected $providers = [];
/**
* @param string $provider Class must implement OCA\Activity\IProvider
* @return void
*/
public function registerProvider(string $provider): void {
$this->providerClasses[$provider] = false;
}
/**
* @return IProvider[]
* @throws \InvalidArgumentException
*/
public function getProviders(): array {
foreach ($this->providerClasses as $class => $false) {
/** @var IProvider $provider */
$provider = \OCP\Server::get($class);
if (!$provider instanceof IProvider) {
throw new \InvalidArgumentException('Invalid activity provider registered');
}
$this->providers[] = $provider;
unset($this->providerClasses[$class]);
}
return $this->providers;
}
/** @var string[] */
protected $settingsClasses = [];
/** @var ISetting[] */
protected $settings = [];
/**
* @param string $setting Class must implement OCA\Activity\ISetting
* @return void
*/
public function registerSetting(string $setting): void {
$this->settingsClasses[$setting] = false;
}
/**
* @return ActivitySettings[]
* @throws \InvalidArgumentException
*/
public function getSettings(): array {
foreach ($this->settingsClasses as $class => $false) {
/** @var ISetting $setting */
$setting = \OCP\Server::get($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;
}
/**
* @param string $id
* @return ActivitySettings
* @throws \InvalidArgumentException when the setting was not found
* @since 11.0.0
*/
public function getSettingById(string $id): ActivitySettings {
$settings = $this->getSettings();
if (isset($settings[$id])) {
return $settings[$id];
}
throw new \InvalidArgumentException('Requested setting does not exist');
}
/**
* @param string $type
* @param int $id
*/
public function setFormattingObject(string $type, int $id): void {
$this->formattingObjectType = $type;
$this->formattingObjectId = $id;
}
/**
* @return bool
*/
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');
}
/**
* @param bool $status Set to true, when parsing events should not use SVG icons
*/
public function setRequirePNG(bool $status): void {
$this->requirePNG = $status;
}
/**
* @return bool
*/
public function getRequirePNG(): bool {
return $this->requirePNG;
}
/**
* Set the user we need to use
*
* @param string|null $currentUserId
* @throws \UnexpectedValueException If the user is invalid
*/
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;
}
/**
* 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
*/
public function getCurrentUserId(): string {
if ($this->currentUserId !== null) {
return $this->currentUserId;
}
if (!$this->session->isLoggedIn()) {
return $this->getUserFromToken();
}
return $this->session->getUser()->getUID();
}
/**
* Get the user for the token
*
* @return string
* @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
*/
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) {
// No unique user found
throw new \UnexpectedValueException('The token is invalid');
}
// Token found login as that user
return array_shift($users);
}
}