]> git.mxchange.org Git - friendica.git/commitdiff
Add photo album show endpoint that lists photos in an album
authorHank Grabowski <hankgrabowski@gmail.com>
Tue, 13 Dec 2022 19:16:08 +0000 (14:16 -0500)
committerHank Grabowski <hankgrabowski@gmail.com>
Tue, 13 Dec 2022 19:16:08 +0000 (14:16 -0500)
src/Module/Api/Friendica/Photoalbum/Show.php [new file with mode: 0644]
static/routes.config.php

diff --git a/src/Module/Api/Friendica/Photoalbum/Show.php b/src/Module/Api/Friendica/Photoalbum/Show.php
new file mode 100644 (file)
index 0000000..20c5c4a
--- /dev/null
@@ -0,0 +1,111 @@
+<?php
+/**
+ * @copyright Copyright (C) 2022, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Api\Friendica\Photoalbum;
+
+use Friendica\App;
+use Friendica\Core\L10n;
+use Friendica\Database\DBA;
+use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto;
+use Friendica\Model\Contact;
+use Friendica\Model\Photo;
+use Friendica\Module\Api\ApiResponse;
+use Friendica\Module\BaseApi;
+use Friendica\Network\HTTPException;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
+
+/**
+ * api/friendica/photoalbum/:name
+ *
+ * @package  Friendica\Module\Api\Friendica\Photoalbum
+ */
+class Show extends BaseApi
+{
+       /** @var FriendicaPhoto */
+       private $friendicaPhoto;
+
+
+       public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
+       {
+               parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->friendicaPhoto = $friendicaPhoto;
+       }
+
+       protected function rawContent(array $request = [])
+       {
+               BaseApi::checkAllowedScope(BaseApi::SCOPE_READ);
+               $uid = BaseApi::getCurrentUserID();
+               $type = $this->getRequestValue($this->parameters, 'extension', 'json');
+               $request = $this->getRequest([
+                       'album'           => '',    // Get pictures in this album
+                       'offset'          => 0,     // Return results offset by this value
+                       'limit'           => 50,    // Maximum number of results to return. Defaults to 50. Max 500
+               ], $request);
+               if (empty($request['album'])) {
+                       throw new HTTPException\BadRequestException('No album name specified.');
+               }
+
+
+               $album = $request['album'];
+               $condition = ["`uid` = ? AND `album` = ?", $uid, $album];
+               $params = ['order' => ['id'], 'group_by' => ['resource-id']];
+
+               //'limit' => [$request['offset'], $request['limit']]
+               $limit = $request['limit'];
+               if ($limit > 500) {
+                       $limit = 500;
+               }
+
+               if ($limit <= 0) {
+                       $limit = 1;
+               }
+
+               if(!empty($request['offset'])) {
+                       $params['limit'] = [$request['offset'], $limit];
+               } else {
+                       $params['limit'] = $limit;
+               }
+
+               $photos = Photo::selectToArray(['resource-id'], $condition, $params);
+
+               $data = ['photo' => []];
+               if (DBA::isResult($photos)) {
+                       foreach ($photos as $photo) {
+                               $element = $this->friendicaPhoto->createFromId($photo['resource-id'], null, $uid, 'json', false);
+
+                               $element['thumb'] = end($element['link']);
+                               unset($element['link']);
+
+                               if ($type == 'xml') {
+                                       $thumb = $element['thumb'];
+                                       unset($element['thumb']);
+                                       $data['photo'][] = ['@attributes' => $element, '1' => $thumb];
+                               } else {
+                                       $data['photo'][] = $element;
+                               }
+                       }
+               }
+
+               $this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
+       }
+}
index dc1f97b68711f190b820411f7217b0657bd68093..e84dde9b9baf7eb468bc4cbd60273f31848216b7 100644 (file)
@@ -94,6 +94,7 @@ $apiRoutes = [
                '/group_update[.{extension:json|xml|rss|atom}]'            => [Module\Api\Friendica\Group\Update::class,           [        R::POST]],
                '/profile/show[.{extension:json|xml|rss|atom}]'            => [Module\Api\Friendica\Profile\Show::class,           [R::GET         ]],
                '/photoalbums[.{extension:json|xml|rss|atom}]'             => [Module\Api\Friendica\Photoalbum\Index::class,       [R::GET         ]],
+               '/photoalbum[.{extension:json|xml|rss|atom}]'              => [Module\Api\Friendica\Photoalbum\Show::class,        [R::GET         ]],
                '/photoalbum/delete[.{extension:json|xml|rss|atom}]'       => [Module\Api\Friendica\Photoalbum\Delete::class,      [        R::POST]],
                '/photoalbum/update[.{extension:json|xml|rss|atom}]'       => [Module\Api\Friendica\Photoalbum\Update::class,      [        R::POST]],
                '/photos/list[.{extension:json|xml|rss|atom}]'             => [Module\Api\Friendica\Photo\Lists::class,            [R::GET         ]],