From de4bf6249c122d44fc2aec7ef070aaf0cb1eeff6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 24 Feb 2025 17:46:27 +0100 Subject: [PATCH] fix(push): Improve test-push output Signed-off-by: Joas Schilling --- lib/Command/TestPush.php | 45 ++++++++++++++++++++++++++++++++-------- lib/Push.php | 33 +++++++++++++++-------------- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/lib/Command/TestPush.php b/lib/Command/TestPush.php index b174820dc..b3a80a2fb 100644 --- a/lib/Command/TestPush.php +++ b/lib/Command/TestPush.php @@ -56,7 +56,13 @@ protected function configure(): void { 'talk', null, InputOption::VALUE_NONE, - 'Test talk devices' + 'Test Talk devices' + ) + ->addOption( + 'files', + null, + InputOption::VALUE_NONE, + 'Test other devices (Files, Notes, …)' ) ; } @@ -75,32 +81,53 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $userId = $input->getArgument('user-id'); - $subject = 'Testing push notifications'; - $user = $this->userManager->get($userId); if (!$user instanceof IUser) { - $output->writeln('Unknown user'); + $output->writeln('Unknown user'); return 1; } + if ($input->getOption('talk')) { + $failed = $this->sendNotification($output, $user, 'talk'); + } else { + $failed = false; + } + if ($input->getOption('files')) { + $failed = $this->sendNotification($output, $user, 'files') || $failed; + } + if (!$input->getOption('talk') && !$input->getOption('files')) { + $failed = $this->sendNotification($output, $user, 'talk') || $failed; + $failed = $this->sendNotification($output, $user, 'files') || $failed; + } + + return $failed ? 1 : 0; + } + + protected function sendNotification(OutputInterface $output, IUser $user, string $clients): bool { + $app = $clients === 'talk' ? 'admin_notification_talk' : 'admin_notifications'; $notification = $this->notificationManager->createNotification(); $datetime = $this->timeFactory->getDateTime(); - $app = $input->getOption('talk') ? 'admin_notification_talk' : 'admin_notifications'; + + $output->writeln(''); + if ($clients === 'talk') { + $output->writeln('Testing Talk clients:'); + } else { + $output->writeln('Testing other clients: Files, Notes, …'); + } try { $notification->setApp($app) ->setUser($user->getUID()) ->setDateTime($datetime) ->setObject('admin_notifications', dechex($datetime->getTimestamp())) - ->setSubject('cli', [$subject]); + ->setSubject('cli', ['Testing push notifications']); $this->app->setOutput($output); $this->notificationManager->notify($notification); } catch (\InvalidArgumentException $e) { $output->writeln('Error while sending the notification'); - return 1; + return true; } - - return 0; + return false; } } diff --git a/lib/Push.php b/lib/Push.php index 4ac64926e..bfe69f373 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -286,13 +286,10 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf } if (empty($devices)) { - $this->printInfo('No devices found for user'); + $this->printInfo('No devices found for user'); return; } - $this->printInfo('Trying to push to ' . count($devices) . ' devices'); - $this->printInfo(''); - if (!$notification->isValidParsed()) { $language = $this->l10nFactory->getUserLanguage($user); $this->printInfo('Language is set to ' . $language); @@ -313,11 +310,16 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf $this->printInfo('Private user key size: ' . strlen($userKey->getPrivate())); $this->printInfo('Public user key size: ' . strlen($userKey->getPublic())); + + $this->printInfo(''); + $this->printInfo('Found ' . count($devices) . ' devices registered for push notifications'); $isTalkNotification = \in_array($notification->getApp(), ['spreed', 'talk', 'admin_notification_talk'], true); $devices = $this->filterDeviceList($devices, $notification->getApp()); if (empty($devices)) { + $this->printInfo('No devices left after filtering'); return; } + $this->printInfo('Trying to push to ' . count($devices) . ' devices'); // We don't push to devices that are older than 60 days $maxAge = time() - 60 * 24 * 60 * 60; @@ -347,6 +349,7 @@ public function pushToDevice(int $id, INotification $notification, ?OutputInterf $this->deletePushToken($device['token']); } } + $this->printInfo(''); if (!$this->deferPayloads) { $this->sendNotificationsToProxies(); @@ -529,7 +532,7 @@ protected function sendNotificationsToProxies(): void { 'app' => 'notifications', ]); - $this->printInfo('Could not send notification to push server [' . $proxyServer . ']: ' . $error); + $this->printInfo('Could not send notification to push server [' . $proxyServer . ']: ' . $error . ''); continue; } catch (\Exception $e) { $this->log->error($e->getMessage(), [ @@ -537,7 +540,7 @@ protected function sendNotificationsToProxies(): void { ]); $error = $e->getMessage() ?: 'no reason given'; - $this->printInfo('Could not send notification to push server [' . get_class($e) . ']: ' . $error); + $this->printInfo('Could not send notification to push server [' . get_class($e) . ']: ' . $error . ''); continue; } @@ -545,22 +548,22 @@ protected function sendNotificationsToProxies(): void { if (is_array($bodyData['unknown'])) { // Proxy returns null when the array is empty foreach ($bodyData['unknown'] as $unknownDevice) { - $this->printInfo('Deleting device because it is unknown by the push server: ' . $unknownDevice); + $this->printInfo('Deleting device because it is unknown by the push server: ' . $unknownDevice . ''); $this->deletePushTokenByDeviceIdentifier($unknownDevice); } } if ($bodyData['failed'] !== 0) { - $this->printInfo('Push notification sent, but ' . $bodyData['failed'] . ' failed'); + $this->printInfo('Push notification sent, but ' . $bodyData['failed'] . ' failed'); } else { - $this->printInfo('Push notification sent successfully'); + $this->printInfo('Push notification sent successfully'); } } elseif ($status !== Http::STATUS_OK) { if ($status === Http::STATUS_TOO_MANY_REQUESTS) { $this->config->setAppValue(Application::APP_ID, 'rate_limit_reached', (string) $this->timeFactory->getTime()); } $error = $body && $bodyData === null ? $body : 'no reason given'; - $this->printInfo('Could not send notification to push server [' . $proxyServer . ']: ' . $error); + $this->printInfo('Could not send notification to push server [' . $proxyServer . ']: ' . $error . ''); $this->log->warning('Could not send notification to push server [{url}]: {error}', [ 'error' => $error, 'url' => $proxyServer, @@ -568,7 +571,7 @@ protected function sendNotificationsToProxies(): void { ]); } else { $error = $body && $bodyData === null ? $body : 'no reason given'; - $this->printInfo('Push notification sent but response was not parsable, using an outdated push proxy? [' . $proxyServer . ']: ' . $error); + $this->printInfo('Push notification sent but response was not parsable, using an outdated push proxy? [' . $proxyServer . ']: ' . $error . ''); $this->log->info('Push notification sent but response was not parsable, using an outdated push proxy? [{url}]: {error}', [ 'error' => $error, 'url' => $proxyServer, @@ -602,7 +605,7 @@ protected function validateToken(int $tokenId, int $maxAge): bool { $this->cache->set('t' . $tokenId, $age, 600); } catch (InvalidTokenException) { // Token does not exist any more, should drop the push device entry - $this->printInfo('InvalidTokenException is thrown'); + $this->printInfo('InvalidTokenException is thrown'); $this->deletePushToken($tokenId); $this->cache->set('t' . $tokenId, 0, 600); return false; @@ -614,7 +617,7 @@ protected function validateToken(int $tokenId, int $maxAge): bool { return true; } - $this->printInfo('Device token "last checked" is older than 60 days: ' . $age); + $this->printInfo('Device token "last checked" is older than 60 days: ' . $age . ''); return false; } @@ -682,14 +685,14 @@ protected function encryptAndSign(Key $userKey, array $device, int $id, INotific if (!openssl_public_encrypt(json_encode($data), $encryptedSubject, $device['devicepublickey'], OPENSSL_PKCS1_PADDING)) { $error = openssl_error_string(); $this->log->error($error, ['app' => 'notifications']); - $this->printInfo('Error while encrypting data: "' . $error . '"'); + $this->printInfo('Error while encrypting data: "' . $error . '"'); throw new \InvalidArgumentException('Failed to encrypt message for device'); } if (openssl_sign($encryptedSubject, $signature, $userKey->getPrivate(), OPENSSL_ALGO_SHA512)) { $this->printInfo('Signed encrypted push subject'); } else { - $this->printInfo('Failed to signed encrypted push subject'); + $this->printInfo('Failed to signed encrypted push subject'); } $base64EncryptedSubject = base64_encode($encryptedSubject); $base64Signature = base64_encode($signature);