Passed
Push — master ( 59402b...899021 )
by John
11:15 queued 10s
created

Notifier::dismissNotification()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 25
rs 9.7
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
7
 *
8
 * @author Joas Schilling <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Files\Notification;
29
30
use OCA\Files\Db\TransferOwnershipMapper;
31
use OCP\AppFramework\Db\DoesNotExistException;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
use OCP\AppFramework\Utility\ITimeFactory;
35
use OCP\IURLGenerator;
36
use OCP\L10N\IFactory;
37
use OCP\Notification\IAction;
38
use OCP\Notification\IDismissableNotifier;
39
use OCP\Notification\IManager;
40
use OCP\Notification\INotification;
41
use OCP\Notification\INotifier;
42
43
class Notifier implements INotifier, IDismissableNotifier {
44
45
	/** @var IFactory */
46
	protected $l10nFactory;
47
48
	/** @var IURLGenerator */
49
	protected $urlGenerator;
50
	/** @var TransferOwnershipMapper */
51
	private $mapper;
52
	/** @var IManager */
53
	private $notificationManager;
54
	/** @var ITimeFactory */
55
	private $timeFactory;
56
57
	/**
58
	 * @param IFactory $l10nFactory
59
	 * @param IURLGenerator $urlGenerator
60
	 */
61
	public function __construct(IFactory $l10nFactory,
62
								IURLGenerator $urlGenerator,
63
								TransferOwnershipMapper $mapper,
64
								IManager $notificationManager,
65
								ITimeFactory $timeFactory) {
66
		$this->l10nFactory = $l10nFactory;
67
		$this->urlGenerator = $urlGenerator;
68
		$this->mapper = $mapper;
69
		$this->notificationManager = $notificationManager;
70
		$this->timeFactory = $timeFactory;
71
	}
72
73
	public function getID(): string {
74
		return 'files';
75
	}
76
77
	public function getName(): string {
78
		return $this->l10nFactory->get('files')->t('Files');
79
	}
80
81
	/**
82
	 * @param INotification $notification
83
	 * @param string $languageCode The code of the language that should be used to prepare the notification
84
	 * @return INotification
85
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
86
	 */
87
	public function prepare(INotification $notification, string $languageCode): INotification {
88
		if ($notification->getApp() !== 'files') {
89
			throw new \InvalidArgumentException('Unhandled app');
90
		}
91
92
		if ($notification->getSubject() === 'transferownershipRequest') {
93
			return $this->handleTransferownershipRequest($notification, $languageCode);
94
		}
95
		if ($notification->getSubject() === 'transferOwnershipFailedSource') {
96
			return $this->handleTransferOwnershipFailedSource($notification, $languageCode);
97
		}
98
		if ($notification->getSubject() === 'transferOwnershipFailedTarget') {
99
			return $this->handleTransferOwnershipFailedTarget($notification, $languageCode);
100
		}
101
		if ($notification->getSubject() === 'transferOwnershipDoneSource') {
102
			return $this->handleTransferOwnershipDoneSource($notification, $languageCode);
103
		}
104
		if ($notification->getSubject() === 'transferOwnershipDoneTarget') {
105
			return $this->handleTransferOwnershipDoneTarget($notification, $languageCode);
106
		}
107
108
		throw new \InvalidArgumentException('Unhandled subject');
109
	}
110
111
	public function handleTransferownershipRequest(INotification $notification, string $languageCode): INotification {
112
		$l = $this->l10nFactory->get('files', $languageCode);
113
		$id = $notification->getObjectId();
114
		$param = $notification->getSubjectParameters();
115
116
		$approveAction = $notification->createAction()
117
			->setParsedLabel($l->t('Accept'))
118
			->setPrimary(true)
119
			->setLink(
120
				$this->urlGenerator->getAbsoluteURL(
121
					$this->urlGenerator->linkTo(
122
						'',
123
						'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
124
					)
125
				),
126
				IAction::TYPE_POST
127
			);
128
129
		$disapproveAction = $notification->createAction()
130
			->setParsedLabel($l->t('Reject'))
131
			->setPrimary(false)
132
			->setLink(
133
				$this->urlGenerator->getAbsoluteURL(
134
					$this->urlGenerator->linkTo(
135
						'',
136
						'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
137
					)
138
				),
139
				IAction::TYPE_DELETE
140
			);
141
142
		$notification->addParsedAction($approveAction)
143
			->addParsedAction($disapproveAction)
144
			->setRichSubject(
145
				$l->t('Incoming ownership transfer from {user}'),
146
				[
147
					'user' => [
148
						'type' => 'user',
149
						'id' => $param['sourceUser'],
150
						'name' => $param['sourceUser'],
151
					],
152
				])
153
			->setParsedSubject(str_replace('{user}', $param['sourceUser'], $l->t('Incoming ownership transfer from {user}')))
154
			->setRichMessage(
155
				$l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."),
156
				[
157
					'path' => [
158
						'type' => 'highlight',
159
						'id' => $param['targetUser'] . '::' . $param['nodeName'],
160
						'name' => $param['nodeName'],
161
					]
162
				])
163
			->setParsedMessage(str_replace('{path}', $param['nodeName'], $l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour.")));
164
165
		return $notification;
166
	}
167
168
	public function handleTransferOwnershipFailedSource(INotification $notification,  string $languageCode): INotification {
169
		$l = $this->l10nFactory->get('files', $languageCode);
170
		$param = $notification->getSubjectParameters();
171
172
		$notification->setRichSubject($l->t('Ownership transfer failed'))
173
			->setParsedSubject($l->t('Ownership transfer failed'))
174
175
			->setRichMessage(
176
				$l->t('Your ownership transfer of {path} to {user} failed.'),
177
				[
178
					'path' => [
179
						'type' => 'highlight',
180
						'id' => $param['targetUser'] . '::' . $param['nodeName'],
181
						'name' => $param['nodeName'],
182
					],
183
					'user' => [
184
						'type' => 'user',
185
						'id' => $param['targetUser'],
186
						'name' => $param['targetUser'],
187
					],
188
				])
189
			->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['targetUser']], $l->t('Your ownership transfer of {path} to {user} failed.')));
190
		return $notification;
191
	}
