]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Friendica/Photo.php
API: Fix interoperability between Friendica and Mastodon part
[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 = null, 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 (is_int($scale)) {
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
79                 $data['media-id'] = $data['id'];
80                 $data['id']       = $data['resource-id'];
81
82                 if (is_int($scale)) {
83                         $data['data'] = base64_encode(ModelPhoto::getImageDataForPhoto($data));
84                 }
85
86                 if ($type == 'xml') {
87                         $data['links'] = [];
88                 } else {
89                         $data['link'] = [];
90                 }
91
92                 foreach ($photos as $id => $photo) {
93                         $link = $this->baseUrl->get() . '/photo/' . $data['resource-id'] . '-' . $photo['scale'] . Images::getExtensionByMimeType($data['type']);
94                         if ($type == 'xml') {
95                                 $data['links'][$photo['scale'] . ':link']['@attributes'] = [
96                                         'type'  => $data['type'],
97                                         'scale' => $photo['scale'],
98                                         'href'  => $link
99                                 ];
100                         } else {
101                                 $data['link'][$id] = $link;
102                         }
103                         if (is_null($scale)) {
104                                 $data['scales'][] = [
105                                         'id'     => $photo['id'],
106                                         'scale'  => $photo['scale'],
107                                         'link'   => $link,
108                                         'width'  => $photo['width'],
109                                         'height' => $photo['height'],
110                                         'size'   => $photo['datasize'],
111                                 ];
112                         }
113                 }
114
115                 unset($data['backend-class']);
116                 unset($data['backend-ref']);
117                 unset($data['resource-id']);
118
119                 if ($with_posts) {
120                         // retrieve item element for getting activities (like, dislike etc.) related to photo
121                         $condition = ['uid' => $uid, 'resource-id' => $photo_id];
122
123                         $item = Post::selectFirst(['id', 'uid', 'uri', 'uri-id', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition);
124                 }
125                 if (!empty($item)) {
126                         $data['friendica_activities'] = $this->activities->createFromUriId($item['uri-id'], $item['uid'], $type);
127
128                         // retrieve comments on photo
129                         $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)",
130                                 $item['parent'], $uid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT];
131
132                         $statuses = Post::selectForUser($uid, [], $condition);
133
134                         // prepare output of comments
135                         $commentData = [];
136                         while ($status = DBA::fetch($statuses)) {
137                                 $commentData[] = $this->status->createFromUriId($status['uri-id'], $status['uid'])->toArray();
138                         }
139                         DBA::close($statuses);
140
141                         $comments = [];
142                         if ($type == 'xml') {
143                                 $k = 0;
144                                 foreach ($commentData as $comment) {
145                                         $comments[$k++ . ':comment'] = $comment;
146                                 }
147                         } else {
148                                 foreach ($commentData as $comment) {
149                                         $comments[] = $comment;
150                                 }
151                         }
152                         $data['friendica_comments'] = $comments;
153
154                         // include info if rights on photo and rights on item are mismatching
155                         $data['rights_mismatch'] = $data['allow_cid'] != $item['allow_cid'] ||
156                                 $data['deny_cid'] != $item['deny_cid'] ||
157                                 $data['allow_gid'] != $item['allow_gid'] ||
158                                 $data['deny_gid'] != $item['deny_gid'];
159                 } elseif ($with_posts) {
160                         $data['friendica_activities'] = [];
161                         $data['friendica_comments']   = [];
162                         $data['rights_mismatch']      = false;
163                 }
164
165                 return $data;
166         }
167 }