Completed
Pull Request — master (#452)
by Joas
05:24
created

FilesHooks::fileCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * ownCloud - Activities App
5
 *
6
 * @author Frank Karlitschek, Joas Schilling
7
 * @copyright 2013 Frank Karlitschek [email protected]
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
 * License as published by the Free Software Foundation; either
12
 * version 3 of the License, or any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public
20
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace OCA\Activity;
24
25
use OC\Files\Filesystem;
26
use OC\Files\View;
27
use OCA\Activity\Extension\Files;
28
use OCA\Activity\Extension\Files_Sharing;
29
use OCP\Activity\IManager;
30
use OCP\Files\Mount\IMountPoint;
31
use OCP\Files\NotFoundException;
32
use OCP\IDBConnection;
33
use OCP\IGroup;
34
use OCP\IGroupManager;
35
use OCP\IUser;
36
use OCP\Share;
37
use OCP\Util;
38
39
/**
40
 * The class to handle the filesystem hooks
41
 */
42
class FilesHooks {
43
	const USER_BATCH_SIZE = 50;
44
45
	/** @var \OCP\Activity\IManager */
46
	protected $manager;
47
48
	/** @var \OCA\Activity\Data */
49
	protected $activityData;
50
51
	/** @var \OCA\Activity\UserSettings */
52
	protected $userSettings;
53
54
	/** @var \OCP\IGroupManager */
55
	protected $groupManager;
56
57
	/** @var \OCP\IDBConnection */
58
	protected $connection;
59
60
	/** @var \OC\Files\View */
61
	protected $view;
62
63
	/** @var string|false */
64
	protected $currentUser;
65
66
	/**
67
	 * Constructor
68
	 *
69
	 * @param IManager $manager
70
	 * @param Data $activityData
71
	 * @param UserSettings $userSettings
72
	 * @param IGroupManager $groupManager
73
	 * @param View $view
74
	 * @param IDBConnection $connection
75
	 * @param string|false $currentUser
76
	 */
77 47
	public function __construct(IManager $manager, Data $activityData, UserSettings $userSettings, IGroupManager $groupManager, View $view, IDBConnection $connection, $currentUser) {
78 47
		$this->manager = $manager;
79 47
		$this->activityData = $activityData;
80 47
		$this->userSettings = $userSettings;
81 47
		$this->groupManager = $groupManager;
82 47
		$this->view = $view;
83 47
		$this->connection = $connection;
84 47
		$this->currentUser = $currentUser;
85 47
	}
86
87
	/**
88
	 * @return string|false Current UserID if logged in, false otherwise
89
	 */
90 2
	protected function getCurrentUser() {
91 2
		return $this->currentUser;
92
	}
93
94
	/**
95
	 * Store the create hook events
96
	 * @param string $path Path of the file that has been created
97
	 */
98 2
	public function fileCreate($path) {
99 2
		if ($this->getCurrentUser() !== false) {
100 1
			$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, 'created_self', 'created_by');
101 1
		} else {
102 1
			$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, '', 'created_public');
103
		}
104 2
	}
105
106
	/**
107
	 * Store the update hook events
108
	 * @param string $path Path of the file that has been modified
109
	 */
110 1
	public function fileUpdate($path) {
111 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CHANGED, 'changed_self', 'changed_by');
112 1
	}
113
114
	/**
115
	 * Store the delete hook events
116
	 * @param string $path Path of the file that has been deleted
117
	 */
118 1
	public function fileDelete($path) {
119 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_DELETED, 'deleted_self', 'deleted_by');
120 1
	}
121
122
	/**
123
	 * Store the restore hook events
124
	 * @param string $path Path of the file that has been restored
125
	 */
126 1
	public function fileRestore($path) {
127 1
		$this->addNotificationsForFileAction($path, Files::TYPE_SHARE_RESTORED, 'restored_self', 'restored_by');
128 1
	}
