Completed
Push — master ( ecc14f...aee800 )
by Joas
03:05
created

FilesHooks::shareByLink()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 2
nop 3
crap 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Frank Karlitschek <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
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, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Activity;
26
27
use OC\Files\Filesystem;
28
use OC\Files\View;
29
use OCA\Activity\Extension\Files;
30
use OCA\Activity\Extension\Files_Sharing;
31
use OCP\Activity\IManager;
32
use OCP\Files\Mount\IMountPoint;
33
use OCP\Files\NotFoundException;
34
use OCP\IDBConnection;
35
use OCP\IGroup;
36
use OCP\IGroupManager;
37
use OCP\ILogger;
38
use OCP\IURLGenerator;
39
use OCP\IUser;
40
use OCP\Share;
41
use OCP\Share\IShare;
42
43
/**
44
 * The class to handle the filesystem hooks
45
 */
46
class FilesHooks {
47
	const USER_BATCH_SIZE = 50;
48
49
	/** @var \OCP\Activity\IManager */
50
	protected $manager;
51
52
	/** @var \OCA\Activity\Data */
53
	protected $activityData;
54
55
	/** @var \OCA\Activity\UserSettings */
56
	protected $userSettings;
57
58
	/** @var \OCP\IGroupManager */
59
	protected $groupManager;
60
61
	/** @var \OCP\IDBConnection */
62
	protected $connection;
63
64
	/** @var \OC\Files\View */
65
	protected $view;
66
67
	/** @var IURLGenerator */
68
	protected $urlGenerator;
69
70
	/** @var ILogger */
71
	protected $logger;
72
73
	/** @var CurrentUser */
74
	protected $currentUser;
75
76
	/** @var string|bool */
77
	protected $moveCase = false;
78
	/** @var string[] */
79
	protected $oldParentUsers;
80
	/** @var string */
81
	protected $oldParentPath;
82
	/** @var string */
83
	protected $oldParentOwner;
84
	/** @var string */
85
	protected $oldParentId;
86
87
	/**
88
	 * Constructor
89
	 *
90
	 * @param IManager $manager
91
	 * @param Data $activityData
92
	 * @param UserSettings $userSettings
93
	 * @param IGroupManager $groupManager
94
	 * @param View $view
95
	 * @param IDBConnection $connection
96
	 * @param IURLGenerator $urlGenerator
97
	 * @param ILogger $logger
98
	 * @param CurrentUser $currentUser
99
	 */
100 47
	public function __construct(IManager $manager, Data $activityData, UserSettings $userSettings, IGroupManager $groupManager, View $view, IDBConnection $connection, IURLGenerator $urlGenerator, ILogger $logger, CurrentUser $currentUser) {
101 47
		$this->manager = $manager;
102 47
		$this->activityData = $activityData;
103 47
		$this->userSettings = $userSettings;
104 47
		$this->groupManager = $groupManager;
105 47
		$this->view = $view;
106 47
		$this->connection = $connection;
107 47
		$this->urlGenerator = $urlGenerator;
108 47
		$this->logger = $logger;
109 47
		$this->currentUser = $currentUser;
110 47
	}
111
112
	/**
113
	 * Store the create hook events
114
	 * @param string $path Path of the file that has been created
115
	 */
116 2
	public function fileCreate($path) {
117 2
		if ($this->currentUser->getUserIdentifier() !== '') {
118 1
			$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, 'created_self', 'created_by');
119
		} else {
120 1
			$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, '', 'created_public');
121
		}
122 2
	}
123
124
	/**
125
	 * Store the update hook events
126
	 * @param string $path Path of the file that has been modified
127
	 */
128 1
	public function fileUpdate($path) {
129 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CHANGED, 'changed_self', 'changed_by');
130 1
	}
131
132
	/**
133
	 * Store the delete hook events
134
	 * @param string $path Path of the file that has been deleted
135
	 */
136 1
	public function fileDelete($path) {
137 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_DELETED, 'deleted_self', 'deleted_by');
138 1
	}
139
140
	/**
141
	 * Store the restore hook events
142
	 * @param string $path Path of the file that has been restored
143
	 */
144 1
	public function fileRestore($path) {
145 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_RESTORED, 'restored_self', 'restored_by');
146 1
	}
147
148
	/**
149
	 * Creates the entries for file actions on $file_path
150
	 *
151
	 * @param string $filePath         The file that is being changed
152
	 * @param int    $activityType     The activity type
153
	 * @param string $subject          The subject for the actor
154
	 * @param string $subjectBy        The subject for other users (with "by $actor")
155
	 */
