Completed
Push — scanner_improvements ( 4bed41 )
by Pauli
10:32
created

ShareHooks::itemUnshared()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Morris Jobke <[email protected]>
10
 * @copyright Morris Jobke 2014
11
 */
12
13
namespace OCA\Music\Hooks;
14
15
use \OCA\Music\App\Music;
16
17
class ShareHooks {
18
19
	/**
20
	 * Invoke auto update of music database after item gets unshared
21
	 * @param array $params contains the params of the removed share
22
	 */
23 View Code Duplication
	static public function itemUnshared($params) {
24
		$app = new Music();
25
26
		$container = $app->getContainer();
27
		$scanner = $container->query('Scanner');
28
		$sharedFileId = $params['itemSource'];
29
		$shareWithUser = $params['shareWith'];
30
31
		if ($params['itemType'] === 'folder') {
32
			$ownerHome = $container->query('UserFolder');
33
			$nodes = $ownerHome->getById($sharedFileId);
34
			if (count($nodes) > 0) {
35
				$sharedFolder = $nodes[0];
36
				$scanner->deleteFolder($sharedFolder, $shareWithUser);
37
			}
38
		}
39
		else if ($params['itemType'] === 'file') {
40
			$scanner->delete((int)$sharedFileId, $shareWithUser);
41
		}
42
	}
43
44
	/**
45
	 * Invoke auto update of music database after item gets shared
46
	 * @param array $params contains the params of the added share
47
	 */
48 View Code Duplication
	static public function itemShared($params) {
49
		if ($params['itemType'] === 'folder') {
50
			// Do not auto-update database when a folder is shared. The folder might contain
51
			// thousands of audio files, and indexing them could take minutes or hours. The sharee
52
			// user will be prompted to update database the next time she opens the Music app.
53
		} else if ($params['itemType'] === 'file') {
54
			$app = new Music();
55
			$container = $app->getContainer();
56
			$scanner = $container->query('Scanner');
57
			$sharerFolder = $container->query('UserFolder');
58
			$file = $sharerFolder->getById($params['itemSource'])[0]; // file object with sharer path
59
			$userId = $params['shareWith'];
60
			$userFolder = $scanner->resolveUserFolder($userId);
61
			$filePath = $userFolder->getPath() . $params['itemTarget']; // file path for sharee
62
			$scanner->update($file, $userId, $userFolder, $filePath);
63
		}
64
	}
65
66
	public function register() {
67
		// FIXME: this is temporarily static because core emitters are not future
68
		// proof, therefore legacy code in here
69
		\OCP\Util::connectHook('OCP\Share', 'post_unshare', __CLASS__, 'itemUnshared');
70
		\OCP\Util::connectHook('OCP\Share', 'post_shared',  __CLASS__, 'itemShared');
71
	}
72
}
73