diff --git a/lib/Controller/DelegationController.php b/lib/Controller/DelegationController.php index 96b8ab4af1..0a982a4529 100644 --- a/lib/Controller/DelegationController.php +++ b/lib/Controller/DelegationController.php @@ -82,7 +82,7 @@ public function delegate(int $accountId, string $userId): JSONResponse { } try { - $delegation = $this->delegationService->delegate($accountId, $userId); + $delegation = $this->delegationService->delegate($account, $userId, $this->currentUserId); } catch (DelegationExistsException) { return new JSONResponse(['message' => 'Delegation already exists'], Http::STATUS_CONFLICT); } @@ -109,7 +109,7 @@ public function unDelegate(int $accountId, string $userId): JSONResponse { return new JSONResponse([], Http::STATUS_UNAUTHORIZED); } - $this->delegationService->unDelegate($accountId, $userId); + $this->delegationService->unDelegate($account, $userId, $this->currentUserId); return new JSONResponse([], Http::STATUS_OK); } } diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index dedd2e7ebd..b3f2d26b41 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -72,8 +72,57 @@ public function prepare(INotification $notification, string $languageCode): INot ] ]); break; + case 'account_delegation': + $parameters = $notification->getSubjectParameters(); + $messageParameters = $notification->getMessageParameters(); + $delegated = $messageParameters['delegated']; + if ($delegated) { + $notification->setRichSubject($l->t('{account_email} has been delegated to you'), [ + 'account_email' => [ + 'type' => 'highlight', + 'id' => (string)$parameters['id'], + 'name' => $parameters['account_email'] + ] + ]); + $notification->setRichMessage($l->t('{user} delegated {account} to you'), + [ + 'user' => [ + 'type' => 'user', + 'id' => $messageParameters['current_user_id'], + 'name' => $messageParameters['current_user_display_name'], + ], + 'account' => [ + 'type' => 'highlight', + 'id' => (string)$messageParameters['id'], + 'name' => $messageParameters['account_email'] + ] + ]); + } else { + $notification->setRichSubject($l->t('{account_email} is no longer delegated to you'), [ + 'account_email' => [ + 'type' => 'highlight', + 'id' => (string)$parameters['id'], + 'name' => $parameters['account_email'] + ] + ]); + $notification->setRichMessage($l->t('{user} revoked delagation for {account}'), + [ + 'user' => [ + 'type' => 'user', + 'id' => $messageParameters['current_user_id'], + 'name' => $messageParameters['current_user_display_name'], + ], + 'account' => [ + 'type' => 'highlight', + 'id' => (string)$messageParameters['id'], + 'name' => $messageParameters['account_email'] + ] + ]); + } + + break; default: - throw new UnknownNotificationException(); + throw new UnknownNotificationException(); } return $notification; diff --git a/lib/Service/DelegationService.php b/lib/Service/DelegationService.php index b2e1d9fbde..00050c7d53 100644 --- a/lib/Service/DelegationService.php +++ b/lib/Service/DelegationService.php @@ -9,6 +9,7 @@ namespace OCA\Mail\Service; +use OCA\Mail\Account; use OCA\Mail\Db\AliasMapper; use OCA\Mail\Db\Delegation; use OCA\Mail\Db\DelegationMapper; @@ -18,6 +19,9 @@ use OCA\Mail\Db\MessageMapper; use OCA\Mail\Exception\DelegationExistsException; use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IUserManager; +use OCP\Notification\IManager; class DelegationService { @@ -28,10 +32,14 @@ public function __construct( private MessageMapper $messageMapper, private AliasMapper $aliasMapper, private LocalMessageMapper $localMessageMapper, + private IUserManager $userManager, + private IManager $notificationManager, + private ITimeFactory $time, ) { } - public function delegate(int $accountId, string $userId): Delegation { + public function delegate(Account $account, string $userId, string $currentUserId): Delegation { + $accountId = $account->getId(); try { $this->delegationMapper->find($accountId, $userId); throw new DelegationExistsException("Delegation already exists for account $accountId and user $userId"); @@ -42,7 +50,9 @@ public function delegate(int $accountId, string $userId): Delegation { $delegation = new Delegation(); $delegation->setAccountId($accountId); $delegation->setUserId($userId); - return $this->delegationMapper->insert($delegation); + $result = $this->delegationMapper->insert($delegation); + $this->notify($userId, $currentUserId, $account, true); + return $result; } public function findDelegatedToUsersForAccount(int $accountId): array { @@ -52,9 +62,11 @@ public function findDelegatedToUsersForAccount(int $accountId): array { /** * @throws DoesNotExistException */ - public function unDelegate(int $accountId, string $userId): void { + public function unDelegate(Account $account, string $userId, string $currentUserId): void { + $accountId = $account->getId(); $delegation = $this->delegationMapper->find($accountId, $userId); $this->delegationMapper->delete($delegation); + $this->notify($userId, $currentUserId, $account, false); } /** @@ -103,4 +115,37 @@ public function resolveLocalMessageUserId(int $localMessageId, string $currentUs $accountId = $this->localMessageMapper->findAccountIdForLocalMessage($localMessageId); return $this->resolveAccountUserId($accountId, $currentUserId); } + + /** + * Send a notification on delegation + * @param string $userId The user the account is being delegated to + * @param string $currentUserId Current user + * @param Account $account The delegated account + * @param bool $delegated true for delegate|false for undelegate + * @return void + */ + private function notify(string $userId, string $currentUserId, Account $account, bool $delegated) { + $notification = $this->notificationManager->createNotification(); + $displayName = $this->userManager->get($currentUserId)->getDisplayName(); + $time = $this->time->getDateTime('now'); + $notification + ->setApp('mail') + ->setUser($userId) + ->setObject('delegation', (string)$account->getId()) + ->setSubject('account_delegation', [ + 'id' => $account->getId(), + 'account_email' => $account->getEmail(), + + ]) + ->setDateTime($time) + ->setMessage('account_delegation_changed', [ + 'id' => $account->getId(), + 'delegated' => $delegated, + 'current_user_id' => $currentUserId, + 'current_user_display_name' => $displayName, + 'account_email' => $account->getEmail(), + ] + ); + $this->notificationManager->notify($notification); + } }