129
130
	/**
131
	 * Creates the entries for file actions on $file_path
132
	 *
133
	 * @param string $filePath         The file that is being changed
134
	 * @param int    $activityType     The activity type
135
	 * @param string $subject          The subject for the actor
136
	 * @param string $subjectBy        The subject for other users (with "by $actor")
137
	 */
138 3
	protected function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) {
139
		// Do not add activities for .part-files
140 3
		if (substr($filePath, -5) === '.part') {
141 1
			return;
142
		}
143
144 2
		list($filePath, $uidOwner, $fileId) = $this->getSourcePathAndOwner($filePath);
145 2
		$affectedUsers = $this->getUserPathsFromPath($filePath, $uidOwner);
146 2
		$filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType);
147 2
		$filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType);
148
149 2
		foreach ($affectedUsers as $user => $path) {
150 2
			if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) {
151 2
				continue;
152
			}
153
154 2
			if ($user === $this->currentUser) {
155 1
				$userSubject = $subject;
156 1
				$userParams = array($path);
157 1
			} else {
158 1
				$userSubject = $subjectBy;
159 1
				$userParams = array($path, $this->currentUser);
160
			}
161
162 2
			$this->addNotificationsForUser(
163 2
				$user, $userSubject, $userParams,
164 2
				$fileId, $path, true,
165 2
				!empty($filteredStreamUsers[$user]),
166 2
				!empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0,
167
				$activityType
168 2
			);
169 2
		}
170 2
	}
171
172
	/**
173
	 * Returns a "username => path" map for all affected users
174
	 *
175
	 * @param string $path
176
	 * @param string $uidOwner
177
	 * @return array
178
	 */
179
	protected function getUserPathsFromPath($path, $uidOwner) {
180
		return Share::getUsersSharingFile($path, $uidOwner, true, true);
181
	}
182
183
	/**
184
	 * Return the source
185
	 *
186
	 * @param string $path
187
	 * @return array
188
	 */
189
	protected function getSourcePathAndOwner($path) {
190
		$uidOwner = Filesystem::getOwner($path);
191
		$fileId = 0;
192
193
		if ($uidOwner !== $this->currentUser) {
194
			Filesystem::initMountPoints($uidOwner);
195
		}
196
		$info = Filesystem::getFileInfo($path);
197
		if ($info !== false) {
198
			$ownerView = new View('/' . $uidOwner . '/files');
199
			$fileId = (int) $info['fileid'];
200
			$path = $ownerView->getPath($fileId);
201
		}
202
203
		return array($path, $uidOwner, $fileId);
204
	}
205
206
	/**
207
	 * Manage sharing events
208
	 * @param array $params The hook params
209
	 */
210 3
	public function share($params) {
211 3
		if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
212 3
			if ($params['shareWith']) {
213 2
				if ((int) $params['shareType'] === Share::SHARE_TYPE_USER) {
214 1
					$this->shareFileOrFolderWithUser($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget']);
215 2
				} else if ((int) $params['shareType'] === Share::SHARE_TYPE_GROUP) {
216 1
					$this->shareFileOrFolderWithGroup($params['shareWith'], (int) $params['fileSource'], $params['itemType'], $params['fileTarget'], (int) $params['id']);
217 1
				}
218 2
			} else {
219 1
				$this->shareFileOrFolder((int) $params['fileSource'], $params['itemType']);
220
			}
221 3
		}
222 3
	}
223
224
	/**
225
	 * Sharing a file or folder with a user
226
	 *
227
	 * @param string $shareWith
228
	 * @param int $fileSource File ID that is being shared
229
	 * @param string $itemType File type that is being shared (file or folder)
230
	 * @param string $fileTarget File path
231
	 */
