Rich object string parameters for notifications

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling
2021-04-01 16:53:30 +02:00
committed by Julius Härtl
parent e60219c9df
commit 454d515192
2 changed files with 105 additions and 12 deletions

View File

@@ -25,6 +25,7 @@ namespace OCA\Deck\Notification;
use OCA\Deck\Db\BoardMapper; use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\StackMapper;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
@@ -41,6 +42,8 @@ class Notifier implements INotifier {
protected $userManager; protected $userManager;
/** @var CardMapper */ /** @var CardMapper */
protected $cardMapper; protected $cardMapper;
/** @var StackMapper */
protected $stackMapper;
/** @var BoardMapper */ /** @var BoardMapper */
protected $boardMapper; protected $boardMapper;
@@ -49,12 +52,14 @@ class Notifier implements INotifier {
IURLGenerator $url, IURLGenerator $url,
IUserManager $userManager, IUserManager $userManager,
CardMapper $cardMapper, CardMapper $cardMapper,
StackMapper $stackMapper,
BoardMapper $boardMapper BoardMapper $boardMapper
) { ) {
$this->l10nFactory = $l10nFactory; $this->l10nFactory = $l10nFactory;
$this->url = $url; $this->url = $url;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->cardMapper = $cardMapper; $this->cardMapper = $cardMapper;
$this->stackMapper = $stackMapper;
$this->boardMapper = $boardMapper; $this->boardMapper = $boardMapper;
} }
@@ -100,6 +105,11 @@ class Notifier implements INotifier {
if (!$boardId) { if (!$boardId) {
throw new AlreadyProcessedException(); throw new AlreadyProcessedException();
} }
$card = $this->cardMapper->find($cardId);
$stackId = $card->getStackId();
$stack = $this->stackMapper->find($stackId);
$initiator = $this->userManager->get($params[2]); $initiator = $this->userManager->get($params[2]);
if ($initiator !== null) { if ($initiator !== null) {
$dn = $initiator->getDisplayName(); $dn = $initiator->getDisplayName();
@@ -110,8 +120,22 @@ class Notifier implements INotifier {
(string) $l->t('The card "%s" on "%s" has been assigned to you by %s.', [$params[0], $params[1], $dn]) (string) $l->t('The card "%s" on "%s" has been assigned to you by %s.', [$params[0], $params[1], $dn])
); );
$notification->setRichSubject( $notification->setRichSubject(
(string) $l->t('{user} has assigned the card "%s" on "%s" to you.', [$params[0], $params[1]]), $l->t('{user} has assigned the card {deck-card} on {deck-board} to you.'),
[ [
'deck-card' => [
'type' => 'deck-card',
'id' => $cardId,
'name' => $params[0],
'boardname' => $params[1],
'stackname' => $stack->getTitle(),
'link' => $this->url->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId . '/card/' . $cardId . '',
],
'deck-board' => [
'type' => 'deck-board',
'id' => $boardId,
'name' => $params[1],
'link' => $this->url->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId,
],
'user' => [ 'user' => [
'type' => 'user', 'type' => 'user',
'id' => $params[2], 'id' => $params[2],
@@ -127,9 +151,33 @@ class Notifier implements INotifier {
if (!$boardId) { if (!$boardId) {
throw new AlreadyProcessedException(); throw new AlreadyProcessedException();
} }
$card = $this->cardMapper->find($cardId);
$stackId = $card->getStackId();
$stack = $this->stackMapper->find($stackId);
$notification->setParsedSubject( $notification->setParsedSubject(
(string) $l->t('The card "%s" on "%s" has reached its due date.', $params) (string) $l->t('The card "%s" on "%s" has reached its due date.', $params)
); );
$notification->setRichSubject(
$l->t('The card {deck-card} on {deck-board} has reached its due date.'),
[
'deck-card' => [
'type' => 'deck-card',
'id' => $cardId,
'name' => $params[0],
'boardname' => $params[1],
'stackname' => $stack->getTitle(),
'link' => $this->url->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId . '/card/' . $cardId . '',
],
'deck-board' => [
'type' => 'deck-board',
'id' => $boardId,
'name' => $params[1],
'link' => $this->url->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId,
],
]
);
$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId . '/card/' . $cardId . ''); $notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId . '/card/' . $cardId . '');
break; break;
case 'card-comment-mentioned': case 'card-comment-mentioned':
@@ -138,6 +186,11 @@ class Notifier implements INotifier {
if (!$boardId) { if (!$boardId) {
throw new AlreadyProcessedException(); throw new AlreadyProcessedException();
} }
$card = $this->cardMapper->find($cardId);
$stackId = $card->getStackId();
$stack = $this->stackMapper->find($stackId);
$initiator = $this->userManager->get($params[2]); $initiator = $this->userManager->get($params[2]);
if ($initiator !== null) { if ($initiator !== null) {
$dn = $initiator->getDisplayName(); $dn = $initiator->getDisplayName();
@@ -148,8 +201,16 @@ class Notifier implements INotifier {
(string) $l->t('%s has mentioned you in a comment on "%s".', [$dn, $params[0]]) (string) $l->t('%s has mentioned you in a comment on "%s".', [$dn, $params[0]])
); );
$notification->setRichSubject( $notification->setRichSubject(
(string) $l->t('{user} has mentioned you in a comment on "%s".', [$params[0]]), $l->t('{user} has mentioned you in a comment on {deck-card}.'),
[ [
'deck-card' => [
'type' => 'deck-card',
'id' => $cardId,
'name' => $params[0],
'boardname' => $params[1],
'stackname' => $stack->getTitle(),
'link' => $this->url->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId . '/card/' . $cardId . '',
],
'user' => [ 'user' => [
'type' => 'user', 'type' => 'user',
'id' => $params[2], 'id' => $params[2],
@@ -177,8 +238,14 @@ class Notifier implements INotifier {
(string) $l->t('The board "%s" has been shared with you by %s.', [$params[0], $dn]) (string) $l->t('The board "%s" has been shared with you by %s.', [$params[0], $dn])
); );
$notification->setRichSubject( $notification->setRichSubject(
(string) $l->t('{user} has shared the board %s with you.', [$params[0]]), $l->t('{user} has shared {deck-board} with you.'),
[ [
'deck-board' => [
'type' => 'deck-board',
'id' => $boardId,
'name' => $params[0],
'link' => $this->url->linkToRouteAbsolute('deck.page.index') . '#/board/' . $boardId,
],
'user' => [ 'user' => [
'type' => 'user', 'type' => 'user',
'id' => $params[1], 'id' => $params[1],

View File

@@ -25,29 +25,33 @@ namespace OCA\Deck\Notification;
use OCA\Deck\Db\BoardMapper; use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\StackMapper;
use OCP\IL10N; use OCP\IL10N;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\L10N\IFactory; use OCP\L10N\IFactory;
use OCP\Notification\INotification; use OCP\Notification\INotification;
use PHPUnit\Framework\MockObject\MockObject;
class NotifierTest extends \Test\TestCase { class NotifierTest extends \Test\TestCase {
/** @var IFactory */ /** @var IFactory|MockObject */
protected $l10nFactory; protected $l10nFactory;
/** @var IURLGenerator */ /** @var IURLGenerator|MockObject */
protected $url; protected $url;
/** @var IUserManager */ /** @var IUserManager|MockObject */
protected $userManager; protected $userManager;
/** @var CardMapper */ /** @var CardMapper|MockObject */
protected $cardMapper; protected $cardMapper;
/** @var StackMapper|MockObject */
protected $stackMapper;
/** @var BoardMapper */ /** @var BoardMapper */
protected $boardMapper; protected $boardMapper;
/** @var IL10N|MockObject */
protected $l10n;
/** @var Notifier */ /** @var Notifier */
protected $notifier; protected $notifier;
/** @var IL10N */
protected $l10n;
public function setUp(): void { public function setUp(): void {
parent::setUp(); parent::setUp();
@@ -55,12 +59,14 @@ class NotifierTest extends \Test\TestCase {
$this->url = $this->createMock(IURLGenerator::class); $this->url = $this->createMock(IURLGenerator::class);
$this->userManager = $this->createMock(IUserManager::class); $this->userManager = $this->createMock(IUserManager::class);
$this->cardMapper = $this->createMock(CardMapper::class); $this->cardMapper = $this->createMock(CardMapper::class);
$this->stackMapper = $this->createMock(StackMapper::class);
$this->boardMapper = $this->createMock(BoardMapper::class); $this->boardMapper = $this->createMock(BoardMapper::class);
$this->notifier = new Notifier( $this->notifier = new Notifier(
$this->l10nFactory, $this->l10nFactory,
$this->url, $this->url,
$this->userManager, $this->userManager,
$this->cardMapper, $this->cardMapper,
$this->stackMapper,
$this->boardMapper $this->boardMapper
); );
$this->l10n = \OC::$server->getL10N('deck'); $this->l10n = \OC::$server->getL10N('deck');
@@ -149,7 +155,7 @@ class NotifierTest extends \Test\TestCase {
->with($expectedMessage); ->with($expectedMessage);
$notification->expects($this->once()) $notification->expects($this->once())
->method('setRichSubject') ->method('setRichSubject')
->with('{user} has mentioned you in a comment on "Card title".'); ->with('{user} has mentioned you in a comment on {deck-card}.');
$this->url->expects($this->once()) $this->url->expects($this->once())
@@ -218,11 +224,25 @@ class NotifierTest extends \Test\TestCase {
->with($expectedMessage); ->with($expectedMessage);
$notification->expects($this->once()) $notification->expects($this->once())
->method('setRichSubject') ->method('setRichSubject')
->with('{user} has assigned the card "Card title" on "Board title" to you.', [ ->with('{user} has assigned the card {deck-card} on {deck-board} to you.', [
'user' => [ 'user' => [
'type' => 'user', 'type' => 'user',
'id' => 'otheruser', 'id' => 'otheruser',
'name' => $dn, 'name' => $dn,
],
'deck-card' => [
'type' => 'deck-card',
'id' => '123',
'name' => 'Card title',
'boardname' => 'Board title',
'stackname' => null,
'link' => '#/board/123/card/123',
],
'deck-board' => [
'type' => 'deck-board',
'id' => 123,
'name' => 'Board title',
'link' => '#/board/123',
] ]
]); ]);
@@ -288,11 +308,17 @@ class NotifierTest extends \Test\TestCase {
->with($expectedMessage); ->with($expectedMessage);
$notification->expects($this->once()) $notification->expects($this->once())
->method('setRichSubject') ->method('setRichSubject')
->with('{user} has shared the board Board title with you.', [ ->with('{user} has shared {deck-board} with you.', [
'user' => [ 'user' => [
'type' => 'user', 'type' => 'user',
'id' => 'otheruser', 'id' => 'otheruser',
'name' => $dn, 'name' => $dn,
],
'deck-board' => [
'type' => 'deck-board',
'id' => 123,
'name' => 'Board title',
'link' => '#/board/123',
] ]
]); ]);