-
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathDaysController.php
More file actions
234 lines (198 loc) · 6.44 KB
/
Copy pathDaysController.php
File metadata and controls
234 lines (198 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
* @author Varun Patil <radialapps@gmail.com>
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OCA\Memories\Controller;
use OCA\Memories\ClustersBackend;
use OCA\Memories\Util;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
class DaysController extends GenericApiController
{
/**
* @NoAdminRequired
*
* @PublicPage
*/
public function days(): Http\Response
{
return Util::guardEx(function () {
$list = $this->tq->getDays(
$this->isRecursive(),
$this->isArchive(),
$this->isMonthView(),
$this->isReverse(),
$this->getTransformations(),
);
// Preload some day responses
$this->preloadDays($list);
return new JSONResponse($list, Http::STATUS_OK);
});
}
/**
* @NoAdminRequired
*
* @PublicPage
*
* @param int[] $dayIds
*/
public function day(array $dayIds): Http\Response
{
return Util::guardEx(function () use ($dayIds) {
// Run actual query
$list = $this->tq->getDay(
$dayIds,
$this->isRecursive(),
$this->isArchive(),
$this->isHidden(),
$this->isMonthView(),
$this->isReverse(),
$this->getTransformations(),
);
return new JSONResponse($list, Http::STATUS_OK);
});
}
/**
* @NoAdminRequired
*
* @PublicPage
*/
public function dayGet(string $id): Http\Response
{
// Split at commas and convert all parts to int
return $this->day(array_map(static fn ($p) => (int) $p, explode(',', $id)));
}
/**
* Get transformations depending on the request.
*/
private function getTransformations(): array
{
$transforms = [];
// Add clustering transforms
$clusterTs = ClustersBackend\Manager::getTransforms($this->request);
$transforms = array_merge($transforms, $clusterTs);
// Other transforms not allowed for public shares
if (!Util::isLoggedIn()) {
return $transforms;
}
// Filter only favorites
if ($this->request->getParam('fav')) {
$transforms[] = [$this->tq, 'transformFavoriteFilter'];
}
// Filter only videos
if ($this->request->getParam('vid')) {
$transforms[] = [$this->tq, 'transformVideoFilter'];
}
// Filter only live photos
if ($this->request->getParam('live')) {
$transforms[] = [$this->tq, 'transformLivePhotoFilter'];
}
// Filter only panoramas
if ($this->request->getParam('pano')) {
$transforms[] = [$this->tq, 'transformPanoFilter'];
}
// Filter geological bounds
if ($bounds = $this->request->getParam('mapbounds')) {
$transforms[] = [$this->tq, 'transformMapBoundsFilter', $bounds];
}
// Limit number of responses for day query
if ($limit = $this->request->getParam('limit')) {
$transforms[] = [$this->tq, 'transformLimit', (int) $limit];
}
// Add extra fields for native callers
if (Util::callerIsNative()) {
$transforms[] = [$this->tq, 'transformNativeQuery'];
}
return $transforms;
}
/**
* Preload a few "day" at the start of "days" response.
*
* @param array $days the days array (modified in place)
*/
private function preloadDays(array &$days): void
{
// Do not preload anything for native clients.
// Since the contents of preloads are trusted, clients will not load locals.
if (Util::callerIsNative() || $this->noPreload()) {
return;
}
// Construct map of dayid-day
$totalCount = 0;
$drefMap = [];
foreach ($days as &$day) {
if ($count = (int) $day['count']) {
$totalCount += max($count, 10); // max 5 days
}
$dayId = (int) $day['dayid'];
$drefMap[$dayId] = &$day;
if ($totalCount >= 50) { // should be enough
break;
}
}
if (!$totalCount) {
return;
}
// Preload photos for these days
$details = $this->tq->getDay(
array_keys($drefMap),
$this->isRecursive(),
$this->isArchive(),
$this->isHidden(),
$this->isMonthView(),
$this->isReverse(),
$this->getTransformations(),
);
// Load details into map byref
foreach ($details as $photo) {
$dayId = (int) $photo['dayid'];
if (!($drefMap[$dayId] ?? null)) {
continue;
}
if (!($drefMap[$dayId]['detail'] ?? null)) {
$drefMap[$dayId]['detail'] = [];
}
$drefMap[$dayId]['detail'][] = $photo;
}
}
private function isRecursive(): bool
{
return null === $this->request->getParam('folder') || $this->request->getParam('recursive');
}
private function isArchive(): bool
{
return null !== $this->request->getParam('archive');
}
private function isHidden(): bool
{
return null !== $this->request->getParam('hidden');
}
private function noPreload(): bool
{
return null !== $this->request->getParam('nopreload');
}
private function isMonthView(): bool
{
return null !== $this->request->getParam('monthView');
}
private function isReverse(): bool
{
return null !== $this->request->getParam('reverse');
}
}