232 2
	protected function shareFileOrFolderWithUser($shareWith, $fileSource, $itemType, $fileTarget) {
233
		// User performing the share
234 2
		$this->shareNotificationForSharer('shared_user_self', $shareWith, $fileSource, $itemType);
235 2
		$this->shareNotificationForOriginalOwners($this->currentUser, 'reshared_user_by', $shareWith, $fileSource, $itemType);
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks:...tionForOriginalOwners() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
236
237
		// New shared user
238 2
		$this->addNotificationsForUser(
239 2
			$shareWith, 'shared_with_by', array($fileTarget, $this->currentUser),
240 2
			(int) $fileSource, $fileTarget, ($itemType === 'file'),
241 2
			$this->userSettings->getUserSetting($shareWith, 'stream', Files_Sharing::TYPE_SHARED),
242 2
			$this->userSettings->getUserSetting($shareWith, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($shareWith, 'setting', 'batchtime') : 0
243 2
		);
244 2
	}
245
246
	/**
247
	 * Sharing a file or folder with a group
248
	 *
249
	 * @param string $shareWith
250
	 * @param int $fileSource File ID that is being shared
251
	 * @param string $itemType File type that is being shared (file or folder)
252
	 * @param string $fileTarget File path
253
	 * @param int $shareId The Share ID of this share
254
	 */
255 6
	protected function shareFileOrFolderWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId) {
256
		// Members of the new group
257 6
		$group = $this->groupManager->get($shareWith);
258 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...
259 1
			return;
260
		}
261
262
		// User performing the share
263 5
		$this->shareNotificationForSharer('shared_group_self', $shareWith, $fileSource, $itemType);
264 5
		$this->shareNotificationForOriginalOwners($this->currentUser, 'reshared_group_by', $shareWith, $fileSource, $itemType);
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks:...tionForOriginalOwners() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
265
266 5
		$offset = 0;
267 5
		$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
268 5
		while (!empty($users)) {
269 4
			$this->addNotificationsForGroupUsers($users, $fileSource, $itemType, $fileTarget, $shareId);
270 4
			$offset += self::USER_BATCH_SIZE;
271 4
			$users = $group->searchUsers('', self::USER_BATCH_SIZE, $offset);
272 4
		}
273 5
	}
274
275
	/**
276
	 * @param IUser[] $usersInGroup
277
	 * @param int $fileSource File ID that is being shared
278
	 * @param string $itemType File type that is being shared (file or folder)
279
	 * @param string $fileTarget File path
280
	 * @param int $shareId The Share ID of this share
281
	 */
282 4
	protected function addNotificationsForGroupUsers(array $usersInGroup, $fileSource, $itemType, $fileTarget, $shareId) {
283 4
		$affectedUsers = [];
284
285 4
		foreach ($usersInGroup as $user) {
286 4
			$affectedUsers[$user->getUID()] = $fileTarget;
287 4
		}
288
289
		// Remove the triggering user, we already managed his notifications
290 4
		unset($affectedUsers[$this->currentUser]);
291
292 4
		if (empty($affectedUsers)) {
293 1
			return;
294
		}
295
296 3
		$userIds = array_keys($affectedUsers);
297 3
		$filteredStreamUsersInGroup = $this->userSettings->filterUsersBySetting($userIds, 'stream', Files_Sharing::TYPE_SHARED);
298 3
		$filteredEmailUsersInGroup = $this->userSettings->filterUsersBySetting($userIds, 'email', Files_Sharing::TYPE_SHARED);
299
300 3
		$affectedUsers = $this->fixPathsForShareExceptions($affectedUsers, $shareId);
301 3
		foreach ($affectedUsers as $user => $path) {
302 3
			if (empty($filteredStreamUsersInGroup[$user]) && empty($filteredEmailUsersInGroup[$user])) {
303 2
				continue;
304
			}
305
306 1
			$this->addNotificationsForUser(
307 1
				$user, 'shared_with_by', array($path, $this->currentUser),
308 1
				$fileSource, $path, ($itemType === 'file'),
309 1
				!empty($filteredStreamUsersInGroup[$user]),
310 1
				!empty($filteredEmailUsersInGroup[$user]) ? $filteredEmailUsersInGroup[$user] : 0
311 1
			);
312 3
		}
313 3
	}
