Completed
Push — master ( 91df00...58343d )
by Phil
10:40
created

FedShareManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\FederatedFileSharing;
23
24
use OCA\Files_Sharing\Activity;
25
use OCP\Activity\IManager;
26
use OCP\Files\NotFoundException;
27
use OCP\ILogger;
28
use OCP\IUserManager;
29
use OCP\Share\IShare;
30
31
/**
32
 * Class FedShareManager holds the share logic
33
 *
34
 * @package OCA\FederatedFileSharing
35
 */
36
class FedShareManager {
37
	/**
38
	 * @var FederatedShareProvider
39
	 */
40
	private $federatedShareProvider;
41
42
	/**
43
	 * @var IUserManager
44
	 */
45
	private $userManager;
46
47
	/**
48
	 * @var IManager
49
	 */
50
	private $activityManager;
51
52
	/**
53
	 * @var ILogger
54
	 */
55
	private $logger;
56
57
	/**
58
	 * FedShareManager constructor.
59
	 *
60
	 * @param FederatedShareProvider $federatedShareProvider
61
	 * @param IUserManager $userManager
62
	 * @param IManager $activityManager
63
	 * @param ILogger $logger
64
	 */
65
	public function __construct(FederatedShareProvider $federatedShareProvider,
66
								IUserManager $userManager,
67
								IManager $activityManager,
68
								ILogger $logger) {
69
		$this->federatedShareProvider = $federatedShareProvider;
70
		$this->userManager = $userManager;
71
		$this->activityManager = $activityManager;
72
		$this->logger = $logger;
73
	}
74
75
	/**
76
	 *
77
	 *
78
	 * @param IShare $share
79
	 *
80
	 * @throws \OCP\Files\InvalidPathException
81
	 * @throws \OCP\Files\NotFoundException
82
	 */
83 View Code Duplication
	public function acceptShare(IShare $share) {
84
		$uid = $this->getCorrectUid($share);
85
		$fileId = $share->getNode()->getId();
86
		list($file, $link) = $this->getFile($uid, $fileId);
87
		$this->publishActivity(
88
			$uid,
89
			Activity::SUBJECT_REMOTE_SHARE_ACCEPTED,
90
			[$share->getSharedWith(), \basename($file)],
91
			'files',
92
			$fileId,
93
			$file,
94
			$link
95
		);
96
	}
97
98
	/**
99
	 * Delete declined share and create a activity
100
	 *
101
	 * @param IShare $share
102
	 *
103
	 * @throws \OCP\Files\InvalidPathException
104
	 * @throws \OCP\Files\NotFoundException
105
	 */
106 View Code Duplication
	public function declineShare(IShare $share) {
107
		$uid = $this->getCorrectUid($share);
108
		$fileId = $share->getNode()->getId();
109
		$this->federatedShareProvider->removeShareFromTable($share);
110
		list($file, $link) = $this->getFile($uid, $fileId);
111
		$this->publishActivity(
112
			$uid,
113
			Activity::SUBJECT_REMOTE_SHARE_DECLINED,
114
			[$share->getSharedWith(), \basename($file)],
115
			'files',
116
			$fileId,
117
			$file,
118
			$link
119
		);
120
	}
121
122
	/**
123
	 * Publish a new activity
124
	 *
125
	 * @param string $affectedUser
126
	 * @param string $subject
127
	 * @param array $subjectParams
128
	 * @param string $objectType
129
	 * @param int $objectId
130
	 * @param string $objectName
131
	 * @param string $link
132
	 *
133
	 * @return void
134
	 */
135
	protected function publishActivity($affectedUser,
136
									   $subject,
137
									   $subjectParams,
138
									   $objectType,
139
									   $objectId,
140
									   $objectName,
141
									   $link
142
	) {
143
		$event = $this->activityManager->generateEvent();
144
		$event->setApp(Activity::FILES_SHARING_APP)
145
			->setType(Activity::TYPE_REMOTE_SHARE)
146
			->setAffectedUser($affectedUser)
147
			->setSubject($subject, $subjectParams)
148
			->setObject($objectType, $objectId, $objectName)
149
			->setLink($link);
150
		$this->activityManager->publish($event);
151
	}
152
153
	/**
154
	 * Get file
155
	 *
156
	 * @param string $user
157
	 * @param int $fileSource
158
	 *
159
	 * @return array with internal path of the file and a absolute link to it
160
	 */
161
	protected function getFile($user, $fileSource) {
162
		\OC_Util::setupFS($user);
163
164
		try {
165
			$file = \OC\Files\Filesystem::getPath($fileSource);
166
		} catch (NotFoundException $e) {
167
			$file = null;
168
		}
169
		// FIXME:  use permalink here, see ViewController for reference
170
		$args = \OC\Files\Filesystem::is_dir($file)
171
			? ['dir' => $file]
172
			: ['dir' => \dirname($file), 'scrollto' => $file];
173
		$link = \OCP\Util::linkToAbsolute('files', 'index.php', $args);
174
175
		return [$file, $link];
176
	}
177
178
	/**
179
	 * Check if we are the initiator or the owner of a re-share
180
	 * and return the correct UID
181
	 *
182
	 * @param IShare $share
183
	 *
184
	 * @return string
185
	 */
186
	protected function getCorrectUid(IShare $share) {
187
		if ($this->userManager->userExists($share->getShareOwner())) {
188
			return $share->getShareOwner();
189
		}
190
191
		return $share->getSharedBy();
192
	}
193
}
194