156 3
	protected function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) {
157
		// Do not add activities for .part-files
158 3
		if (substr($filePath, -5) === '.part') {
159 1
			return;
160
		}
161
162 2
		list($filePath, $uidOwner, $fileId) = $this->getSourcePathAndOwner($filePath);
163 2
		if ($fileId === 0) {
164
			// Could not find the file for the owner ...
165
			return;
166
		}
167
168 2
		$affectedUsers = $this->getUserPathsFromPath($filePath, $uidOwner);
169 2
		$filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType);
170 2
		$filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType);
171
172 2
		foreach ($affectedUsers as $user => $path) {
173 2
			$user = (string) $user;
174 2
			if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
175 2
				continue;
176
			}
177
178 2
			if ($user === $this->currentUser->getUID()) {
179 1
				$userSubject = $subject;
180 1
				$userParams = [[$fileId => $path]];
181
			} else {
182 1
				$userSubject = $subjectBy;
183 1
				$userParams = [[$fileId => $path], $this->currentUser->getUserIdentifier()];
184
			}
185
186 2
			$this->addNotificationsForUser(
187
				$user, $userSubject, $userParams,
188 2
				$fileId, $path, true,
189 2
				!empty($filteredStreamUsers[$user]),
190 2
				!empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0,
191
				$activityType
192
			);
193
		}
194 2
	}
195
196
	/**
197
	 * Collect some information for move/renames
198
	 *
199
	 * @param string $oldPath Path of the file that has been moved
200
	 * @param string $newPath Path of the file that has been moved
201
	 */
202
	public function fileMove($oldPath, $newPath) {
203
		if (substr($oldPath, -5) === '.part' || substr($newPath, -5) === '.part') {
204
			// Do not add activities for .part-files
205
			$this->moveCase = false;
206
			return;
207
		}
208
209
		$oldDir = dirname($oldPath);
210
		$newDir = dirname($newPath);
211
212
		if ($oldDir === $newDir) {
213
			/**
214
			 * a/b moved to a/c
215
			 *
216
			 * Cases:
217
			 * - a/b shared: no visible change
218
			 * - a/ shared: rename
219
			 */
220
			$this->moveCase = 'rename';
221
			return;
222
		}
223
224
		if (strpos($oldDir, $newDir) === 0) {
225
			/**
226
			 * a/b/c moved to a/c
227
			 *
228
			 * Cases:
229
			 * - a/b/c shared: no visible change
230
			 * - a/b/ shared: delete
231
			 * - a/ shared: move/rename
232
			 */
233
			$this->moveCase = 'moveUp';
234
		} else if (strpos($newDir, $oldDir) === 0) {
235
			/**
236
			 * a/b moved to a/c/b
237
			 *
238
			 * Cases:
239
			 * - a/b shared: no visible change
240
			 * - a/c/ shared: add
241
			 * - a/ shared: move/rename
242
			 */
243
			$this->moveCase = 'moveDown';
244
		} else {
245
			/**
246
			 * a/b/c moved to a/d/c
247
			 *
248
			 * Cases:
249
			 * - a/b/c shared: no visible change
250
			 * - a/b/ shared: delete
251
			 * - a/d/ shared: add
252
			 * - a/ shared: move/rename
253
			 */
254
			$this->moveCase = 'moveCross';
255
		}
256
257
		list($this->oldParentPath, $this->oldParentOwner, $this->oldParentId) = $this->getSourcePathAndOwner($oldDir);
258
		if ($this->oldParentId === 0) {
259
			// Could not find the file for the owner ...
260
			$this->moveCase = false;
261
			return;
262
		}
263
		$this->oldParentUsers = $this->getUserPathsFromPath($this->oldParentPath, $this->oldParentOwner);
264
	}
265
266
267
	/**
268
	 * Store the move hook events
269
	 *
270
	 * @param string $oldPath Path of the file that has been moved
271
	 * @param string $newPath Path of the file that has been moved
272
	 */
273
	public function fileMovePost($oldPath, $newPath) {
274
		// Do not add activities for .part-files
275
		if ($this->moveCase === false) {
276
			return;
277
		}
278
279
		switch ($this->moveCase) {
280
			case 'rename':
281
				$this->fileRenaming($oldPath, $newPath);
282
				break;
283
			case 'moveUp':
284
			case 'moveDown':
285
			case 'moveCross':
286
				$this->fileMoving($oldPath, $newPath);
287
				break;
288
		}
289
290
		$this->moveCase = false;
291
	}
292
293
294
	/**
295
	 * Renaming a file inside the same folder (a/b to a/c)
296
	 *
297
	 * @param string $oldPath
298
	 * @param string $newPath
299
	 */
