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

live updates 🎉 #4273

Merged
merged 6 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCA\Deck\Event\AclCreatedEvent;
use OCA\Deck\Event\AclDeletedEvent;
use OCA\Deck\Event\AclUpdatedEvent;
use OCA\Deck\Event\BoardUpdatedEvent;
use OCA\Deck\Event\CardCreatedEvent;
use OCA\Deck\Event\CardDeletedEvent;
use OCA\Deck\Event\CardUpdatedEvent;
Expand Down Expand Up @@ -154,6 +155,13 @@ public function register(IRegistrationContext $context): void {
// Event listening for realtime updates via notify_push
$context->registerEventListener(SessionCreatedEvent::class, LiveUpdateListener::class);
$context->registerEventListener(SessionClosedEvent::class, LiveUpdateListener::class);
$context->registerEventListener(BoardUpdatedEvent::class, LiveUpdateListener::class);
$context->registerEventListener(CardCreatedEvent::class, LiveUpdateListener::class);
$context->registerEventListener(CardUpdatedEvent::class, LiveUpdateListener::class);
$context->registerEventListener(CardDeletedEvent::class, LiveUpdateListener::class);
$context->registerEventListener(AclCreatedEvent::class, LiveUpdateListener::class);
$context->registerEventListener(AclUpdatedEvent::class, LiveUpdateListener::class);
$context->registerEventListener(AclDeletedEvent::class, LiveUpdateListener::class);

$context->registerNotifierService(Notifier::class);
$context->registerEventListener(LoadAdditionalScriptsEvent::class, ResourceAdditionalScriptsListener::class);
Expand Down
21 changes: 18 additions & 3 deletions lib/Db/StackMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@
use OCP\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\ICache;
use OCP\ICacheFactory;

/** @template-extends DeckMapper<Stack> */
class StackMapper extends DeckMapper implements IPermissionMapper {
private CappedMemoryCache $stackCache;
private CardMapper $cardMapper;
private ICache $cache;

public function __construct(IDBConnection $db, CardMapper $cardMapper) {
public function __construct(
IDBConnection $db,
CardMapper $cardMapper,
ICacheFactory $cacheFactory
) {
parent::__construct($db, 'deck_stacks', Stack::class);
$this->cardMapper = $cardMapper;
$this->stackCache = new CappedMemoryCache();
$this->cache = $cacheFactory->createDistributed('deck-stackMapper');
}


Expand Down Expand Up @@ -157,12 +165,19 @@ public function isOwner($userId, $id): bool {
* @throws \OCP\DB\Exception
*/
public function findBoardId($id): ?int {
$result = $this->cache->get('findBoardId:' . $id);
if ($result !== null) {
return $result !== false ? $result : null;
}
try {
$entity = $this->find($id);
return $entity->getBoardId();
$result = $entity->getBoardId();
} catch (DoesNotExistException $e) {
$result = false;
} catch (MultipleObjectsReturnedException $e) {
}
return null;
$this->cache->set('findBoardId:' . $id, $result);

return $result !== false ? $result : null;
}
}
43 changes: 43 additions & 0 deletions lib/Event/BoardUpdatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* @copyright Copyright (c) 2022 chandi Langecker <git@chandi.it>
*
* @author chandi Langecker <git@chandi.it>
*
* @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/>.
*
*/

declare(strict_types=1);


namespace OCA\Deck\Event;

use OCP\EventDispatcher\Event;

class BoardUpdatedEvent extends Event {
private $boardId;

public function __construct(int $boardId) {
parent::__construct();

$this->boardId = $boardId;
}

public function getBoardId(): int {
return $this->boardId;
}
}
12 changes: 12 additions & 0 deletions lib/Event/CardUpdatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,17 @@

namespace OCA\Deck\Event;

use OCA\Deck\Db\Card;

class CardUpdatedEvent extends ACardEvent {
private $cardBefore;

public function __construct(Card $card, Card $before = null) {
parent::__construct($card);
$this->cardBefore = $before;
}

public function getCardBefore() {
return $this->cardBefore;
}
}
38 changes: 33 additions & 5 deletions lib/Listeners/LiveUpdateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@

namespace OCA\Deck\Listeners;

use OCA\Deck\Db\StackMapper;
use OCA\Deck\NotifyPushEvents;
use OCA\Deck\Event\AAclEvent;
use OCA\Deck\Event\ACardEvent;
use OCA\Deck\Event\BoardUpdatedEvent;
use OCA\Deck\Event\CardUpdatedEvent;
use OCA\Deck\Event\SessionClosedEvent;
use OCA\Deck\Event\SessionCreatedEvent;
use OCA\Deck\Service\SessionService;
Expand All @@ -37,18 +42,20 @@
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

/** @template-implements IEventListener<Event|SessionCreatedEvent|SessionClosedEvent> */
/** @template-implements IEventListener<Event|SessionCreatedEvent|SessionClosedEvent|AAclEvent|ACardEvent|CardUpdatedEvent|BoardUpdatedEvent> */
class LiveUpdateListener implements IEventListener {
private LoggerInterface $logger;
private SessionService $sessionService;
private IRequest $request;
private StackMapper $stackMapper;
private $queue;

public function __construct(
ContainerInterface $container,
IRequest $request,
LoggerInterface $logger,
SessionService $sessionService
SessionService $sessionService,
StackMapper $stackMapper
) {
try {
$this->queue = $container->get(IQueue::class);
Expand All @@ -59,6 +66,7 @@ public function __construct(
$this->logger = $logger;
$this->sessionService = $sessionService;
$this->request = $request;
$this->stackMapper = $stackMapper;
}

public function handle(Event $event): void {
Expand All @@ -68,17 +76,37 @@ public function handle(Event $event): void {
}

try {
// the web frontend is adding the Session-ID as a header on every request
// the web frontend is adding the Session-ID as a header
// TODO: verify the token! this currently allows to spoof a token from someone
// else, preventing this person from getting any live updates
// else, preventing this person from getting updates
$causingSessionToken = $this->request->getHeader('x-nc-deck-session');
if (
$event instanceof SessionCreatedEvent ||
$event instanceof SessionClosedEvent
$event instanceof SessionClosedEvent ||
$event instanceof BoardUpdatedEvent ||
$event instanceof AAclEvent
) {
$this->sessionService->notifyAllSessions($this->queue, $event->getBoardId(), NotifyPushEvents::DeckBoardUpdate, [
'id' => $event->getBoardId()
], $causingSessionToken);
} elseif ($event instanceof ACardEvent) {
$boardId = $this->stackMapper->findBoardId($event->getCard()->getStackId());
$this->sessionService->notifyAllSessions($this->queue, $boardId, NotifyPushEvents::DeckCardUpdate, [
'boardId' => $boardId,
'cardId' => $event->getCard()->getId()
], $causingSessionToken);

// if card got moved to a diferent board, we should notify
// also sessions active on the previous board
if ($event instanceof CardUpdatedEvent && $event->getCardBefore()) {
$previousBoardId = $this->stackMapper->findBoardId($event->getCardBefore()->getStackId());
if ($boardId !== $previousBoardId) {
$this->sessionService->notifyAllSessions($this->queue, $previousBoardId, NotifyPushEvents::DeckCardUpdate, [
'boardId' => $boardId,
'cardId' => $event->getCard()->getId()
], $causingSessionToken);
}
}
}
} catch (\Exception $e) {
$this->logger->error('Error when handling live update event', ['exception' => $e]);
Expand Down
1 change: 1 addition & 0 deletions lib/NotifyPushEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@

class NotifyPushEvents {
public const DeckBoardUpdate = 'deck_board_update';
public const DeckCardUpdate = 'deck_card_update';
}
2 changes: 2 additions & 0 deletions lib/Service/BoardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use OCA\Deck\Db\LabelMapper;
use OCP\IUserManager;
use OCA\Deck\BadRequestException;
use OCA\Deck\Event\BoardUpdatedEvent;
use OCA\Deck\Validators\BoardServiceValidator;
use OCP\IURLGenerator;
use OCP\Server;
Expand Down Expand Up @@ -379,6 +380,7 @@ public function update($id, $title, $color, $archived) {
$this->boardMapper->mapOwner($board);
$this->activityManager->triggerUpdateEvents(ActivityManager::DECK_OBJECT_BOARD, $changes, ActivityManager::SUBJECT_BOARD_UPDATE);
$this->changeHelper->boardChanged($board->getId());
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($board->getId()));

return $board;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Service/CardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public function update($id, $title, $stackId, $type, $owner, $description = '',
}
$this->changeHelper->cardChanged($card->getId(), true);

$this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card));
$this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $changes->getBefore()));

return $card;
}
Expand Down Expand Up @@ -443,6 +443,8 @@ public function reorder($id, $stackId, $order) {
$result[$card->getOrder()] = $card;
}
$this->changeHelper->cardChanged($id, false);
$this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card));

return array_values($result);
}

