|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
|
4
|
|
|
* @copyright Copyright (c) 2016 Arthur Schiwon <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @license GNU AGPL version 3 or any later version |
|
10
|
|
|
* |
|
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
14
|
|
|
* License, or (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace OCA\AnnouncementCenter\Notification; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
use OCA\AnnouncementCenter\Manager; |
|
30
|
|
|
use OCP\Comments\ICommentsManager; |
|
31
|
|
|
use OCP\Comments\NotFoundException; |
|
32
|
|
|
use OCP\IURLGenerator; |
|
33
|
|
|
use OCP\IUser; |
|
34
|
|
|
use OCP\IUserManager; |
|
35
|
|
|
use OCP\L10N\IFactory; |
|
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 ICommentsManager */ |
|
45
|
|
|
protected $commentsManager; |
|
46
|
|
|
|
|
47
|
|
|
/** @var IFactory */ |
|
48
|
|
|
protected $l10nFactory; |
|
49
|
|
|
|
|
50
|
|
|
/** @var IUserManager */ |
|
51
|
|
|
protected $userManager; |
|
52
|
|
|
|
|
53
|
|
|
/** @var IURLGenerator */ |
|
54
|
|
|
protected $urlGenerator; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param Manager $manager |
|
58
|
|
|
* @param ICommentsManager $commentsManager |
|
59
|
|
|
* @param IFactory $l10nFactory |
|
60
|
|
|
* @param IUserManager $userManager |
|
61
|
|
|
* @param IURLGenerator $urlGenerator |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function __construct(Manager $manager, ICommentsManager $commentsManager, IFactory $l10nFactory, IUserManager $userManager, IURLGenerator $urlGenerator) { |
|
64
|
1 |
|
$this->manager = $manager; |
|
65
|
1 |
|
$this->commentsManager = $commentsManager; |
|
66
|
1 |
|
$this->userManager = $userManager; |
|
67
|
1 |
|
$this->l10nFactory = $l10nFactory; |
|
68
|
1 |
|
$this->userManager = $userManager; |
|
69
|
1 |
|
$this->urlGenerator = $urlGenerator; |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param INotification $notification |
|
74
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification |
|
75
|
|
|
* @return INotification |
|
76
|
|
|
* @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
77
|
|
|
*/ |
|
78
|
|
|
public function prepare(INotification $notification, $languageCode) { |
|
79
|
|
|
if ($notification->getApp() !== 'announcementcenter') { |
|
80
|
|
|
// Not my app => throw |
|
81
|
|
|
throw new \InvalidArgumentException(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Read the language from the notification |
|
85
|
|
|
$l = $this->l10nFactory->get('announcementcenter', $languageCode); |
|
86
|
|
|
|
|
87
|
|
|
switch ($notification->getSubject()) { |
|
88
|
|
|
// Deal with known subjects |
|
89
|
|
|
case 'announced': |
|
90
|
|
|
$params = $notification->getSubjectParameters(); |
|
91
|
|
|
$user = $this->userManager->get($params[0]); |
|
92
|
|
|
if ($user instanceof IUser) { |
|
|
|
|
|
|
93
|
|
|
$displayName = $user->getDisplayName(); |
|
94
|
|
|
} else { |
|
95
|
|
|
$displayName = $params[0]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$announcement = $this->manager->getAnnouncement((int) $notification->getObjectId(), false, false, false); |
|
99
|
|
|
$subject = str_replace("\n", ' ', $announcement['subject']); |
|
100
|
|
|
$parsedParameters = [$displayName, $subject]; |
|
101
|
|
|
|
|
102
|
|
|
$notification->setParsedMessage($announcement['message']) |
|
103
|
|
|
->setRichSubject( |
|
104
|
|
|
$l->t('{user} announced “{announcement}”'), |
|
105
|
|
|
[ |
|
106
|
|
|
'user' => [ |
|
107
|
|
|
'type' => 'user', |
|
108
|
|
|
'id' => $params[0], |
|
109
|
|
|
'name' => $displayName, |
|
110
|
|
|
], |
|
111
|
|
|
'announcement' => [ |
|
112
|
|
|
'type' => 'announcement', |
|
113
|
|
|
'id' => $notification->getObjectId(), |
|
114
|
|
|
'name' => $subject, |
|
115
|
|
|
'link' => $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index') . '#' . $notification->getObjectId(), |
|
116
|
|
|
], |
|
117
|
|
|
] |
|
118
|
|
|
) |
|
119
|
|
|
->setParsedSubject( |
|
120
|
|
|
(string) $l->t('%1$s announced “%2$s”', $parsedParameters) |
|
121
|
|
|
); |
|
122
|
|
|
return $notification; |
|
123
|
|
|
|
|
124
|
|
|
case 'mention': |
|
125
|
|
|
try { |
|
126
|
|
|
$comment = $this->commentsManager->get($notification->getObjectId()); |
|
127
|
|
|
} catch(NotFoundException $e) { |
|
|
|
|
|
|
128
|
|
|
// needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all |
|
129
|
|
|
throw new \InvalidArgumentException('Comment not found', 0, $e); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$displayName = $comment->getActorId(); |
|
133
|
|
|
$isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER; |
|
134
|
|
|
if ($comment->getActorType() === 'users') { |
|
135
|
|
|
$commenter = $this->userManager->get($comment->getActorId()); |
|
136
|
|
|
if ($commenter instanceof IUser) { |
|
|
|
|
|
|
137
|
|
|
$displayName = $commenter->getDisplayName(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$parameters = $notification->getSubjectParameters(); |
|
142
|
|
|
if ($parameters[0] !== 'announcement') { |
|
143
|
|
|
throw new \InvalidArgumentException('Unsupported comment object'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$announcement = $this->manager->getAnnouncement($comment->getObjectId(), false, false, false); |
|
147
|
|
|
$announcementSubject = str_replace("\n", ' ', $announcement['subject']); |
|
148
|
|
|
|
|
149
|
|
|
if ($isDeletedActor) { |
|
150
|
|
|
$notification->setParsedSubject($l->t( |
|
151
|
|
|
'A (now) deleted user mentioned you in a comment on “%1$s”.', |
|
152
|
|
|
[$announcementSubject] |
|
153
|
|
|
)) |
|
154
|
|
|
->setRichSubject( |
|
155
|
|
|
$l->t('A (now) deleted user mentioned you in a comment on “{announcement}”'), |
|
156
|
|
|
[ |
|
157
|
|
|
'announcement' => [ |
|
158
|
|
|
'type' => 'announcement', |
|
159
|
|
|
'id' => $comment->getObjectId(), |
|
160
|
|
|
'name' => $announcementSubject, |
|
161
|
|
|
'link' => $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index') . '#' . $comment->getObjectId(), |
|
162
|
|
|
], |
|
163
|
|
|
] |
|
164
|
|
|
); |
|
165
|
|
|
} else { |
|
166
|
|
|
$notification->setParsedSubject($l->t( |
|
167
|
|
|
'%1$s mentioned you in a comment on “%2$s”.', |
|
168
|
|
|
[$displayName, $announcementSubject] |
|
169
|
|
|
)) |
|
170
|
|
|
->setRichSubject( |
|
171
|
|
|
$l->t('{user} mentioned you in a comment on “{announcement}”'), |
|
172
|
|
|
[ |
|
173
|
|
|
'user' => [ |
|
174
|
|
|
'type' => 'user', |
|
175
|
|
|
'id' => $comment->getActorId(), |
|
176
|
|
|
'name' => $displayName, |
|
177
|
|
|
], |
|
178
|
|
|
'announcement' => [ |
|
179
|
|
|
'type' => 'announcement', |
|
180
|
|
|
'id' => $comment->getObjectId(), |
|
181
|
|
|
'name' => $announcementSubject, |
|
182
|
|
|
'link' => $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index') . '#' . $comment->getObjectId(), |
|
183
|
|
|
], |
|
184
|
|
|
] |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $notification; |
|
189
|
|
|
|
|
190
|
|
|
default: |
|
191
|
|
|
// Unknown subject => Unknown notification => throw |
|
192
|
|
|
throw new \InvalidArgumentException(); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.