314
315
	/**
316
	 * Check when there was a naming conflict and the target is different
317
	 * for some of the users
318
	 *
319
	 * @param array $affectedUsers
320
	 * @param int $shareId
321
	 * @return mixed
322
	 */
323
	protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) {
324
		$queryBuilder = $this->connection->getQueryBuilder();
325
		$queryBuilder->select(['share_with', 'file_target'])
326
			->from('share')
327
			->where($queryBuilder->expr()->eq('parent', $queryBuilder->createParameter('parent')))
328
			->setParameter('parent', (int) $shareId);
329
		$query = $queryBuilder->execute();
330
331
		while ($row = $query->fetch()) {
332
			$affectedUsers[$row['share_with']] = $row['file_target'];
333
		}
334
335
		return $affectedUsers;
336
	}
337
338
	/**
339
	 * Sharing a file or folder via link/public
340
	 *
341
	 * @param int $fileSource File ID that is being shared
342
	 * @param string $itemType File type that is being shared (file or folder)
343
	 */
344 2
	protected function shareFileOrFolder($fileSource, $itemType) {
345 2
		$this->view->chroot('/' . $this->currentUser . '/files');
346
347
		try {
348 2
			$path = $this->view->getPath($fileSource);
349 2
		} 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...
350 1
			return;
351
		}
352
353 1
		$this->shareNotificationForOriginalOwners($this->currentUser, 'reshared_link_by', '', $fileSource, $itemType);
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks:...tionForOriginalOwners() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
354
355 1
		$this->addNotificationsForUser(
356 1
			$this->currentUser, 'shared_link_self', array($path),
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
357 1
			(int) $fileSource, $path, ($itemType === 'file'),
358 1
			$this->userSettings->getUserSetting($this->currentUser, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\UserSettings::getUserSetting() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
359 1
			$this->userSettings->getUserSetting($this->currentUser, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($this->currentUser, 'setting', 'batchtime') : 0
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\UserSettings::getUserSetting() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
360 1
		);
361 1
	}
362
363
	/**
364
	 * Add notifications for the user that shares a file/folder
365
	 *
366
	 * @param string $subject
367
	 * @param string $shareWith
368
	 * @param int $fileSource
369
	 * @param string $itemType
370
	 */
371 2
	protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) {
372 2
		$this->view->chroot('/' . $this->currentUser . '/files');
373
374
		try {
375 2
			$path = $this->view->getPath($fileSource);
376 2
		} 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...
377 1
			return;
378
		}
379
380 1
		$this->addNotificationsForUser(
381 1
			$this->currentUser, $subject, array($path, $shareWith),
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\FilesHooks::addNotificationsForUser() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
382 1
			$fileSource, $path, ($itemType === 'file'),
383 1
			$this->userSettings->getUserSetting($this->currentUser, 'stream', Files_Sharing::TYPE_SHARED),
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\UserSettings::getUserSetting() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
384 1
			$this->userSettings->getUserSetting($this->currentUser, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($this->currentUser, 'setting', 'batchtime') : 0
0 ignored issues
show
Security Bug introduced by
It seems like $this->currentUser can also be of type false; however, OCA\Activity\UserSettings::getUserSetting() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
385 1
		);
386 1
	}
387
388
	/**
389
	 * Add notifications for the user that shares a file/folder
390
	 *
391
	 * @param string $owner
392
	 * @param string $subject
393
	 * @param string $shareWith
394
	 * @param int $fileSource
395
	 * @param string $itemType
396
	 */
397 2
	protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) {
398 2
		$this->view->chroot('/' . $owner . '/files');
399
400
		try {
401 2
			$path = $this->view->getPath($fileSource);
402 2
		} 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...
403 1
			return;
404
		}
