Skip to content

Commit

Permalink
feat: Use notify push for sync messages during editing
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Jul 27, 2023
1 parent e96d170 commit 90c9562
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 7 deletions.
26 changes: 26 additions & 0 deletions lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Exception;
use InvalidArgumentException;
use OCA\Files_Sharing\SharedStorage;
use OCA\NotifyPush\Queue\IQueue;
use OCA\Text\AppInfo\Application;
use OCA\Text\Db\Document;
use OCA\Text\Db\Session;
Expand All @@ -43,7 +44,10 @@
use OCP\IL10N;
use OCP\IRequest;
use OCP\Lock\LockedException;
use OCP\Server;
use OCP\Share\IShare;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;

class ApiService {
Expand Down Expand Up @@ -201,6 +205,7 @@ public function push(Session $session, Document $document, $version, $steps, $aw
}
try {
$result = $this->documentService->addStep($document, $session, $steps, $version);
$this->addToPushQueue($session, $document, $version);
} catch (InvalidArgumentException $e) {
return new DataResponse($e->getMessage(), 422);
} catch (DoesNotExistException $e) {
Expand All @@ -210,6 +215,27 @@ public function push(Session $session, Document $document, $version, $steps, $aw
return new DataResponse($result);
}

private function addToPushQueue(Session $session, Document $document, int $version): void {
try {
$queue = Server::get(IQueue::class);
$syncResponse = $this->sync($session, $document, $version);
$sessions = $this->sessionService->getActiveSessions($document->getId());
$sessions = array_values(array_unique(array_map(fn ($session) => $session['userId'], $sessions)));
foreach ($sessions as $userId) {
// Get sync response
$queue->push('notify_custom', [
'user' => $userId,
'message' => 'text_steps',
'body' => [
'documentId' => $document->getId(),
'response' => $syncResponse->getData(),
],
]);
}
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
}
}

public function sync(Session $session, Document $document, $version = 0, $autosaveContent = null, $documentState = null, bool $force = false, bool $manualSave = false, $token = null): DataResponse {
$documentId = $session->getDocumentId();
try {
Expand Down
192 changes: 186 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@nextcloud/l10n": "^2.2.0",
"@nextcloud/logger": "^2.5.0",
"@nextcloud/moment": "^1.2.1",
"@nextcloud/notify_push": "^1.1.3",
"@nextcloud/router": "^2.1.2",
"@nextcloud/vue": "^8.0.0-beta.2",
"@quartzy/markdown-it-mentions": "^0.2.0",
Expand Down
34 changes: 34 additions & 0 deletions src/services/NotifyService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import mitt from 'mitt'
import { listen } from '@nextcloud/notify_push'

if (!window._nc_text_notify) {
const useNotifyPush = listen('text_steps', (messageType, messageBody) => {
window._nc_text_notify?.emit('notify_push', { messageType, messageBody })
})
window._nc_text_notify = useNotifyPush ? mitt() : null
}

export default () => {
return window._nc_text_notify
}
Loading

0 comments on commit 90c9562

Please sign in to comment.