300
	protected function fileRenaming($oldPath, $newPath) {
301
		$dirName = dirname($newPath);
302
		$fileName = basename($newPath);
303
		$oldFileName = basename($oldPath);
304
305
		list(, , $fileId) = $this->getSourcePathAndOwner($newPath);
306
		list($parentPath, $parentOwner, $parentId) = $this->getSourcePathAndOwner($dirName);
307
		if ($fileId === 0 || $parentId === 0) {
308
			// Could not find the file for the owner ...
309
			return;
310
		}
311
		$affectedUsers = $this->getUserPathsFromPath($parentPath, $parentOwner);
312
313
		$filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', Files::TYPE_SHARE_CHANGED);
314
		$filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', Files::TYPE_SHARE_CHANGED);
315
316
		foreach ($affectedUsers as $user => $path) {
317
			if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
318
				continue;
319
			}
320
321
			if ($user === $this->currentUser->getUID()) {
322
				$userSubject = 'renamed_self';
323
				$userParams = [
324
					[$fileId => $path . '/' . $fileName],
325
					[$fileId => $path . '/' . $oldFileName],
326
				];
327
			} else {
328
				$userSubject = 'renamed_by';
329
				$userParams = [
330
					[$fileId => $path . '/' . $fileName],
331
					$this->currentUser->getUserIdentifier(),
332
					[$fileId => $path . '/' . $oldFileName],
333
				];
334
			}
335
336
			$this->addNotificationsForUser(
337
				$user, $userSubject, $userParams,
338
				$fileId, $path . '/' . $fileName, true,
339
				!empty($filteredStreamUsers[$user]),
340
				!empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0,
341
				Files::TYPE_SHARE_CHANGED
342
			);
343
		}
344
	}
345
346
	/**
347
	 * Moving a file from one folder to another
348
	 *
349
	 * @param string $oldPath
350
	 * @param string $newPath
351
	 */
352
	protected function fileMoving($oldPath, $newPath) {
353
		$dirName = dirname($newPath);
354
		$fileName = basename($newPath);
355
		$oldFileName = basename($oldPath);
356
357
		list(, , $fileId) = $this->getSourcePathAndOwner($newPath);
358
		list($parentPath, $parentOwner, $parentId) = $this->getSourcePathAndOwner($dirName);
359
		if ($fileId === 0 || $parentId === 0) {
360
			// Could not find the file for the owner ...
361
			return;
362
		}
363
		$affectedUsers = $this->getUserPathsFromPath($parentPath, $parentOwner);
364
365
		$beforeUsers = array_keys($this->oldParentUsers);
366
		$afterUsers = array_keys($affectedUsers);
367
368
		$deleteUsers = array_diff($beforeUsers, $afterUsers);
369
		$this->generateDeleteActivities($deleteUsers, $this->oldParentUsers, $fileId, $oldFileName);
370
371
		$addUsers = array_diff($afterUsers, $beforeUsers);
372
		$this->generateAddActivities($addUsers, $affectedUsers, $fileId, $fileName);
373
374
		$moveUsers = array_intersect($beforeUsers, $afterUsers);
375
		$this->generateMoveActivities($moveUsers, $this->oldParentUsers, $affectedUsers, $fileId, $oldFileName, $parentId, $fileName);
376
	}
377
378
	/**
379
	 * @param string[] $users
380
	 * @param string[] $pathMap
381
	 * @param int $fileId
382
	 * @param string $oldFileName
383
	 */
384 View Code Duplication
	protected function generateDeleteActivities($users, $pathMap, $fileId, $oldFileName) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
385
		if (empty($users)) {
386
			return;
387
		}
388
389
		$filteredStreamUsers = $this->userSettings->filterUsersBySetting($users, 'stream', Files::TYPE_SHARE_DELETED);
390
		$filteredEmailUsers = $this->userSettings->filterUsersBySetting($users, 'email', Files::TYPE_SHARE_DELETED);
391
392
		foreach ($users as $user) {
393
			if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
394
				continue;
395
			}
396
397
			$path = $pathMap[$user];
398
399
			if ($user === $this->currentUser->getUID()) {
400
				$userSubject = 'deleted_self';
401
				$userParams = [[$fileId => $path . '/' . $oldFileName]];
402
			} else {
403
				$userSubject = 'deleted_by';
404
				$userParams = [[$fileId => $path . '/' . $oldFileName], $this->currentUser->getUserIdentifier()];
405
			}
406
407
			$this->addNotificationsForUser(
408
				$user, $userSubject, $userParams,
409
				$fileId, $path . '/' . $oldFileName, true,
410
				!empty($filteredStreamUsers[$user]),
411
				!empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0,
412
				Files::TYPE_SHARE_DELETED
413
			);
414
		}
415
	}
