]> git.mxchange.org Git - friendica.git/commitdiff
Photo delete is moved as well
authorMichael <heluecht@pirati.ca>
Wed, 10 Nov 2021 23:31:42 +0000 (23:31 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 10 Nov 2021 23:31:42 +0000 (23:31 +0000)
include/api.php
src/Module/Api/Friendica/Photo/Delete.php [new file with mode: 0644]
static/routes.config.php
tests/legacy/ApiTest.php

index c5363e2845be5c73f874919e053ee7586670ccc4..cf633374f7df8aa6ab0f1f90d0896f6fff1bde96 100644 (file)
@@ -3868,53 +3868,6 @@ function api_fr_photo_create_update($type)
        throw new InternalServerErrorException("unknown error - this error on uploading or updating a photo should never happen");
 }
 
-/**
- * delete a single photo from the database through api
- *
- * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
- * @return string|array
- * @throws BadRequestException
- * @throws ForbiddenException
- * @throws InternalServerErrorException
- */
-function api_fr_photo_delete($type)
-{
-       if (api_user() === false) {
-               throw new ForbiddenException();
-       }
-
-       // input params
-       $photo_id = $_REQUEST['photo_id'] ?? null;
-
-       // do several checks on input parameters
-       // we do not allow calls without photo id
-       if ($photo_id == null) {
-               throw new BadRequestException("no photo_id specified");
-       }
-
-       // check if photo is existing in database
-       if (!Photo::exists(['resource-id' => $photo_id, 'uid' => api_user()])) {
-               throw new BadRequestException("photo not available");
-       }
-
-       // now we can perform on the deletion of the photo
-       $result = Photo::delete(['uid' => api_user(), 'resource-id' => $photo_id]);
-
-       // return success of deletion or error message
-       if ($result) {
-               // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
-               // to the user and the contacts of the users (drop_items() do all the necessary magic to avoid orphans in database and federate deletion)
-               $condition = ['uid' => api_user(), 'resource-id' => $photo_id, 'type' => 'photo'];
-               Item::deleteForUser($condition, api_user());
-
-               $result = ['result' => 'deleted', 'message' => 'photo with id `' . $photo_id . '` has been deleted from server.'];
-               return BaseApi::formatData("photo_delete", $type, ['$result' => $result]);
-       } else {
-               throw new InternalServerErrorException("unknown error on deleting photo from database table");
-       }
-}
-
-
 /**
  * returns the details of a specified photo id, if scale is given, returns the photo data in base 64
  *
@@ -4034,7 +3987,6 @@ function api_account_update_profile_image($type)
 api_register_func('api/friendica/photos/list', 'api_fr_photos_list', true);
 api_register_func('api/friendica/photo/create', 'api_fr_photo_create_update', true, API_METHOD_POST);
 api_register_func('api/friendica/photo/update', 'api_fr_photo_create_update', true, API_METHOD_POST);
-api_register_func('api/friendica/photo/delete', 'api_fr_photo_delete', true, API_METHOD_DELETE);
 api_register_func('api/friendica/photo', 'api_fr_photo_detail', true);
 api_register_func('api/account/update_profile_image', 'api_account_update_profile_image', true, API_METHOD_POST);
 
diff --git a/src/Module/Api/Friendica/Photo/Delete.php b/src/Module/Api/Friendica/Photo/Delete.php
new file mode 100644 (file)
index 0000000..702c11e
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, 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\Photo;
+
+use Friendica\Model\Item;
+use Friendica\Model\Photo;
+use Friendica\Module\BaseApi;
+use Friendica\Network\HTTPException\BadRequestException;
+use Friendica\Network\HTTPException\InternalServerErrorException;
+
+/**
+ * API endpoint: /api/friendica/photo/delete
+ */
+class Delete extends BaseApi
+{
+       public static function rawContent(array $parameters = [])
+       {
+               self::checkAllowedScope(self::SCOPE_WRITE);
+               $uid = self::getCurrentUserID();
+
+               $request = self::getRequest([
+                       'photo_id' => null, // Photo id
+               ]);
+
+               // do several checks on input parameters
+               // we do not allow calls without photo id
+               if ($request['photo_id'] == null) {
+                       throw new BadRequestException("no photo_id specified");
+               }
+
+               // check if photo is existing in database
+               if (!Photo::exists(['resource-id' => $request['photo_id'], 'uid' => $uid])) {
+                       throw new BadRequestException("photo not available");
+               }
+
+               // now we can perform on the deletion of the photo
+               $result = Photo::delete(['uid' => $uid, 'resource-id' => $request['photo_id']]);
+
+               // return success of deletion or error message
+               if ($result) {
+                       // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
+                       // to the user and the contacts of the users (drop_items() do all the necessary magic to avoid orphans in database and federate deletion)
+                       $condition = ['uid' => $uid, 'resource-id' => $request['photo_id'], 'type' => 'photo'];
+                       Item::deleteForUser($condition, $uid);
+
+                       $result = ['result' => 'deleted', 'message' => 'photo with id `' . $request['photo_id'] . '` has been deleted from server.'];
+                       self::exit('photo_delete', ['$result' => $result], $parameters['extension'] ?? null);
+               } else {
+                       throw new InternalServerErrorException("unknown error on deleting photo from database table");
+               }
+       }
+}
index 6625d9695bdb09db3c1c3ef31f4ba5f7d5e1f74c..0b50adb0fdb5e8da8a3ae9f60dc594d67b6f58ef 100644 (file)
@@ -87,7 +87,7 @@ $apiRoutes = [
                '/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\Index::class,             [R::GET         ]],
                '/photo/create[.{extension:json|xml|rss|atom}]'            => [Module\Api\Friendica\Index::class,             [        R::POST]],
-               '/photo/delete[.{extension:json|xml|rss|atom}]'            => [Module\Api\Friendica\Index::class,             [R::DELETE, R::POST]],
+               '/photo/delete[.{extension:json|xml|rss|atom}]'            => [Module\Api\Friendica\Photo\Delete::class,      [R::DELETE, R::POST]],
                '/photo/update[.{extension:json|xml|rss|atom}]'            => [Module\Api\Friendica\Index::class,             [        R::POST]],
                '/photo[.{extension:json|xml|rss|atom}]'                   => [Module\Api\Friendica\Index::class,             [R::GET         ]],
        ],
index c4cbde1076c15cb828e800e9c0bc130f2cef9c8f..6f57f3bbd7b4263485bdbc65a99ab30f5c01c52d 100644 (file)
@@ -3351,8 +3351,8 @@ class ApiTest extends FixtureTest
         */
        public function testApiFrPhotoDelete()
        {
-               $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               api_fr_photo_delete('json');
+               // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
+               // api_fr_photo_delete('json');
        }
 
        /**
@@ -3362,9 +3362,9 @@ class ApiTest extends FixtureTest
         */
        public function testApiFrPhotoDeleteWithoutAuthenticatedUser()
        {
-               $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
-               $_SESSION['authenticated'] = false;
-               api_fr_photo_delete('json');
+               // $this->expectException(\Friendica\Network\HTTPException\ForbiddenException::class);
+               // $_SESSION['authenticated'] = false;
+               // api_fr_photo_delete('json');
        }
 
        /**
@@ -3374,9 +3374,9 @@ class ApiTest extends FixtureTest
         */
        public function testApiFrPhotoDeleteWithPhotoId()
        {
-               $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
-               $_REQUEST['photo_id'] = 1;
-               api_fr_photo_delete('json');
+               // $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class);
+               // $_REQUEST['photo_id'] = 1;
+               // api_fr_photo_delete('json');
        }
 
        /**