Skip to content

Commit 77800d1

Browse files
committed
feat: add user rate limiting for requests
Signed-off-by: Benjamin Frueh <benjamin.frueh@gmail.com>
1 parent 1d4b555 commit 77800d1

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

js/app.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@
4646
$context.html($context.html() + ' ' + t('data_request', 'sent!'));
4747
$context.removeClass('loading');
4848
},
49-
error: function (response) {
50-
$context.prop('disabled', '');
51-
$context.removeClass('loading');
52-
if(response.responseJSON && response.responseJSON.ocs.data.error) {
53-
$context.siblings('span.warning')
54-
.removeClass('hidden')
55-
.html(response.responseJSON.ocs.data.error);
56-
}
57-
}
49+
error: function (response) {
50+
if (response.status !== 429) $context.prop('disabled', '');
51+
$context.removeClass('loading');
52+
53+
const errorMessage = response.status === 429
54+
? t('data_request', 'Already requested, please try again later.')
55+
: (response.responseJSON?.ocs?.data?.error || t('data_request', 'Request failed'));
56+
57+
$context.siblings('span.warning')
58+
.removeClass('hidden')
59+
.html(errorMessage);
60+
}
5861
});
5962
}
6063
};

lib/Controller/DataRequestController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OCA\DataRequest\Exceptions\HintedRuntime;
1111
use OCA\DataRequest\Services\Request;
1212
use OCP\AppFramework\Http;
13+
use OCP\AppFramework\Http\Attribute\UserRateLimit;
1314
use OCP\AppFramework\Http\DataResponse;
1415
use OCP\AppFramework\OCSController;
1516
use OCP\IRequest;
@@ -33,6 +34,7 @@ public function __construct(
3334
* @NoAdminRequired
3435
* @PasswordConfirmationRequired
3536
*/
37+
#[UserRateLimit(limit: 1, period: 3600)]
3638
public function export(): DataResponse {
3739
return $this->processRequest(function (): void {
3840
$this->dataRequest->sendExportRequest();
@@ -43,6 +45,7 @@ public function export(): DataResponse {
4345
* @NoAdminRequired
4446
* @PasswordConfirmationRequired
4547
*/
48+
#[UserRateLimit(limit: 1, period: 3600)]
4649
public function deletion(): DataResponse {
4750
return $this->processRequest(function (): void {
4851
$this->dataRequest->sendDeleteRequest();

0 commit comments

Comments
 (0)