-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathNotifier.php
117 lines (103 loc) · 3.12 KB
/
Notifier.php
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
<?php
/**
* Nextcloud - openproject
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Julien Veyssier <eneiluj@posteo.net>
* @copyright Julien Veyssier 2021
*/
namespace OCA\OpenProject\Notification;
use InvalidArgumentException;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCA\OpenProject\AppInfo\Application;
class Notifier implements INotifier {
/** @var IFactory */
protected $factory;
/** @var IUserManager */
protected $userManager;
/** @var INotificationManager */
protected $notificationManager;
/** @var IURLGenerator */
protected $url;
/**
* @param IFactory $factory
* @param IUserManager $userManager
* @param INotificationManager $notificationManager
* @param IURLGenerator $urlGenerator
*/
public function __construct(IFactory $factory,
IUserManager $userManager,
INotificationManager $notificationManager,
IURLGenerator $urlGenerator) {
$this->factory = $factory;
$this->userManager = $userManager;
$this->notificationManager = $notificationManager;
$this->url = $urlGenerator;
}
/**
* Identifier of the notifier, only use [a-z0-9_]
*
* @return string
* @since 17.0.0
*/
public function getID(): string {
return 'integration_openproject';
}
/**
* Human readable name describing the notifier
*
* @return string
* @since 17.0.0
*/
public function getName(): string {
return $this->factory->get('integration_openproject')->t('OpenProject');
}
/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws InvalidArgumentException When the notification was not prepared by a notifier
* @since 9.0.0
*/
public function prepare(INotification $notification, string $languageCode): INotification {
if ($notification->getApp() !== 'integration_openproject') {
// Not my app => throw
throw new InvalidArgumentException();
}
$l = $this->factory->get('integration_openproject', $languageCode);
switch ($notification->getSubject()) {
case 'new_open_tickets':
$p = $notification->getSubjectParameters();
$nbNotifications = (int) ($p['nbNotifications'] ?? 0);
$content = $l->t('OpenProject activity');
$richSubjectInstance = [
'type' => 'file',
'id' => 0,
'name' => $p['link'],
'path' => '',
'link' => $p['link'],
];
$notification->setParsedSubject($content)
->setParsedMessage('--')
->setLink($p['link'] ?? '')
->setRichMessage(
$l->n('You have %s new notification in {instance}', 'You have %s new notifications in {instance}', $nbNotifications, [$nbNotifications]),
[
'instance' => $richSubjectInstance,
]
)
->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app.svg')));
return $notification;
default:
// Unknown subject => Unknown notification => throw
throw new InvalidArgumentException();
}
}
}