Remove notification on unshare/unassign and add type hints

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2021-04-07 09:49:51 +02:00
parent 2c56101789
commit 6b8f24a3b8
4 changed files with 85 additions and 38 deletions

View File

@@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net> * @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
* *
@@ -24,19 +27,24 @@
namespace OCA\Deck\Notification; namespace OCA\Deck\Notification;
use DateTime; use DateTime;
use Exception;
use OCA\Deck\AppInfo\Application; use OCA\Deck\AppInfo\Application;
use OCA\Deck\Db\Acl; use OCA\Deck\Db\Acl;
use OCA\Deck\Db\AssignmentMapper; use OCA\Deck\Db\AssignmentMapper;
use OCA\Deck\Db\Board; use OCA\Deck\Db\Board;
use OCA\Deck\Db\BoardMapper; use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Db\Card;
use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\User; use OCA\Deck\Db\User;
use OCA\Deck\Service\ConfigService; use OCA\Deck\Service\ConfigService;
use OCA\Deck\Service\PermissionService; use OCA\Deck\Service\PermissionService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\Comments\IComment; use OCP\Comments\IComment;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\Notification\IManager; use OCP\Notification\IManager;
use OCP\Notification\INotification;
class NotificationHelper { class NotificationHelper {
@@ -80,10 +88,10 @@ class NotificationHelper {
} }
/** /**
* @param $card * @throws DoesNotExistException
* @throws \OCP\AppFramework\Db\DoesNotExistException * @throws Exception thrown on invalid due date
*/ */
public function sendCardDuedate($card) { public function sendCardDuedate(Card $card): void {
// check if notification has already been sent // check if notification has already been sent
// ideally notifications should not be deleted once seen by the user so we can // ideally notifications should not be deleted once seen by the user so we can
// also deliver due date notifications for users who have been added later to a board // also deliver due date notifications for users who have been added later to a board
@@ -117,7 +125,7 @@ class NotificationHelper {
$notification $notification
->setApp('deck') ->setApp('deck')
->setUser((string)$user->getUID()) ->setUser((string)$user->getUID())
->setObject('card', $card->getId()) ->setObject('card', (string)$card->getId())
->setSubject('card-overdue', [ ->setSubject('card-overdue', [
$card->getTitle(), $board->getTitle() $card->getTitle(), $board->getTitle()
]) ])
@@ -128,25 +136,29 @@ class NotificationHelper {
$this->cardMapper->markNotified($card); $this->cardMapper->markNotified($card);
} }
public function markDuedateAsRead($card) { public function markDuedateAsRead(Card $card): void {
$notification = $this->notificationManager->createNotification(); $notification = $this->notificationManager->createNotification();
$notification $notification
->setApp('deck') ->setApp('deck')
->setObject('card', $card->getId()) ->setObject('card', (string)$card->getId())
->setSubject('card-overdue', []); ->setSubject('card-overdue', []);
$this->notificationManager->markProcessed($notification); $this->notificationManager->markProcessed($notification);
} }
public function sendCardAssigned($card, $userId) { public function sendCardAssigned(Card $card, string $userId): void {
$boardId = $this->cardMapper->findBoardId($card->getId()); $boardId = $this->cardMapper->findBoardId($card->getId());
try {
$board = $this->getBoard($boardId); $board = $this->getBoard($boardId);
} catch (Exception $e) {
return;
}
$notification = $this->notificationManager->createNotification(); $notification = $this->notificationManager->createNotification();
$notification $notification
->setApp('deck') ->setApp('deck')
->setUser((string) $userId) ->setUser($userId)
->setDateTime(new DateTime()) ->setDateTime(new DateTime())
->setObject('card', $card->getId()) ->setObject('card', (string)$card->getId())
->setSubject('card-assigned', [ ->setSubject('card-assigned', [
$card->getTitle(), $card->getTitle(),
$board->getTitle(), $board->getTitle(),
@@ -155,29 +167,53 @@ class NotificationHelper {
$this->notificationManager->notify($notification); $this->notificationManager->notify($notification);
} }
public function markCardAssignedAsRead(Card $card, string $userId): void {
$notification = $this->notificationManager->createNotification();
$notification
->setApp('deck')
->setUser($userId)
->setObject('card', (string)$card->getId())
->setSubject('card-assigned', []);
$this->notificationManager->markProcessed($notification);
}
/** /**
* Send notifications that a board was shared with a user/group * Send notifications that a board was shared with a user/group
*
* @param $boardId
* @param Acl $acl
* @throws \InvalidArgumentException
*/ */
public function sendBoardShared($boardId, $acl) { public function sendBoardShared(int $boardId, Acl $acl, bool $markAsRead = false): void {
try {
$board = $this->getBoard($boardId); $board = $this->getBoard($boardId);
} catch (Exception $e) {
return;
}
if ($acl->getType() === Acl::PERMISSION_TYPE_USER) { if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
$notification = $this->generateBoardShared($board, $acl->getParticipant()); $notification = $this->generateBoardShared($board, $acl->getParticipant());
if ($markAsRead) {
$this->notificationManager->markProcessed($notification);
} else {
$notification->setDateTime(new DateTime());
$this->notificationManager->notify($notification); $this->notificationManager->notify($notification);
} }
}
if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) { if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
$group = $this->groupManager->get($acl->getParticipant()); $group = $this->groupManager->get($acl->getParticipant());
if ($group === null) {
return;
}
foreach ($group->getUsers() as $user) { foreach ($group->getUsers() as $user) {
$notification = $this->generateBoardShared($board, $user->getUID()); $notification = $this->generateBoardShared($board, $user->getUID());
if ($markAsRead) {
$this->notificationManager->markProcessed($notification);
} else {
$notification->setDateTime(new DateTime());
$this->notificationManager->notify($notification); $this->notificationManager->notify($notification);
} }
} }
} }
}
public function sendMention(IComment $comment) { public function sendMention(IComment $comment): void {
foreach ($comment->getMentions() as $mention) { foreach ($comment->getMentions() as $mention) {
$card = $this->cardMapper->find($comment->getObjectId()); $card = $this->cardMapper->find($comment->getObjectId());
$boardId = $this->cardMapper->findBoardId($card->getId()); $boardId = $this->cardMapper->findBoardId($card->getId());
@@ -194,27 +230,22 @@ class NotificationHelper {
} }
/** /**
* @param $boardId * @throws DoesNotExistException
* @return Board * @throws MultipleObjectsReturnedException
* @throws \OCP\AppFramework\Db\DoesNotExistException
*/ */
private function getBoard($boardId, bool $withLabels = false, bool $withAcl = false) { private function getBoard(int $boardId, bool $withLabels = false, bool $withAcl = false): Board {
if (!array_key_exists($boardId, $this->boards)) { if (!array_key_exists($boardId, $this->boards)) {
$this->boards[$boardId] = $this->boardMapper->find($boardId, $withLabels, $withAcl); $this->boards[$boardId] = $this->boardMapper->find($boardId, $withLabels, $withAcl);
} }
return $this->boards[$boardId]; return $this->boards[$boardId];
} }
/** private function generateBoardShared(Board $board, string $userId): INotification {
* @param Board $board
*/
private function generateBoardShared($board, $userId) {
$notification = $this->notificationManager->createNotification(); $notification = $this->notificationManager->createNotification();
$notification $notification
->setApp('deck') ->setApp('deck')
->setUser((string) $userId) ->setUser($userId)
->setDateTime(new DateTime()) ->setObject('board', (string)$board->getId())
->setObject('board', $board->getId())
->setSubject('board-shared', [$board->getTitle(), $this->currentUser]); ->setSubject('board-shared', [$board->getTitle(), $this->currentUser]);
return $notification; return $notification;
} }

View File

@@ -74,6 +74,8 @@ class AssignmentService {
* @var IEventDispatcher * @var IEventDispatcher
*/ */
private $eventDispatcher; private $eventDispatcher;
/** @var string|null */
private $currentUser;
public function __construct( public function __construct(
PermissionService $permissionService, PermissionService $permissionService,
@@ -138,8 +140,7 @@ class AssignmentService {
} }
if ($userId !== $this->currentUser) { if ($type === Assignment::TYPE_USER && $userId !== $this->currentUser) {
/* Notifyuser about the card assignment */
$this->notificationHelper->sendCardAssigned($card, $userId); $this->notificationHelper->sendCardAssigned($card, $userId);
} }
@@ -183,8 +184,12 @@ class AssignmentService {
$assignment = $this->assignedUsersMapper->delete($assignment); $assignment = $this->assignedUsersMapper->delete($assignment);
$card = $this->cardMapper->find($cardId); $card = $this->cardMapper->find($cardId);
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $card, ActivityManager::SUBJECT_CARD_USER_UNASSIGN, ['assigneduser' => $userId]); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $card, ActivityManager::SUBJECT_CARD_USER_UNASSIGN, ['assigneduser' => $userId]);
if ($type === Assignment::TYPE_USER && $userId !== $this->currentUser) {
$this->notificationHelper->markCardAssignedAsRead($card, $userId);
}
$this->changeHelper->cardChanged($cardId); $this->changeHelper->cardChanged($cardId);
$this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card));
return $assignment; return $assignment;

View File

@@ -510,11 +510,10 @@ class BoardService {
$acl->setPermissionEdit($edit); $acl->setPermissionEdit($edit);
$acl->setPermissionShare($share); $acl->setPermissionShare($share);
$acl->setPermissionManage($manage); $acl->setPermissionManage($manage);
$this->notificationHelper->sendBoardShared($boardId, $acl);
$newAcl = $this->aclMapper->insert($acl); $newAcl = $this->aclMapper->insert($acl);
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $newAcl, ActivityManager::SUBJECT_BOARD_SHARE);
$this->notificationHelper->sendBoardShared((int)$boardId, $acl);
$this->boardMapper->mapAcl($newAcl); $this->boardMapper->mapAcl($newAcl);
$this->changeHelper->boardChanged($boardId); $this->changeHelper->boardChanged($boardId);
@@ -599,9 +598,8 @@ class BoardService {
} }
} }
$this->notificationHelper->sendBoardShared($acl->getBoardId(), $acl);
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $acl, ActivityManager::SUBJECT_BOARD_UNSHARE); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_BOARD, $acl, ActivityManager::SUBJECT_BOARD_UNSHARE);
$this->notificationHelper->sendBoardShared($acl->getBoardId(), $acl, true);
$this->changeHelper->boardChanged($acl->getBoardId()); $this->changeHelper->boardChanged($acl->getBoardId());
$version = \OCP\Util::getVersion()[0]; $version = \OCP\Util::getVersion()[0];

View File

@@ -243,6 +243,7 @@ class CardService {
$this->cardMapper->update($card); $this->cardMapper->update($card);
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $card, ActivityManager::SUBJECT_CARD_DELETE); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $card, ActivityManager::SUBJECT_CARD_DELETE);
$this->notificationHelper->markDuedateAsRead($card);
$this->changeHelper->cardChanged($card->getId(), false); $this->changeHelper->cardChanged($card->getId(), false);
$this->eventDispatcher->dispatchTyped(new CardDeletedEvent($card)); $this->eventDispatcher->dispatchTyped(new CardDeletedEvent($card));
@@ -322,6 +323,15 @@ class CardService {
$card->setOrder($order); $card->setOrder($order);
$card->setOwner($owner); $card->setOwner($owner);
$card->setDuedate($duedate); $card->setDuedate($duedate);
$resetDuedateNotification = false;
if (
$card->getDuedate() === null ||
(new \DateTime($card->getDuedate())) != (new \DateTime($changes->getBefore()->getDuedate()))
) {
$card->setNotified(false);
$resetDuedateNotification = true;
}
if ($deletedAt !== null) { if ($deletedAt !== null) {
$card->setDeletedAt($deletedAt); $card->setDeletedAt($deletedAt);
} }
@@ -341,6 +351,9 @@ class CardService {
$card = $this->cardMapper->update($card); $card = $this->cardMapper->update($card);
if ($resetDuedateNotification) {
$this->notificationHelper->markDuedateAsRead($card);
}
$this->changeHelper->cardChanged($card->getId(), true); $this->changeHelper->cardChanged($card->getId(), true);
$this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card)); $this->eventDispatcher->dispatchTyped(new CardUpdatedEvent($card));