Skip to content

Commit 7cedf66

Browse files
committed
chore(refactor): extract PushService from ApiService
Signed-off-by: Max <max@nextcloud.com>
1 parent 1bb5586 commit 7cedf66

2 files changed

Lines changed: 59 additions & 26 deletions

File tree

lib/Service/ApiService.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Exception;
1313
use InvalidArgumentException;
14-
use OCA\NotifyPush\Queue\IQueue;
1514
use OCA\Text\Context\ContextManager;
1615
use OCA\Text\Context\IContext;
1716
use OCA\Text\Context\NewSessionData;
@@ -30,15 +29,14 @@
3029

3130
class ApiService {
3231
public function __construct(
33-
private readonly ConfigService $configService,
3432
private readonly ContextManager $contextManager,
3533
private readonly SessionService $sessionService,
3634
private readonly DocumentService $documentService,
3735
private readonly FileService $fileService,
3836
private readonly LoggerInterface $logger,
3937
private readonly LockService $lockService,
4038
private readonly IL10N $l10n,
41-
private readonly ?IQueue $queue,
39+
private readonly PushService $pushService,
4240
) {
4341
}
4442

@@ -98,8 +96,7 @@ public function push(Session $session, Document $document, int $version, array $
9896
return new DataResponse(['error' => $this->l10n->t('Editing session has expired. Please reload the page.')], Http::STATUS_PRECONDITION_FAILED);
9997
}
10098
try {
101-
$result = $this->documentService->addStep($document, $session, $steps, $version, $recoveryAttempt, $token);
102-
$this->addToPushQueue($document, [$awareness, ...array_values($steps)]);
99+
$result = $this->pushService->push($session, $document, $version, $steps, $awareness, $recoveryAttempt, $token);
103100
} catch (InvalidArgumentException $e) {
104101
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_UNPROCESSABLE_ENTITY);
105102
} catch (DoesNotExistException) {
@@ -111,27 +108,6 @@ public function push(Session $session, Document $document, int $version, array $
111108
return new DataResponse($result);
112109
}
113110

114-
private function addToPushQueue(Document $document, array $steps): void {
115-
if ($this->queue === null || !$this->configService->isNotifyPushSyncEnabled()) {
116-
return;
117-
}
118-
119-
$sessions = $this->sessionService->getActiveSessions($document->id);
120-
$userIds = array_values(array_filter(array_unique(
121-
array_map(fn ($session): ?string => $session['userId'], $sessions)
122-
)));
123-
foreach ($userIds as $userId) {
124-
$this->queue->push('notify_custom', [
125-
'user' => $userId,
126-
'message' => 'text_steps',
127-
'body' => [
128-
'documentId' => $document->getId(),
129-
'steps' => array_values(array_filter($steps)),
130-
],
131-
]);
132-
}
133-
}
134-
135111
public function sync(Session $session, Document $document, int $version = 0, ?string $shareToken = null): DataResponse {
136112
$documentId = $session->getDocumentId();
137113
$result = [];

lib/Service/PushService.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace OCA\Text\Service;
9+
10+
use InvalidArgumentException;
11+
use OCA\NotifyPush\Queue\IQueue;
12+
use OCA\Text\Db\Document;
13+
use OCA\Text\Db\Session;
14+
use OCP\AppFramework\Db\DoesNotExistException;
15+
use OCP\Files\NotPermittedException;
16+
17+
class PushService {
18+
public function __construct(
19+
private readonly ConfigService $configService,
20+
private readonly SessionService $sessionService,
21+
private readonly DocumentService $documentService,
22+
private readonly ?IQueue $queue,
23+
) {
24+
}
25+
26+
/**
27+
* @throws InvalidArgumentException
28+
* @throws DoesNotExistException
29+
* @throws NotPermittedException
30+
*/
31+
public function push(Session $session, Document $document, int $version, array $steps, string $awareness, ?int $recoveryAttempt, ?string $token = null): array {
32+
$result = $this->documentService->addStep($document, $session, $steps, $version, $recoveryAttempt, $token);
33+
$this->addToPushQueue($document, [$awareness, ...array_values($steps)]);
34+
return $result;
35+
}
36+
37+
private function addToPushQueue(Document $document, array $steps): void {
38+
if ($this->queue === null || !$this->configService->isNotifyPushSyncEnabled()) {
39+
return;
40+
}
41+
$sessions = $this->sessionService->getActiveSessions($document->id);
42+
$userIds = array_values(array_filter(array_unique(
43+
array_map(fn ($session): ?string => $session['userId'], $sessions)
44+
)));
45+
foreach ($userIds as $userId) {
46+
$this->queue->push('notify_custom', [
47+
'user' => $userId,
48+
'message' => 'text_steps',
49+
'body' => [
50+
'documentId' => $document->getId(),
51+
'steps' => array_values(array_filter($steps)),
52+
],
53+
]);
54+
}
55+
}
56+
57+
}

0 commit comments

Comments
 (0)