Skip to content

Commit

Permalink
Enable Activity Integration #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Rello committed Nov 16, 2019
1 parent 30416e7 commit 49f96d3
Show file tree
Hide file tree
Showing 9 changed files with 504 additions and 4 deletions.
12 changes: 12 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ From datasouce to reporting. Data Analytics provides modular datasources, a real
<php min-version="7.0" max-version="7.3"/>
<nextcloud min-version="16" max-version="17"/>
</dependencies>
<activity>
<settings>
<setting>OCA\Analytics\Activity\SettingDataset</setting>
<setting>OCA\Analytics\Activity\SettingData</setting>
</settings>
<providers>
<provider>OCA\Analytics\Activity\Provider</provider>
</providers>
<filters>
<filter>OCA\Analytics\Activity\Filter</filter>
</filters>
</activity>
</info>
104 changes: 104 additions & 0 deletions lib/Activity/ActivityManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Data Analytics
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the LICENSE.md file.
*
* @author Marcel Scherello <audioplayer@scherello.de>
* @copyright 2019 Marcel Scherello
*/

namespace OCA\Analytics\Activity;

use OCA\Analytics\Service\DatasetService;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\IL10N;
use OCP\IUser;


class ActivityManager
{

const OBJECT_DATASET = 'analytics_dataset';
const OBJECT_DATA = 'analytics_data';
const SUBJECT_DATASET_ADD = 'dataset_add';
const SUBJECT_DATASET_DELETE = 'dataset_delete';
const SUBJECT_DATASET_SHARE = 'dataset_share';
const SUBJECT_DATA_ADD = 'data_add';
const SUBJECT_DATA_ADD_API = 'data_add_api';
private $manager;
private $l10n;
private $userId;
private $DatasetService;

public function __construct(
IManager $manager,
IL10N $l10n,
DatasetService $DatasetService,
$userId
)
{
$this->manager = $manager;
$this->l10n = $l10n;
$this->userId = $userId;
$this->DatasetService = $DatasetService;
}

public function triggerEvent($datasetId, $eventType, $eventSubject)
{
try {
$event = $this->createEvent($datasetId, $eventType, $eventSubject);
if ($event !== null) {
$this->sendToUsers($event);
}
} catch (\Exception $e) {
// Ignore exception for undefined activities on update events
}
}

private function createEvent($datasetId, $eventType, $eventSubject, $ownActivity = true, $author = null)
{

$dataset = $this->DatasetService->getOwnDataset($datasetId);
$event = $this->manager->generateEvent();
$event->setApp('analytics')
->setType($eventType)
->setAuthor($this->userId)
->setObject('report', (int)$datasetId, $dataset['name'])
->setSubject($eventSubject, ['author' => $this->userId])
->setTimestamp(time());
return $event;
}

/**
* Publish activity to all users that are part of the board of a given object
*
* @param IEvent $event
*/
private function sendToUsers(IEvent $event)
{
$event->setAffectedUser('admin');
$this->manager->publish($event);


switch ($event->getObjectType()) {
case self::DECK_OBJECT_BOARD:
$mapper = $this->boardMapper;
break;
case self::DECK_OBJECT_CARD:
$mapper = $this->cardMapper;
break;
}
$boardId = $mapper->findBoardId($event->getObjectId());
/** @var IUser $user */
foreach ($this->permissionService->findUsers($boardId) as $user) {
$event->setAffectedUser($user->getUID());
/** @noinspection DisconnectedForeachInstructionInspection */
$this->manager->publish($event);
}


}
}
89 changes: 89 additions & 0 deletions lib/Activity/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Data Analytics
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the LICENSE.md file.
*
* @author Marcel Scherello <audioplayer@scherello.de>
* @copyright 2019 Marcel Scherello
*/

namespace OCA\Analytics\Activity;

use OCP\IL10N;
use OCP\IURLGenerator;

class Filter implements \OCP\Activity\IFilter
{

private $l10n;
private $urlGenerator;

public function __construct(
IL10N $l10n,
IURLGenerator $urlGenerator
)
{
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
}

/**
* @return string Lowercase a-z and underscore only identifier
* @since 11.0.0
*/
public function getIdentifier()
{
return 'analytics';
}

/**
* @return string A translated string
* @since 11.0.0
*/
public function getName()
{
return $this->l10n->t('Data Analytics');
}

/**
* @return int whether the filter should be rather on the top or bottom of
* the admin section. The filters are arranged in ascending order of the
* priority values. It is required to return a value between 0 and 100.
* @since 11.0.0
*/
public function getPriority()
{
return 90;
}

/**
* @return string Full URL to an icon, empty string when none is given
* @since 11.0.0
*/
public function getIcon()
{
return $this->urlGenerator->imagePath('analytics', 'app-dark.svg');
}

/**
* @param string[] $types
* @return string[] An array of allowed apps from which activities should be displayed
* @since 11.0.0
*/
public function filterTypes(array $types)
{
return array_merge($types, ['analytics_dataset'], ['analytics_data']);
}

/**
* @return string[] An array of allowed apps from which activities should be displayed
* @since 11.0.0
*/
public function allowedApps()
{
return ['analytics'];
}
}

98 changes: 98 additions & 0 deletions lib/Activity/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Data Analytics
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the LICENSE.md file.
*
* @author Marcel Scherello <audioplayer@scherello.de>
* @copyright 2019 Marcel Scherello
*/

namespace OCA\Analytics\Activity;

use InvalidArgumentException;
use OCP\Activity\IEvent;
use OCP\Activity\IProvider;
use OCP\IL10N;
use OCP\IURLGenerator;

class Provider implements IProvider
{

private $l10n;
private $userId;
private $urlGenerator;

public function __construct(IURLGenerator $urlGenerator,
IL10N $l10n,
$userId)
{
$this->userId = $userId;
$this->urlGenerator = $urlGenerator;
$this->l10n = $l10n;
}

/**
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws InvalidArgumentException
* @since 11.0.0
*/
public function parse($language, IEvent $event, IEvent $previousEvent = null)
{
if ($event->getApp() !== 'analytics') {
throw new InvalidArgumentException();
}

$parsedSubject = '';
$ownActivity = ($event->getAuthor() === $this->userId);

switch ($event->getSubject()) {
case ActivityManager::SUBJECT_DATASET_ADD:
$parsedSubject = $this->l10n->t('You have created a new report {report}');
break;
case ActivityManager::SUBJECT_DATASET_DELETE:
$parsedSubject = $this->l10n->t('You have deleted the report {report}');
break;
case ActivityManager::SUBJECT_DATASET_SHARE:
if ($ownActivity) {
$parsedSubject = $this->l10n->t('You shared report {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . $this->l10n->t(' shared report {report} with you');
}
break;
case ActivityManager::SUBJECT_DATA_ADD:
if ($ownActivity) {
$parsedSubject = $this->l10n->t('You have added new data to report {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . $this->l10n->t(' has added new data to report {report}');
}
break;
case ActivityManager::SUBJECT_DATA_ADD_API:
$parsedSubject = $this->l10n->t('New data was add via API to report {report}');
break;
}

$event->setRichSubject(
$parsedSubject,
['report' => [
'type' => 'highlight',
'id' => $event->getObjectId(),
'name' => basename($event->getObjectName()),
'link' => $this->Url($event->getObjectId()),
]]
);

$event->setParsedSubject($parsedSubject)
->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('analytics', 'app-dark.svg')));
return $event;
}

public function Url($endpoint)
{
return $this->urlGenerator->linkToRouteAbsolute('analytics.page.index');
}
}
Loading

0 comments on commit 49f96d3

Please sign in to comment.