Use an IEvent object instead of a huge parameter list

This commit is contained in:
Joas Schilling 2015-08-19 17:37:55 +02:00
parent e985dcc5a0
commit 4314c8fc6f
6 changed files with 548 additions and 47 deletions

View file

@ -111,9 +111,14 @@ class Server2Server {
if ($share) {
list($file, $link) = self::getFile($share['uid_owner'], $share['file_source']);
\OC::$server->getActivityManager()->publishActivity(
Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_ACCEPTED, array($share['share_with'], basename($file)), '', array(),
$file, $link, $share['uid_owner'], Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW, 'files', $share['file_source']);
$event = \OC::$server->getActivityManager()->generateEvent();
$event->setApp(Activity::FILES_SHARING_APP)
->setType(Activity::TYPE_REMOTE_SHARE)
->setAffectedUser($share['uid_owner'])
->setSubject(Activity::SUBJECT_REMOTE_SHARE_ACCEPTED, [$share['share_with'], basename($file)])
->setObject('files', $share['file_source'], $file)
->setLink($link);
\OC::$server->getActivityManager()->publish($event);
}
return new \OC_OCS_Result();
@ -142,9 +147,14 @@ class Server2Server {
list($file, $link) = $this->getFile($share['uid_owner'], $share['file_source']);
\OC::$server->getActivityManager()->publishActivity(
Activity::FILES_SHARING_APP, Activity::SUBJECT_REMOTE_SHARE_DECLINED, array($share['share_with'], basename($file)), '', array(),
$file, $link, $share['uid_owner'], Activity::TYPE_REMOTE_SHARE, Activity::PRIORITY_LOW, 'files', $share['file_source']);
$event = \OC::$server->getActivityManager()->generateEvent();
$event->setApp(Activity::FILES_SHARING_APP)
->setType(Activity::TYPE_REMOTE_SHARE)
->setAffectedUser($share['uid_owner'])
->setSubject(Activity::SUBJECT_REMOTE_SHARE_DECLINED, [$share['share_with'], basename($file)])
->setObject('files', $share['file_source'], $file)
->setLink($link);
\OC::$server->getActivityManager()->publish($event);
}
return new \OC_OCS_Result();

View file

@ -0,0 +1,257 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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\Activity\IManager;
class Event implements IEvent {
/** @var IManager */
protected $manager;
/** @var string */
protected $user;
/** @var array */
protected $data = [
'app' => null,
'type' => null,
'affected_user' => null,
'author' => null,
'timestamp' => null,
'subject' => null,
'subject_parameters' => null,
'message' => '',
'message_parameters' => [],
'object_type' => '',
'object_id' => 0,
'object_name' => '',
'link' => '',
];
/**
* Set the app of the activity
*
* @param string $app
* @return IEvent
* @since 8.2.0
*/
public function setApp($app) {
$this->data['app'] = (string) $app;
return $this;
}
/**
* Set the type of the activity
*
* @param string $type
* @return IEvent
* @since 8.2.0
*/
public function setType($type) {
$this->data['type'] = (string) $type;
return $this;
}
/**
* Set the affected user of the activity
*
* @param string $affectedUser
* @return IEvent
* @since 8.2.0
*/
public function setAffectedUser($affectedUser) {
$this->data['affected_user'] = (string) $affectedUser;
return $this;
}
/**
* Set the author of the activity
*
* @param string $author
* @return IEvent
* @since 8.2.0
*/
public function setAuthor($author) {
$this->data['author'] = (string) $author;
return $this;
}
/**
* Set the author of the activity
*
* @param int $timestamp
* @return IEvent
* @since 8.2.0
*/
public function setTimestamp($timestamp) {
$this->data['timestamp'] = (int) $timestamp;
return $this;
}
/**
* Set the subject of the activity
*
* @param string $subject
* @param array $parameters
* @return IEvent
* @since 8.2.0
*/
public function setSubject($subject, array $parameters = []) {
$this->data['subject'] = (string) $subject;
$this->data['subject_parameters'] = $parameters;
return $this;
}
/**
* Set the message of the activity
*
* @param string $message
* @param array $parameters
* @return IEvent
* @since 8.2.0
*/
public function setMessage($message, array $parameters = []) {
$this->data['message'] = (string) $message;
$this->data['message_parameters'] = $parameters;
return $this;
}
/**
* Set the object of the activity
*
* @param string $objectType
* @param int $objectId
* @param string $objectName
* @return IEvent
* @since 8.2.0
*/
public function setObject($objectType, $objectId, $objectName = '') {
$this->data['object_type'] = (string) $objectType;
$this->data['object_id'] = (int) $objectId;
$this->data['object_name'] = (string) $objectName;
return $this;
}
/**
* Set the link of the activity
*
* @param string $link
* @return IEvent
* @since 8.2.0
*/
public function setLink($link) {
$this->data['link'] = (string) $link;
return $this;
}
/**
* @return string
*/
public function getApp() {
return $this->data['app'];
}
/**
* @return string
*/
public function getType() {
return $this->data['type'];
}
/**
* @return string
*/
public function getAffectedUser() {
return $this->data['affected_user'];
}
/**
* @return string
*/
public function getAuthor() {
return $this->data['author'];
}
/**
* @return int
*/
public function getTimestamp() {
return $this->data['timestamp'];
}
/**
* @return string
*/
public function getSubject() {
return $this->data['subject'];
}
/**
* @return array
*/
public function getSubjectParameters() {
return $this->data['subject_parameters'];
}
/**
* @return string
*/
public function getMessage() {
return $this->data['message'];
}
/**
* @return array
*/
public function getMessageParameters() {
return $this->data['message_parameters'];
}
/**
* @return string
*/
public function getObjectType() {
return $this->data['object_type'];
}
/**
* @return string
*/
public function getObjectId() {
return $this->data['object_id'];
}
/**
* @return string
*/
public function getObjectName() {
return $this->data['object_name'];
}
/**
* @return string
*/
public function getLink() {
return $this->data['link'];
}
}

View file

@ -24,11 +24,14 @@
namespace OC;
use OC\Activity\Event;
use OCP\Activity\IConsumer;
use OCP\Activity\IEvent;
use OCP\Activity\IExtension;
use OCP\Activity\IManager;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
class ActivityManager implements IManager {
@ -123,6 +126,49 @@ class ActivityManager implements IManager {
return $this->extensions;
}
/**
* @return IEvent
*/
public function generateEvent() {
return new Event();
}
/**
* Publish an event to the activity consumers
*
* @param IEvent $event
* @return null
* @throws \BadMethodCallException if required values have not been set
*/
public function publish(IEvent $event) {
if (!$event->getApp()) {
throw new \BadMethodCallException('App not set', 1);
}
if (!$event->getType()) {
throw new \BadMethodCallException('Type not set', 2);
}
if ($event->getSubject() === null || $event->getSubjectParameters() === null) {
throw new \BadMethodCallException('Subject not set', 3);
}
if ($event->getAffectedUser() === null) {
throw new \BadMethodCallException('Affected user not set', 4);
}
if ($event->getAuthor() === null) {
if ($this->session->getUser() instanceof IUser) {
$event->setAuthor($this->session->getUser()->getUID());
}
}
if (!$event->getTimestamp()) {
$event->setTimestamp(time());
}
foreach ($this->getConsumers() as $c) {
$c->receive($event);
}
}
/**
* @param string $app The app where this event is associated with
* @param string $subject A short description of the event
@ -133,32 +179,20 @@ class ActivityManager implements IManager {
* @param string $link A link where this event is associated with
* @param string $affectedUser Recipient of the activity
* @param string $type Type of the notification
* @param int $priority Priority of the notification (@deprecated)
* @param string $objectType Object type can be used to filter the activities later (e.g. files)
* @param int $objectId Object id can be used to filter the activities later (e.g. the ID of the cache entry)
* @param int $priority Priority of the notification
* @return null
*/
public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority, $objectType = '', $objectId = 0) {
foreach($this->getConsumers() as $c) {
public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
$event = $this->generateEvent();
$event->setApp($app)
->setType($type)
->setAffectedUser($affectedUser)
->setSubject($subject, $subjectParams)
->setMessage($message, $messageParams)
->setObject('', 0, $file)
->setLink($link);
try {
$c->receive(
$app,
$subject,
$subjectParams,
$message,
$messageParams,
$file,
$link,
$affectedUser,
$type,
$objectType,
$objectId
);
} catch (\Exception $ex) {
// TODO: log the exception
}
}
$this->publish($event);
}
/**

View file

@ -37,21 +37,11 @@ namespace OCP\Activity;
*/
interface IConsumer {
/**
* @param string $app The app where this event is associated with
* @param string $subject A short description of the event
* @param array $subjectParams Array with parameters that are filled in the subject
* @param string $message A longer description of the event
* @param array $messageParams Array with parameters that are filled in the message
* @param string $file The file including path where this event is associated with
* @param string $link A link where this event is associated with
* @param string $affectedUser Recipient of the notification
* @param string $type Type of the notification
* @param string $objectType Object type can be used to filter the activities later (e.g. files)
* @param int $objectId Object id can be used to filter the activities later (e.g. the ID of the cache entry)
* @param IEvent $event
* @return null
* @since 6.0.0
* @since 8.2.0 Added $objectType and $objectId
* @since 8.2.0 Replaced the parameters with an IEvent object
*/
public function receive($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $objectType, $objectId);
public function receive(IEvent $event);
}

View file

@ -0,0 +1,200 @@
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
/**
* Public interface of ownCloud for apps to use.
* Activity/IEvent interface
*/
// use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP\Activity;
/**
* Interface IEvent
*
* @package OCP\Activity
* @since 8.2.0
*/
interface IEvent {
/**
* Set the app of the activity
*
* @param string $app
* @return IEvent
* @since 8.2.0
*/
public function setApp($app);
/**
* Set the type of the activity
*
* @param string $type
* @return IEvent
* @since 8.2.0
*/
public function setType($type);
/**
* Set the affected user of the activity
*
* @param string $user
* @return IEvent
* @since 8.2.0
*/
public function setAffectedUser($user);
/**
* Set the author of the activity
*
* @param string $author
* @return IEvent
* @since 8.2.0
*/
public function setAuthor($author);
/**
* Set the author of the activity
*
* @param int $timestamp
* @return IEvent
* @since 8.2.0
*/
public function setTimestamp($timestamp);
/**
* Set the subject of the activity
*
* @param string $subject
* @param array $parameters
* @return IEvent
* @since 8.2.0
*/
public function setSubject($subject, array $parameters = []);
/**
* Set the message of the activity
*
* @param string $message
* @param array $parameters
* @return IEvent
* @since 8.2.0
*/
public function setMessage($message, array $parameters = []);
/**
* Set the object of the activity
*
* @param string $objectType
* @param int $objectId
* @param string $objectName
* @return IEvent
* @since 8.2.0
*/
public function setObject($objectType, $objectId, $objectName = '');
/**
* Set the link of the activity
*
* @param string $link
* @return IEvent
* @since 8.2.0
*/
public function setLink($link);
/**
* @return string
* @since 8.2.0
*/
public function getApp();
/**
* @return string
* @since 8.2.0
*/
public function getType();
/**
* @return string
* @since 8.2.0
*/
public function getAffectedUser();
/**
* @return string
* @since 8.2.0
*/
public function getAuthor();
/**
* @return int
* @since 8.2.0
*/
public function getTimestamp();
/**
* @return string
* @since 8.2.0
*/
public function getSubject();
/**
* @return array
* @since 8.2.0
*/
public function getSubjectParameters();
/**
* @return string
* @since 8.2.0
*/
public function getMessage();
/**
* @return array
* @since 8.2.0
*/
public function getMessageParameters();
/**
* @return string
* @since 8.2.0
*/
public function getObjectType();
/**
* @return string
* @since 8.2.0
*/
public function getObjectId();
/**
* @return string
* @since 8.2.0
*/
public function getObjectName();
/**
* @return string
* @since 8.2.0
*/
public function getLink();
}

View file

@ -38,6 +38,18 @@ namespace OCP\Activity;
* @since 6.0.0
*/
interface IManager {
/**
* @return IEvent
* @since 8.2.0
*/
public function generateEvent();
/**
* @param IEvent $event
* @return null
* @since 8.2.0
*/
public function publish(IEvent $event);
/**
* @param string $app The app where this event is associated with
@ -49,14 +61,12 @@ interface IManager {
* @param string $link A link where this event is associated with
* @param string $affectedUser Recipient of the activity
* @param string $type Type of the notification
* @param int $priority Priority of the notification (@deprecated)
* @param string $objectType Object type can be used to filter the activities later (e.g. files)
* @param int $objectId Object id can be used to filter the activities later (e.g. the ID of the cache entry)
* @param int $priority Priority of the notification
* @return null
* @since 6.0.0
* @since 8.2.0 Added $objectType and $objectId
* @deprecated 8.2.0 Grab an IEvent from generateEvent() instead and use the publish() method
*/
public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority, $objectType = '', $objectId = 0);
public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority);
/**
* In order to improve lazy loading a closure can be registered which will be called in case