]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Media/Metadata/Create.php
Happy New Year 2023!
[friendica.git] / src / Module / Api / Twitter / Media / Metadata / Create.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Twitter\Media\Metadata;
23
24 use Friendica\Core\Logger;
25 use Friendica\Model\Photo;
26 use Friendica\Module\BaseApi;
27 use Friendica\Network\HTTPException\BadRequestException;
28 use Friendica\Util\Network;
29
30 /**
31  * Updates media meta data (picture descriptions)
32  *
33  * @see https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-metadata-create
34  */
35 class Create extends BaseApi
36 {
37         protected function post(array $request = [])
38         {
39                 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
40                 $uid = BaseApi::getCurrentUserID();
41
42                 $postdata = Network::postdata();
43
44                 if (empty($postdata)) {
45                         throw new BadRequestException('No post data');
46                 }
47
48                 $data = json_decode($postdata, true);
49                 if (empty($data)) {
50                         throw new BadRequestException('Invalid post data');
51                 }
52
53                 if (empty($data['media_id']) || empty($data['alt_text'])) {
54                         throw new BadRequestException('Missing post data values');
55                 }
56
57                 if (empty($data['alt_text']['text'])) {
58                         throw new BadRequestException('No alt text.');
59                 }
60
61                 Logger::info('Updating metadata', ['media_id' => $data['media_id']]);
62
63                 $condition = ['id' => $data['media_id'], 'uid' => $uid];
64
65                 $photo = Photo::selectFirst(['resource-id'], $condition);
66                 if (empty($photo['resource-id'])) {
67                         throw new BadRequestException('Metadata not found.');
68                 }
69
70                 Photo::update(['desc' => $data['alt_text']['text']], ['resource-id' => $photo['resource-id']]);
71         }
72 }