Merge pull request #3690 from nextcloud/bugfix/optimise_notifier_queries

Optimise queries when preparing card related notifications
This commit is contained in:
Julius Härtl
2022-04-11 13:47:44 +02:00
committed by GitHub
4 changed files with 55 additions and 24 deletions

View File

@@ -25,6 +25,13 @@ namespace OCA\Deck\Db;
use Sabre\VObject\Component\VCalendar;
/**
* @method int getId()
* @method int getBoardId()
* @method int getDeletedAt()
* @method int getLastModified()
* @method int getOrder()
*/
class Stack extends RelationalEntity {
protected $title;
protected $boardId;

View File

@@ -48,6 +48,25 @@ class StackMapper extends DeckMapper implements IPermissionMapper {
return $this->findEntity($sql, [$id]);
}
/**
* @param $cardId
* @return Stack|null
*/
public function findStackFromCardId($cardId): ?Stack {
$sql = <<<SQL
SELECT s.*
FROM `*PREFIX*deck_stacks` as `s`
INNER JOIN `*PREFIX*deck_cards` as `c` ON s.id = c.stack_id
WHERE c.id = ?
SQL;
try {
return $this->findEntity($sql, [$cardId]);
} catch (MultipleObjectsReturnedException|DoesNotExistException $e) {
}
return null;
}
public function findAll($boardId, $limit = null, $offset = null) {
$sql = 'SELECT * FROM `*PREFIX*deck_stacks` WHERE `board_id` = ? AND deleted_at = 0 ORDER BY `order`, `id`';

View File

@@ -101,15 +101,12 @@ class Notifier implements INotifier {
switch ($notification->getSubject()) {
case 'card-assigned':
$cardId = $notification->getObjectId();
$boardId = $this->cardMapper->findBoardId($cardId);
$stack = $this->stackMapper->findStackFromCardId($cardId);
$boardId = $stack ? $stack->getBoardId() : null;
if (!$boardId) {
throw new AlreadyProcessedException();
}
$card = $this->cardMapper->find($cardId);
$stackId = $card->getStackId();
$stack = $this->stackMapper->find($stackId);
$initiator = $this->userManager->get($params[2]);
if ($initiator !== null) {
$dn = $initiator->getDisplayName();
@@ -147,15 +144,12 @@ class Notifier implements INotifier {
break;
case 'card-overdue':
$cardId = $notification->getObjectId();
$boardId = $this->cardMapper->findBoardId($cardId);
$stack = $this->stackMapper->findStackFromCardId($cardId);
$boardId = $stack ? $stack->getBoardId() : null;
if (!$boardId) {
throw new AlreadyProcessedException();
}
$card = $this->cardMapper->find($cardId);
$stackId = $card->getStackId();
$stack = $this->stackMapper->find($stackId);
$notification->setParsedSubject(
(string) $l->t('The card "%s" on "%s" has reached its due date.', $params)
);
@@ -182,15 +176,12 @@ class Notifier implements INotifier {
break;
case 'card-comment-mentioned':
$cardId = $notification->getObjectId();
$boardId = $this->cardMapper->findBoardId($cardId);
$stack = $this->stackMapper->findStackFromCardId($cardId);
$boardId = $stack ? $stack->getBoardId() : null;
if (!$boardId) {
throw new AlreadyProcessedException();
}
$card = $this->cardMapper->find($cardId);
$stackId = $card->getStackId();
$stack = $this->stackMapper->find($stackId);
$initiator = $this->userManager->get($params[2]);
if ($initiator !== null) {
$dn = $initiator->getDisplayName();