]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Friendica/Photo.php
Fix Friendica Photo GET API endpoint to work without explicit scale term
[friendica.git] / src / Factory / Api / Friendica / Photo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-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\Factory\Api\Friendica;
23
24 use Friendica\App\BaseURL;
25 use Friendica\BaseFactory;
26 use Friendica\Database\DBA;
27 use Friendica\Factory\Api\Twitter\Status;
28 use Friendica\Model\Item;
29 use Friendica\Model\Photo as ModelPhoto;
30 use Friendica\Model\Post;
31 use Friendica\Network\HTTPException;
32 use Psr\Log\LoggerInterface;
33 use Friendica\Util\Images;
34
35 class Photo extends BaseFactory
36 {
37         /** @var BaseURL */
38         private $baseUrl;
39         /** @var Status */
40         private $status;
41         /** @var Activities */
42         private $activities;
43
44         public function __construct(LoggerInterface $logger, BaseURL $baseURL, Status $status, Activities $activities)
45         {
46                 parent::__construct($logger);
47
48                 $this->activities = $activities;
49                 $this->status     = $status;
50                 $this->baseUrl    = $baseURL;
51         }
52
53         /**
54          * @param string $photo_id
55          * @param int    $scale
56          * @param int    $uid
57          * @param string $type
58          * @return Array
59          */
60         public function createFromId(string $photo_id, int $scale, int $uid, string $type = 'json', bool $with_posts = true): array
61         {
62                 $fields = ['resource-id', 'created', 'edited', 'title', 'desc', 'album', 'filename','type',
63                         'height', 'width', 'datasize', 'profile', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid',
64                         'backend-class', 'backend-ref', 'id', 'scale'];
65
66                 $condition = ['uid' => $uid, 'resource-id' => $photo_id];
67                 if ($scale >= 0) {
68                         $fields = array_merge(['data'], $fields);
69
70                         $condition['scale'] = $scale;
71                 }
72
73                 $photos = ModelPhoto::selectToArray($fields, $condition);
74                 if (empty($photos)) {
75                         throw new HTTPException\NotFoundException();
76                 }
77                 $data       = $photos[0];
78                 $data['id'] = $data['resource-id'];
79                 if (is_int($scale)) {
80                         $data['data'] = base64_encode(ModelPhoto::getImageDataForPhoto($data));
81                 } else {
82                         unset($data['datasize']); //needed only with scale param
83                 }
84
85                 if ($type == 'xml') {
86                         $data['links'] = [];
87                 } else {
88                         $data['link'] = [];
89                 }
90
91                 foreach ($photos as $id => $photo) {
92                         $link = $this->baseUrl->get() . '/photo/' . $data['resource-id'] . '-' . $photo['scale'] . Images::getExtensionByMimeType($data['type']);
93                         if ($type == 'xml') {
94                                 $data['links'][$photo['scale'] . ':link']['@attributes'] = [
95                                         'type'  => $data['type'],
96                                         'scale' => $photo['scale'],
97                                         'href'  => $link
98                                 ];
99                         } else {
100                                 $data['link'][$id] = $link;
101                         }
102                 }
103
104                 unset($data['backend-class']);
105                 unset($data['backend-ref']);
106                 unset($data['resource-id']);
107                 unset($data['scale']);
108
109                 if ($with_posts) {
110                         // retrieve item element for getting activities (like, dislike etc.) related to photo
111                         $condition = ['uid' => $uid, 'resource-id' => $photo_id];
112
113                         $item = Post::selectFirst(['id', 'uid', 'uri', 'uri-id', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition);
114                 }
115                 if (!empty($item)) {
116                         $data['friendica_activities'] = $this->activities->createFromUriId($item['uri-id'], $item['uid'], $type);
117
118                         // retrieve comments on photo
119                         $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)",
120                                 $item['parent'], $uid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT];
121
122                         $statuses = Post::selectForUser($uid, [], $condition);
123
124                         // prepare output of comments
125                         $commentData = [];
126                         while ($status = DBA::fetch($statuses)) {
127                                 $commentData[] = $this->status->createFromUriId($status['uri-id'], $status['uid'])->toArray();
128                         }
129                         DBA::close($statuses);
130
131                         $comments = [];
132                         if ($type == 'xml') {
133                                 $k = 0;
134                                 foreach ($commentData as $comment) {
135                                         $comments[$k++ . ':comment'] = $comment;
136                                 }
137                         } else {
138                                 foreach ($commentData as $comment) {
139                                         $comments[] = $comment;
140                                 }
141                         }
142                         $data['friendica_comments'] = $comments;
143
144                         // include info if rights on photo and rights on item are mismatching
145                         $data['rights_mismatch'] = $data['allow_cid'] != $item['allow_cid'] ||
146                                 $data['deny_cid'] != $item['deny_cid'] ||
147                                 $data['allow_gid'] != $item['allow_gid'] ||
148                                 $data['deny_gid'] != $item['deny_gid'];
149                 } elseif ($with_posts) {
150                         $data['friendica_activities'] = [];
151                         $data['friendica_comments']   = [];
152                         $data['rights_mismatch']      = false;
153                 }
154
155                 return $data;
156         }
157 }