192
193
	public function handleTransferOwnershipFailedTarget(INotification $notification,  string $languageCode): INotification {
194
		$l = $this->l10nFactory->get('files', $languageCode);
195
		$param = $notification->getSubjectParameters();
196
197
		$notification->setRichSubject($l->t('Ownership transfer failed'))
198
			->setParsedSubject($l->t('Ownership transfer failed'))
199
200
			->setRichMessage(
201
				$l->t('The ownership transfer of {path} from {user} failed.'),
202
				[
203
					'path' => [
204
						'type' => 'highlight',
205
						'id' => $param['sourceUser'] . '::' . $param['nodeName'],
206
						'name' => $param['nodeName'],
207
					],
208
					'user' => [
209
						'type' => 'user',
210
						'id' => $param['sourceUser'],
211
						'name' => $param['sourceUser'],
212
					],
213
				])
214
			->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['sourceUser']], $l->t('The ownership transfer of {path} from {user} failed.')));
215
216
		return $notification;
217
	}
218
219
	public function handleTransferOwnershipDoneSource(INotification $notification,  string $languageCode): INotification {
220
		$l = $this->l10nFactory->get('files', $languageCode);
221
		$param = $notification->getSubjectParameters();
222
223
		$notification->setRichSubject($l->t('Ownership transfer done'))
224
			->setParsedSubject($l->t('Ownership transfer done'))
225
226
			->setRichMessage(
227
				$l->t('Your ownership transfer of {path} to {user} has completed.'),
228
				[
229
					'path' => [
230
						'type' => 'highlight',
231
						'id' => $param['targetUser'] . '::' . $param['nodeName'],
232
						'name' => $param['nodeName'],
233
					],
234
					'user' => [
235
						'type' => 'user',
236
						'id' => $param['targetUser'],
237
						'name' => $param['targetUser'],
238
					],
239
				])
240
			->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['targetUser']], $l->t('Your ownership transfer of {path} to {user} has completed.')));
241
242
		return $notification;
243
	}
244
245
	public function handleTransferOwnershipDoneTarget(INotification $notification,  string $languageCode): INotification {
246
		$l = $this->l10nFactory->get('files', $languageCode);
247
		$param = $notification->getSubjectParameters();
248
249
		$notification->setRichSubject($l->t('Ownership transfer done'))
250
			->setParsedSubject($l->t('Ownership transfer done'))
251
252
			->setRichMessage(
253
				$l->t('The ownership transfer of {path} from {user} has completed.'),
254
				[
255
					'path' => [
256
						'type' => 'highlight',
257
						'id' => $param['sourceUser'] . '::' . $param['nodeName'],
258
						'name' => $param['nodeName'],
259
					],
260
					'user' => [
261
						'type' => 'user',
262
						'id' => $param['sourceUser'],
263
						'name' => $param['sourceUser'],
264
					],
265
				])
266
			->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['sourceUser']], $l->t('The ownership transfer of {path} from {user} has completed.')));
267
268
		return $notification;
269
	}
270
271
	public function dismissNotification(INotification $notification): void {
272
		if ($notification->getApp() !== 'files') {
273
			throw new \InvalidArgumentException('Unhandled app');
274
		}
275
276
		// TODO: This should all be moved to a service that also the transferownershipContoller uses.
277
		try {
278
			$transferOwnership = $this->mapper->getById((int)$notification->getObjectId());
279
		} catch (DoesNotExistException $e) {
280
			return;
281
		}
282
283
		$notification = $this->notificationManager->createNotification();
284
		$notification->setUser($transferOwnership->getSourceUser())
285
			->setApp('files')
286
			->setDateTime($this->timeFactory->getDateTime())
287
			->setSubject('transferownershipRequestDenied', [
288
				'sourceUser' => $transferOwnership->getSourceUser(),
289
				'targetUser' => $transferOwnership->getTargetUser(),
290
				'nodeName' => $transferOwnership->getNodeName()
291
			])
292
			->setObject('transfer', (string)$transferOwnership->getId());
293
		$this->notificationManager->notify($notification);
294
295
		$this->mapper->delete($transferOwnership);
296
	}
297
}
298