Skip to content

Commit ea65b91

Browse files
Merge pull request #2241 from nextcloud/backport/2225/stable30
[stable30] fix(push): Improve test-push output
2 parents 6031f55 + de4bf62 commit ea65b91

2 files changed

Lines changed: 54 additions & 24 deletions

File tree

lib/Command/TestPush.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ protected function configure(): void {
5656
'talk',
5757
null,
5858
InputOption::VALUE_NONE,
59-
'Test talk devices'
59+
'Test Talk devices'
60+
)
61+
->addOption(
62+
'files',
63+
null,
64+
InputOption::VALUE_NONE,
65+
'Test other devices (Files, Notes, …)'
6066
)
6167
;
6268
}
@@ -75,32 +81,53 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7581
}
7682

7783
$userId = $input->getArgument('user-id');
78-
$subject = 'Testing push notifications';
79-
8084
$user = $this->userManager->get($userId);
8185
if (!$user instanceof IUser) {
82-
$output->writeln('Unknown user');
86+
$output->writeln('<error>Unknown user</error>');
8387
return 1;
8488
}
8589

90+
if ($input->getOption('talk')) {
91+
$failed = $this->sendNotification($output, $user, 'talk');
92+
} else {
93+
$failed = false;
94+
}
95+
if ($input->getOption('files')) {
96+
$failed = $this->sendNotification($output, $user, 'files') || $failed;
97+
}
98+
if (!$input->getOption('talk') && !$input->getOption('files')) {
99+
$failed = $this->sendNotification($output, $user, 'talk') || $failed;
100+
$failed = $this->sendNotification($output, $user, 'files') || $failed;
101+
}
102+
103+
return $failed ? 1 : 0;
104+
}
105+
106+
protected function sendNotification(OutputInterface $output, IUser $user, string $clients): bool {
107+
$app = $clients === 'talk' ? 'admin_notification_talk' : 'admin_notifications';
86108
$notification = $this->notificationManager->createNotification();
87109
$datetime = $this->timeFactory->getDateTime();
88-
$app = $input->getOption('talk') ? 'admin_notification_talk' : 'admin_notifications';
110+
111+
$output->writeln('');
112+
if ($clients === 'talk') {
113+
$output->writeln('Testing Talk clients:');
114+
} else {
115+
$output->writeln('Testing other clients: Files, Notes, …');
116+
}
89117

90118
try {
91119
$notification->setApp($app)
92120
->setUser($user->getUID())
93121
->setDateTime($datetime)
94122
->setObject('admin_notifications', dechex($datetime->getTimestamp()))
95-
->setSubject('cli', [$subject]);
123+
->setSubject('cli', ['Testing push notifications']);
96124

97125
$this->app->setOutput($output);
98126
$this->notificationManager->notify($notification);
99127
} catch (\InvalidArgumentException $e) {
100128
$output->writeln('Error while sending the notification');
101-
return 1;
129+
return true;
102130
}
103-
104-
return 0;
131+
return false;
105132
}
106133
}

lib/Push.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,10 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
286286
}
287287

288288
if (empty($devices)) {
289-
$this->printInfo('No devices found for user');
289+
$this->printInfo('<comment>No devices found for user</comment>');
290290
return;
291291
}
292292

293-
$this->printInfo('Trying to push to ' . count($devices) . ' devices');
294-
$this->printInfo('');
295-
296293
if (!$notification->isValidParsed()) {
297294
$language = $this->l10nFactory->getUserLanguage($user);
298295
$this->printInfo('Language is set to ' . $language);
@@ -313,11 +310,16 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
313310
$this->printInfo('Private user key size: ' . strlen($userKey->getPrivate()));
314311
$this->printInfo('Public user key size: ' . strlen($userKey->getPublic()));
315312

313+
314+
$this->printInfo('');
315+
$this->printInfo('Found ' . count($devices) . ' devices registered for push notifications');
316316
$isTalkNotification = \in_array($notification->getApp(), ['spreed', 'talk', 'admin_notification_talk'], true);
317317
$devices = $this->filterDeviceList($devices, $notification->getApp());
318318
if (empty($devices)) {
319+
$this->printInfo('<comment>No devices left after filtering</comment>');
319320
return;
320321
}
322+
$this->printInfo('Trying to push to ' . count($devices) . ' devices');
321323

322324
// We don't push to devices that are older than 60 days
323325
$maxAge = time() - 60 * 24 * 60 * 60;
@@ -347,6 +349,7 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf
347349
$this->deletePushToken($device['token']);
348350
}
349351
}
352+
$this->printInfo('');
350353

351354
if (!$this->deferPayloads) {
352355
$this->sendNotificationsToProxies();
@@ -529,46 +532,46 @@ protected function sendNotificationsToProxies(): void {
529532
'app' => 'notifications',
530533
]);
531534

