-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathAdminNotifications.php
More file actions
222 lines (199 loc) Β· 7.28 KB
/
Copy pathAdminNotifications.php
File metadata and controls
222 lines (199 loc) Β· 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017, Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Notifications\Notifier;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\IAction;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
class AdminNotifications implements INotifier {
/** @var IFactory */
protected $l10nFactory;
/** @var IURLGenerator */
protected $urlGenerator;
/** @var IUserManager */
protected $userManager;
/** @var IRootFolder */
protected $rootFolder;
public function __construct(IFactory $l10nFactory,
IURLGenerator $urlGenerator,
IUserManager $userManager,
IRootFolder $rootFolder) {
$this->l10nFactory = $l10nFactory;
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
$this->rootFolder = $rootFolder;
}
/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return 'admin_notifications';
}
/**
* Human-readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->l10nFactory->get('notifications')->t('Admin notifications');
}
/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
*/
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'admin_notifications' && $notification->getApp() !== 'admin_notification_talk') {
throw new \InvalidArgumentException('Unknown app');
}
switch ($notification->getSubject()) {
case 'dummy':
$subjectParams = $notification->getSubjectParameters();
$numActions = (int) $subjectParams[0];
$user = $this->userManager->get($notification->getUser());
assert($user instanceof IUser);
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$dirList = $userFolder->getDirectoryListing();
if (empty($dirList)) {
$file1 = $userFolder;
} else {
$file1 = array_pop($dirList);
}
if (empty($dirList)) {
$file2 = $userFolder;
} else {
$file2 = array_shift($dirList);
if ($file2 instanceof Folder) {
$dirList = $file2->getDirectoryListing();
if (!empty($dirList)) {
$file2 = array_shift($dirList);
}
}
}
$path1 = rtrim($file1->getPath(), '/');
if (strpos($path1, '/' . $notification->getUser() . '/files/') === 0) {
// Remove /user/files/...
[,,, $path1] = explode('/', $path1, 4);
}
$path2 = rtrim($file2->getPath(), '/');
if (strpos($path2, '/' . $notification->getUser() . '/files/') === 0) {
// Remove /user/files/...
[,,, $path2] = explode('/', $path2, 4);
}
$loremIpsum = 'User {actor} owns a file {item}';
$loremIpsumLong = 'Lorem {user-2} dolor sit {file-3}, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.' . "\n" . 'Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
$notification->setRichSubject($loremIpsum, [
'actor' => [
'type' => 'user',
'id' => $user->getUID(),
'name' => $user->getDisplayName(),
],
'item' => [
'type' => 'file',
'id' => $file1->getId(),
'name' => $file1->getName(),
'size' => $file1->getSize(),
'path' => $path1,
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $file1->getId()]),
'mimetype' => $file1->getMimetype(),
],
]);
$notification->setRichMessage($loremIpsumLong, [
'user-2' => [
'type' => 'user',
'id' => $user->getUID(),
'name' => $user->getDisplayName(),
],
'file-3' => [
'type' => 'file',
'id' => $file2->getId(),
'name' => $file2->getName(),
'size' => $file2->getSize(),
'path' => $path2,
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $file2->getId()]),
'mimetype' => $file2->getMimetype(),
],
]);
$primary = $notification->createAction();
$primary->setPrimary(true);
$primary->setParsedLabel('3 is prim(e|ary)');
$primary->setLink(
'https://en.wikipedia.org/wiki/3#Mathematics',
IAction::TYPE_WEB
);
$secondary = $notification->createAction();
$secondary->setPrimary(false);
$secondary->setParsedLabel('Get status');
$secondary->setLink(
$this->urlGenerator->getAbsoluteURL('status.php'),
IAction::TYPE_GET
);
$three = $notification->createAction();
$three->setPrimary(false);
$three->setParsedLabel('Delete status.php');
$three->setLink(
$this->urlGenerator->getAbsoluteURL('status.php'),
IAction::TYPE_DELETE
);
$numActions = min(3, $numActions);
switch ($numActions) {
case 3:
$notification->addParsedAction($three);
// no break
case 2:
$notification->addParsedAction($secondary);
// no break
case 1:
$notification->addParsedAction($primary);
}
$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('notifications', 'notifications-dark.svg')));
return $notification;
// Deal with known subjects
case 'cli':
case 'ocs':
$subjectParams = $notification->getSubjectParameters();
$notification->setParsedSubject($subjectParams[0]);
$messageParams = $notification->getMessageParameters();
if (isset($messageParams[0]) && $messageParams[0] !== '') {
$notification->setParsedMessage($messageParams[0]);
}
$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('notifications', 'notifications-dark.svg')));
return $notification;
default:
throw new \InvalidArgumentException('Unknown subject');
}
}
}