1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Björn Schießle <[email protected]> |
4
|
|
|
* @author Joas Schilling <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
7
|
|
|
* @license AGPL-3.0 |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OCA\FederatedFileSharing; |
24
|
|
|
|
25
|
|
|
use OCP\Notification\INotification; |
26
|
|
|
use OCP\Notification\INotifier; |
27
|
|
|
|
28
|
|
|
class Notifier implements INotifier { |
29
|
|
|
/** @var \OCP\L10N\IFactory */ |
30
|
|
|
protected $factory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \OCP\L10N\IFactory $factory |
34
|
|
|
*/ |
35
|
|
|
public function __construct(\OCP\L10N\IFactory $factory) { |
36
|
|
|
$this->factory = $factory; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param INotification $notification |
41
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification |
42
|
|
|
* @return INotification |
43
|
|
|
*/ |
44
|
|
|
public function prepare(INotification $notification, $languageCode) { |
45
|
|
|
if ($notification->getApp() !== 'files_sharing') { |
46
|
|
|
// Not my app => throw |
47
|
|
|
throw new \InvalidArgumentException(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Read the language from the notification |
51
|
|
|
$l = $this->factory->get('files_sharing', $languageCode); |
52
|
|
|
|
53
|
|
|
switch ($notification->getObjectType()) { |
54
|
|
|
// Deal with known subjects |
55
|
|
|
case 'remote_share': |
56
|
|
|
$params = $notification->getSubjectParameters(); |
57
|
|
View Code Duplication |
if ($params[0] !== $params[1] && $params[1] !== null) { |
|
|
|
|
58
|
|
|
$notification->setParsedSubject( |
59
|
|
|
(string) $l->t('"%1$s" shared "%3$s" with you (on behalf of "%2$s")', $params) |
60
|
|
|
); |
61
|
|
|
} else { |
62
|
|
|
$notification->setParsedSubject( |
63
|
|
|
(string) $l->t('"%1$s" shared "%3$s" with you', $params) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$messageParams = $notification->getMessageParameters(); |
68
|
|
View Code Duplication |
if ($messageParams[0] !== $messageParams[1] && $messageParams[1] !== null) { |
|
|
|
|
69
|
|
|
$notification->setParsedMessage( |
70
|
|
|
(string) $l->t('"%1$s" invited you to view "%3$s" (on behalf of "%2$s")', $messageParams) |
71
|
|
|
); |
72
|
|
|
} else { |
73
|
|
|
$notification->setParsedMessage( |
74
|
|
|
(string) $l->t('"%1$s" invited you to view "%3$s"', $messageParams) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Deal with the actions for a known subject |
79
|
|
View Code Duplication |
foreach ($notification->getActions() as $action) { |
|
|
|
|
80
|
|
|
switch ($action->getLabel()) { |
81
|
|
|
case 'accept': |
82
|
|
|
$action->setParsedLabel( |
83
|
|
|
(string) $l->t('Accept') |
84
|
|
|
) |
85
|
|
|
->setPrimary(true); |
86
|
|
|
break; |
87
|
|
|
|
88
|
|
|
case 'decline': |
89
|
|
|
$action->setParsedLabel( |
90
|
|
|
(string) $l->t('Decline') |
91
|
|
|
); |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$notification->addParsedAction($action); |
96
|
|
|
} |
97
|
|
|
return $notification; |
98
|
|
|
|
99
|
|
|
default: |
100
|
|
|
// Unknown subject => Unknown notification => throw |
101
|
|
|
throw new \InvalidArgumentException(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.