]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Friendica/Photoalbum/Show.php
da16cbedc2174fa59f049fee50256c2e497a033e
[friendica.git] / src / Module / Api / Friendica / Photoalbum / Show.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
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
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Friendica\Photoalbum;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Database\DBA;
27 use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Photo;
30 use Friendica\Module\Api\ApiResponse;
31 use Friendica\Module\BaseApi;
32 use Friendica\Network\HTTPException;
33 use Friendica\Util\Profiler;
34 use Psr\Log\LoggerInterface;
35
36 /**
37  * api/friendica/photoalbum/:name
38  *
39  * @package  Friendica\Module\Api\Friendica\Photoalbum
40  */
41 class Show extends BaseApi
42 {
43         /** @var FriendicaPhoto */
44         private $friendicaPhoto;
45
46
47         public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
48         {
49                 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50
51                 $this->friendicaPhoto = $friendicaPhoto;
52         }
53
54         protected function rawContent(array $request = [])
55         {
56                 BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
57                 $uid = BaseApi::getCurrentUserID();
58                 $type = $this->getRequestValue($this->parameters, 'extension', 'json');
59                 $request = $this->getRequest([
60                         'album'           => '',    // Get pictures in this album
61                         'offset'          => 0,     // Return results offset by this value
62                         'limit'           => 50,    // Maximum number of results to return. Defaults to 50. Max 500
63                         'latest_first'    => false, // Whether to reverse the order so newest are first
64                 ], $request);
65                 if (empty($request['album'])) {
66                         throw new HTTPException\BadRequestException('No album name specified.');
67                 }
68
69
70                 $orderDescending = $request['latest_first'];
71                 $album = $request['album'];
72                 $condition = ["`uid` = ? AND `album` = ?", $uid, $album];
73                 $params = ['order' => ['id' => $orderDescending], 'group_by' => ['resource-id']];
74
75                 //'limit' => [$request['offset'], $request['limit']]
76                 $limit = $request['limit'];
77                 if ($limit > 500) {
78                         $limit = 500;
79                 }
80
81                 if ($limit <= 0) {
82                         $limit = 1;
83                 }
84
85                 if(!empty($request['offset'])) {
86                         $params['limit'] = [$request['offset'], $limit];
87                 } else {
88                         $params['limit'] = $limit;
89                 }
90
91                 $photos = Photo::selectToArray(['resource-id'], $condition, $params);
92
93                 $data = ['photo' => []];
94                 if (DBA::isResult($photos)) {
95                         foreach ($photos as $photo) {
96                                 $element = $this->friendicaPhoto->createFromId($photo['resource-id'], null, $uid, 'json', false);
97
98                                 $element['thumb'] = end($element['link']);
99                                 unset($element['link']);
100
101                                 if ($type == 'xml') {
102                                         $thumb = $element['thumb'];
103                                         unset($element['thumb']);
104                                         $data['photo'][] = ['@attributes' => $element, '1' => $thumb];
105                                 } else {
106                                         $data['photo'][] = $element;
107                                 }
108                         }
109                 }
110
111                 $this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
112         }
113 }