416
417
	/**
418
	 * @param string[] $users
419
	 * @param string[] $pathMap
420
	 * @param int $fileId
421
	 * @param string $fileName
422
	 */
423 View Code Duplication
	protected function generateAddActivities($users, $pathMap, $fileId, $fileName) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
424
		if (empty($users)) {
425
			return;
426
		}
427
428
		$filteredStreamUsers = $this->userSettings->filterUsersBySetting($users, 'stream', Files::TYPE_SHARE_CREATED);
429
		$filteredEmailUsers = $this->userSettings->filterUsersBySetting($users, 'email', Files::TYPE_SHARE_CREATED);
430
431
		foreach ($users as $user) {
432
			if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
433
				continue;
434
			}
435
436
			$path = $pathMap[$user];
437
438
			if ($user === $this->currentUser->getUID()) {
439
				$userSubject = 'created_self';
440
				$userParams = [[$fileId => $path . '/' . $fileName]];
441
			} else {
442
				$userSubject = 'created_by';
443
				$userParams = [[$fileId => $path . '/' . $fileName], $this->currentUser->getUserIdentifier()];
444
			}
445
446
			$this->addNotificationsForUser(
447
				$user, $userSubject, $userParams,
448
				$fileId, $path . '/' . $fileName, true,
449
				!empty($filteredStreamUsers[$user]),
450
				!empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0,
451
				Files::TYPE_SHARE_CREATED
452
			);
453
		}
454
	}
455
456
	/**
457
	 * @param string[] $users
458
	 * @param string[] $beforePathMap
459
	 * @param string[] $afterPathMap
460
	 * @param int $fileId
461
	 * @param string $oldFileName
462
	 * @param int $newParentId
463
	 * @param string $fileName
464
	 */
465
	protected function generateMoveActivities($users, $beforePathMap, $afterPathMap, $fileId, $oldFileName, $newParentId, $fileName) {
466
		if (empty($users)) {
467
			return;
468
		}
469
470
		$filteredStreamUsers = $this->userSettings->filterUsersBySetting($users, 'stream', Files::TYPE_SHARE_CHANGED);
471
		$filteredEmailUsers = $this->userSettings->filterUsersBySetting($users, 'email', Files::TYPE_SHARE_CHANGED);
472
473
		foreach ($users as $user) {
474
			if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
475
				continue;
476
			}
477
478
			if ($oldFileName === $fileName) {
479
				$userParams = [[$newParentId => $afterPathMap[$user] . '/']];
480
			} else {
481
				$userParams = [[$fileId => $afterPathMap[$user] . '/' . $fileName]];
482
			}
483
484
			if ($user === $this->currentUser->getUID()) {
485
				$userSubject = 'moved_self';
486
			} else {
487
				$userSubject = 'moved_by';
488
				$userParams[] = $this->currentUser->getUserIdentifier();
489
			}
490
			$userParams[] = [$fileId => $beforePathMap[$user] . '/' . $oldFileName];
491
492
			$this->addNotificationsForUser(
493
				$user, $userSubject, $userParams,
494
				$fileId, $afterPathMap[$user] . '/' . $fileName, true,
495
				!empty($filteredStreamUsers[$user]),
496
				!empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0,
497
				Files::TYPE_SHARE_CHANGED
498
			);
499
		}
500
	}
501
502
	/**
503
	 * Returns a "username => path" map for all affected users
504
	 *
505
	 * @param string $path
506
	 * @param string $uidOwner
507
	 * @return array
508
	 */
509
	protected function getUserPathsFromPath($path, $uidOwner) {
510
		return Share::getUsersSharingFile($path, $uidOwner, true, true);
511
	}
512
513
	/**
514
	 * Return the source
515
	 *
516
	 * @param string $path
517
	 * @return array
518
	 */
519
	protected function getSourcePathAndOwner($path) {
520
		$view = Filesystem::getView();
521
		$owner = $view->getOwner($path);
522
		$owner = !is_string($owner) || $owner === '' ? null : $owner;
523
		$fileId = 0;
524
		$currentUser = $this->currentUser->getUID();
525
526
		if ($owner === null || $owner !== $currentUser) {
527
			/** @var \OCP\Files\Storage\IStorage $storage */
528
			list($storage,) = $view->resolvePath($path);
529
530
			if ($owner !== null && !$storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) {
531
				Filesystem::initMountPoints($owner);
532
			} else {
533
				// Probably a remote user, let's try to at least generate activities
534
				// for the current user
535
				if ($currentUser === null) {
536
					list(,$owner,) = explode('/', $view->getAbsolutePath($path), 3);
537
				} else {
538
					$owner = $currentUser;
539
				}
540
			}
541
		}
542
543
		$info = Filesystem::getFileInfo($path);
544
		if ($info !== false) {
545
			$ownerView = new View('/' . $owner . '/files');
546
			$fileId = (int) $info['fileid'];
547
			$path = $ownerView->getPath($fileId);
548
		}
549
550
		return array($path, $owner, $fileId);
551
	}
