-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathGenerate.php
More file actions
198 lines (176 loc) Β· 6.17 KB
/
Copy pathGenerate.php
File metadata and controls
198 lines (176 loc) Β· 6.17 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Notifications\Command;
use OCA\Notifications\App;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\RichObjectStrings\IValidator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Generate extends Command {
public function __construct(
protected ITimeFactory $timeFactory,
protected IUserManager $userManager,
protected IManager $notificationManager,
protected IValidator $richValidator,
protected App $notificationApp,
) {
parent::__construct();
}
#[\Override]
protected function configure(): void {
$this
->setName('notification:generate')
->setDescription('Generate a notification for the given user')
->addArgument(
'user-id',
InputArgument::REQUIRED,
'User ID of the user to notify'
)
->addArgument(
'short-message',
InputArgument::REQUIRED,
'Short message to be sent to the user (max. 255 characters)'
)
->addOption(
'short-parameters',
null,
InputOption::VALUE_REQUIRED,
'JSON encoded array of Rich objects to fill the short-message, see https://github.com/nextcloud/server/blob/master/lib/public/RichObjectStrings/Definitions.php for more information',
)
->addOption(
'long-message',
'l',
InputOption::VALUE_REQUIRED,
'Long message to be sent to the user (max. 4000 characters)',
''
)
->addOption(
'long-parameters',
null,
InputOption::VALUE_REQUIRED,
'JSON encoded array of Rich objects to fill the long-message, see https://github.com/nextcloud/server/blob/master/lib/public/RichObjectStrings/Definitions.php for more information',
)
->addOption(
'object-type',
null,
InputOption::VALUE_REQUIRED,
'If an object type and id is provided, previous notifications with the same type and id will be deleted for this user (max. 64 characters)',
)
->addOption(
'object-id',
null,
InputOption::VALUE_REQUIRED,
'If an object type and id is provided, previous notifications with the same type and id will be deleted for this user (max. 64 characters)',
)
->addOption(
'dummy',
'd',
InputOption::VALUE_NONE,
'Create a full-flexed dummy notification for client debugging with actions and parameters (short-message will be casted to integer and is the number of actions (max 3))'
)
->addOption(
'output-id-only',
null,
InputOption::VALUE_NONE,
'When specified only the notification ID that was generated will be printed in case of success'
)
;
}
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int {
$userId = (string)$input->getArgument('user-id');
$subject = (string)$input->getArgument('short-message');
$subjectParametersString = (string)$input->getOption('short-parameters');
$message = (string)$input->getOption('long-message');
$messageParametersString = (string)$input->getOption('long-parameters');
$dummy = $input->getOption('dummy');
$idOnly = $input->getOption('output-id-only');
$objectType = $input->getOption('object-type');
$objectId = $input->getOption('object-id');
$user = $this->userManager->get($userId);
if (!$user instanceof IUser) {
$output->writeln('Unknown user');
return 1;
}
if (!$dummy) {
if ($subject === '' || strlen($subject) > 255) {
$output->writeln('Too long or empty short-message');
return 1;
}
if ($message !== '' && strlen($message) > 4000) {
$output->writeln('Too long long-message');
return 1;
}
if ($subjectParametersString !== '') {
$subjectParameters = json_decode($subjectParametersString, true);
if (!is_array($subjectParameters)) {
$output->writeln('Short message parameters is not a valid json array');
return 1;
}
$this->richValidator->validate($subject, $subjectParameters);
$storeSubjectParameters = ['subject' => $subject, 'parameters' => $subjectParameters];
} else {
$storeSubjectParameters = [$subject];
}
if ($message !== '' && $messageParametersString !== '') {
$messageParameters = json_decode($messageParametersString, true);
if (!is_array($messageParameters)) {
$output->writeln('Long message parameters is not a valid json array');
return 1;
}
$this->richValidator->validate($message, $messageParameters);
$storeMessageParameters = ['message' => $message, 'parameters' => $messageParameters];
} else {
$storeMessageParameters = [$message];
}
$subjectTitle = 'cli';
} else {
$storeSubjectParameters = [(int)$subject];
$storeMessageParameters = [];
$subjectTitle = 'dummy';
}
$notification = $this->notificationManager->createNotification();
$datetime = $this->timeFactory->getDateTime();
$isCustomObject = $objectId !== null && $objectType !== null;
if (!$isCustomObject) {
$objectId = dechex($datetime->getTimestamp());
$objectType = 'admin_notifications';
}
try {
$notification->setApp('admin_notifications')
->setUser($user->getUID())
->setObject($objectType, $objectId);
if ($isCustomObject) {
$this->notificationManager->markProcessed($notification);
if (!$idOnly) {
$output->writeln('<comment>Previous notification for ' . $objectType . '/' . $objectId . ' marked as processed</comment>');
}
}
$notification->setDateTime($datetime)
->setSubject($subjectTitle, $storeSubjectParameters);
if ($message !== '') {
$notification->setMessage('cli', $storeMessageParameters);
}
$this->notificationManager->notify($notification);
} catch (\InvalidArgumentException) {
$output->writeln('Error while sending the notification');
return 1;
}
if ($idOnly) {
$output->writeln((string)$this->notificationApp->getLastInsertedId());
} else {
$output->writeln('<info>Notification with ID ' . (string)($this->notificationApp->getLastInsertedId() ?? 0) . '</info>');
}
return 0;
}
}