Skip to content

Commit

Permalink
feat(statistics): data collect
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Dec 16, 2024
1 parent 4b6a34a commit b76a132
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@

use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Viewer\Event\LoadViewer;
use OCA\Whiteboard\Events\WhiteboardOpenedEvent;
use OCA\Whiteboard\Events\WhiteboardUpdatedEvent;
use OCA\Whiteboard\Listener\AddContentSecurityPolicyListener;
use OCA\Whiteboard\Listener\BeforeTemplateRenderedListener;
use OCA\Whiteboard\Listener\FileCreatedListener;
use OCA\Whiteboard\Listener\LoadViewerListener;
use OCA\Whiteboard\Listener\RegisterTemplateCreatorListener;
use OCA\Whiteboard\Listener\WhiteboardOpenedListener;
use OCA\Whiteboard\Listener\WhiteboardUpdatedListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Template\ITemplateManager;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\IL10N;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
use OCP\Util;
Expand All @@ -44,6 +50,9 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadViewer::class, LoadViewerListener::class);
$context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerEventListener(NodeCreatedEvent::class, FileCreatedListener::class);
$context->registerEventListener(WhiteboardOpenedEvent::class, WhiteboardOpenedListener::class);
$context->registerEventListener(WhiteboardUpdatedEvent::class, WhiteboardUpdatedListener::class);
}

