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();
78 $data['id'] = $data['resource-id'];
80 $data['data'] = base64_encode(ModelPhoto::getImageDataForPhoto($data));
82 unset($data['datasize']); //needed only with scale param
91 foreach ($photos as $id => $photo) {
92 $link = $this->baseUrl->get() . '/photo/' . $data['resource-id'] . '-' . $photo['scale'] . Images::getExtensionByMimeType($data['type']);
94 $data['links'][$photo['scale'] . ':link']['@attributes'] = [
95 'type' => $data['type'],
96 'scale' => $photo['scale'],
100 $data['link'][$id] = $link;
104 unset($data['backend-class']);
105 unset($data['backend-ref']);
106 unset($data['resource-id']);
107 unset($data['scale']);
110 // retrieve item element for getting activities (like, dislike etc.) related to photo
111 $condition = ['uid' => $uid, 'resource-id' => $photo_id];
113 $item = Post::selectFirst(['id', 'uid', 'uri', 'uri-id', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition);
116 $data['friendica_activities'] = $this->activities->createFromUriId($item['uri-id'], $item['uid'], $type);
118 // retrieve comments on photo
119 $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)",
120 $item['parent'], $uid, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT];
122 $statuses = Post::selectForUser($uid, [], $condition);
124 // prepare output of comments
126 while ($status = DBA::fetch($statuses)) {
127 $commentData[] = $this->status->createFromUriId($status['uri-id'], $status['uid'])->toArray();
129 DBA::close($statuses);
132 if ($type == 'xml') {
134 foreach ($commentData as $comment) {
135 $comments[$k++ . ':comment'] = $comment;
138 foreach ($commentData as $comment) {
139 $comments[] = $comment;
142 $data['friendica_comments'] = $comments;
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;