Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove deprecated code and refactor #225

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@
namespace OCA\Files_DownloadLimit\AppInfo;

use OCA\Files_DownloadLimit\Capabilities;
use OCA\Files\Event\LoadSidebar;
use OCA\Files_DownloadLimit\Listener\BeforeTemplateRenderedListener;
use OCA\Files_DownloadLimit\Listener\LoadSidebarListener;
use OCA\Files_DownloadLimit\Listener\ShareLinkAccessedListener;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Files_Sharing\Event\ShareLinkAccessedEvent;
use OCA\Files\Event\LoadSidebar;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\IEventDispatcher;

class Application extends App implements IBootstrap {
public const APP_ID = 'files_downloadlimit';
Expand All @@ -48,13 +47,9 @@ public function __construct(array $urlParams = []) {
}

public function register(IRegistrationContext $context): void {
/** @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $this->getContainer()->query(IEventDispatcher::class);

// Add listeners
$eventDispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$eventDispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
$eventDispatcher->addServiceListener(ShareLinkAccessedEvent::class, ShareLinkAccessedListener::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
$context->registerEventListener(ShareLinkAccessedEvent::class, ShareLinkAccessedListener::class);

$context->registerCapability(Capabilities::class);
}
Expand Down
48 changes: 15 additions & 33 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
use OCA\Files_DownloadLimit\Db\Limit;
use OCA\Files_DownloadLimit\Db\LimitMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
Expand All @@ -43,37 +43,21 @@
use OCP\Share\IShare;

class ApiController extends OCSController {

/** @var IConfig */
private $config;

/** @var IManager */
private $shareManager;

/** @var IUserSession */
private $userSession;

/** @var LimitMapper */
private $mapper;

public function __construct(IRequest $request,
IConfig $config,
IManager $shareManager,
IUserSession $userSession,
LimitMapper $mapper) {
public function __construct(
IRequest $request,
private IConfig $config,
private IManager $shareManager,
private IUserSession $userSession,
private LimitMapper $mapper,
) {
parent::__construct(Application::APP_ID, $request);
$this->config = $config;
$this->shareManager = $shareManager;
$this->userSession = $userSession;
$this->mapper = $mapper;
}

/**
* @NoAdminRequired
*
* Set the download limit for a given link share
*/
public function setDownloadLimit(string $token, int $limit): Response {
#[NoAdminRequired]
public function setDownloadLimit(string $token, int $limit): DataResponse {
$this->validateToken($token);

// Count needs to be at least 1
Expand Down Expand Up @@ -106,11 +90,10 @@ public function setDownloadLimit(string $token, int $limit): Response {
}

/**
* @NoAdminRequired
*
* Remove the download limit for a given link share
*/
public function removeDownloadLimit(string $token): Response {
#[NoAdminRequired]
public function removeDownloadLimit(string $token): DataResponse {
$this->validateToken($token);

try {
Expand All @@ -119,16 +102,15 @@ public function removeDownloadLimit(string $token): Response {
} catch (DoesNotExistException $e) {
// Ignore if does not exists
}

return new DataResponse();
}

/**
* @NoAdminRequired
*
* Get the download limit for a given link share
*/
public function getDownloadLimit(string $token): Response {
#[NoAdminRequired]
public function getDownloadLimit(string $token): DataResponse {
$this->validateToken($token);

try {
Expand Down
19 changes: 5 additions & 14 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,10 @@
use OCP\Util;

class BeforeTemplateRenderedListener implements IEventListener {

/** @var IInitialState */
private $initialStateService;

/** @var LimitMapper */
private $limitMapper;

public function __construct(IInitialState $initialStateService,
LimitMapper $limitMapper) {
$this->initialStateService = $initialStateService;
$this->limitMapper = $limitMapper;
public function __construct(
private IInitialState $initialStateService,
private LimitMapper $limitMapper,
) {
}

public function handle(Event $event): void {
Expand All @@ -55,9 +48,6 @@ public function handle(Event $event): void {

try {
$shareLimit = $this->limitMapper->get($event->getShare()->getToken());
} catch (\Exception $e) {
// No limits are set, ignoring...
return;
} catch (\Throwable $e) {
// No limits are set, ignoring...
return;
Expand All @@ -67,6 +57,7 @@ public function handle(Event $event): void {
'limit' => $shareLimit->getLimit(),
'downloads' => $shareLimit->getDownloads(),
]);

Util::addScript(Application::APP_ID, Application::APP_ID . '-public');
}
}
24 changes: 6 additions & 18 deletions lib/Listener/ShareLinkAccessedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

namespace OCA\Files_DownloadLimit\Listener;

use OCA\Files_DownloadLimit\Db\Limit;
use OCA\Files_DownloadLimit\Db\LimitMapper;
use OCA\Files_Sharing\Event\ShareLinkAccessedEvent;
use OCP\AppFramework\Db\DoesNotExistException;
Expand All @@ -36,22 +35,11 @@
use OCP\Share\IManager;

class ShareLinkAccessedListener implements IEventListener {

/** @var IConfig */
private $config;

/** @var IManager */
private $manager;

/** @var LimitMapper */
private $mapper;

public function __construct(IConfig $config,
IManager $manager,
LimitMapper $mapper) {
$this->config = $config;
$this->manager = $manager;
$this->mapper = $mapper;
public function __construct(
private IConfig $config,
private IManager $manager,
private LimitMapper $mapper,
) {
}

public function handle(Event $event): void {
Expand Down Expand Up @@ -81,7 +69,7 @@ public function handle(Event $event): void {
$this->mapper->delete($shareLimit);
return;
}

// Else, we just update the current download count
$shareLimit->setDownloads($downloads);
$this->mapper->update($shareLimit);
Expand Down
Loading