405
406 1
		$this->addNotificationsForUser(
407 1
			$owner, $subject, array($path, $this->currentUser, $shareWith),
408 1
			$fileSource, $path, ($itemType === 'file'),
409 1
			$this->userSettings->getUserSetting($owner, 'stream', Files_Sharing::TYPE_SHARED),
410 1
			$this->userSettings->getUserSetting($owner, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($owner, 'setting', 'batchtime') : 0
411 1
		);
412 1
	}
413
414
	/**
415
	 * Add notifications for the owners whose files have been reshared
416
	 *
417
	 * @param string $currentOwner
418
	 * @param string $subject
419
	 * @param string $shareWith
420
	 * @param int $fileSource
421
	 * @param string $itemType
422
	 */
423 10
	protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) {
424
		// Get the full path of the current user
425 10
		$this->view->chroot('/' . $currentOwner . '/files');
426
427
		try {
428 10
			$path = $this->view->getPath($fileSource);
429 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...
430 1
			return;
431
		}
432
433
		/**
434
		 * Get the original owner and his path
435
		 */
436 9
		$owner = $this->view->getOwner($path);
437 9
		if ($owner !== $currentOwner) {
438 7
			$this->reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType);
439 7
		}
440
441
		/**
442
		 * Get the sharee who shared the item with the currentUser
443
		 */
444 9
		$this->view->chroot('/' . $currentOwner . '/files');
445 9
		$mount = $this->view->getMount($path);
446 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...
447 1
			return;
448
		}
449
450 8
		$storage = $mount->getStorage();
451 8
		if (!$storage->instanceOfStorage('OC\Files\Storage\Shared')) {
452 1
			return;
453
		}
454
455
		/** @var \OC\Files\Storage\Shared $storage */
456 7
		$shareOwner = $storage->getSharedFrom();
457 7
		if ($shareOwner === '' || $shareOwner === null || $shareOwner === $owner || $shareOwner === $currentOwner) {
458 5
			return;
459
		}
460
461 2
		$this->reshareNotificationForSharer($shareOwner, $subject, $shareWith, $fileSource, $itemType);
462 2
	}
463
464
	/**
465
	 * Adds the activity and email for a user when the settings require it
466
	 *
467
	 * @param string $user
468
	 * @param string $subject
469
	 * @param array $subjectParams
470
	 * @param int $fileId
471
	 * @param string $path
472
	 * @param bool $isFile If the item is a file, we link to the parent directory
473
	 * @param bool $streamSetting
474
	 * @param int $emailSetting
475
	 * @param string $type
476
	 */
477 9
	protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) {
478 9
		if (!$streamSetting && !$emailSetting) {
479 1
			return;
480
		}
481
482 8
		$selfAction = $user === $this->currentUser;
483 8
		$app = $type === Files_Sharing::TYPE_SHARED ? 'files_sharing' : 'files';
484 8
		$link = Util::linkToAbsolute('files', 'index.php', array(
485 8
			'dir' => ($isFile) ? dirname($path) : $path,
486 8
		));
487
488 8
		$objectType = ($fileId) ? 'files' : '';
489
490 8
		$event = $this->manager->generateEvent();
491 8
		$event->setApp($app)
492 8
			->setType($type)
493 8
			->setAffectedUser($user)
494 8
			->setAuthor($this->currentUser)
495 8
			->setTimestamp(time())
496 8
			->setSubject($subject, $subjectParams)
497 8
			->setObject($objectType, $fileId, $path)
498 8
			->setLink($link);
499
500
		// Add activity to stream
501 8
		if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser, 'setting', 'self'))) {
502 3
			$this->activityData->send($event);
503 3
		}
504
505
		// Add activity to mail queue
506 8
		if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser, 'setting', 'selfemail'))) {
507 3
			$latestSend = time() + $emailSetting;
508 3
			$this->activityData->storeMail($event, $latestSend);
509 3
		}
510 8
	}
511
}
512