|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Christoph Wurst <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Christoph Wurst <[email protected]> |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace OCA\Settings\Activity; |
|
25
|
|
|
|
|
26
|
|
|
use InvalidArgumentException; |
|
27
|
|
|
use OCP\Activity\IEvent; |
|
28
|
|
|
use OCP\Activity\IManager; |
|
29
|
|
|
use OCP\Activity\IProvider; |
|
30
|
|
|
use OCP\IGroup; |
|
31
|
|
|
use OCP\IGroupManager; |
|
32
|
|
|
use OCP\IURLGenerator; |
|
33
|
|
|
use OCP\IUser; |
|
34
|
|
|
use OCP\IUserManager; |
|
35
|
|
|
use OCP\L10N\IFactory as L10nFactory; |
|
36
|
|
|
|
|
37
|
|
|
class GroupProvider implements IProvider { |
|
38
|
|
|
public const ADDED_TO_GROUP = 'group_added'; |
|
39
|
|
|
public const REMOVED_FROM_GROUP = 'group_removed'; |
|
40
|
|
|
|
|
41
|
|
|
/** @var L10nFactory */ |
|
42
|
|
|
private $l10n; |
|
43
|
|
|
/** @var IURLGenerator */ |
|
44
|
|
|
private $urlGenerator; |
|
45
|
|
|
/** @var IManager */ |
|
46
|
|
|
private $activityManager; |
|
47
|
|
|
/** @var IUserManager */ |
|
48
|
|
|
protected $userManager; |
|
49
|
|
|
/** @var IGroupManager */ |
|
50
|
|
|
protected $groupManager; |
|
51
|
|
|
|
|
52
|
|
|
/** @var string[] */ |
|
53
|
|
|
protected $groupDisplayNames = []; |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
public function __construct(L10nFactory $l10n, |
|
57
|
|
|
IURLGenerator $urlGenerator, |
|
58
|
|
|
IManager $activityManager, |
|
59
|
|
|
IUserManager $userManager, |
|
60
|
|
|
IGroupManager $groupManager) { |
|
61
|
|
|
$this->urlGenerator = $urlGenerator; |
|
62
|
|
|
$this->l10n = $l10n; |
|
63
|
|
|
$this->activityManager = $activityManager; |
|
64
|
|
|
$this->userManager = $userManager; |
|
65
|
|
|
$this->groupManager = $groupManager; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
69
|
|
|
if ($event->getType() !== 'group_settings') { |
|
70
|
|
|
throw new InvalidArgumentException(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$l = $this->l10n->get('settings', $language); |
|
74
|
|
|
|
|
75
|
|
|
$params = $event->getSubjectParameters(); |
|
76
|
|
|
$parsedParameters = [ |
|
77
|
|
|
'user' => $this->generateUserParameter($params['user']), |
|
78
|
|
|
'group' => $this->generateGroupParameter($params['group']), |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
if (isset($params['actor'])) { |
|
82
|
|
|
$parsedParameters['actor'] = $this->generateUserParameter($params['actor']); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
switch ($event->getSubject()) { |
|
86
|
|
|
case self::ADDED_TO_GROUP: |
|
87
|
|
|
if (isset($parsedParameters['actor'])) { |
|
88
|
|
|
if ($this->activityManager->getCurrentUserId() === $params['user']) { |
|
89
|
|
|
$subject = $l->t('{actor} added you to group {group}'); |
|
90
|
|
|
} elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) { |
|
91
|
|
|
$subject = $l->t('You added {user} to group {group}'); |
|
92
|
|
|
} else { |
|
93
|
|
|
$subject = $l->t('{actor} added {user} to group {group}'); |
|
94
|
|
|
} |
|
95
|
|
|
} elseif ($this->activityManager->getCurrentUserId() === $params['user']) { |
|
96
|
|
|
$subject = $l->t('An administrator added you to group {group}'); |
|
97
|
|
|
} else { |
|
98
|
|
|
$subject = $l->t('An administrator added {user} to group {group}'); |
|
99
|
|
|
} |
|
100
|
|
|
break; |
|
101
|
|
|
case self::REMOVED_FROM_GROUP: |
|
102
|
|
|
if (isset($parsedParameters['actor'])) { |
|
103
|
|
|
if ($this->activityManager->getCurrentUserId() === $params['user']) { |
|
104
|
|
|
$subject = $l->t('{actor} removed you from group {group}'); |
|
105
|
|
|
} elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) { |
|
106
|
|
|
$subject = $l->t('You removed {user} from group {group}'); |
|
107
|
|
|
} else { |
|
108
|
|
|
$subject = $l->t('{actor} removed {user} from group {group}'); |
|
109
|
|
|
} |
|
110
|
|
|
} elseif ($this->activityManager->getCurrentUserId() === $params['user']) { |
|
111
|
|
|
$subject = $l->t('An administrator removed you from group {group}'); |
|
112
|
|
|
} else { |
|
113
|
|
|
$subject = $l->t('An administrator removed {user} from group {group}'); |
|
114
|
|
|
} |
|
115
|
|
|
break; |
|
116
|
|
|
default: |
|
117
|
|
|
throw new InvalidArgumentException(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->setSubjects($event, $subject, $parsedParameters); |
|
121
|
|
|
|
|
122
|
|
|
return $event; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param IEvent $event |
|
127
|
|
|
* @param string $subject |
|
128
|
|
|
* @param array $parameters |
|
129
|
|
|
* @throws \InvalidArgumentException |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function setSubjects(IEvent $event, string $subject, array $parameters): void { |
|
132
|
|
|
$placeholders = $replacements = []; |
|
133
|
|
|
foreach ($parameters as $placeholder => $parameter) { |
|
134
|
|
|
$placeholders[] = '{' . $placeholder . '}'; |
|
135
|
|
|
$replacements[] = $parameter['name']; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
|
139
|
|
|
->setRichSubject($subject, $parameters); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param string $gid |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function generateGroupParameter(string $gid): array { |
|
147
|
|
|
if (!isset($this->groupDisplayNames[$gid])) { |
|
148
|
|
|
$this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return [ |
|
152
|
|
|
'type' => 'user-group', |
|
153
|
|
|
'id' => $gid, |
|
154
|
|
|
'name' => $this->groupDisplayNames[$gid], |
|
155
|
|
|
]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $gid |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function getGroupDisplayName(string $gid): string { |
|
163
|
|
|
$group = $this->groupManager->get($gid); |
|
164
|
|
|
if ($group instanceof IGroup) { |
|
165
|
|
|
return $group->getDisplayName(); |
|
166
|
|
|
} |
|
167
|
|
|
return $gid; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function generateUserParameter(string $uid): array { |
|
171
|
|
|
return [ |
|
172
|
|
|
'type' => 'user', |
|
173
|
|
|
'id' => $uid, |
|
174
|
|
|
'name' => $this->userManager->getDisplayName($uid) ?? $uid, |
|
175
|
|
|
]; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|