-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathProvider.php
More file actions
280 lines (247 loc) Β· 8.44 KB
/
Provider.php
File metadata and controls
280 lines (247 loc) Β· 8.44 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Forms\Activity;
use Exception;
use OCA\Forms\Db\FormMapper;
use OCA\Forms\Service\CirclesService;
use OCP\Activity\Exceptions\UnknownActivityException;
use OCP\Activity\IEvent;
use OCP\Activity\IEventMerger;
use OCP\Activity\IProvider;
use OCP\AppFramework\Db\IMapperException;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\RichObjectStrings\IValidator;
use Psr\Log\LoggerInterface;
class Provider implements IProvider {
public function __construct(
protected string $appName,
private FormMapper $formMapper,
private IEventMerger $eventMerger,
private IGroupManager $groupManager,
private LoggerInterface $logger,
private IURLGenerator $urlGenerator,
private IUserManager $userManager,
private IFactory $l10nFactory,
private IValidator $validator,
private CirclesService $circlesService,
) {
}
/**
* Beautify the stored Event for Output.
* @param string $language
* @param IEvent $event
* @param IEvent|null $previousEvent
* @return IEvent
* @throws UnknownActivityException
*/
public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
// Throw Exception, if not our activity. Necessary for workflow of Activity-App.
if ($event->getApp() !== $this->appName) {
throw new UnknownActivityException();
}
$l10n = $this->l10nFactory->get($this->appName, $language);
$subjectString = $this->getSubjectString($l10n, $event->getSubject());
$parameters = $this->getRichParams($l10n, $event->getSubject(), $event->getSubjectParameters());
$event->setParsedSubject($this->parseSubjectString($subjectString, $parameters));
$event->setRichSubject($subjectString, $parameters);
$event->setIcon($this->getEventIcon($event->getSubject()));
// For Subject NewShare, merge by users
if ($event->getSubject() === ActivityConstants::SUBJECT_NEWSUBMISSION) {
$event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
}
return $event;
}
/**
* Provide the translated string with placeholders
* @param $subject The events subject
* @return string
* @throws UnknownActivityException
*/
public function getSubjectString(IL10N $l10n, string $subject): string {
switch ($subject) {
case ActivityConstants::SUBJECT_NEWSHARE:
return $l10n->t('{user} has shared the form {formTitle} with you');
case ActivityConstants::SUBJECT_NEWGROUPSHARE:
return $l10n->t('{user} has shared the form {formTitle} with group {group}');
case ActivityConstants::SUBJECT_NEWCIRCLESHARE:
return $l10n->t('{user} has shared the form {formTitle} with team {circle}');
case ActivityConstants::SUBJECT_NEWSUBMISSION:
return $l10n->t('Your form {formTitle} was answered by {user}');
default:
$this->logger->warning('Some unknown activity has been found: ' . $subject);
throw new UnknownActivityException();
}
}
/**
* Simply insert the parameters into the string. Returns a simple human readable string.
* @param string $subjectString The string with placeholders
* @param array $parameters Array of Rich-Parameters as created by getRichParams()
* @return string
*/
public function parseSubjectString(string $subjectString, array $parameters): string {
$placeholders = [];
$replacements = [];
foreach ($parameters as $paramKey => $paramValue) {
$placeholders[] = '{' . $paramKey . '}';
$replacements[] = $paramValue['name'];
}
return str_replace($placeholders, $replacements, $subjectString);
}
/**
* Create the necessary Rich-Params out of the given SubjectParameters as stored on Activity-Db.
* @param string $subject The stored Subject as defined in ActivityConstants
* @param array $params Array of stored SubjectParameters
* @return array
*/
public function getRichParams(IL10N $l10n, string $subject, array $params): array {
switch ($subject) {
case ActivityConstants::SUBJECT_NEWSHARE:
return [
'user' => $this->getRichUser($l10n, $params['userId']),
'formTitle' => $this->getRichFormTitle($params['formTitle'], $params['formHash'])
];
case ActivityConstants::SUBJECT_NEWGROUPSHARE:
return [
'user' => $this->getRichUser($l10n, $params['userId']),
'formTitle' => $this->getRichFormTitle($params['formTitle'], $params['formHash']),
'group' => $this->getRichGroup($params['groupId'])
];
case ActivityConstants::SUBJECT_NEWCIRCLESHARE:
return [
'user' => $this->getRichUser($l10n, $params['userId']),
'formTitle' => $this->getRichFormTitle($params['formTitle'], $params['formHash']),
'circle' => $this->getRichCircle($params['circleId'])
];
case ActivityConstants::SUBJECT_NEWSUBMISSION:
return [
'user' => $this->getRichUser($l10n, $params['userId']),
'formTitle' => $this->getRichFormTitle($params['formTitle'], $params['formHash'], 'results')
];
default:
return [];
}
}
/**
* Turn a userId into an rich-user array.
* @param string $userId
* @return array
*/
public function getRichUser(IL10N $l10n, string $userId): array {
// Special handling for anonyomous users
if (substr($userId, 0, 10) === 'anon-user-') {
return [
'type' => 'highlight',
'id' => $userId,
// TRANSLATORS Shown as a users display-name
'name' => $l10n->t('Anonymous user')
];
}
// Get Displayname, if user still exists. Otherwise just show stored userId
$user = $this->userManager->get($userId);
$displayName = '';
if ($user === null) {
$displayName = $userId;
} else {
$displayName = $user->getDisplayName();
}
return [
'type' => 'user',
'id' => $userId,
'name' => $displayName
];
}
/**
* Turn a groupId into an rich-group array.
* @param string $groupId
* @return array
*/
public function getRichGroup(string $groupId): array {
// Get Displayname, if group still exists. Otherwise just show stored groupId
$group = $this->groupManager->get($groupId);
$displayName = '';
if ($group === null) {
$displayName = $groupId;
} else {
$displayName = $group->getDisplayName();
}
return [
'type' => 'user-group',
'id' => $groupId,
'name' => $displayName
];
}
/**
* Turn a circleId into a rich-circle array.
*
* @param string $groupId
* @return array
*/
public function getRichCircle(string $circleId): array {
$displayName = $circleId;
$link = '';
$circle = $this->circlesService->getCircle($circleId);
if (!is_null($circle)) {
$displayName = $circle->getDisplayName();
$link = $circle->getUrl();
}
return [
'type' => 'circle',
'id' => $circleId,
'name' => $displayName,
'link' => $link,
];
}
/**
* Turn formTitle and formHash into the Rich-Array.
* Link is default to Form-Submission, but can be forwarded to specific route.
* @param string $formTitle Stored Form-Title. Only used as fallback, if form not found
* @param string $formHash Hash of the form
* @param string $route Optional Path of specific route to append to hash-url
* @return array
*/
public function getRichFormTitle(string $formTitle, string $formHash, string $route = ''): array {
// Base-url to Forms app. Fallback in case of MapperException.
$formLink = $this->urlGenerator->linkToRouteAbsolute('forms.page.index');
try {
// Overwrite formTitle if form is found (i.e. still exists).
$formTitle = $this->formMapper->findbyHash($formHash)->getTitle();
// Append hash and route
$formLink .= $formHash;
if ($route !== '') {
$formLink .= '/' . $route;
}
} catch (IMapperException $e) {
// Ignore if not found, just use stored title
}
$richFormTitle = [
'type' => 'forms-form',
'id' => $formHash,
'name' => $formTitle,
'link' => $formLink
];
return $richFormTitle;
}
/**
* Selects the appropriate icon, depending on the subject
* @param string $subject The events subject
* @return string
*/
public function getEventIcon(string $subject): string {
switch ($subject) {
case ActivityConstants::SUBJECT_NEWSHARE:
case ActivityConstants::SUBJECT_NEWGROUPSHARE:
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/shared.svg'));
case ActivityConstants::SUBJECT_NEWSUBMISSION:
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'actions/add.svg'));
default:
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('forms', 'forms-dark.svg'));
}
}
}