552
553
	/**
554
	 * Manage sharing events
555
	 * @param array $params The hook params
556
	 */
557 3
	public function share($params) {
558 3
		if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
559 3
			if ((int) $params['shareType'] === Share::SHARE_TYPE_USER) {
560 1
				$this->shareWithUser($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget']);
561 2
			} else if ((int) $params['shareType'] === Share::SHARE_TYPE_GROUP) {
562 1
				$this->shareWithGroup($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget'], (int) $params['id']);
563 1
			} else if ((int) $params['shareType'] === Share::SHARE_TYPE_LINK) {
564 1
				$this->shareByLink((int) $params['fileSource'], $params['itemType'], $params['uidOwner']);
565
			}
566
		}
567 3
	}
568
569
	/**
570
	 * Sharing a file or folder with a user
571
	 *
572
	 * @param string $shareWith
573
	 * @param int $fileSource File ID that is being shared
574
	 * @param string $itemType File type that is being shared (file or folder)
575
	 * @param string $fileTarget File path
576
	 */
577 2
	protected function shareWithUser($shareWith, $fileSource, $itemType, $fileTarget) {
578
		// User performing the share
579 2
		$this->shareNotificationForSharer('shared_user_self', $shareWith, $fileSource, $itemType);
580 2 View Code Duplication
		if ($this->currentUser->getUID() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
581 2
			$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'reshared_user_by', $shareWith, $fileSource, $itemType);
582
		}
583
584
		// New shared user
585 2
		$this->addNotificationsForUser(
586 2
			$shareWith, 'shared_with_by', [[$fileSource => $fileTarget], $this->currentUser->getUserIdentifier()],
587 2
			(int) $fileSource, $fileTarget, $itemType === 'file',
588 2
			$this->userSettings->getUserSetting($shareWith, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...s_Sharing::TYPE_SHARED) targeting OCA\Activity\UserSettings::getUserSetting() can also be of type integer; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
589 2
			$this->userSettings->getUserSetting($shareWith, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($shareWith, 'setting', 'batchtime') : 0
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...ting', 'batchtime') : 0 can also be of type boolean; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
590
		);
591 2
	}
592
593
	/**
594
	 * Sharing a file or folder with a group
595
	 *
596
	 * @param string $shareWith
597
	 * @param int $fileSource File ID that is being shared
598
	 * @param string $itemType File type that is being shared (file or folder)
599
	 * @param string $fileTarget File path
600
	 * @param int $shareId The Share ID of this share
601
	 */
602 6
	protected function shareWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId) {
603
		// Members of the new group
604 6
		$group = $this->groupManager->get($shareWith);
605 6
		if (!($group instanceof IGroup)) {
0 ignored issues
show
Bug introduced by
The class OCP\IGroup does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
606 1
			return;
607
		}
608
609
		// User performing the share
610 5
		$this->shareNotificationForSharer('shared_group_self', $shareWith, $fileSource, $itemType);
611 5 View Code Duplication
		if ($this->currentUser->getUID() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
612 5
			$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'reshared_group_by', $shareWith, $fileSource, $itemType);
613
		}
614
615 5
		$offset = 0;
616 5
		$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
617 5
		while (!empty($users)) {
618 4
			$this->addNotificationsForGroupUsers($users, 'shared_with_by', $fileSource, $itemType, $fileTarget, $shareId);
619 4
			$offset += self::USER_BATCH_SIZE;
620 4
			$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
621
		}
622 5
	}
623
624
	/**
625
	 * Sharing a file or folder via link/public
626
	 *
627
	 * @param int $fileSource File ID that is being shared
628
	 * @param string $itemType File type that is being shared (file or folder)
629
	 * @param string $linkOwner
630
	 */