public function boot(IBootContext $context): void {
Expand Down
5 changes: 5 additions & 0 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function update(): DataResponse {
try {
$serverUrl = $this->request->getParam('serverUrl');
$secret = $this->request->getParam('secret');
$enableStatistics = $this->request->getParam('enableStatistics');

if ($serverUrl !== null) {
$this->configService->setCollabBackendUrl($serverUrl);
Expand All @@ -44,6 +45,10 @@ public function update(): DataResponse {
$this->configService->setWhiteboardSharedSecret($secret);
}

if ($enableStatistics !== null) {
$this->configService->setWhiteboardEnableStatistics($enableStatistics);
}

return new DataResponse([
'jwt' => $this->jwtService->generateJWTFromPayload([ 'serverUrl' => $serverUrl ])
]);
Expand Down
10 changes: 10 additions & 0 deletions lib/Controller/WhiteboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace OCA\Whiteboard\Controller;

use Exception;
use OCA\Whiteboard\Events\WhiteboardOpenedEvent;
use OCA\Whiteboard\Events\WhiteboardUpdatedEvent;
use OCA\Whiteboard\Exception\InvalidUserException;
use OCA\Whiteboard\Exception\UnauthorizedException;
use OCA\Whiteboard\Service\Authentication\GetUserFromIdServiceFactory;
Expand All @@ -23,6 +25,7 @@
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;

/**
Expand All @@ -39,6 +42,7 @@ public function __construct(
private WhiteboardContentService $contentService,
private ExceptionService $exceptionService,
private ConfigService $configService,
private IEventDispatcher $dispatcher,
) {
parent::__construct($appName, $request);
}
Expand All @@ -58,6 +62,9 @@ public function show(int $fileId): DataResponse {

$data = $this->contentService->getContent($file);

$event = new WhiteboardOpenedEvent($file, $user, $data);
$this->dispatcher->dispatchTyped($event);

return new DataResponse(['data' => $data]);
} catch (Exception $e) {
return $this->exceptionService->handleException($e);
Expand All @@ -79,6 +86,9 @@ public function update(int $fileId, array $data): DataResponse {

$this->contentService->updateContent($file, $data);

$event = new WhiteboardUpdatedEvent($file, $user, $data);
$this->dispatcher->dispatchTyped($event);

return new DataResponse(['status' => 'success']);
} catch (Exception $e) {
return $this->exceptionService->handleException($e);
Expand Down
33 changes: 33 additions & 0 deletions lib/Events/AbstractWhiteboardEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Whiteboard\Events;

use OCA\Whiteboard\Model\User;
use OCP\EventDispatcher\Event;
use OCP\Files\File;

abstract class AbstractWhiteboardEvent extends Event {
public function __construct(
private File $file,
private User $user,
private array $data,
) {
}

public function getFile(): File {
return $this->file;
}

public function getUser(): User {
return $this->user;
}

public function getData(): array {
return $this->data;
}
}
11 changes: 11 additions & 0 deletions lib/Events/WhiteboardOpenedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Whiteboard\Events;

class WhiteboardOpenedEvent extends AbstractWhiteboardEvent {
}
11 changes: 11 additions & 0 deletions lib/Events/WhiteboardUpdatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Whiteboard\Events;

class WhiteboardUpdatedEvent extends AbstractWhiteboardEvent {
}
51 changes: 51 additions & 0 deletions lib/Listener/FileCreatedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Whiteboard\Listener;

use OCA\Whiteboard\Service\EventsService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\IUserSession;

/** @template-implements IEventListener<NodeCreatedEvent|Event> */
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress MissingTemplateParam
*/
final class FileCreatedListener implements IEventListener {
public function __construct(
protected EventsService $eventsService,
protected IUserSession $userSession,
) {
}

public function handle(Event $event): void {
if (!($event instanceof NodeCreatedEvent)) {
return;
}

$node = $event->getNode();

if ($node->getExtension() !== 'whiteboard') {
return;
}

$currentUser = $this->userSession->getUser();

$this->eventsService->insertEvent([
'user' => $currentUser ? $currentUser->getUID() : $node->getOwner()->getUID(),
'type' => 'created',
'share_token' => '',
'fileid' => $node->getId(),
'elements' => '',
'size' => 0,
'timestamp' => time(),
]);
}
}
47 changes: 47 additions & 0 deletions lib/Listener/WhiteboardOpenedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Whiteboard\Listener;

use OCA\Whiteboard\Events\WhiteboardOpenedEvent;
use OCA\Whiteboard\Model\PublicSharingUser;
use OCA\Whiteboard\Service\EventsService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\NodeCreatedEvent;

/** @template-implements IEventListener<NodeCreatedEvent|Event> */
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress MissingTemplateParam
*/
final class WhiteboardOpenedListener implements IEventListener {
public function __construct(
protected EventsService $eventsService,
) {
}

public function handle(Event $event): void {
if (!($event instanceof WhiteboardOpenedEvent)) {
return;
}

$user = $event->getUser();
$file = $event->getFile();
$data = $event->getData();

$this->eventsService->insertEvent([
'user' => $user->getUID(),
'type' => 'opened',
'share_token' => $user instanceof PublicSharingUser ? $user->getPublicSharingToken() : '',
'fileid' => $file->getId(),
'elements' => $data['elements'] ?? '',
'size' => $file->getSize(),
'timestamp' => time(),
]);
}
}
47 changes: 47 additions & 0 deletions lib/Listener/WhiteboardUpdatedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Whiteboard\Listener;

use OCA\Whiteboard\Events\WhiteboardUpdatedEvent;
use OCA\Whiteboard\Model\PublicSharingUser;
use OCA\Whiteboard\Service\EventsService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\NodeCreatedEvent;

/** @template-implements IEventListener<NodeCreatedEvent|Event> */
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress MissingTemplateParam
*/
final class WhiteboardUpdatedListener implements IEventListener {
public function __construct(
protected EventsService $eventsService,
) {
}

public function handle(Event $event): void {
if (!($event instanceof WhiteboardUpdatedEvent)) {
return;
}

$user = $event->getUser();
$file = $event->getFile();
$data = $event->getData();

$this->eventsService->insertEvent([
'user' => $user->getUID(),
'type' => 'opened',
'share_token' => $user instanceof PublicSharingUser ? $user->getPublicSharingToken() : '',
'fileid' => $file->getId(),
'elements' => $data['elements'] ?? '',
'size' => $file->getSize(),
'timestamp' => time(),
]);
}
}
89 changes: 89 additions & 0 deletions lib/Migration/Version1000Date20241213132620.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Whiteboard\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version1000Date20241213132620 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

if (!$schema->hasTable('whiteboard_events')) {
$table = $schema->createTable('whiteboard_events');
$table->addColumn('id', Types::BIGINT, [
'autoincrement' => true,
'notnull' => true,
'length' => 20,
]);
$table->addColumn('user', Types::STRING, [
'notnull' => false,
'length' => 64,
]);
$table->addColumn('type', Types::STRING, [
'notnull' => false,
'length' => 255,
]);
$table->addColumn('share_token', Types::STRING, [
'notnull' => false,
'length' => 64,
]);
$table->addColumn('fileid', Types::BIGINT, [
'notnull' => false,
'length' => 20,
]);
$table->addColumn('elements', Types::JSON, [
'notnull' => false,
]);
$table->addColumn('size', Types::INTEGER, [
'notnull' => false,
'length' => 11,
'default' => 0,
]);
$table->addColumn('timestamp', Types::INTEGER, [
'notnull' => true,
'length' => 11,
'default' => 0,
]);
$table->setPrimaryKey(['id']);
$table->addIndex(['user'], 'user_index');
$table->addIndex(['fileid'], 'fileid_index');
}

return $schema;
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}
}
Loading

0 comments on commit b76a132

Please sign in to comment.