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\Controller; |
25
|
|
|
|
26
|
|
|
use OCP\AppFramework\Http; |
27
|
|
|
use OCP\AppFramework\Http\DataResponse; |
28
|
|
|
use OCP\AppFramework\OCSController; |
29
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
30
|
|
|
use OCP\IRequest; |
31
|
|
|
use OCP\IUser; |
32
|
|
|
use OCP\IUserManager; |
33
|
|
|
use OCP\Notification\IManager; |
34
|
|
|
|
35
|
|
|
class APIController extends OCSController { |
36
|
|
|
|
37
|
|
|
/** @var ITimeFactory */ |
38
|
|
|
protected $timeFactory; |
39
|
|
|
|
40
|
|
|
/** @var IUserManager */ |
41
|
|
|
protected $userManager; |
42
|
|
|
|
43
|
|
|
/** @var IManager */ |
44
|
|
|
protected $notificationManager; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $appName |
48
|
|
|
* @param IRequest $request |
49
|
|
|
* @param ITimeFactory $timeFactory |
50
|
|
|
* @param IUserManager $userManager |
51
|
|
|
* @param IManager $notificationManager |
52
|
|
|
*/ |
53
|
7 |
View Code Duplication |
public function __construct($appName, IRequest $request, ITimeFactory $timeFactory, IUserManager $userManager, IManager $notificationManager) { |
|
|
|
|
54
|
7 |
|
parent::__construct($appName, $request); |
55
|
|
|
|
56
|
7 |
|
$this->timeFactory = $timeFactory; |
57
|
7 |
|
$this->userManager = $userManager; |
58
|
7 |
|
$this->notificationManager = $notificationManager; |
59
|
7 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $userId |
63
|
|
|
* @param string $shortMessage |
64
|
|
|
* @param string $longMessage |
65
|
|
|
* @return DataResponse |
66
|
|
|
*/ |
67
|
7 |
|
public function generateNotification(string $userId, string $shortMessage, string $longMessage = ''): DataResponse { |
68
|
7 |
|
$user = $this->userManager->get($userId); |
69
|
|
|
|
70
|
7 |
|
if (!$user instanceof IUser) { |
|
|
|
|
71
|
1 |
|
return new DataResponse(null, Http::STATUS_NOT_FOUND); |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
if ($shortMessage === '' || strlen($shortMessage) > 255) { |
75
|
2 |
|
return new DataResponse(null, Http::STATUS_BAD_REQUEST); |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
if ($longMessage !== '' && strlen($longMessage) > 4000) { |
79
|
1 |
|
return new DataResponse(null, Http::STATUS_BAD_REQUEST); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
$notification = $this->notificationManager->createNotification(); |
83
|
3 |
|
$time = $this->timeFactory->getTime(); |
84
|
3 |
|
$datetime = new \DateTime(); |
85
|
3 |
|
$datetime->setTimestamp($time); |
86
|
|
|
|
87
|
|
|
try { |
88
|
3 |
|
$notification->setApp('admin_notifications') |
89
|
3 |
|
->setUser($user->getUID()) |
90
|
3 |
|
->setDateTime($datetime) |
91
|
3 |
|
->setObject('admin_notifications', dechex($time)) |
92
|
3 |
|
->setSubject('ocs', [$shortMessage]); |
93
|
|
|
|
94
|
3 |
|
if ($longMessage !== '') { |
95
|
2 |
|
$notification->setMessage('ocs', [$longMessage]); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
$this->notificationManager->notify($notification); |
99
|
1 |
|
} catch (\InvalidArgumentException $e) { |
100
|
1 |
|
return new DataResponse(null, Http::STATUS_INTERNAL_SERVER_ERROR); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
return new DataResponse(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.