-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
504 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
Oops, something went wrong.