3 * @copyright Copyright (C) 2010-2021, 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\Module\Api\Friendica\Photoalbum;
24 use Friendica\Database\DBA;
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;
33 * API endpoint: /api/friendica/photoalbum/delete
35 class Delete extends BaseApi
37 protected function post(array $request = [])
39 self::checkAllowedScope(self::SCOPE_WRITE);
40 $uid = self::getCurrentUserID();
42 $request = $this->getRequest([
43 'album' => '', // Album name
46 // we do not allow calls without album string
47 if (empty($request['album'])) {
48 throw new BadRequestException("no albumname specified");
50 // check if album is existing
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");
57 $resourceIds = array_column($photos, 'resource-id');
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);
64 // now let's delete all photos from the album
65 $result = Photo::delete(['uid' => $uid, 'album' => $request['album']]);
67 // return success of deletion or error message
69 $answer = ['result' => 'deleted', 'message' => 'album `' . $request['album'] . '` with all containing photos has been deleted.'];
70 $this->response->exit('photoalbum_delete', ['$result' => $answer], $this->parameters['extension'] ?? null);
72 throw new InternalServerErrorException("unknown error - deleting from database failed");