Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 67 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
Notifier | |
0.00% |
0 / 67 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getID | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
prepare | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | /** |
3 | * Nextcloud - cospend |
4 | * |
5 | * This file is licensed under the Affero General Public License version 3 or |
6 | * later. See the COPYING file. |
7 | * |
8 | * @author Julien Veyssier <eneiluj@posteo.net> |
9 | * @copyright Julien Veyssier 2019 |
10 | */ |
11 | |
12 | namespace OCA\Cospend\Notification; |
13 | |
14 | |
15 | use InvalidArgumentException; |
16 | use OCP\IConfig; |
17 | use OCP\IURLGenerator; |
18 | use OCP\IUser; |
19 | use OCP\IUserManager; |
20 | use OCP\L10N\IFactory; |
21 | use OCP\Notification\IManager as INotificationManager; |
22 | use OCP\Notification\INotification; |
23 | use OCP\Notification\INotifier; |
24 | |
25 | class Notifier implements INotifier { |
26 | |
27 | /** @var IFactory */ |
28 | protected $factory; |
29 | |
30 | /** @var IUserManager */ |
31 | protected $userManager; |
32 | |
33 | /** @var INotificationManager */ |
34 | protected $notificationManager; |
35 | |
36 | /** @var IURLGenerator */ |
37 | protected $url; |
38 | /** |
39 | * @var string|null |
40 | */ |
41 | private $userId; |
42 | /** |
43 | * @var IConfig |
44 | */ |
45 | private $config; |
46 | |
47 | /** |
48 | * @param IFactory $factory |
49 | * @param IConfig $config |
50 | * @param IUserManager $userManager |
51 | * @param INotificationManager $notificationManager |
52 | * @param IURLGenerator $urlGenerator |
53 | * @param string|null $userId |
54 | */ |
55 | public function __construct(IFactory $factory, |
56 | IConfig $config, |
57 | IUserManager $userManager, |
58 | INotificationManager $notificationManager, |
59 | IURLGenerator $urlGenerator, |
60 | ?string $userId) { |
61 | $this->factory = $factory; |
62 | $this->userManager = $userManager; |
63 | $this->notificationManager = $notificationManager; |
64 | $this->url = $urlGenerator; |
65 | $this->userId = $userId; |
66 | $this->config = $config; |
67 | } |
68 | |
69 | /** |
70 | * Identifier of the notifier, only use [a-z0-9_] |
71 | * |
72 | * @return string |
73 | * @since 17.0.0 |
74 | */ |
75 | public function getID(): string { |
76 | return 'cospend'; |
77 | } |
78 | /** |
79 | * Human readable name describing the notifier |
80 | * |
81 | * @return string |
82 | * @since 17.0.0 |
83 | */ |
84 | public function getName(): string { |
85 | return $this->factory->get('cospend')->t('Cospend'); |
86 | } |
87 | |
88 | /** |
89 | * @param INotification $notification |
90 | * @param string $languageCode The code of the language that should be used to prepare the notification |
91 | * @return INotification |
92 | * @throws InvalidArgumentException When the notification was not prepared by a notifier |
93 | * @since 9.0.0 |
94 | */ |
95 | public function prepare(INotification $notification, string $languageCode): INotification { |
96 | if ($notification->getApp() !== 'cospend') { |
97 | // Not my app => throw |
98 | throw new InvalidArgumentException(); |
99 | } |
100 | |
101 | $l = $this->factory->get('cospend', $languageCode); |
102 | |
103 | switch ($notification->getSubject()) { |
104 | case 'add_user_share': |
105 | $p = $notification->getSubjectParameters(); |
106 | $user = $this->userManager->get($p[0]); |
107 | if ($user instanceof IUser) { |
108 | $richSubjectUser = [ |
109 | 'type' => 'user', |
110 | 'id' => $p[0], |
111 | 'name' => $user->getDisplayName(), |
112 | ]; |
113 | |
114 | $subject = $l->t('Cospend project shared'); |
115 | $content = $l->t('User "%s" shared Cospend project "%s" with you.', [$p[0], $p[1]]); |
116 | $iconUrl = $this->url->getAbsoluteURL( |
117 | $this->url->imagePath('core', 'actions/share.svg') |
118 | ); |
119 | |
120 | $notification |
121 | ->setParsedSubject($subject) |
122 | ->setParsedMessage($content) |
123 | ->setLink($this->url->linkToRouteAbsolute('cospend.page.index')) |
124 | ->setRichMessage( |
125 | $l->t('{user} shared project %s with you', [$p[1]]), |
126 | [ |
127 | 'user' => $richSubjectUser, |
128 | ] |
129 | ) |
130 | ->setIcon($iconUrl); |
131 | } |
132 | return $notification; |
133 | |
134 | case 'delete_user_share': |
135 | $p = $notification->getSubjectParameters(); |
136 | $user = $this->userManager->get($p[0]); |
137 | if ($user instanceof IUser) { |
138 | $richSubjectUser = [ |
139 | 'type' => 'user', |
140 | 'id' => $p[0], |
141 | 'name' => $user->getDisplayName(), |
142 | ]; |
143 | $subject = $l->t('Cospend project share removed'); |
144 | $content = $l->t('User "%s" stopped sharing Cospend project "%s" with you.', [$p[0], $p[1]]); |
145 | $theme = $this->config->getUserValue($this->userId, 'accessibility', 'theme'); |
146 | $red = ($theme === 'dark') |
147 | ? '46BA61' |
148 | : 'E9322D'; |
149 | $iconUrl = $this->url->getAbsoluteURL('/index.php/svg/core/actions/share?color=' . $red); |
150 | |
151 | $notification |
152 | ->setParsedSubject($subject) |
153 | ->setParsedMessage($content) |
154 | ->setLink($this->url->linkToRouteAbsolute('cospend.page.index')) |
155 | ->setRichMessage( |
156 | $l->t('{user} stopped sharing project %s with you', [$p[1]]), |
157 | [ |
158 | 'user' => $richSubjectUser, |
159 | ] |
160 | ) |
161 | ->setIcon($iconUrl); |
162 | } |
163 | return $notification; |
164 | |
165 | default: |
166 | // Unknown subject => Unknown notification => throw |
167 | throw new InvalidArgumentException(); |
168 | } |
169 | } |
170 | } |