631 2
	protected function shareByLink($fileSource, $itemType, $linkOwner) {
632 2
		$this->view->chroot('/' . $linkOwner . '/files');
633
634
		try {
635 2
			$path = $this->view->getPath($fileSource);
636 1
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
637 1
			return;
638
		}
639
640 1
		$this->shareNotificationForOriginalOwners($linkOwner, 'reshared_link_by', '', $fileSource, $itemType);
641
642 1
		$this->addNotificationsForUser(
643 1
			$linkOwner, 'shared_link_self', [[$fileSource => $path]],
644 1
			(int) $fileSource, $path, $itemType === 'file',
645 1
			$this->userSettings->getUserSetting($linkOwner, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...s_Sharing::TYPE_SHARED) targeting OCA\Activity\UserSettings::getUserSetting() can also be of type integer; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
646 1
			$this->userSettings->getUserSetting($linkOwner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($linkOwner, 'setting', 'batchtime') : 0
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...ting', 'batchtime') : 0 can also be of type boolean; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
647
		);
648 1
	}
649
650
	/**
651
	 * Manage unsharing events
652
	 * @param IShare $share
653
	 * @throws \OCP\Files\NotFoundException
654
	 */
655
	public function unShare(IShare $share) {
656
		if (in_array($share->getNodeType(), ['file', 'folder'], true)) {
657
			if ($share->getShareType() === Share::SHARE_TYPE_USER) {
658
				$this->unshareFromUser($share);
659
			} else if ($share->getShareType() === Share::SHARE_TYPE_GROUP) {
660
				$this->unshareFromGroup($share);
661
			} else if ($share->getShareType() === Share::SHARE_TYPE_LINK) {
662
				$this->unshareLink($share);
663
			}
664
		}
665
	}
666
667
	/**
668
	 * Unharing a file or folder from a user
669
	 *
670
	 * @param IShare $share
671
	 * @throws \OCP\Files\NotFoundException
672
	 */
673
	protected function unshareFromUser(IShare $share) {
674
		// User performing the share
675
		$this->shareNotificationForSharer('unshared_user_self', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
676
677
		// Owner
678 View Code Duplication
		if ($this->currentUser->getUID() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
679
			$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'unshared_user_by', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
680
		}
681
682
		// Recipient
683
		$this->addNotificationsForUser(
684
			$share->getSharedWith(), 'unshared_by', [[$share->getNodeId() => $share->getTarget()], $this->currentUser->getUserIdentifier()],
685
			$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
686
			$this->userSettings->getUserSetting($share->getSharedWith(), 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...s_Sharing::TYPE_SHARED) targeting OCA\Activity\UserSettings::getUserSetting() can also be of type integer; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
687
			$this->userSettings->getUserSetting($share->getSharedWith(), 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($share->getSharedWith(), 'setting', 'batchtime') : 0
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...ting', 'batchtime') : 0 can also be of type boolean; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
688
		);
689
	}
690
691
	/**
692
	 * Unsharing a file or folder from a group
693
	 *
694
	 * @param IShare $share
695
	 * @throws \OCP\Files\NotFoundException
696
	 */
697
	protected function unshareFromGroup(IShare $share) {
698
		// Members of the new group
699
		$group = $this->groupManager->get($share->getSharedWith());
700
		if (!($group instanceof IGroup)) {
0 ignored issues
show
Bug introduced by
The class OCP\IGroup does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
701
			return;
702
		}
703
704
		// User performing the share
705
		$this->shareNotificationForSharer('unshared_group_self', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
706 View Code Duplication
		if ($this->currentUser->getUID() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
707
			$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'unshared_group_by', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
708
		}
709
710
		$offset = 0;
711
		$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
712
		while (!empty($users)) {
713
			$this->addNotificationsForGroupUsers($users, 'unshared_by', $share->getNodeId(), $share->getNodeType(), $share->getTarget(), $share->getId());
714
			$offset += self::USER_BATCH_SIZE;
715
			$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
716
		}
717
	}
718
719
	/**
720
	 * Sharing a file or folder via link/public
721
	 *
722
	 * @param IShare $share
723
	 * @throws \OCP\Files\NotFoundException
724
	 */
725
	protected function unshareLink(IShare $share) {
726
		$owner = $share->getSharedBy();
727
		if ($this->currentUser->getUID() === null) {
728
			// Link expired
729
			$actionSharer = 'link_expired';
730
			$actionOwner = 'link_by_expired';
731
		} else {
732
			$actionSharer = 'unshared_link_self';
733
			$actionOwner = 'unshared_link_by';
734
		}
735
736
		$this->addNotificationsForUser(
737
			$owner, $actionSharer, [[$share->getNodeId() => $share->getTarget()]],
738
			$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
739
			$this->userSettings->getUserSetting($owner, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...s_Sharing::TYPE_SHARED) targeting OCA\Activity\UserSettings::getUserSetting() can also be of type integer; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
740
			$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : 0
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...ting', 'batchtime') : 0 can also be of type boolean; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
741
		);
742
743
		if ($share->getSharedBy() !== $share->getShareOwner()) {
744
			$owner = $share->getShareOwner();
745
			$this->addNotificationsForUser(
746
				$owner, $actionOwner, [[$share->getNodeId() => $share->getTarget()], $share->getSharedBy()],
747
				$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
748
				$this->userSettings->getUserSetting($owner, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...s_Sharing::TYPE_SHARED) targeting OCA\Activity\UserSettings::getUserSetting() can also be of type integer; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
749
				$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : 0
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...ting', 'batchtime') : 0 can also be of type boolean; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
750
			);
751
		}
752
	}
753
754
	/**
755
	 * @param IUser[] $usersInGroup
756
	 * @param string $actionUser
757
	 * @param int $fileSource File ID that is being shared
758
	 * @param string $itemType File type that is being shared (file or folder)
759
	 * @param string $fileTarget File path
760
	 * @param int $shareId The Share ID of this share
761
	 */
762 4
	protected function addNotificationsForGroupUsers(array $usersInGroup, $actionUser, $fileSource, $itemType, $fileTarget, $shareId) {
763 4
		$affectedUsers = [];
764
765 4
		foreach ($usersInGroup as $user) {
766 4
			$affectedUsers[$user->getUID()] = $fileTarget;
767
		}
768
769
		// Remove the triggering user, we already managed his notifications
770 4
		unset($affectedUsers[$this->currentUser->getUID()]);
771
772 4
		if (empty($affectedUsers)) {
773 1
			return;
774
		}
775
776 3
		$userIds = array_keys($affectedUsers);
777 3
		$filteredStreamUsersInGroup = $this->userSettings->filterUsersBySetting($userIds, 'stream', Files_Sharing::TYPE_SHARED);
778 3
		$filteredEmailUsersInGroup = $this->userSettings->filterUsersBySetting($userIds, 'email', Files_Sharing::TYPE_SHARED);
779
780 3
		$affectedUsers = $this->fixPathsForShareExceptions($affectedUsers, $shareId);
781 3
		foreach ($affectedUsers as $user => $path) {
782 3
			if (empty($filteredStreamUsersInGroup[$user]) && empty($filteredEmailUsersInGroup[$user])) {
783 2
				continue;
784
			}
785
786 1
			$this->addNotificationsForUser(
787 1
				$user, $actionUser, [[$fileSource => $path], $this->currentUser->getUserIdentifier()],
788 1
				$fileSource, $path, ($itemType === 'file'),
789 1
				!empty($filteredStreamUsersInGroup[$user]),
790 1
				!empty($filteredEmailUsersInGroup[$user]) ? $filteredEmailUsersInGroup[$user] : 0
791
			);
792
		}
793 3
	}
794
795
	/**
796
	 * Check when there was a naming conflict and the target is different
797
	 * for some of the users
798
	 *
799
	 * @param array $affectedUsers
800
	 * @param int $shareId
801
	 * @return mixed
802
	 */
803
	protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) {
804
		$queryBuilder = $this->connection->getQueryBuilder();
805
		$queryBuilder->select(['share_with', 'file_target'])
806
			->from('share')
807
			->where($queryBuilder->expr()->eq('parent', $queryBuilder->createParameter('parent')))
808
			->setParameter('parent', (int) $shareId);
809
		$query = $queryBuilder->execute();
810
811
		while ($row = $query->fetch()) {
812
			$affectedUsers[$row['share_with']] = $row['file_target'];
813
		}
814
815
		return $affectedUsers;
816
	}
817
818
	/**
819
	 * Add notifications for the user that shares a file/folder
820
	 *
821
	 * @param string $subject
822
	 * @param string $shareWith
823
	 * @param int $fileSource
824
	 * @param string $itemType
825
	 */
826 2
	protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) {
827 2
		$sharer = $this->currentUser->getUID();
828 2
		if ($sharer === null) {
829
			return;
830
		}
831
832 2
		$this->view->chroot('/' . $sharer . '/files');
833
834
		try {
835 2
			$path = $this->view->getPath($fileSource);
836 1
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
837 1
			return;
838
		}
839
840 1
		$this->addNotificationsForUser(
841 1
			$sharer, $subject, [[$fileSource => $path], $shareWith],
842 1
			$fileSource, $path, ($itemType === 'file'),
843 1
			$this->userSettings->getUserSetting($sharer, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...s_Sharing::TYPE_SHARED) targeting OCA\Activity\UserSettings::getUserSetting() can also be of type integer; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
844 1
			$this->userSettings->getUserSetting($sharer, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($sharer, 'setting', 'batchtime') : 0
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...ting', 'batchtime') : 0 can also be of type boolean; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
845
		);
846 1
	}
847
848
	/**
849
	 * Add notifications for the user that shares a file/folder
850
	 *
851
	 * @param string $owner
852
	 * @param string $subject
853
	 * @param string $shareWith
854
	 * @param int $fileSource
855
	 * @param string $itemType
856
	 */
857 2
	protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) {
858 2
		$this->view->chroot('/' . $owner . '/files');
859
860
		try {
861 2
			$path = $this->view->getPath($fileSource);
862 1
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
863 1
			return;
864
		}
865
866 1
		$this->addNotificationsForUser(
867 1
			$owner, $subject, [[$fileSource => $path], $this->currentUser->getUserIdentifier(), $shareWith],
868 1
			$fileSource, $path, ($itemType === 'file'),
869 1
			$this->userSettings->getUserSetting($owner, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...s_Sharing::TYPE_SHARED) targeting OCA\Activity\UserSettings::getUserSetting() can also be of type integer; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
870 1
			$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : 0
0 ignored issues
show
Bug introduced by
It seems like $this->userSettings->get...ting', 'batchtime') : 0 can also be of type boolean; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
871
		);
872 1
	}
873
874
	/**
875
	 * Add notifications for the owners whose files have been reshared
876
	 *
877
	 * @param string $currentOwner
878
	 * @param string $subject
879
	 * @param string $shareWith
880
	 * @param int $fileSource
881
	 * @param string $itemType
882
	 */
883 10
	protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) {
884
		// Get the full path of the current user
885 10
		$this->view->chroot('/' . $currentOwner . '/files');
886
887
		try {
888 10
			$path = $this->view->getPath($fileSource);
889 1
		} catch (NotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\NotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
890 1
			return;
891
		}
892
893
		/**
894
		 * Get the original owner and his path
895
		 */
896 9
		$owner = $this->view->getOwner($path);
897 9
		if ($owner !== $currentOwner) {
898 7
			$this->reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType);
899
		}
900
901
		/**
902
		 * Get the sharee who shared the item with the currentUser
903
		 */
904 9
		$this->view->chroot('/' . $currentOwner . '/files');
905 9
		$mount = $this->view->getMount($path);
906 9
		if (!($mount instanceof IMountPoint)) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Mount\IMountPoint does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
907 1
			return;
908
		}
