2013-09-29 22:31:12 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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/>
|
2013-09-29 22:31:12 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OC;
|
|
|
|
|
|
|
|
|
|
|
|
use OCP\Activity\IConsumer;
|
2014-07-03 15:48:15 +04:00
|
|
|
use OCP\Activity\IExtension;
|
2013-10-09 20:06:21 +04:00
|
|
|
use OCP\Activity\IManager;
|
2015-03-25 17:18:11 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserSession;
|
2013-09-29 22:31:12 +04:00
|
|
|
|
2013-10-09 20:06:21 +04:00
|
|
|
class ActivityManager implements IManager {
|
2015-03-25 17:18:11 +03:00
|
|
|
/** @var IRequest */
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/** @var IUserSession */
|
|
|
|
protected $session;
|
|
|
|
|
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor of the controller
|
|
|
|
*
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param IUserSession $session
|
|
|
|
* @param IConfig $config
|
|
|
|
*/
|
|
|
|
public function __construct(IRequest $request,
|
|
|
|
IUserSession $session,
|
|
|
|
IConfig $config) {
|
|
|
|
$this->request = $request;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
2013-09-29 22:31:12 +04:00
|
|
|
|
2014-07-03 15:48:15 +04:00
|
|
|
/**
|
|
|
|
* @var \Closure[]
|
|
|
|
*/
|
2013-09-29 22:31:12 +04:00
|
|
|
private $consumers = array();
|
|
|
|
|
2014-07-03 15:48:15 +04:00
|
|
|
/**
|
|
|
|
* @var \Closure[]
|
|
|
|
*/
|
|
|
|
private $extensions = array();
|
|
|
|
|
2015-01-23 17:37:15 +03:00
|
|
|
/** @var array list of filters "name" => "is valid" */
|
2015-02-13 13:50:55 +03:00
|
|
|
protected $validFilters = array(
|
|
|
|
'all' => true,
|
|
|
|
'by' => true,
|
|
|
|
'self' => true,
|
|
|
|
);
|
2015-01-23 17:37:15 +03:00
|
|
|
|
|
|
|
/** @var array list of type icons "type" => "css class" */
|
|
|
|
protected $typeIcons = array();
|
|
|
|
|
|
|
|
/** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */
|
|
|
|
protected $specialParameters = array();
|
|
|
|
|
2013-09-29 22:31:12 +04:00
|
|
|
/**
|
|
|
|
* @param $app
|
|
|
|
* @param $subject
|
2013-10-09 20:06:21 +04:00
|
|
|
* @param $subjectParams
|
2013-09-29 22:31:12 +04:00
|
|
|
* @param $message
|
2013-10-09 20:06:21 +04:00
|
|
|
* @param $messageParams
|
2013-09-29 22:31:12 +04:00
|
|
|
* @param $file
|
|
|
|
* @param $link
|
2013-10-09 20:06:21 +04:00
|
|
|
* @param $affectedUser
|
|
|
|
* @param $type
|
|
|
|
* @param $priority
|
2013-09-29 22:31:12 +04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-10-09 20:06:21 +04:00
|
|
|
function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
|
2013-09-29 22:31:12 +04:00
|
|
|
foreach($this->consumers as $consumer) {
|
|
|
|
$c = $consumer();
|
|
|
|
if ($c instanceof IConsumer) {
|
2013-10-09 20:06:21 +04:00
|
|
|
try {
|
|
|
|
$c->receive(
|
|
|
|
$app,
|
|
|
|
$subject,
|
|
|
|
$subjectParams,
|
|
|
|
$message,
|
|
|
|
$messageParams,
|
|
|
|
$file,
|
|
|
|
$link,
|
|
|
|
$affectedUser,
|
|
|
|
$type,
|
|
|
|
$priority);
|
|
|
|
} catch (\Exception $ex) {
|
2014-04-21 17:44:54 +04:00
|
|
|
// TODO: log the exception
|
2013-10-09 20:06:21 +04:00
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
function registerConsumer(\Closure $callable) {
|
|
|
|
array_push($this->consumers, $callable);
|
|
|
|
}
|
2013-10-09 20:06:21 +04:00
|
|
|
|
2014-07-03 15:48:15 +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
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function registerExtension(\Closure $callable) {
|
|
|
|
array_push($this->extensions, $callable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will return additional notification types as specified by other apps
|
|
|
|
*
|
|
|
|
* @param string $languageCode
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getNotificationTypes($languageCode) {
|
|
|
|
$notificationTypes = array();
|
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
|
|
|
$result = $c->getNotificationTypes($languageCode);
|
|
|
|
if (is_array($result)) {
|
|
|
|
$notificationTypes = array_merge($notificationTypes, $result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notificationTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-13 13:51:55 +03:00
|
|
|
* @param string $method
|
2014-07-03 15:48:15 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-02-13 13:51:55 +03:00
|
|
|
function getDefaultTypes($method) {
|
|
|
|
$defaultTypes = array();
|
2014-07-03 15:48:15 +04:00
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
2015-02-13 13:51:55 +03:00
|
|
|
$types = $c->getDefaultTypes($method);
|
|
|
|
if (is_array($types)) {
|
|
|
|
$defaultTypes = array_merge($types, $defaultTypes);
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-13 13:51:55 +03:00
|
|
|
return $defaultTypes;
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-13 13:51:55 +03:00
|
|
|
* @param string $type
|
|
|
|
* @return string
|
2014-07-03 15:48:15 +04:00
|
|
|
*/
|
2015-02-13 13:51:55 +03:00
|
|
|
function getTypeIcon($type) {
|
|
|
|
if (isset($this->typeIcons[$type])) {
|
|
|
|
return $this->typeIcons[$type];
|
|
|
|
}
|
|
|
|
|
2014-07-03 15:48:15 +04:00
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
2015-02-13 13:51:55 +03:00
|
|
|
$icon = $c->getTypeIcon($type);
|
|
|
|
if (is_string($icon)) {
|
|
|
|
$this->typeIcons[$type] = $icon;
|
|
|
|
return $icon;
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-13 13:51:55 +03:00
|
|
|
|
|
|
|
$this->typeIcons[$type] = '';
|
|
|
|
return '';
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $app
|
|
|
|
* @param string $text
|
|
|
|
* @param array $params
|
|
|
|
* @param boolean $stripPath
|
|
|
|
* @param boolean $highlightParams
|
|
|
|
* @param string $languageCode
|
|
|
|
* @return string|false
|
|
|
|
*/
|
|
|
|
function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
|
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
|
|
|
$translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode);
|
|
|
|
if (is_string($translation)) {
|
|
|
|
return $translation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:54:50 +03:00
|
|
|
/**
|
|
|
|
* @param string $app
|
|
|
|
* @param string $text
|
|
|
|
* @return array|false
|
|
|
|
*/
|
|
|
|
function getSpecialParameterList($app, $text) {
|
2015-01-23 17:37:15 +03:00
|
|
|
if (isset($this->specialParameters[$app][$text])) {
|
|
|
|
return $this->specialParameters[$app][$text];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->specialParameters[$app])) {
|
|
|
|
$this->specialParameters[$app] = array();
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:54:50 +03:00
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
|
|
|
$specialParameter = $c->getSpecialParameterList($app, $text);
|
|
|
|
if (is_array($specialParameter)) {
|
2015-01-23 17:37:15 +03:00
|
|
|
$this->specialParameters[$app][$text] = $specialParameter;
|
2014-12-11 16:54:50 +03:00
|
|
|
return $specialParameter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:37:15 +03:00
|
|
|
$this->specialParameters[$app][$text] = false;
|
2014-12-11 16:54:50 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-03 15:48:15 +04:00
|
|
|
/**
|
|
|
|
* @param array $activity
|
|
|
|
* @return integer|false
|
|
|
|
*/
|
|
|
|
function getGroupParameter($activity) {
|
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
|
|
|
$parameter = $c->getGroupParameter($activity);
|
|
|
|
if ($parameter !== false) {
|
|
|
|
return $parameter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getNavigation() {
|
|
|
|
$entries = array(
|
|
|
|
'apps' => array(),
|
|
|
|
'top' => array(),
|
|
|
|
);
|
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
|
|
|
$additionalEntries = $c->getNavigation();
|
|
|
|
if (is_array($additionalEntries)) {
|
|
|
|
$entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']);
|
|
|
|
$entries['top'] = array_merge($entries['top'], $additionalEntries['top']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filterValue
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function isFilterValid($filterValue) {
|
2015-01-23 17:37:15 +03:00
|
|
|
if (isset($this->validFilters[$filterValue])) {
|
|
|
|
return $this->validFilters[$filterValue];
|
|
|
|
}
|
|
|
|
|
2014-07-03 15:48:15 +04:00
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
|
|
|
if ($c->isFilterValid($filterValue) === true) {
|
2015-01-23 17:37:15 +03:00
|
|
|
$this->validFilters[$filterValue] = true;
|
2014-07-03 15:48:15 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 17:37:15 +03:00
|
|
|
$this->validFilters[$filterValue] = false;
|
2014-07-03 15:48:15 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-13 13:51:55 +03:00
|
|
|
/**
|
|
|
|
* @param array $types
|
|
|
|
* @param string $filter
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function filterNotificationTypes($types, $filter) {
|
|
|
|
if (!$this->isFilterValid($filter)) {
|
|
|
|
return $types;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
|
|
|
$result = $c->filterNotificationTypes($types, $filter);
|
|
|
|
if (is_array($result)) {
|
|
|
|
$types = $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $types;
|
|
|
|
}
|
|
|
|
|
2014-07-03 15:48:15 +04:00
|
|
|
/**
|
|
|
|
* @param string $filter
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getQueryForFilter($filter) {
|
2015-02-13 13:50:55 +03:00
|
|
|
if (!$this->isFilterValid($filter)) {
|
|
|
|
return [null, null];
|
|
|
|
}
|
2014-11-28 13:18:15 +03:00
|
|
|
|
|
|
|
$conditions = array();
|
|
|
|
$parameters = array();
|
|
|
|
|
2014-07-03 15:48:15 +04:00
|
|
|
foreach($this->extensions as $extension) {
|
|
|
|
$c = $extension();
|
|
|
|
if ($c instanceof IExtension) {
|
|
|
|
$result = $c->getQueryForFilter($filter);
|
|
|
|
if (is_array($result)) {
|
2014-11-28 13:18:15 +03:00
|
|
|
list($condition, $parameter) = $result;
|
|
|
|
if ($condition && is_array($parameter)) {
|
|
|
|
$conditions[] = $condition;
|
|
|
|
$parameters = array_merge($parameters, $parameter);
|
|
|
|
}
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-28 13:18:15 +03:00
|
|
|
if (empty($conditions)) {
|
|
|
|
return array(null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
|
2014-07-03 15:48:15 +04:00
|
|
|
}
|
2015-03-25 17:18:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
if (!$this->session->isLoggedIn()) {
|
|
|
|
return $this->getUserFromToken();
|
|
|
|
} else {
|
|
|
|
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() {
|
|
|
|
$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 (sizeof($users) !== 1) {
|
|
|
|
// 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
|
|
|
}
|