532-
$this->printInfo('Could not send notification to push server [' . $proxyServer . ']: ' . $error);
535+
$this->printInfo('<error>Could not send notification to push server [' . $proxyServer . ']: ' . $error . '</error>');
533536
continue;
534537
} catch (\Exception $e) {
535538
$this->log->error($e->getMessage(), [
536539
'exception' => $e,
537540
]);
538541

539542
$error = $e->getMessage() ?: 'no reason given';
540-
$this->printInfo('Could not send notification to push server [' . get_class($e) . ']: ' . $error);
543+
$this->printInfo('<error>Could not send notification to push server [' . get_class($e) . ']: ' . $error . '</error>');
541544
continue;
542545
}
543546

544547
if (is_array($bodyData) && array_key_exists('unknown', $bodyData) && array_key_exists('failed', $bodyData)) {
545548
if (is_array($bodyData['unknown'])) {
546549
// Proxy returns null when the array is empty
547550
foreach ($bodyData['unknown'] as $unknownDevice) {
548-
$this->printInfo('Deleting device because it is unknown by the push server: ' . $unknownDevice);
551+
$this->printInfo('<comment>Deleting device because it is unknown by the push server: ' . $unknownDevice . '</comment>');
549552
$this->deletePushTokenByDeviceIdentifier($unknownDevice);
550553
}
551554
}
552555

553556
if ($bodyData['failed'] !== 0) {
554-
$this->printInfo('Push notification sent, but ' . $bodyData['failed'] . ' failed');
557+
$this->printInfo('<comment>Push notification sent, but ' . $bodyData['failed'] . ' failed</comment>');
555558
} else {
556-
$this->printInfo('Push notification sent successfully');
559+
$this->printInfo('<info>Push notification sent successfully</info>');
557560
}
558561
} elseif ($status !== Http::STATUS_OK) {
559562
if ($status === Http::STATUS_TOO_MANY_REQUESTS) {
560563
$this->config->setAppValue(Application::APP_ID, 'rate_limit_reached', (string) $this->timeFactory->getTime());
561564
}
562565
$error = $body && $bodyData === null ? $body : 'no reason given';
563-
$this->printInfo('Could not send notification to push server [' . $proxyServer . ']: ' . $error);
566+
$this->printInfo('<error>Could not send notification to push server [' . $proxyServer . ']: ' . $error . '</error>');
564567
$this->log->warning('Could not send notification to push server [{url}]: {error}', [
565568
'error' => $error,
566569
'url' => $proxyServer,
567570
'app' => 'notifications',
568571
]);
569572
} else {
570573
$error = $body && $bodyData === null ? $body : 'no reason given';
571-
$this->printInfo('Push notification sent but response was not parsable, using an outdated push proxy? [' . $proxyServer . ']: ' . $error);
574+
$this->printInfo('<comment>Push notification sent but response was not parsable, using an outdated push proxy? [' . $proxyServer . ']: ' . $error . '</comment>');
572575
$this->log->info('Push notification sent but response was not parsable, using an outdated push proxy? [{url}]: {error}', [
573576
'error' => $error,
574577
'url' => $proxyServer,
@@ -602,7 +605,7 @@ protected function validateToken(int $tokenId, int $maxAge): bool {
602605
$this->cache->set('t' . $tokenId, $age, 600);
603606
} catch (InvalidTokenException) {
604607
// Token does not exist any more, should drop the push device entry
605-
$this->printInfo('InvalidTokenException is thrown');
608+
$this->printInfo('<error>InvalidTokenException is thrown</error>');
606609
$this->deletePushToken($tokenId);
607610
$this->cache->set('t' . $tokenId, 0, 600);
608611
return false;
@@ -614,7 +617,7 @@ protected function validateToken(int $tokenId, int $maxAge): bool {
614617
return true;
615618
}
616619

617-
$this->printInfo('Device token "last checked" is older than 60 days: ' . $age);
620+
$this->printInfo('<comment>Device token "last checked" is older than 60 days: ' . $age . '</comment>');
618621
return false;
619622
}
620623

@@ -682,14 +685,14 @@ protected function encryptAndSign(Key $userKey, array $device, int $id, INotific
682685
if (!openssl_public_encrypt(json_encode($data), $encryptedSubject, $device['devicepublickey'], OPENSSL_PKCS1_PADDING)) {
683686
$error = openssl_error_string();
684687
$this->log->error($error, ['app' => 'notifications']);
685-
$this->printInfo('Error while encrypting data: "' . $error . '"');
688+
$this->printInfo('<error>Error while encrypting data: "' . $error . '"</error>');
686689
throw new \InvalidArgumentException('Failed to encrypt message for device');
687690
}
688691

689692
if (openssl_sign($encryptedSubject, $signature, $userKey->getPrivate(), OPENSSL_ALGO_SHA512)) {
690693
$this->printInfo('Signed encrypted push subject');
691694
} else {
692-
$this->printInfo('Failed to signed encrypted push subject');
695+
$this->printInfo('<error>Failed to signed encrypted push subject</error>');
693696
}
694697
$base64EncryptedSubject = base64_encode($encryptedSubject);
695698
$base64Signature = base64_encode($signature);

0 commit comments

Comments
 (0)