1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Joas Schilling <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Notifications\Command; |
25
|
|
|
|
26
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
27
|
|
|
use OCP\IUser; |
28
|
|
|
use OCP\IUserManager; |
29
|
|
|
use OCP\Notification\IManager; |
30
|
|
|
use Symfony\Component\Console\Command\Command; |
31
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
32
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
33
|
|
|
use Symfony\Component\Console\Input\InputOption; |
34
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
35
|
|
|
|
36
|
|
|
class Generate extends Command { |
37
|
|
|
|
38
|
|
|
/** @var ITimeFactory */ |
39
|
|
|
protected $timeFactory; |
40
|
|
|
|
41
|
|
|
/** @var IUserManager */ |
42
|
|
|
protected $userManager; |
43
|
|
|
|
44
|
|
|
/** @var IManager */ |
45
|
|
|
protected $notificationManager; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ITimeFactory $timeFactory |
49
|
|
|
* @param IUserManager $userManager |
50
|
|
|
* @param IManager $notificationManager |
51
|
|
|
*/ |
52
|
7 |
View Code Duplication |
public function __construct(ITimeFactory $timeFactory, IUserManager $userManager, IManager $notificationManager) { |
|
|
|
|
53
|
7 |
|
parent::__construct(); |
54
|
|
|
|
55
|
7 |
|
$this->timeFactory = $timeFactory; |
56
|
7 |
|
$this->userManager = $userManager; |
57
|
7 |
|
$this->notificationManager = $notificationManager; |
58
|
7 |
|
} |
59
|
|
|
|
60
|
7 |
|
protected function configure() { |
61
|
|
|
$this |
62
|
7 |
|
->setName('notification:generate') |
63
|
7 |
|
->setDescription('Generate a notification for the given user') |
64
|
7 |
|
->addArgument( |
65
|
7 |
|
'user-id', |
66
|
7 |
|
InputArgument::REQUIRED, |
67
|
7 |
|
'User ID of the user to notify' |
68
|
|
|
) |
69
|
7 |
|
->addArgument( |
70
|
7 |
|
'short-message', |
71
|
7 |
|
InputArgument::REQUIRED, |
72
|
7 |
|
'Short message to be sent to the user (max. 255 characters)' |
73
|
|
|
) |
74
|
7 |
|
->addOption( |
75
|
7 |
|
'long-message', |
76
|
7 |
|
'l', |
77
|
7 |
|
InputOption::VALUE_REQUIRED, |
78
|
7 |
|
'Long mesage to be sent to the user (max. 4000 characters)', |
79
|
7 |
|
'' |
80
|
|
|
) |
81
|
|
|
; |
82
|
7 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param InputInterface $input |
86
|
|
|
* @param OutputInterface $output |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
7 |
|
protected function execute(InputInterface $input, OutputInterface $output): int { |
90
|
|
|
|
91
|
7 |
|
$userId = $input->getArgument('user-id'); |
92
|
7 |
|
$subject = $input->getArgument('short-message'); |
93
|
7 |
|
$message = $input->getOption('long-message'); |
94
|
|
|
|
95
|
7 |
|
$user = $this->userManager->get($userId); |
96
|
7 |
|
if (!$user instanceof IUser) { |
|
|
|
|
97
|
1 |
|
$output->writeln('Unknown user'); |
98
|
1 |
|
return 1; |
99
|
|
|
} |
100
|
|
|
|
101
|
6 |
|
if ($subject === '' || strlen($subject) > 255) { |
102
|
2 |
|
$output->writeln('Too long or empty short-message'); |
103
|
2 |
|
return 1; |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
if ($message !== '' && strlen($message) > 4000) { |
107
|
1 |
|
$output->writeln('Too long long-message'); |
108
|
1 |
|
return 1; |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
$notification = $this->notificationManager->createNotification(); |
112
|
3 |
|
$time = $this->timeFactory->getTime(); |
113
|
3 |
|
$datetime = new \DateTime(); |
114
|
3 |
|
$datetime->setTimestamp($time); |
115
|
|
|
|
116
|
|
|
try { |
117
|
3 |
|
$notification->setApp('admin_notifications') |
118
|
3 |
|
->setUser($user->getUID()) |
119
|
3 |
|
->setDateTime($datetime) |
120
|
3 |
|
->setObject('admin_notifications', dechex($time)) |
121
|
3 |
|
->setSubject('cli', [$subject]); |
122
|
|
|
|
123
|
3 |
|
if ($message !== '') { |
124
|
2 |
|
$notification->setMessage('cli', [$message]); |
125
|
|
|
} |
126
|
|
|
|
127
|
3 |
|
$this->notificationManager->notify($notification); |
128
|
1 |
|
} catch (\InvalidArgumentException $e) { |
129
|
1 |
|
$output->writeln('Error while sending the notification'); |
130
|
1 |
|
return 1; |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
return 0; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.