Skip to content
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
7 changes: 6 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
namespace OCA\Files_DownloadLimit\AppInfo;

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 OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand All @@ -46,8 +48,11 @@ public function __construct(array $urlParams = []) {
public function register(IRegistrationContext $context): void {
/** @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $this->getContainer()->query(IEventDispatcher::class);
$eventDispatcher->addServiceListener(ShareLinkAccessedEvent::class, ShareLinkAccessedListener::class);

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

public function boot(IBootContext $context): void {
Expand Down
69 changes: 69 additions & 0 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2021 John Molakvoæ <[email protected]>
*
* @author John Molakvoæ <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Files_DownloadLimit\Listener;

use OCA\Files_DownloadLimit\AppInfo\Application;
use OCA\Files_DownloadLimit\Db\LimitMapper;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
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 handle(Event $event): void {
if (!$event instanceof BeforeTemplateRenderedEvent) {
return;
}

try {
$shareLimit = $this->limitMapper->get($event->getShare()->getToken());
} catch (\Exception $e) {
// No limits are set, ignoring...
return;
}

$this->initialStateService->provideInitialState('download_limit', [
'limit' => $shareLimit->getLimit(),
'downloads' => $shareLimit->getDownloads(),
]);
Util::addScript(Application::APP_ID, Application::APP_ID . '-public');
}
}
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"@nextcloud/axios": "^1.6.0",
"@nextcloud/initial-state": "^1.2.0",
"@nextcloud/l10n": "^1.4.1",
"@nextcloud/router": "^2.0.0",
"@nextcloud/vue": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/models/DownloadLimitAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*
*/

import ActionInput from '@nextcloud/vue/dist/Components/ActionInput'
import { translate as t } from '@nextcloud/l10n'
import ActionInput from '@nextcloud/vue/dist/Components/ActionInput'
import debounce from 'debounce'

import { setDownloadLimit } from '../service/DownloadLimitService'
Expand Down
2 changes: 1 addition & 1 deletion src/models/LimitEnableAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

import { translate as t } from '@nextcloud/l10n'
import ActionCheckbox from '@nextcloud/vue/dist/Components/ActionCheckbox'
import { deleteDownloadLimit, getDownloadLimit } from '../service/DownloadLimitService'

import { deleteDownloadLimit, getDownloadLimit } from '../service/DownloadLimitService'
export default class LimitEnableAction {

// internal state
Expand Down
39 changes: 39 additions & 0 deletions src/public.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @copyright Copyright (c) 2021 John Molakvoæ <[email protected]>
*
* @author John Molakvoæ <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { loadState } from '@nextcloud/initial-state'
import { translatePlural as n } from '@nextcloud/l10n'

const { limit, downloads } = loadState(appName, 'download_limit', { limit: -1, downloads: 0 })
console.debug('[DEBUG]', appName, { limit, downloads })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use @nextcloud/logger here instead as a followup?


window.addEventListener('DOMContentLoaded', function() {
if (limit > 0) {
const count = limit - downloads
const container = document.getElementById('header-primary-action')
const span = document.createElement('span')

span.style = 'color: var(--color-primary-text); padding: 0 10px;'
span.innerText = n('files_downloadlimit', '1 more download allowed', '{count} more downloads allowed', count, { count })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely workable, I would suggest 1 remaining download allowed for singular and {count} remaining downloads allowed for the plural version :)


container.prepend(span)
}
})
3 changes: 3 additions & 0 deletions webpack.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const path = require('path')
const webpackConfig = require('@nextcloud/webpack-vue-config')

webpackConfig.entry.public = path.resolve(path.join('src', 'public.js'))

module.exports = webpackConfig