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\Photo as ModelPhoto;
29 use Friendica\Model\Post;
30 use Friendica\Network\HTTPException;
31 use Psr\Log\LoggerInterface;
32 use Friendica\Util\Images;
34 class Photo extends BaseFactory
40 /** @var Activities */
43 public function __construct(LoggerInterface $logger, BaseURL $baseURL, Status $status, Activities $activities)
45 parent::__construct($logger);
47 $this->activities = $activities;
48 $this->status = $status;
49 $this->baseUrl = $baseURL;
53 * @param string $photo_id
59 public function createFromId(string $photo_id, int $scale = null, int $uid, string $type = 'json', bool $with_posts = true): array
61 $fields = ['resource-id', 'created', 'edited', 'title', 'desc', 'album', 'filename','type',
62 'height', 'width', 'datasize', 'profile', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid',
63 'backend-class', 'backend-ref', 'id', 'scale'];
65 $condition = ['uid' => $uid, 'resource-id' => $photo_id];
67 $fields = array_merge(['data'], $fields);
69 $condition['scale'] = $scale;
72 $photos = ModelPhoto::selectToArray($fields, $condition);
74 throw new HTTPException\NotFoundException();
77 $data['id'] = $data['resource-id'];
79 $data['data'] = base64_encode(ModelPhoto::getImageDataForPhoto($data));
81 unset($data['datasize']); //needed only with scale param
90 foreach ($photos as $id => $photo) {
91 $link = $this->baseUrl->get() . '/photo/' . $data['resource-id'] . '-' . $photo['scale'] . Images::getExtensionByMimeType($data['type']);
93 $data['links'][$photo['scale'] . ':link']['@attributes'] = [
94 'type' => $data['type'],
95 'scale' => $photo['scale'],
99 $data['link'][$id] = $link;
103 unset($data['backend-class']);
104 unset($data['backend-ref']);
105 unset($data['resource-id']);
106 unset($data['scale']);
109 // retrieve item element for getting activities (like, dislike etc.) related to photo
110 $condition = ['uid' => $uid, 'resource-id' => $photo_id];
112 $item = Post::selectFirst(['id', 'uid', 'uri', 'uri-id', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition);
115 $data['friendica_activities'] = $this->activities->createFromUriId($item['uri-id'], $item['uid'], $type);
117 // retrieve comments on photo
118 $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)",
119 $item['parent'], $uid, GRAVITY_PARENT, GRAVITY_COMMENT];
121 $statuses = Post::selectForUser($uid, [], $condition);
123 // prepare output of comments
125 while ($status = DBA::fetch($statuses)) {
126 $commentData[] = $this->status->createFromUriId($status['uri-id'], $status['uid'])->toArray();
128 DBA::close($statuses);
131 if ($type == 'xml') {
133 foreach ($commentData as $comment) {
134 $comments[$k++ . ':comment'] = $comment;
137 foreach ($commentData as $comment) {
138 $comments[] = $comment;
141 $data['friendica_comments'] = $comments;
143 // include info if rights on photo and rights on item are mismatching
144 $data['rights_mismatch'] = $data['allow_cid'] != $item['allow_cid'] ||
145 $data['deny_cid'] != $item['deny_cid'] ||
146 $data['allow_gid'] != $item['allow_gid'] ||
147 $data['deny_gid'] != $item['deny_gid'];
148 } elseif ($with_posts) {
149 $data['friendica_activities'] = [];
150 $data['friendica_comments'] = [];
151 $data['rights_mismatch'] = false;