Merge pull request #4273 from alangecker/live-updates

live updates 🎉
This commit is contained in:
Julius Härtl
2023-02-21 22:18:45 +01:00
committed by GitHub
16 changed files with 170 additions and 13 deletions

View File

@@ -36,6 +36,7 @@ use OCA\Deck\Db\CardMapper;
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;
@@ -154,6 +155,13 @@ class Application extends App implements IBootstrap {
// 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);

View File

@@ -29,16 +29,24 @@ use OCP\AppFramework\Db\MultipleObjectsReturnedException;
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');
}
@@ -157,12 +165,19 @@ class StackMapper extends DeckMapper implements IPermissionMapper {
* @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;
}
}

View File

@@ -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;
}
}

View File

@@ -26,5 +26,17 @@ declare(strict_types=1);
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;
}
}

View File

@@ -26,7 +26,12 @@ declare(strict_types=1);
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;
@@ -37,18 +42,20 @@ use OCP\IRequest;
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);
@@ -59,6 +66,7 @@ class LiveUpdateListener implements IEventListener {
$this->logger = $logger;
$this->sessionService = $sessionService;
$this->request = $request;
$this->stackMapper = $stackMapper;
}
public function handle(Event $event): void {
@@ -68,17 +76,37 @@ class LiveUpdateListener implements IEventListener {
}
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]);

View File

@@ -26,4 +26,5 @@ namespace OCA\Deck;
class NotifyPushEvents {
public const DeckBoardUpdate = 'deck_board_update';
public const DeckCardUpdate = 'deck_card_update';
}

View File

@@ -56,6 +56,7 @@ use OCA\Deck\Db\BoardMapper;
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;
@@ -379,6 +380,7 @@ class BoardService {
$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;
}

View File

@@ -353,7 +353,7 @@ class CardService {
}
$this->changeHelper->cardChanged($card->getId(), true);
$this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card));
$this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card, $changes->getBefore()));
return $card;
}
@@ -443,6 +443,8 @@ class CardService {
$result[$card->getOrder()] = $card;
}
$this->changeHelper->cardChanged($id, false);
$this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card));
return array_values($result);
}

View File

@@ -36,10 +36,12 @@ use OCA\Deck\Db\ChangeHelper;
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 {
@@ -55,6 +57,7 @@ class StackService {
private ActivityManager $activityManager;
private ChangeHelper $changeHelper;
private LoggerInterface $logger;
private IEventDispatcher $eventDispatcher;
private StackServiceValidator $stackServiceValidator;
public function __construct(
@@ -70,6 +73,7 @@ class StackService {
ActivityManager $activityManager,
ChangeHelper $changeHelper,
LoggerInterface $logger,
IEventDispatcher $eventDispatcher,
StackServiceValidator $stackServiceValidator
) {
$this->stackMapper = $stackMapper;
@@ -84,6 +88,7 @@ class StackService {
$this->activityManager = $activityManager;
$this->changeHelper = $changeHelper;
$this->logger = $logger;
$this->eventDispatcher = $eventDispatcher;
$this->stackServiceValidator = $stackServiceValidator;
}
@@ -237,6 +242,7 @@ class StackService {
ActivityManager::DECK_OBJECT_BOARD, $stack, ActivityManager::SUBJECT_STACK_CREATE
);
$this->changeHelper->boardChanged($boardId);
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($boardId));
return $stack;
}
@@ -265,6 +271,7 @@ class StackService {
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;
@@ -306,6 +313,7 @@ class StackService {
ActivityManager::DECK_OBJECT_BOARD, $changes, ActivityManager::SUBJECT_STACK_UPDATE
);
$this->changeHelper->boardChanged($stack->getBoardId());
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($stack->getBoardId()));
return $stack;
}
@@ -345,6 +353,7 @@ class StackService {
$result[$stack->getOrder()] = $stack;
}
$this->changeHelper->boardChanged($stackToSort->getBoardId());
$this->eventDispatcher->dispatchTyped(new BoardUpdatedEvent($stackToSort->getBoardId()));
return $result;
}

View File

@@ -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;

View File

@@ -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)
})
/**
* is the notify_push app active and can
* provide us with real time updates?

View File

@@ -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) {

View File

@@ -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 }) {

View File

@@ -84,14 +84,16 @@ export default {
call = 'loadArchivedStacks'
}
const stacks = await apiClient[call](boardId)
const cards = []
for (const i in stacks) {
const stack = stacks[i]
for (const j in stack.cards) {
commit('addCard', stack.cards[j])
cards.push(stack.cards[j])
}
delete stack.cards
commit('addStack', stack)
}
commit('setCards', cards)
},
createStack({ commit }, stack) {
stack.boardId = this.state.currentBoard.id

View File

@@ -219,6 +219,7 @@ class BoardServiceTest extends TestCase {
public function testUpdate() {
$board = new Board();
$board->setId(123);
$board->setTitle('MyBoard');
$board->setOwner('admin');
$board->setColor('00ff00');

View File

@@ -34,6 +34,7 @@ use OCA\Deck\Db\LabelMapper;
use OCA\Deck\Db\Stack;
use OCA\Deck\Db\StackMapper;
use OCA\Deck\Validators\StackServiceValidator;
use OCP\EventDispatcher\IEventDispatcher;
use Psr\Log\LoggerInterface;
use \Test\TestCase;
@@ -71,6 +72,8 @@ class StackServiceTest extends TestCase {
private $changeHelper;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
private $logger;
/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
private $eventDispatcher;
/** @var StackServiceValidator|\PHPUnit\Framework\MockObject\MockObject */
private $stackServiceValidator;
@@ -88,6 +91,7 @@ class StackServiceTest extends TestCase {
$this->activityManager = $this->createMock(ActivityManager::class);
$this->changeHelper = $this->createMock(ChangeHelper::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->stackServiceValidator = $this->createMock(StackServiceValidator::class);
$this->stackService = new StackService(
@@ -103,6 +107,7 @@ class StackServiceTest extends TestCase {
$this->activityManager,
$this->changeHelper,
$this->logger,
$this->eventDispatcher,
$this->stackServiceValidator
);
}
@@ -198,6 +203,7 @@ class StackServiceTest extends TestCase {
$this->permissionService->expects($this->once())->method('checkPermission');
$stackToBeDeleted = new Stack();
$stackToBeDeleted->setId(1);
$stackToBeDeleted->setBoardId(1);
$this->stackMapper->expects($this->once())->method('find')->willReturn($stackToBeDeleted);
$this->stackMapper->expects($this->once())->method('update')->willReturn($stackToBeDeleted);
$this->cardMapper->expects($this->once())->method('findAll')->willReturn([]);
@@ -246,6 +252,7 @@ class StackServiceTest extends TestCase {
private function createStack($id, $order) {
$stack = new Stack();
$stack->setId($id);
$stack->setBoardId(1);
$stack->setOrder($order);
return $stack;
}