1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Data Analytics |
5
|
|
|
* |
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
7
|
|
|
* later. See the LICENSE.md file. |
8
|
|
|
* |
9
|
|
|
* @author Marcel Scherello <[email protected]> |
10
|
|
|
* @copyright 2020 Marcel Scherello |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Analytics\Notification; |
14
|
|
|
|
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use OCP\ILogger; |
17
|
|
|
use OCP\IURLGenerator; |
18
|
|
|
use OCP\IUserManager; |
19
|
|
|
use OCP\L10N\IFactory; |
20
|
|
|
use OCP\Notification\AlreadyProcessedException; |
21
|
|
|
use OCP\Notification\INotification; |
22
|
|
|
use OCP\Notification\INotifier; |
23
|
|
|
|
24
|
|
|
class Notifier implements INotifier |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** @var IFactory */ |
28
|
|
|
protected $l10nFactory; |
29
|
|
|
|
30
|
|
|
/** @var IUserManager */ |
31
|
|
|
protected $userManager; |
32
|
|
|
|
33
|
|
|
/** @var IURLGenerator */ |
34
|
|
|
protected $urlGenerator; |
35
|
|
|
|
36
|
|
|
private $logger; |
37
|
|
|
|
38
|
|
|
public function __construct(IFactory $l10nFactory, |
39
|
|
|
IUserManager $userManager, |
40
|
|
|
ILogger $logger, |
41
|
|
|
IURLGenerator $urlGenerator) |
42
|
|
|
{ |
43
|
|
|
$this->l10nFactory = $l10nFactory; |
44
|
|
|
$this->userManager = $userManager; |
45
|
|
|
$this->logger = $logger; |
46
|
|
|
$this->urlGenerator = $urlGenerator; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Identifier of the notifier, only use [a-z0-9_] |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* @since 17.0.0 |
54
|
|
|
*/ |
55
|
|
|
public function getID(): string |
56
|
|
|
{ |
57
|
|
|
return 'analytics'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Human readable name describing the notifier |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
* @since 17.0.0 |
65
|
|
|
*/ |
66
|
|
|
public function getName(): string |
67
|
|
|
{ |
68
|
|
|
return $this->l10nFactory->get('analytics')->t('Announcements'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param INotification $notification |
73
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification |
74
|
|
|
* @return INotification |
75
|
|
|
* @throws InvalidArgumentException When the notification was not prepared by a notifier |
76
|
|
|
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted |
77
|
|
|
* @since 9.0.0 |
78
|
|
|
*/ |
79
|
|
|
public function prepare(INotification $notification, string $languageCode): INotification |
80
|
|
|
{ |
81
|
|
|
if ($notification->getApp() !== 'analytics') { |
82
|
|
|
// Not my app => throw |
83
|
|
|
throw new InvalidArgumentException('Unknown app'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Read the language from the notification |
87
|
|
|
$l = $this->l10nFactory->get('analytics', $languageCode); |
88
|
|
|
$parsedSubject = ''; |
89
|
|
|
|
90
|
|
|
//$this->logger->error('Notifier 90: '.$notification->getSubject()); |
91
|
|
|
switch ($notification->getSubject()) { |
92
|
|
|
case NotificationManager::SUBJECT_THRESHOLD: |
93
|
|
|
$parsedSubject = $l->t("Exception in Report '{report}'. The value of '{subject}' reached the threshold of '{rule} {value}'"); |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
$link = $this->urlGenerator->linkToRouteAbsolute('analytics.page.index') . '#/r/' . $notification->getObjectId(); |
97
|
|
|
|
98
|
|
|
$parameters = $notification->getSubjectParameters(); |
99
|
|
|
$notification->setRichSubject( |
100
|
|
|
$parsedSubject, |
101
|
|
|
[ |
102
|
|
|
'report' => [ |
103
|
|
|
'type' => 'highlight', |
104
|
|
|
'id' => $notification->getObjectId(), |
105
|
|
|
'name' => $parameters['report'], |
106
|
|
|
'link' => $link, |
107
|
|
|
], |
108
|
|
|
'subject' => [ |
109
|
|
|
'type' => 'highlight', |
110
|
|
|
'id' => $notification->getObjectId(), |
111
|
|
|
'name' => $parameters['subject'], |
112
|
|
|
], |
113
|
|
|
'rule' => [ |
114
|
|
|
'type' => 'highlight', |
115
|
|
|
'id' => $notification->getObjectId(), |
116
|
|
|
'name' => $parameters['rule'], |
117
|
|
|
], |
118
|
|
|
'value' => [ |
119
|
|
|
'type' => 'highlight', |
120
|
|
|
'id' => $notification->getObjectId(), |
121
|
|
|
'name' => $parameters['value'], |
122
|
|
|
] |
123
|
|
|
] |
124
|
|
|
); |
125
|
|
|
$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('analytics', 'app-dark.svg'))); |
126
|
|
|
$this->setParsedSubjectFromRichSubject($notification); |
127
|
|
|
return $notification; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// This is a little helper function which automatically sets the simple parsed subject |
131
|
|
|
// based on the rich subject you set. |
132
|
|
|
protected function setParsedSubjectFromRichSubject(INotification $notification) |
133
|
|
|
{ |
134
|
|
|
$placeholders = $replacements = []; |
135
|
|
|
foreach ($notification->getRichSubjectParameters() as $placeholder => $parameter) { |
136
|
|
|
$placeholders[] = '{' . $placeholder . '}'; |
137
|
|
|
if ($parameter['type'] === 'file') { |
138
|
|
|
$replacements[] = $parameter['path']; |
139
|
|
|
} else { |
140
|
|
|
$replacements[] = $parameter['name']; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$notification->setParsedSubject(str_replace($placeholders, $replacements, $notification->getRichSubject())); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |