Completed
Pull Request — master (#124)
by Joas
04:04
created

FilesHooks::fileMoving()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 17
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 2
nop 2
crap 12
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 47
	 */
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
	}
111
112
	/**
113
	 * Store the create hook events
114
	 * @param string $path Path of the file that has been created
115 2
	 */
116 2
	public function fileCreate($path) {
117 1
		if ($this->currentUser->getUserIdentifier() !== '') {
118 1
			$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, 'created_self', 'created_by');
119 1
		} else {
120
			$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, '', 'created_public');
121 2
		}
122
	}
123
124
	/**
125
	 * Store the update hook events
126
	 * @param string $path Path of the file that has been modified
127 1
	 */
128 1
	public function fileUpdate($path) {
129 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CHANGED, 'changed_self', 'changed_by');
130
	}
131
132
	/**
133
	 * Store the delete hook events
134
	 * @param string $path Path of the file that has been deleted
135 1
	 */
136 1
	public function fileDelete($path) {
137 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_DELETED, 'deleted_self', 'deleted_by');
138
	}
139
140
	/**
141
	 * Store the restore hook events
142
	 * @param string $path Path of the file that has been restored
143 1
	 */
144 1
	public function fileRestore($path) {
145 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_RESTORED, 'restored_self', 'restored_by');
146
	}
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 3
	 */
156
	protected function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) {
157 3
		// Do not add activities for .part-files
158 1
		if (substr($filePath, -5) === '.part') {
159
			return;
160
		}
161 2
162 2
		list($filePath, $uidOwner, $fileId) = $this->getSourcePathAndOwner($filePath);
163
		if ($fileId === 0) {
164
			// Could not find the file for the owner ...
165
			return;
166
		}
167 2
168 2
		$affectedUsers = $this->getUserPathsFromPath($filePath, $uidOwner);
169 2
		$filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType);
170
		$filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType);
171 2
172 2
		foreach ($affectedUsers as $user => $path) {
173 2
			$user = (string) $user;
174 2
			if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
175
				continue;
176
			}
177 2
178 1
			if ($user === $this->currentUser->getUID()) {
179 1
				$userSubject = $subject;
180 1
				$userParams = [[$fileId => $path]];
181 1
			} else {
182 1
				$userSubject = $subjectBy;
183
				$userParams = [[$fileId => $path], $this->currentUser->getUserIdentifier()];
184
			}
185 2
186 2
			$this->addNotificationsForUser(
187 2
				$user, $userSubject, $userParams,
188 2
				$fileId, $path, true,
189 2
				!empty($filteredStreamUsers[$user]),
190
				!empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0,
191 2
				$activityType
192 2
			);
193 2
		}
194
	}
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 3
	 */
557 3
	public function share($params) {
558 3
		if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
559 1
			if ((int) $params['shareType'] === Share::SHARE_TYPE_USER) {
560 3
				$this->shareWithUser($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget']);
561 1
			} else if ((int) $params['shareType'] === Share::SHARE_TYPE_GROUP) {
562 2
				$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 3
			}
566 3
		}
567
	}
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
	protected function shareWithUser($shareWith, $fileSource, $itemType, $fileTarget) {
578
		// User performing the share
579
		$this->shareNotificationForSharer('shared_user_self', $shareWith, $fileSource, $itemType);
580 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
			$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'reshared_user_by', $shareWith, $fileSource, $itemType);
582
		}
583
584
		// New shared user
585
		$this->addNotificationsForUser(
586
			$shareWith, 'shared_with_by', [[$fileSource => $fileTarget], $this->currentUser->getUserIdentifier()],
587
			(int) $fileSource, $fileTarget, $itemType === 'file',
588
			$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
			$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
	}
592
593 2
	/**
594 2
	 * Sharing a file or folder with a group
595 2
	 *
596 2
	 * @param string $shareWith
597 2
	 * @param int $fileSource File ID that is being shared
598 2
	 * @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
	protected function shareWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId) {
603
		// Members of the new group
604
		$group = $this->groupManager->get($shareWith);
605 2
		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 2
			return;
607 2
		}
608 2
609
		// User performing the share
610
		$this->shareNotificationForSharer('shared_group_self', $shareWith, $fileSource, $itemType);
611 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...
612 2
			$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'reshared_group_by', $shareWith, $fileSource, $itemType);
613 2
		}
614 2
615 2
		$offset = 0;
616 2
		$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
617 2
		while (!empty($users)) {
618
			$this->addNotificationsForGroupUsers($users, 'shared_with_by', $fileSource, $itemType, $fileTarget, $shareId);
619
			$offset += self::USER_BATCH_SIZE;
620
			$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
621
		}
622
	}
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 6
	 * @param string $linkOwner
630 6
	 */
631 6
	protected function shareByLink($fileSource, $itemType, $linkOwner) {
632 6
		$owner = $this->currentUser->getUID();
633 6
		$this->view->chroot('/' . $owner . '/files');
634 6
635
		try {
636
			$path = $this->view->getPath($fileSource);
637
		} 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...
638
			return;
639
		}
640
641 6
		$this->shareNotificationForOriginalOwners($owner, 'reshared_link_by', '', $fileSource, $itemType);
642 6
643 1
		$this->addNotificationsForUser(
644
			$linkOwner, 'shared_link_self', [[$fileSource => $path]],
645
			(int) $fileSource, $path, $itemType === 'file',
646
			$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...
647 5
			$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...
648 5
		);
649 5
	}
650 5
651
	/**
652 5
	 * Manage unsharing events
653 5
	 * @param IShare $share
654 5
	 * @throws \OCP\Files\NotFoundException
655 4
	 */
656 4
	public function unShare(IShare $share) {
657 4
		if (in_array($share->getNodeType(), ['file', 'folder'], true)) {
658 4
			if ($share->getShareType() === Share::SHARE_TYPE_USER) {
659 5
				$this->unshareFromUser($share);
660
			} else if ($share->getShareType() === Share::SHARE_TYPE_GROUP) {
661
				$this->unshareFromGroup($share);
662
			} else if ($share->getShareType() === Share::SHARE_TYPE_LINK) {
663
				$this->unshareLink($share);
664
			}
665
		}
666
	}
667
668
	/**
669 4
	 * Unharing a file or folder from a user
670 4
	 *
671
	 * @param IShare $share
672 4
	 * @throws \OCP\Files\NotFoundException
673 4
	 */
674 4
	protected function unshareFromUser(IShare $share) {
675
		// User performing the share
676
		$this->shareNotificationForSharer('unshared_user_self', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
677 4
678
		// Owner
679 4 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...
680 1
			$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'unshared_user_by', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
681
		}
682
683 3
		// Recipient
684 3
		$this->addNotificationsForUser(
685 3
			$share->getSharedWith(), 'unshared_by', [[$share->getNodeId() => $share->getTarget()], $this->currentUser->getUserIdentifier()],
686
			$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
687 3
			$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...
688 3
			$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...
689 3
		);
690 2
	}
691
692
	/**
693 1
	 * Unsharing a file or folder from a group
694 1
	 *
695 1
	 * @param IShare $share
696 1
	 * @throws \OCP\Files\NotFoundException
697 1
	 */
698 1
	protected function unshareFromGroup(IShare $share) {
699 3
		// Members of the new group
700 3
		$group = $this->groupManager->get($share->getSharedWith());
701
		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...
702
			return;
703
		}
704
705
		// User performing the share
706
		$this->shareNotificationForSharer('unshared_group_self', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
707 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...
708
			$this->shareNotificationForOriginalOwners($this->currentUser->getUID(), 'unshared_group_by', $share->getSharedWith(), $share->getNodeId(), $share->getNodeType());
709
		}
710
711
		$offset = 0;
712
		$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
713
		while (!empty($users)) {
714
			$this->addNotificationsForGroupUsers($users, 'unshared_by', $share->getNodeId(), $share->getNodeType(), $share->getTarget(), $share->getId());
715
			$offset += self::USER_BATCH_SIZE;
716
			$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
717
		}
718
	}
719
720
	/**
721
	 * Sharing a file or folder via link/public
722
	 *
723
	 * @param IShare $share
724
	 * @throws \OCP\Files\NotFoundException
725
	 */
726
	protected function unshareLink(IShare $share) {
727
		$owner = $share->getSharedBy();
728
		if ($this->currentUser->getUID() === null) {
729
			// Link expired
730
			$actionSharer = 'link_expired';
731
			$actionOwner = 'link_by_expired';
732
		} else {
733 2
			$actionSharer = 'unshared_link_self';
734 2
			$actionOwner = 'unshared_link_by';
735 2
		}
736 2
737 2
		$this->addNotificationsForUser(
738 2
			$owner, $actionSharer, [[$share->getNodeId() => $share->getTarget()]],
739
			$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
740
			$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...
741
			$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...
742
		);
743
744
		if ($share->getSharedBy() !== $share->getShareOwner()) {
745
			$owner = $share->getShareOwner();
746
			$this->addNotificationsForUser(
747
				$owner, $actionOwner, [[$share->getNodeId() => $share->getTarget()], $share->getSharedBy()],
748
				$share->getNodeId(), $share->getTarget(), $share->getNodeType() === 'file',
749 2
				$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...
750
				$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...
751
			);
752 2
		}
753 2
	}
754 1
755
	/**
756
	 * @param IUser[] $usersInGroup
757 1
	 * @param string $actionUser
758
	 * @param int $fileSource File ID that is being shared
759 1
	 * @param string $itemType File type that is being shared (file or folder)
760 1
	 * @param string $fileTarget File path
761 1
	 * @param int $shareId The Share ID of this share
762 1
	 */
763 1
	protected function addNotificationsForGroupUsers(array $usersInGroup, $actionUser, $fileSource, $itemType, $fileTarget, $shareId) {
764 1
		$affectedUsers = [];
765 1
766
		foreach ($usersInGroup as $user) {
767
			$affectedUsers[$user->getUID()] = $fileTarget;
768
		}
769
770
		// Remove the triggering user, we already managed his notifications
771
		unset($affectedUsers[$this->currentUser->getUID()]);
772
773
		if (empty($affectedUsers)) {
774
			return;
775 2
		}
776 2
777 2
		$userIds = array_keys($affectedUsers);
778
		$filteredStreamUsersInGroup = $this->userSettings->filterUsersBySetting($userIds, 'stream', Files_Sharing::TYPE_SHARED);
779
		$filteredEmailUsersInGroup = $this->userSettings->filterUsersBySetting($userIds, 'email', Files_Sharing::TYPE_SHARED);
780
781 2
		$affectedUsers = $this->fixPathsForShareExceptions($affectedUsers, $shareId);
782
		foreach ($affectedUsers as $user => $path) {
783
			if (empty($filteredStreamUsersInGroup[$user]) && empty($filteredEmailUsersInGroup[$user])) {
784 2
				continue;
785 2
			}
786 1
787
			$this->addNotificationsForUser(
788
				$user, $actionUser, [[$fileSource => $path], $this->currentUser->getUserIdentifier()],
789 1
				$fileSource, $path, ($itemType === 'file'),
790 1
				!empty($filteredStreamUsersInGroup[$user]),
791 1
				!empty($filteredEmailUsersInGroup[$user]) ? $filteredEmailUsersInGroup[$user] : 0
792 1
			);
793 1
		}
794 1
	}
795 1
796
	/**
797
	 * Check when there was a naming conflict and the target is different
798
	 * for some of the users
799
	 *
800
	 * @param array $affectedUsers
801
	 * @param int $shareId
802
	 * @return mixed
803
	 */
804
	protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) {
805
		$queryBuilder = $this->connection->getQueryBuilder();
806 2
		$queryBuilder->select(['share_with', 'file_target'])
807 2
			->from('share')
808
			->where($queryBuilder->expr()->eq('parent', $queryBuilder->createParameter('parent')))
809
			->setParameter('parent', (int) $shareId);
810 2
		$query = $queryBuilder->execute();
811 2
812 1
		while ($row = $query->fetch()) {
813
			$affectedUsers[$row['share_with']] = $row['file_target'];
814
		}
815 1
816 1
		return $affectedUsers;
817 1
	}
818 1
819 1
	/**
820 1
	 * Add notifications for the user that shares a file/folder
821 1
	 *
822
	 * @param string $subject
823
	 * @param string $shareWith
824
	 * @param int $fileSource
825
	 * @param string $itemType
826
	 */
827
	protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) {
828
		$sharer = $this->currentUser->getUID();
829
		if ($sharer === null) {
830
			return;
831
		}
832 10
833
		$this->view->chroot('/' . $sharer . '/files');
834 10
835
		try {
836
			$path = $this->view->getPath($fileSource);
837 10
		} 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...
838 10
			return;
839 1
		}
840
841
		$this->addNotificationsForUser(
842
			$sharer, $subject, [[$fileSource => $path], $shareWith],
843
			$fileSource, $path, ($itemType === 'file'),
844
			$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...
845 9
			$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...
846 9
		);
847 7
	}
848 7
849
	/**
850
	 * Add notifications for the user that shares a file/folder
851
	 *
852
	 * @param string $owner
853 9
	 * @param string $subject
854 9
	 * @param string $shareWith
855 9
	 * @param int $fileSource
856 1
	 * @param string $itemType
857
	 */
858
	protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) {
859 8
		$this->view->chroot('/' . $owner . '/files');
860 8
861 1
		try {
862
			$path = $this->view->getPath($fileSource);
863
		} 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...
864
			return;
865 7
		}
866 7
867 5
		$this->addNotificationsForUser(
868
			$owner, $subject, [[$fileSource => $path], $this->currentUser->getUserIdentifier(), $shareWith],
869
			$fileSource, $path, ($itemType === 'file'),
870 2
			$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...
871 2
			$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...
872
		);
873
	}
874
875
	/**
876
	 * Add notifications for the owners whose files have been reshared
877
	 *
878
	 * @param string $currentOwner
879
	 * @param string $subject
880
	 * @param string $shareWith
881
	 * @param int $fileSource
882
	 * @param string $itemType
883
	 */
884
	protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) {
885
		// Get the full path of the current user
886 11
		$this->view->chroot('/' . $currentOwner . '/files');
887 11
888 1
		try {
889
			$path = $this->view->getPath($fileSource);
890
		} 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...
891 10
			return;
892 10
		}
893 10
894 10
		/**
895 10
		 * Get the original owner and his path
896
		 */
897 10
		$owner = $this->view->getOwner($path);
898
		if ($owner !== $currentOwner) {
899 10
			$this->reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType);
900
		}
901 10
902 10
		/**
903 10
		 * Get the sharee who shared the item with the currentUser
904 10
		 */
905 10
		$this->view->chroot('/' . $currentOwner . '/files');
906 10
		$mount = $this->view->getMount($path);
907 10
		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...
908
			return;
909 10
		}
910
911 10
		$storage = $mount->getStorage();
912 10
		if (!$storage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
913 10
			return;
914
		}
915
916
		/** @var \OCA\Files_Sharing\SharedStorage $storage */
917
		$shareOwner = $storage->getSharedFrom();
918 10
		if ($shareOwner === '' || $shareOwner === null || $shareOwner === $owner || $shareOwner === $currentOwner) {
919 3
			return;
920 3
		}
921
922
		$this->reshareNotificationForSharer($shareOwner, $subject, $shareWith, $fileSource, $itemType);
923 10
	}
924 5
925 5
	/**
926 5
	 * Adds the activity and email for a user when the settings require it
927 10
	 *
928
	 * @param string $user
929
	 * @param string $subject
930
	 * @param array $subjectParams
931
	 * @param int $fileId
932
	 * @param string $path
933
	 * @param bool $isFile If the item is a file, we link to the parent directory
934
	 * @param bool $streamSetting
935
	 * @param int $emailSetting
936
	 * @param string $type
937
	 */
938
	protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) {
939
		if (!$streamSetting && !$emailSetting) {
940
			return;
941
		}
942
943
		$selfAction = $user === $this->currentUser->getUID();
944
		$app = $type === Files_Sharing::TYPE_SHARED ? 'files_sharing' : 'files';
945
		$link = $this->urlGenerator->linkToRouteAbsolute('files.view.index', array(
946
			'dir' => ($isFile) ? dirname($path) : $path,
947
		));
948
949
		$objectType = ($fileId) ? 'files' : '';
950
951
		$event = $this->manager->generateEvent();
952
		try {
953
			$event->setApp($app)
954
				->setType($type)
955
				->setAffectedUser($user)
956
				->setTimestamp(time())
957
				->setSubject($subject, $subjectParams)
958
				->setObject($objectType, $fileId, $path)
959
				->setLink($link);
960
961
			if ($this->currentUser->getUID() !== null) {
962
				// Allow this to be empty for guests
963
				$event->setAuthor($this->currentUser->getUID());
964
			}
965
		} catch (\InvalidArgumentException $e) {
966
			$this->logger->logException($e);
967
		}
968
969
		// Add activity to stream
970
		if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser->getUID(), 'setting', 'self'))) {
971
			$this->activityData->send($event);
972
		}
973
974
		// Add activity to mail queue
975
		if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser->getUID(), 'setting', 'selfemail'))) {
976
			$latestSend = time() + $emailSetting;
977
			$this->activityData->storeMail($event, $latestSend);
978
		}
979
	}
980
}
981