Expand Down
9 changes: 9 additions & 0 deletions lib/Service/StackService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
use OCA\Deck\Db\LabelMapper;
use OCA\Deck\Db\Stack;
use OCA\Deck\Db\StackMapper;
use OCA\Deck\Event\BoardUpdatedEvent;
use OCA\Deck\Model\CardDetails;
use OCA\Deck\NoPermissionException;
use OCA\Deck\StatusException;
use OCA\Deck\Validators\StackServiceValidator;
use OCP\EventDispatcher\IEventDispatcher;
use Psr\Log\LoggerInterface;

class StackService {
Expand All @@ -55,6 +57,7 @@ class StackService {
private ActivityManager $activityManager;
private ChangeHelper $changeHelper;
private LoggerInterface $logger;
private IEventDispatcher $eventDispatcher;
private StackServiceValidator $stackServiceValidator;

public function __construct(
Expand All @@ -70,6 +73,7 @@ public function __construct(
ActivityManager $activityManager,
ChangeHelper $changeHelper,
LoggerInterface $logger,
IEventDispatcher $eventDispatcher,
StackServiceValidator $stackServiceValidator
) {
$this->stackMapper = $stackMapper;
Expand All @@ -84,6 +88,7 @@ public function __construct(
$this->activityManager = $activityManager;
$this->changeHelper = $changeHelper;
$this->logger = $logger;
$this->eventDispatcher = $eventDispatcher;
$this->stackServiceValidator = $stackServiceValidator;
}

Expand Down Expand Up @@ -237,6 +242,7 @@ public function create($title, $boardId, $order) {
ActivityManager::DECK_OBJECT_BOARD, $stack, ActivityManager::SUBJECT_STACK_CREATE
);
$this->changeHelper->boardChanged($boardId);
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($boardId));

return $stack;
}
Expand Down Expand Up @@ -265,6 +271,7 @@ public function delete($id) {
ActivityManager::DECK_OBJECT_BOARD, $stack, ActivityManager::SUBJECT_STACK_DELETE
);
$this->changeHelper->boardChanged($stack->getBoardId());
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($stack->getBoardId()));
$this->enrichStackWithCards($stack);

return $stack;
Expand Down Expand Up @@ -306,6 +313,7 @@ public function update($id, $title, $boardId, $order, $deletedAt) {
ActivityManager::DECK_OBJECT_BOARD, $changes, ActivityManager::SUBJECT_STACK_UPDATE
);
$this->changeHelper->boardChanged($stack->getBoardId());
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($stack->getBoardId()));

