|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2016, Joas Schilling <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\AnnouncementCenter\Notification; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
use OCA\AnnouncementCenter\Manager; |
|
29
|
|
|
use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException; |
|
30
|
|
|
use OCP\IURLGenerator; |
|
31
|
|
|
use OCP\IUser; |
|
32
|
|
|
use OCP\IUserManager; |
|
33
|
|
|
use OCP\L10N\IFactory; |
|
34
|
|
|
use OCP\Notification\AlreadyProcessedException; |
|
35
|
|
|
use OCP\Notification\IManager as INotificationManager; |
|
36
|
|
|
use OCP\Notification\INotification; |
|
37
|
|
|
use OCP\Notification\INotifier; |
|
38
|
|
|
|
|
39
|
|
|
class Notifier implements INotifier { |
|
40
|
|
|
|
|
41
|
|
|
/** @var Manager */ |
|
42
|
|
|
protected $manager; |
|
43
|
|
|
|
|
44
|
|
|
/** @var IFactory */ |
|
45
|
|
|
protected $l10nFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** @var INotificationManager */ |
|
48
|
|
|
protected $notificationManager; |
|
49
|
|
|
|
|
50
|
|
|
/** @var IUserManager */ |
|
51
|
|
|
protected $userManager; |
|
52
|
|
|
|
|
53
|
|
|
/** @var IURLGenerator */ |
|
54
|
|
|
protected $urlGenerator; |
|
55
|
|
|
|
|
56
|
3 |
View Code Duplication |
public function __construct(Manager $manager, |
|
|
|
|
|
|
57
|
|
|
IFactory $l10nFactory, |
|
58
|
|
|
INotificationManager $notificationManager, |
|
59
|
|
|
IUserManager $userManager, |
|
60
|
|
|
IURLGenerator $urlGenerator) { |
|
61
|
3 |
|
$this->manager = $manager; |
|
62
|
3 |
|
$this->l10nFactory = $l10nFactory; |
|
63
|
3 |
|
$this->notificationManager = $notificationManager; |
|
64
|
3 |
|
$this->userManager = $userManager; |
|
65
|
3 |
|
$this->urlGenerator = $urlGenerator; |
|
66
|
3 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Identifier of the notifier, only use [a-z0-9_] |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
* @since 17.0.0 |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getID(): string { |
|
75
|
|
|
return 'announcementcenter'; |
|
76
|
|
|
} |
|
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->l10nFactory->get('announcementcenter')->t('Announcements'); |
|
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
|
|
|
*/ |
|
94
|
2 |
|
public function prepare(INotification $notification, string $languageCode): INotification { |
|
95
|
2 |
|
if ($notification->getApp() !== 'announcementcenter') { |
|
96
|
|
|
// Not my app => throw |
|
97
|
1 |
|
throw new \InvalidArgumentException('Unknown app'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Read the language from the notification |
|
101
|
1 |
|
$l = $this->l10nFactory->get('announcementcenter', $languageCode); |
|
102
|
|
|
|
|
103
|
1 |
|
$i = $notification->getSubject(); |
|
104
|
1 |
|
if ($i !== 'announced') { |
|
105
|
|
|
// Unknown subject => Unknown notification => throw |
|
106
|
1 |
|
throw new \InvalidArgumentException('Unknown subject'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
try { |
|
110
|
|
|
$announcement = $this->manager->getAnnouncement((int)$notification->getObjectId()); |
|
111
|
|
|
} catch (AnnouncementDoesNotExistException $e) { |
|
112
|
|
|
throw new AlreadyProcessedException(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$params = $notification->getSubjectParameters(); |
|
116
|
|
|
$user = $this->userManager->get($params[0]); |
|
117
|
|
|
if ($user instanceof IUser) { |
|
|
|
|
|
|
118
|
|
|
$displayName = $user->getDisplayName(); |
|
119
|
|
|
} else { |
|
120
|
|
|
$displayName = $params[0]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$link = $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index', [ |
|
124
|
|
|
'announcement' => $notification->getObjectId(), |
|
125
|
|
|
]); |
|
126
|
|
|
|
|
127
|
|
|
$notification->setParsedMessage($announcement->getMessage()) |
|
128
|
|
|
->setRichSubject( |
|
129
|
|
|
$l->t('{user} announced “{announcement}”'), |
|
130
|
|
|
[ |
|
131
|
|
|
'user' => [ |
|
132
|
|
|
'type' => 'user', |
|
133
|
|
|
'id' => $params[0], |
|
134
|
|
|
'name' => $displayName, |
|
135
|
|
|
], |
|
136
|
|
|
'announcement' => [ |
|
137
|
|
|
'type' => 'announcement', |
|
138
|
|
|
'id' => $notification->getObjectId(), |
|
139
|
|
|
'name' => $announcement->getSubject(), |
|
140
|
|
|
'link' => $link, |
|
141
|
|
|
], |
|
142
|
|
|
] |
|
143
|
|
|
) |
|
144
|
|
|
->setLink($link) |
|
145
|
|
|
->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('announcementcenter', 'announcementcenter-dark.svg'))); |
|
146
|
|
|
|
|
147
|
|
|
$placeholders = $replacements = []; |
|
148
|
|
|
foreach ($notification->getRichSubjectParameters() as $placeholder => $parameter) { |
|
149
|
|
|
$placeholders[] = '{' . $placeholder . '}'; |
|
150
|
|
|
$replacements[] = $parameter['name']; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$notification->setParsedSubject(str_replace($placeholders, $replacements, $notification->getRichSubject())); |
|
154
|
|
|
|
|
155
|
|
|
return $notification; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.