Skip to content

Commit

Permalink
no activities for added data anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Rello committed Aug 1, 2024
1 parent 591d77b commit ff0d742
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 107 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Chart.js v4.4.3
- NC30 css compatibility
- rework of sharing backend
- no activities for added data anymore

### Fixed
- excel data source: remove all-null columns
Expand Down
47 changes: 38 additions & 9 deletions lib/Activity/ActivityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,61 @@

use OCA\Analytics\Db\ReportMapper;
use OCA\Analytics\Db\DatasetMapper;
use OCA\Analytics\Db\PanoramaMapper;
use OCA\Analytics\Db\ShareMapper;
use OCA\Analytics\Service\ShareService;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use Psr\Log\LoggerInterface;

class ActivityManager
{
const OBJECT_REPORT = 'analytics_report';
const OBJECT_DATA = 'analytics_data';
const OBJECT_PANORAMA = 'analytics_panorama';
const OBJECT_DATASET = 'analytics_dataset';
const OBJECT_DATA = 'analytics_data';

const SUBJECT_REPORT_ADD = 'report_add';
const SUBJECT_REPORT_DELETE = 'report_delete';
const SUBJECT_REPORT_SHARE = 'report_share';

const SUBJECT_PANORAMA_ADD = 'panorama_add';
const SUBJECT_PANORAMA_DELETE = 'panorama_delete';
const SUBJECT_PANORAMA_SHARE = 'panorama_share';

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';
const SUBJECT_DATA_ADD_IMPORT = 'data_add_import';
const SUBJECT_DATA_ADD_DATALOAD = 'data_add_dataload';

const SUBJECT_REPORT_ADD_depr = 'dataset_add';
const SUBJECT_REPORT_DELETE_depr = 'dataset_delete';
const SUBJECT_REPORT_SHARE_depr = 'dataset_share';

private $manager;
private $userId;
private $ShareMapper;
private $logger;
private $ReportMapper;
private $DatasetMapper;
private $PanoramaMapper;

public function __construct(
IManager $manager,
ShareMapper $ShareMapper,
$userId,
ReportMapper $ReportMapper,
DatasetMapper $DatasetMapper,
LoggerInterface $logger
LoggerInterface $logger,
PanoramaMapper $PanoramaMapper
)
{
$this->manager = $manager;
$this->userId = $userId;
$this->ShareMapper = $ShareMapper;
$this->ReportMapper = $ReportMapper;
$this->DatasetMapper = $DatasetMapper;
$this->PanoramaMapper = $PanoramaMapper;
$this->logger = $logger;
}

Expand All @@ -66,6 +79,7 @@ public function triggerEvent($datasetId, $eventType, $eventSubject, string $user
try {
$event = $this->createEvent($datasetId, $eventType, $eventSubject, $user_id);
if ($event !== null) {
// TODO
$this->sendToUsers($event);
}
} catch (\Exception $e) {
Expand All @@ -83,11 +97,13 @@ public function triggerEvent($datasetId, $eventType, $eventSubject, string $user
*/
private function createEvent($objectId, $eventType, $eventSubject, string $user_id = null)
{
if ($eventSubject === ActivityManager::SUBJECT_REPORT_ADD || $eventSubject === ActivityManager::SUBJECT_REPORT_DELETE || $eventSubject === ActivityManager::SUBJECT_REPORT_SHARE) {
if ($eventType === ActivityManager::OBJECT_REPORT) {
$name = $objectId !== 0 ? $this->ReportMapper->readOwn($objectId)['name'] : '';
} else {
} elseif ($eventType === ActivityManager::OBJECT_DATASET) {
$name = $objectId !== 0 ? $this->DatasetMapper->read($objectId)['name'] : '';
$objectId = 0;
} elseif ($eventType === ActivityManager::OBJECT_PANORAMA) {
$name = $objectId !== 0 ? $this->PanoramaMapper->read($objectId)['name'] : '';
}

if ($user_id) $this->userId = $user_id;
Expand All @@ -108,7 +124,20 @@ private function createEvent($objectId, $eventType, $eventSubject, string $user_
*/
private function sendToUsers(IEvent $event)
{
$users = $this->ShareMapper->getSharedReceiver($event->getObjectId());
switch ($event->getType()) {
case ActivityManager::OBJECT_REPORT:
$item_type = ShareService::SHARE_ITEM_TYPE_REPORT;
break;
case ActivityManager::OBJECT_DATASET:
$item_type = ShareService::SHARE_ITEM_TYPE_DATASET;
break;
case ActivityManager::OBJECT_PANORAMA:
$item_type = ShareService::SHARE_ITEM_TYPE_PANORAMA;
break;
}

$users = $this->ShareMapper->getSharedReceiver($item_type, $event->getObjectId());
$this->logger->info('share receiver: ' . $item_type . $event->getObjectId());
foreach ($users as $user) {
$event->setAffectedUser($user['uid_owner']);
$this->manager->publish($event);
Expand Down
2 changes: 1 addition & 1 deletion lib/Activity/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getIcon()
*/
public function filterTypes(array $types)
{
return array_merge($types, ['analytics_report'], ['analytics_dataset'], ['analytics_data']);
return array_merge($types, ['analytics_report'], ['analytics_dataset'], ['analytics_panorama']);
}

/**
Expand Down
189 changes: 106 additions & 83 deletions lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,95 +14,118 @@
use OCP\IL10N;
use OCP\IURLGenerator;

class Provider implements IProvider
{
class Provider implements IProvider {

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

public function __construct(IURLGenerator $urlGenerator,
IL10N $l10n,
$userId)
{
$this->userId = $userId;
$this->urlGenerator = $urlGenerator;
$this->l10n = $l10n;
}
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();
}
/**
* @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);
$parsedSubject = '';
$ownActivity = ($event->getAuthor() === $this->userId);

switch ($event->getSubject()) {
case ActivityManager::SUBJECT_REPORT_ADD:
case ActivityManager::SUBJECT_REPORT_ADD_depr:
$parsedSubject = $this->l10n->t('You created a new report: {report}');
break;
case ActivityManager::SUBJECT_REPORT_DELETE:
case ActivityManager::SUBJECT_REPORT_DELETE_depr:
$parsedSubject = $this->l10n->t('You deleted report {report}');
break;
case ActivityManager::SUBJECT_REPORT_SHARE:
case ActivityManager::SUBJECT_REPORT_SHARE_depr:
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 dataset {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . ' ' .$this->l10n->t('has added new data to dataset {report}');
}
break;
case ActivityManager::SUBJECT_DATA_ADD_IMPORT:
if ($ownActivity) {
$parsedSubject = $this->l10n->t('You have imported data in dataset {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . ' ' .$this->l10n->t('has imported data in dataset {report}');
}
break;
case ActivityManager::SUBJECT_DATA_ADD_API:
$parsedSubject = $this->l10n->t('New data has been added to dataset {report} via API');
break;
case ActivityManager::SUBJECT_DATA_ADD_DATALOAD:
$parsedSubject = $this->l10n->t('New data has been added to dataset {report} via data load');
break;
}
switch ($event->getSubject()) {
case ActivityManager::SUBJECT_REPORT_ADD:
$parsedSubject = $this->l10n->t('You created a new report: {report}');
break;
case ActivityManager::SUBJECT_REPORT_DELETE:
$parsedSubject = $this->l10n->t('You deleted the report {report}');
break;
case ActivityManager::SUBJECT_REPORT_SHARE:
if ($ownActivity) {
$parsedSubject = $this->l10n->t('You shared the report {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . ' ' . $this->l10n->t('shared the report {report} with you');
}
break;

$event->setRichSubject(
$parsedSubject,
['report' => [
'type' => 'highlight',
'id' => $event->getObjectId(),
'name' => '"'.basename($event->getObjectName()).'"',
'link' => $this->Url($event->getObjectId()),
]]
);
case ActivityManager::SUBJECT_DATASET_ADD:
$parsedSubject = $this->l10n->t('You created a new dataset: {report}');
break;
case ActivityManager::SUBJECT_DATASET_DELETE:
$parsedSubject = $this->l10n->t('You deleted the dataset {report}');
break;
case ActivityManager::SUBJECT_DATASET_SHARE:
if ($ownActivity) {
$parsedSubject = $this->l10n->t('You shared the dataset {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . ' ' . $this->l10n->t('shared the dataset {report} with you');
}
break;

$event->setParsedSubject($parsedSubject)
->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath($event->getApp(), 'app-dark.svg')));
return $event;
}
case ActivityManager::SUBJECT_PANORAMA_ADD:
$parsedSubject = $this->l10n->t('You created a new panorama: {report}');
break;
case ActivityManager::SUBJECT_PANORAMA_DELETE:
$parsedSubject = $this->l10n->t('You deleted the panorama {report}');
break;
case ActivityManager::SUBJECT_PANORAMA_SHARE:
if ($ownActivity) {
$parsedSubject = $this->l10n->t('You shared the panorama {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . ' ' . $this->l10n->t('shared the panorama {report} with you');
}
break;

public function Url($endpoint)
{
return $this->urlGenerator->linkToRouteAbsolute('analytics.page.index') . '#/r/' . $endpoint;
}
case ActivityManager::SUBJECT_DATA_ADD:
if ($ownActivity) {
$parsedSubject = $this->l10n->t('You have added new data to dataset {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . ' ' . $this->l10n->t('has added new data to dataset {report}');
}
break;
case ActivityManager::SUBJECT_DATA_ADD_IMPORT:
if ($ownActivity) {
$parsedSubject = $this->l10n->t('You have imported data in dataset {report}');
} else {
$parsedSubject = $event->getSubjectParameters()['author'] . ' ' . $this->l10n->t('has imported data in dataset {report}');
}
break;
case ActivityManager::SUBJECT_DATA_ADD_API:
$parsedSubject = $this->l10n->t('New data has been added to dataset {report} via API');
break;
case ActivityManager::SUBJECT_DATA_ADD_DATALOAD:
$parsedSubject = $this->l10n->t('New data has been added to dataset {report} via data load');
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($event->getApp(), 'app-dark.svg')));
return $event;
}

public function Url($endpoint) {
return $this->urlGenerator->linkToRouteAbsolute('analytics.page.index') . '#/r/' . $endpoint;
}
}
4 changes: 2 additions & 2 deletions lib/Controller/ApiDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function addData(int $datasetId)
}

$this->StorageService->update($datasetId, $params['dimension1'], $params['dimension2'], $params['dimension3']);
$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_API);
//$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_API);

return $this->requestResponse(
true,
Expand Down Expand Up @@ -129,7 +129,7 @@ public function addDataV2(int $datasetId)
}

$this->StorageService->update($datasetId, $dimension1, $dimension2, $value);
$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_API);
//$this->ActivityManager->triggerEvent($datasetId, ActivityManager::OBJECT_DATA, ActivityManager::SUBJECT_DATA_ADD_API);
$message = 'Data update successful';
}

Expand Down
26 changes: 25 additions & 1 deletion lib/Db/PanoramaMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,31 @@ public function readOwn(int $id)
return $result;
}

/**
/**
* get single report
* @param int $id
* @return array
* @throws Exception
*/
public function read(int $id)
{
$sql = $this->db->getQueryBuilder();
$sql->from(self::TABLE_NAME)
->select('*')
->where($sql->expr()->eq('id', $sql->createNamedParameter($id)));
$statement = $sql->executeQuery();
$result = $statement->fetch();
$statement->closeCursor();

if ($result !== false) {
$result['type'] = (int) $result['type'];
$result['parent'] = (int) $result['parent'];
}

return $result;
}

/**
* update report
* @param $id
* @param $name
Expand Down
Loading

0 comments on commit ff0d742

Please sign in to comment.