]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Friendica/Photoalbum/Delete.php
Fix Friendica API Photo Album list documentation to reflect endpoint
[friendica.git] / src / Module / Api / Friendica / Photoalbum / Delete.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Friendica\Photoalbum;
23
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\Item;
27 use Friendica\Model\Photo;
28 use Friendica\Module\BaseApi;
29 use Friendica\Network\HTTPException\BadRequestException;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31
32 /**
33  * API endpoint: /api/friendica/photoalbum/delete
34  */
35 class Delete extends BaseApi
36 {
37         protected function post(array $request = [])
38         {
39                 self::checkAllowedScope(self::SCOPE_WRITE);
40                 $uid = self::getCurrentUserID();
41
42                 $request = $this->getRequest([
43                         'album' => '', // Album name
44                 ], $request);
45
46                 // we do not allow calls without album string
47                 if (empty($request['album'])) {
48                         throw new BadRequestException("no albumname specified");
49                 }
50                 // check if album is existing
51
52                 $photos = DBA::selectToArray('photo', ['resource-id'], ['uid' => $uid, 'album' => $request['album']], ['group_by' => ['resource-id']]);
53                 if (!DBA::isResult($photos)) {
54                         throw new BadRequestException("album not available");
55                 }
56
57                 $resourceIds = array_column($photos, 'resource-id');
58
59                 // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
60                 // to the user and the contacts of the users (drop_items() performs the federation of the deletion to other networks
61                 $condition = ['uid' => $uid, 'resource-id' => $resourceIds, 'post-type' => Item::PT_IMAGE, 'origin' => true];
62                 Item::deleteForUser($condition, $uid);
63
64                 // now let's delete all photos from the album
65                 $result = Photo::delete(['uid' => $uid, 'album' => $request['album']]);
66
67                 // return success of deletion or error message
68                 if ($result) {
69                         Photo::clearAlbumCache($uid);
70                         $answer = ['result' => 'deleted', 'message' => 'album `' . $request['album'] . '` with all containing photos has been deleted.'];
71                         $this->response->exit('photoalbum_delete', ['$result' => $answer], $this->parameters['extension'] ?? null);
72                 } else {
73                         throw new InternalServerErrorException("unknown error - deleting from database failed");
74                 }
75         }
76 }