3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Factory\Api\Friendica;
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;
35 class Photo extends BaseFactory
41 /** @var Activities */
44 public function __construct(LoggerInterface $logger, BaseURL $baseURL, Status $status, Activities $activities)
46 parent::__construct($logger);
48 $this->activities = $activities;
49 $this->status = $status;
50 $this->baseUrl = $baseURL;
54 * @param string $photo_id
60 public function createFromId(string $photo_id, int $scale = null, int $uid, string $type = 'json', bool $with_posts = true): array
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'];
66 $condition = ['uid' => $uid, 'resource-id' => $photo_id];
68 $fields = array_merge(['data'], $fields);
70 $condition['scale'] = $scale;
73 $photos = ModelPhoto::selectToArray($fields, $condition);
75 throw new HTTPException\NotFoundException();
79 $data['media-id'] = $data['id'];
80 $data['id'] = $data['resource-id'];
83 $data['data'] = base64_encode(ModelPhoto::getImageDataForPhoto($data));
92 foreach ($photos as $id => $photo) {
93 $link = $this->baseUrl->get() . '/photo/' . $data['resource-id'] . '-' . $photo['scale'] . Images::getExtensionByMimeType($data['type']);
95 $data['links'][$photo['scale'] . ':link']['@attributes'] = [
96 'type' => $data['type'],
97 'scale' => $photo['scale'],
101 $data['link'][$id] = $link;
103 if (is_null($scale)) {
104 $data['scales'][] = [
105 'id' => $photo['id'],
106 'scale' => $photo['scale'],
108 'width' => $photo['width'],
109 'height' => $photo['height'],
110 'size' => $photo['datasize'],
115 unset($data['backend-class']);
116 unset($data['backend-ref']);
117 unset($data['resource-id']);
120 // retrieve item element for getting activities (like, dislike etc.) related to photo
121 $condition = ['uid' => $uid, 'resource-id' => $photo_id];
123 $item = Post::selectFirst(['id', 'uid', 'uri', 'uri-id', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition);
126 $data['friendica_activities'] = $this->activities->createFromUriId($item['uri-id'], $item['uid'], $type);
128 // retrieve comments on photo
129 $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)",
130 $item['parent'], $uid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT];
132 $statuses = Post::selectForUser($uid, [], $condition);
134 // prepare output of comments
136 while ($status = DBA::fetch($statuses)) {
137 $commentData[] = $this->status->createFromUriId($status['uri-id'], $status['uid'])->toArray();
139 DBA::close($statuses);
142 if ($type == 'xml') {
144 foreach ($commentData as $comment) {
145 $comments[$k++ . ':comment'] = $comment;
148 foreach ($commentData as $comment) {
149 $comments[] = $comment;
152 $data['friendica_comments'] = $comments;
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;