|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Circles - Bring cloud-users closer together. |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Maxence Lange <[email protected]> |
|
9
|
|
|
* @copyright 2017 |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OCA\Circles\Service; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
use Exception; |
|
31
|
|
|
use OCA\Circles\Db\CirclesRequest; |
|
32
|
|
|
use OCA\Circles\Exceptions\BroadcasterIsNotCompatibleException; |
|
33
|
|
|
use OCA\Circles\Exceptions\MemberDoesNotExistException; |
|
34
|
|
|
use OCA\Circles\Exceptions\SharingFrameAlreadyDeliveredException; |
|
35
|
|
|
use OCA\Circles\Exceptions\SharingFrameDoesNotExistException; |
|
36
|
|
|
use OCA\Circles\IBroadcaster; |
|
37
|
|
|
use OCA\Circles\Model\Circle; |
|
38
|
|
|
use OCA\Circles\Model\Member; |
|
39
|
|
|
use OCA\Circles\Model\SharingFrame; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
class SharesService { |
|
43
|
|
|
|
|
44
|
|
|
/** @var string */ |
|
45
|
|
|
private $userId; |
|
46
|
|
|
|
|
47
|
|
|
/** @var ConfigService */ |
|
48
|
|
|
private $configService; |
|
49
|
|
|
|
|
50
|
|
|
/** @var CirclesRequest */ |
|
51
|
|
|
private $circlesRequest; |
|
52
|
|
|
|
|
53
|
|
|
/** @var BroadcastService */ |
|
54
|
|
|
private $broadcastService; |
|
55
|
|
|
|
|
56
|
|
|
/** @var FederatedService */ |
|
57
|
|
|
private $federatedService; |
|
58
|
|
|
|
|
59
|
|
|
/** @var MiscService */ |
|
60
|
|
|
private $miscService; |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* SharesService constructor. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $userId |
|
67
|
|
|
* @param ConfigService $configService |
|
68
|
|
|
* @param CirclesRequest $circlesRequest |
|
69
|
|
|
* @param BroadcastService $broadcastService |
|
70
|
|
|
* @param FederatedService $federatedService |
|
71
|
|
|
* @param MiscService $miscService |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __construct( |
|
74
|
|
|
$userId, |
|
75
|
|
|
ConfigService $configService, |
|
76
|
|
|
CirclesRequest $circlesRequest, |
|
77
|
|
|
BroadcastService $broadcastService, |
|
78
|
|
|
FederatedService $federatedService, |
|
79
|
|
|
MiscService $miscService |
|
80
|
|
|
) { |
|
81
|
|
|
$this->userId = (string)$userId; |
|
82
|
|
|
$this->configService = $configService; |
|
83
|
|
|
$this->circlesRequest = $circlesRequest; |
|
84
|
|
|
$this->broadcastService = $broadcastService; |
|
85
|
|
|
$this->federatedService = $federatedService; |
|
86
|
|
|
$this->miscService = $miscService; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* createFrame() |
|
92
|
|
|
* |
|
93
|
|
|
* Save the Frame containing the Payload. |
|
94
|
|
|
* The Payload will be shared locally, and spread it live if a Broadcaster is set. |
|
95
|
|
|
* Function will also initiate the federated broadcast to linked circles. |
|
96
|
|
|
* |
|
97
|
|
|
* @param SharingFrame $frame |
|
98
|
|
|
* @param string|null $broadcast |
|
99
|
|
|
* |
|
100
|
|
|
* @throws Exception |
|
101
|
|
|
*/ |
|
102
|
|
|
public function createFrame(SharingFrame $frame, $broadcast = null) { |
|
103
|
|
|
|
|
104
|
|
|
$circle = $this->circlesRequest->getCircle($frame->getCircleId(), $this->userId); |
|
105
|
|
|
if ($circle->getHigherViewer() |
|
106
|
|
|
->getLevel() < Member::LEVEL_MEMBER |
|
107
|
|
|
) { |
|
108
|
|
|
throw new MemberDoesNotExistException(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
$this->generateHeaders($frame, $circle, $broadcast); |
|
113
|
|
|
$this->circlesRequest->saveFrame($frame); |
|
114
|
|
|
|
|
115
|
|
|
$this->broadcastService->broadcastFrame($frame->getHeader('broadcast'), $frame); |
|
116
|
|
|
|
|
117
|
|
|
if ($this->configService->isFederatedCirclesAllowed()) { |
|
118
|
|
|
$this->federatedService->initiateRemoteShare( |
|
119
|
|
|
$circle->getUniqueId(), $frame->getUniqueId() |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
} catch (Exception $e) { |
|
123
|
|
|
throw $e; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Generate Headers and few more thing like UniqueId and Author. |
|
130
|
|
|
* Check if the source is NOT Circles. |
|
131
|
|
|
* |
|
132
|
|
|
* @param SharingFrame $frame |
|
133
|
|
|
* @param Circle $circle |
|
134
|
|
|
* @param $broadcast |
|
135
|
|
|
*/ |
|
136
|
|
|
private function generateHeaders(SharingFrame $frame, Circle $circle, $broadcast) { |
|
137
|
|
|
|
|
138
|
|
|
try { |
|
139
|
|
|
$frame->cannotBeFromCircles(); |
|
140
|
|
|
|
|
141
|
|
|
$frame->setAuthor($this->userId); |
|
142
|
|
|
$frame->setHeader('author', $this->userId); |
|
143
|
|
|
$frame->setHeader('circleName', $circle->getName()); |
|
144
|
|
|
$frame->setHeader('circleUniqueId', $circle->getUniqueId()); |
|
145
|
|
|
$frame->setHeader('broadcast', (string)$broadcast); |
|
146
|
|
|
$frame->generateUniqueId(); |
|
147
|
|
|
$frame->setCircleName($circle->getName()); |
|
148
|
|
|
|
|
149
|
|
|
} catch (Exception $e) { |
|
150
|
|
|
throw new $e; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param string $circleUniqueId |
|
156
|
|
|
* @param string $frameUniqueId |
|
157
|
|
|
* |
|
158
|
|
|
* @return null|SharingFrame |
|
159
|
|
|
* @throws SharingFrameAlreadyDeliveredException |
|
160
|
|
|
* @throws SharingFrameDoesNotExistException |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getFrameFromUniqueId($circleUniqueId, $frameUniqueId) { |
|
163
|
|
|
if ($frameUniqueId === null || $frameUniqueId === '') { |
|
164
|
|
|
throw new SharingFrameDoesNotExistException('unknown_share'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
try { |
|
168
|
|
|
$frame = $this->circlesRequest->getFrame($circleUniqueId, $frameUniqueId); |
|
169
|
|
|
if ($frame->getCloudId() !== null) { |
|
170
|
|
|
throw new SharingFrameAlreadyDeliveredException('share_already_delivered'); |
|
171
|
|
|
} |
|
172
|
|
|
} catch (SharingFrameDoesNotExistException $e) { |
|
173
|
|
|
throw new SharingFrameDoesNotExistException('unknown_share'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $frame; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
} |