Skip to content

Commit

Permalink
Merge pull request #1098 from nextcloud/fair-use-push
Browse files Browse the repository at this point in the history
Fair use of push notifications
  • Loading branch information
nickvergessen authored Oct 25, 2021
2 parents 2d5f4ef + db0111a commit 298c227
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
17 changes: 11 additions & 6 deletions composer.lock

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

8 changes: 8 additions & 0 deletions lib/Command/TestPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ protected function configure(): void {
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
if (!$this->notificationManager->isFairUseOfFreePushService()) {
$output->writeln('<error>We want to keep offering our push notification service for free, but large</error>');
$output->writeln('<error>users overload our infrastructure. For this reason we have to rate-limit the</error>');
$output->writeln('<error>use of push notifications. If you need this feature, consider setting up your</error>');
$output->writeln('<error>own push server or using Nextcloud Enterprise.</error>');
return 1;
}

$userId = $input->getArgument('user-id');
$subject = 'Testing push notifications';

Expand Down
10 changes: 10 additions & 0 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ protected function sendNotificationsToProxies(): void {
return;
}

if (!$this->notificationManager->isFairUseOfFreePushService()) {
/**
* We want to keep offering our push notification service for free, but large
* users overload our infrastructure. For this reason we have to rate-limit the
* use of push notifications. If you need this feature, consider setting up your
* own push server or using Nextcloud Enterprise.
*/
return;
}

$client = $this->clientService->newClient();
foreach ($pushNotifications as $proxyServer => $notifications) {
try {
Expand Down
83 changes: 83 additions & 0 deletions tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,83 @@ public function testPushToDeviceEncryptionError() {

$push->pushToDevice(1970, $notification);
}
public function testPushToDeviceNoFairUse() {
$push = $this->getPush(['getDevicesForUser', 'encryptAndSign', 'deletePushToken', 'validateToken', 'deletePushTokenByDeviceIdentifier']);

/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);
$notification
->method('getUser')
->willReturn('valid');

/** @var IUser|MockObject $user */
$user = $this->createMock(IUser::class);

$this->userManager->expects($this->once())
->method('get')
->with('valid')
->willReturn($user);

$push->expects($this->once())
->method('getDevicesForUser')
->willReturn([
[
'proxyserver' => 'proxyserver',
'token' => 16,
'apptype' => 'other',
],
]);

$this->config
->method('getSystemValue')
->with('debug', false)
->willReturn(false);

$this->l10nFactory
->method('getUserLanguage')
->with($user)
->willReturn('ru');

$this->notificationManager->expects($this->once())
->method('prepare')
->with($notification, 'ru')
->willReturnArgument(0);

/** @var Key|MockObject $key */
$key = $this->createMock(Key::class);

$this->keyManager->expects($this->once())
->method('getKey')
->with($user)
->willReturn($key);

$push->expects($this->exactly(1))
->method('validateToken')
->willReturn(true);

$push->expects($this->exactly(1))
->method('encryptAndSign')
->willReturn(['Payload']);

$push->expects($this->never())
->method('deletePushToken');

$this->clientService->expects($this->never())
->method('newClient');

$this->config->expects($this->once())
->method('getSystemValueBool')
->with('has_internet_connection', true)
->willReturn(true);

$this->notificationManager->method('isFairUseOfFreePushService')
->willReturn(false);

$push->method('deletePushTokenByDeviceIdentifier')
->with('123456');

$push->pushToDevice(207787, $notification);
}

public function dataPushToDeviceSending() {
return [
Expand Down Expand Up @@ -631,6 +708,9 @@ public function testPushToDeviceSending($isDebug) {
])
->willThrowException($e);

$this->notificationManager->method('isFairUseOfFreePushService')
->willReturn(true);

$push->method('deletePushTokenByDeviceIdentifier')
->with('123456');

Expand Down Expand Up @@ -762,6 +842,9 @@ public function testPushToDeviceTalkNotification(array $deviceTypes, $isTalkNoti
->with('has_internet_connection', true)
->willReturn(true);

$this->notificationManager->method('isFairUseOfFreePushService')
->willReturn(true);

$push->pushToDevice(200718, $notification);
}
}

0 comments on commit 298c227

Please sign in to comment.