-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathAdminNotifications.php
More file actions
194 lines (175 loc) Β· 6.53 KB
/
Copy pathAdminNotifications.php
File metadata and controls
194 lines (175 loc) Β· 6.53 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
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\IAction;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;
class AdminNotifications implements INotifier {
public function __construct(
protected IFactory $l10nFactory,
protected IURLGenerator $urlGenerator,
protected IUserManager $userManager,
protected IRootFolder $rootFolder,
) {
}
#[\Override]
public function getID(): string {
return 'admin_notifications';
}
#[\Override]
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 UnknownNotificationException When the notification was not prepared by a notifier
*/
#[\Override]
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'admin_notifications' && $notification->getApp() !== 'admin_notification_talk') {
throw new UnknownNotificationException('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((string)$file1->getPath(), '/');
if (str_starts_with($path1, '/' . $notification->getUser() . '/files/')) {
// Remove /user/files/...
[,,, $path1] = explode('/', $path1, 4);
}
$path2 = rtrim((string)$file2->getPath(), '/');
if (str_starts_with($path2, '/' . $notification->getUser() . '/files/')) {
// 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' => (string)$file1->getId(),
'name' => $file1->getName(),
'size' => (string)$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' => (string)$file2->getId(),
'name' => $file2->getName(),
'size' => (string)$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':
case 'self':
$subjectParams = $notification->getSubjectParameters();
if (isset($subjectParams['subject'])) {
// Nextcloud 30+
$notification->setRichSubject($subjectParams['subject'], $subjectParams['parameters']);
} else {
// Legacy before Nextcloud 30 (v3)
$notification->setParsedSubject($subjectParams[0]);
}
$messageParams = $notification->getMessageParameters();
if (!empty($messageParams)) {
if (!empty($messageParams['message'])) {
// Nextcloud 30+
$notification->setRichMessage($messageParams['message'], $messageParams['parameters']);
} elseif (!empty($messageParams[0])) {
// Legacy before Nextcloud 30 (v3)
$notification->setParsedMessage($messageParams[0]);
}
}
$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('notifications', 'notifications-dark.svg')));
return $notification;
default:
throw new UnknownNotificationException('subject');
}
}
}