Skip to content
Draft
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
4 changes: 2 additions & 2 deletions lib/Controller/DelegationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
51 changes: 50 additions & 1 deletion lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 48 additions & 3 deletions lib/Service/DelegationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -28,10 +32,14 @@
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");
Expand All @@ -42,7 +50,9 @@
$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 {
Expand All @@ -52,9 +62,11 @@
/**
* @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);
}

/**
Expand Down Expand Up @@ -103,4 +115,37 @@
$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();

Check failure on line 129 in lib/Service/DelegationService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable33

PossiblyNullReference

lib/Service/DelegationService.php:129:59: PossiblyNullReference: Cannot call method getDisplayName on possibly null value (see https://psalm.dev/083)

Check failure on line 129 in lib/Service/DelegationService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable32

PossiblyNullReference

lib/Service/DelegationService.php:129:59: PossiblyNullReference: Cannot call method getDisplayName on possibly null value (see https://psalm.dev/083)

Check failure on line 129 in lib/Service/DelegationService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullReference

lib/Service/DelegationService.php:129:59: PossiblyNullReference: Cannot call method getDisplayName on possibly null value (see https://psalm.dev/083)
$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);
}
}
Loading