Completed
Push — master ( 799497...96bd71 )
by Julius
15s queued 10s
created

Notifier::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Notification;
25
26
use OCA\Deck\Db\BoardMapper;
27
use OCA\Deck\Db\CardMapper;
28
use OCP\IURLGenerator;
29
use OCP\IUserManager;
30
use OCP\L10N\IFactory;
31
use OCP\Notification\INotification;
32
use OCP\Notification\INotifier;
33
34
class Notifier implements INotifier {
35
	/** @var IFactory */
36
	protected $l10nFactory;
37
	/** @var IURLGenerator */
38
	protected $url;
39
	/** @var IUserManager */
40
	protected $userManager;
41
	/** @var CardMapper */
42
	protected $cardMapper;
43
	/** @var BoardMapper */
44
	protected $boardMapper;
45
46
	public function __construct(
47
		IFactory $l10nFactory,
48
		IURLGenerator $url,
49
		IUserManager $userManager,
50
		CardMapper $cardMapper,
51
		BoardMapper $boardMapper
52
	) {
53
		$this->l10nFactory = $l10nFactory;
54
		$this->url = $url;
55
		$this->userManager = $userManager;
56
		$this->cardMapper = $cardMapper;
57
		$this->boardMapper = $boardMapper;
58
	}
59
60
	/**
61
	 * Identifier of the notifier, only use [a-z0-9_]
62
	 *
63
	 * @return string
64
	 * @since 17.0.0
65
	 */
66
	public function getID(): string {
67
		return 'deck';
68
	}
69
70
	/**
71
	 * Human readable name describing the notifier
72
	 *
73
	 * @return string
74
	 * @since 17.0.0
75
	 */
76
	public function getName(): string {
77
		return $this->l10nFactory->get('deck')->t('Deck');
78
	}
79
80
	/**
81
	 * @param INotification $notification
82
	 * @param string $languageCode The code of the language that should be used to prepare the notification
83
	 * @return INotification
84
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
85
	 * @since 9.0.0
86
	 */
87
	public function prepare(INotification $notification, string $languageCode): INotification {
88
		$l = $this->l10nFactory->get('deck', $languageCode);
89
		if ($notification->getApp() !== 'deck') {
90
			throw new \InvalidArgumentException();
91
		}
92
		$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('deck', 'deck-dark.svg')));
93
		$params = $notification->getSubjectParameters();
94
95
		switch ($notification->getSubject()) {
96
			case 'card-assigned':
97
				$cardId = $notification->getObjectId();
98
				$boardId = $this->cardMapper->findBoardId($cardId);
99
				$initiator = $this->userManager->get($params[2]);
100
				if ($initiator !== null) {
101
					$dn = $initiator->getDisplayName();
102
				} else {
103
					$dn = $params[2];
104
				}
105
				$notification->setParsedSubject(
106
					(string) $l->t('The card "%s" on "%s" has been assigned to you by %s.', [$params[0], $params[1], $dn])
107
				);
108
				$notification->setRichSubject(
109
					(string) $l->t('{user} has assigned the card "%s" on "%s" to you.', [$params[0], $params[1]]),
110
					[
111
						'user' => [
112
							'type' => 'user',
113
							'id' => $params[2],
114
							'name' => $dn,
115
						]
116
					]
117
				);
118
				$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#!/board/' . $boardId . '//card/' . $cardId . '');
119
				break;
120
			case 'card-overdue':
121
				$cardId = $notification->getObjectId();
122
				$boardId = $this->cardMapper->findBoardId($cardId);
123
				$notification->setParsedSubject(
124
					(string) $l->t('The card "%s" on "%s" has reached its due date.', $params)
125
				);
126
				$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#!/board/' . $boardId . '//card/' . $cardId . '');
127
				break;
128
			case 'card-comment-mentioned':
129
				$cardId = $notification->getObjectId();
130
				$boardId = $this->cardMapper->findBoardId($cardId);
131
				$initiator = $this->userManager->get($params[2]);
132
				if ($initiator !== null) {
133
					$dn = $initiator->getDisplayName();
134
				} else {
135
					$dn = $params[2];
136
				}
137
				$notification->setParsedSubject(
138
					(string) $l->t('%s has mentioned you in a comment on "%s".', [$dn, $params[0]])
139
				);
140
				$notification->setRichSubject(
141
					(string) $l->t('{user} has mentioned you in a comment on "%s".', [$params[0]]),
142
					[
143
						'user' => [
144
							'type' => 'user',
145
							'id' => $params[2],
146
							'name' => $dn,
147
						]
148
					]
149
				);
150
				if ($notification->getMessage() === '{message}') {
151
					$notification->setParsedMessage($notification->getMessageParameters()['message']);
152
				}
153
				$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#!/board/' . $boardId . '//card/' . $cardId . '');
154
				break;
155
			case 'board-shared':
156
				$boardId = $notification->getObjectId();
157
				$initiator = $this->userManager->get($params[1]);
158
				if ($initiator !== null) {
159
					$dn = $initiator->getDisplayName();
160
				} else {
161
					$dn = $params[1];
162
				}
163
				$notification->setParsedSubject(
164
					(string) $l->t('The board "%s" has been shared with you by %s.', [$params[0], $dn])
165
				);
166
				$notification->setRichSubject(
167
					(string) $l->t('{user} has shared the board %s with you.', [$params[0]]),
168
					[
169
						'user' => [
170
							'type' => 'user',
171
							'id' => $params[1],
172
							'name' => $dn,
173
						]
174
					]
175
				);
176
				$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#!/board/' . $boardId . '/');
177
				break;
178
		}
179
		return $notification;
180
	}
181
}
182