909
910 8
		$storage = $mount->getStorage();
911 8
		if (!$storage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
912 1
			return;
913
		}
914
915
		/** @var \OCA\Files_Sharing\SharedStorage $storage */
916 7
		$shareOwner = $storage->getSharedFrom();
917 7
		if ($shareOwner === '' || $shareOwner === null || $shareOwner === $owner || $shareOwner === $currentOwner) {
918 5
			return;
919
		}
920
921 2
		$this->reshareNotificationForSharer($shareOwner, $subject, $shareWith, $fileSource, $itemType);
922 2
	}
923
924
	/**
925
	 * Adds the activity and email for a user when the settings require it
926
	 *
927
	 * @param string $user
928
	 * @param string $subject
929
	 * @param array $subjectParams
930
	 * @param int $fileId
931
	 * @param string $path
932
	 * @param bool $isFile If the item is a file, we link to the parent directory
933
	 * @param bool $streamSetting
934
	 * @param int $emailSetting
935
	 * @param string $type
936
	 */
937 11
	protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) {
938 11
		if (!$streamSetting && !$emailSetting) {
939 1
			return;
940
		}
941
942 10
		$selfAction = $user === $this->currentUser->getUID();
943 10
		$app = $type === Files_Sharing::TYPE_SHARED ? 'files_sharing' : 'files';
944 10
		$link = $this->urlGenerator->linkToRouteAbsolute('files.view.index', array(
945 10
			'dir' => ($isFile) ? dirname($path) : $path,
946
		));
947
948 10
		$objectType = ($fileId) ? 'files' : '';
949
950 10
		$event = $this->manager->generateEvent();
951
		try {
952 10
			$event->setApp($app)
953 10
				->setType($type)
954 10
				->setAffectedUser($user)
955 10
				->setTimestamp(time())
956 10
				->setSubject($subject, $subjectParams)
957 10
				->setObject($objectType, $fileId, $path)
958 10
				->setLink($link);
959
960 10
			if ($this->currentUser->getUID() !== null) {
961
				// Allow this to be empty for guests
962 10
				$event->setAuthor($this->currentUser->getUID());
963
			}
964
		} catch (\InvalidArgumentException $e) {
965
			$this->logger->logException($e);
966
		}
967
968
		// Add activity to stream
969 10
		if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser->getUID(), 'setting', 'self'))) {
970 3
			$this->activityData->send($event);
971
		}
972
973
		// Add activity to mail queue
974 10
		if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser->getUID(), 'setting', 'selfemail'))) {
975 5
			$latestSend = time() + $emailSetting;
976 5
			$this->activityData->storeMail($event, $latestSend);
977
		}
978 10
	}
979
}
980