return $stack;
}
Expand Down Expand Up @@ -345,6 +353,7 @@ public function reorder($id, $order) {
$result[$stack->getOrder()] = $stack;
}
$this->changeHelper->boardChanged($stackToSort->getBoardId());
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($stackToSort->getBoardId()));

return $result;
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/SessionList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ export default {
.avatar-wrapper {
background-color: #b9b9b9;
border-radius: 50%;
border-width: 2px;
border-style: solid;
border: 1px solid var(--color-border-dark);
width: var(--size);
height: var(--size);
text-align: center;
Expand Down
12 changes: 12 additions & 0 deletions src/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ hasPush = listen('deck_board_update', (name, body) => {
store.dispatch('refreshBoard', currentBoardId)
})

listen('deck_card_update', (name, body) => {

// ignore update events which we have triggered ourselves
if (isOurSessionToken(body._causingSessionToken)) return

// only handle update events for the currently open board
const currentBoardId = store.state.currentBoard?.id
if (body.boardId !== currentBoardId) return

store.dispatch('loadStacks', currentBoardId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without having checked myself: Would it be possible to only update the changed card?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be definitely nice if we could just fetch the one updated card

})

/**
* is the notify_push app active and can
* provide us with real time updates?
Expand Down
11 changes: 11 additions & 0 deletions src/store/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ export default {
addNewCard(state, card) {
state.cards.push(card)
},
setCards(state, cards) {
const deletedCards = state.cards.filter(_card => {
return cards.findIndex(c => _card.id === c.id) === -1
})
for (const card of deletedCards) {
this.commit('deleteCard', card)
}
for (const card of cards) {
this.commit('addCard', card)
}
},
},
actions: {
async addCard({ commit }, card) {
Expand Down
7 changes: 6 additions & 1 deletion src/store/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,15 @@ export default new Vuex.Store({
commit('setAssignableUsers', board.users)
},

async refreshBoard({ commit }, boardId) {
async refreshBoard({ commit, dispatch }, boardId) {
const board = await apiClient.loadById(boardId)
const etagHasChanged = board.ETag !== this.state.currentBoard.ETag
commit('setCurrentBoard', board)
commit('setAssignableUsers', board.users)

if (etagHasChanged) {
dispatch('loadStacks', boardId)
}
},

toggleShowArchived({ commit }) {
Expand Down
Loading