Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable26] fix(hosted-hpb): Correctly handle API response when the account expir… #11046

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 14 additions & 7 deletions lib/BackgroundJob/CheckHostedSignalingServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCA\Talk\DataObjects\AccountId;
use OCA\Talk\Exceptions\HostedSignalingServerAPIException;
use OCA\Talk\Service\HostedSignalingServerService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\TimedJob;
Expand Down Expand Up @@ -79,9 +80,17 @@ protected function run($argument): void {
$accountId = new AccountId($accountId);
try {
$accountInfo = $this->hostedSignalingServerService->fetchAccountInfo($accountId);
} catch (HostedSignalingServerAPIException $e) { // API or connection issues
// do nothing and just try again later
return;
} catch (HostedSignalingServerAPIException $e) {
if ($e->getCode() === Http::STATUS_NOT_FOUND) {
// Account was deleted, so remove the information locally
$accountInfo = ['status' => 'deleted'];
} elseif ($e->getCode() === Http::STATUS_UNAUTHORIZED) {
// Account is expired and deletion is pending unless it's reactivated.
$accountInfo = ['status' => 'expired'];
} else {
// API or connection issues - do nothing and just try again later
return;
}
}

$oldStatus = $oldAccountInfo['status'] ?? '';
Expand All @@ -92,15 +101,13 @@ protected function run($argument): void {

// the status has changed
if ($oldStatus !== $newStatus) {
if ($oldStatus === 'active') {
if ($newStatus === 'deleted') {
// remove signaling servers if account is not active anymore
$this->config->deleteAppValue('spreed', 'signaling_mode');
$this->config->deleteAppValue('spreed', 'signaling_servers');

$notificationSubject = 'removed';
}

if ($newStatus === 'active') {
} elseif ($newStatus === 'active') {
// add signaling servers if account got active
$this->config->deleteAppValue('spreed', 'signaling_mode');
$this->config->setAppValue('spreed', 'signaling_servers', json_encode([
Expand Down
26 changes: 16 additions & 10 deletions lib/Controller/HostedSignalingServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,25 @@ public function deleteAccount(): DataResponse {

try {
$this->hostedSignalingServerService->deleteAccount(new AccountId($accountId));
} catch (HostedSignalingServerAPIException $e) {
if ($e->getCode() === Http::STATUS_NOT_FOUND) {
// Account was deleted, so remove the information locally
} elseif ($e->getCode() === Http::STATUS_UNAUTHORIZED) {
// Account is expired and deletion is pending unless it's reactivated.
} else {
// API or connection issues - do nothing and just try again later
return new DataResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}

$this->config->deleteAppValue('spreed', 'hosted-signaling-server-account');
$this->config->deleteAppValue('spreed', 'hosted-signaling-server-account-id');

// remove signaling servers if account is not active anymore
$this->config->deleteAppValue('spreed', 'signaling_mode');
$this->config->deleteAppValue('spreed', 'signaling_servers');
$this->config->deleteAppValue('spreed', 'hosted-signaling-server-account');
$this->config->deleteAppValue('spreed', 'hosted-signaling-server-account-id');

$this->logger->info('Deleted hosted signaling server account with ID ' . $accountId);
} catch (HostedSignalingServerAPIException $e) { // API or connection issues
return new DataResponse(['message' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
// remove signaling servers if account is not active anymore
$this->config->deleteAppValue('spreed', 'signaling_mode');
$this->config->deleteAppValue('spreed', 'signaling_servers');

$this->logger->info('Deleted hosted signaling server account with ID ' . $accountId);

return new DataResponse([], Http::STATUS_NO_CONTENT);
}
Expand Down
47 changes: 37 additions & 10 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -922,30 +922,57 @@ protected function parseHostedSignalingServer(INotification $notification, IL10N
->setLink($notification->getLink(), IAction::TYPE_WEB)
->setPrimary(true);

$parsedParameters = [];
$icon = '';
switch ($notification->getSubject()) {
case 'added':
$subject = $l->t('The hosted signaling server is now configured and will be used.');
$subject = $l->t('Hosted signaling server added');
$message = $l->t('The hosted signaling server is now configured and will be used.');
$icon = $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/video.svg'));
break;
case 'removed':
$subject = $l->t('The hosted signaling server was removed and will not be used anymore.');
$subject = $l->t('Hosted signaling server removed');
$message = $l->t('The hosted signaling server was removed and will not be used anymore.');
$icon = $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/video-off.svg'));
break;
case 'changed-status':
$subject = $l->t('The hosted signaling server account has changed the status from "{oldstatus}" to "{newstatus}".');
$subject = $l->t('Hosted signaling server changed');
$message = $l->t('The hosted signaling server account has changed the status from "{oldstatus}" to "{newstatus}".');
$icon = $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/video-switch.svg'));

$parameters = $notification->getSubjectParameters();
$subject = str_replace(
['{oldstatus}', '{newstatus}'],
[$parameters['oldstatus'], $parameters['newstatus']],
$subject
);
$parsedParameters = [
'oldstatus' => $this->createHPBParameter($parameters['oldstatus'], $l),
'newstatus' => $this->createHPBParameter($parameters['newstatus'], $l),
];
break;
default:
throw new \InvalidArgumentException('Unknown subject');
}

return $notification
->setParsedSubject($subject)
->setIcon($notification->getIcon())
->setRichSubject($subject)
->setRichMessage($message, $parsedParameters)
->setIcon($icon)
->addParsedAction($action);
}

protected function hostedHPBStatusToLabel(string $status, IL10N $l): string {
return match ($status) {
'pending' => $l->t('pending'),
'active' => $l->t('active'),
'expired' => $l->t('expired'),
'blocked' => $l->t('blocked'),
'error' => $l->t('error'),
default => $status,
};
}

protected function createHPBParameter(string $status, IL10N $l): array {
return [
'type' => 'highlight',
'id' => $status,
'name' => $this->hostedHPBStatusToLabel($status, $l),
];
}
}
Loading
Loading