3 * @copyright Copyright (C) 2010-2022, 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\Twitter\Statuses;
24 use Friendica\Content\Text\HTML;
25 use Friendica\Content\Text\Markdown;
26 use Friendica\Core\Protocol;
27 use Friendica\Database\DBA;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Item;
31 use Friendica\Model\Photo;
32 use Friendica\Model\Post;
33 use Friendica\Model\User;
34 use Friendica\Module\BaseApi;
35 use Friendica\Protocol\Activity;
36 use Friendica\Util\Images;
38 use HTMLPurifier_Config;
41 * Updates the user’s current status.
43 * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
45 class Update extends BaseApi
47 public function post(array $request = [])
49 self::checkAllowedScope(self::SCOPE_WRITE);
50 $uid = self::getCurrentUserID();
52 $request = self::getRequest([
56 'in_reply_to_status_id' => 0,
61 'include_entities' => false,
64 $owner = User::getOwnerDataById($uid);
66 if (!empty($request['htmlstatus'])) {
67 $body = HTML::toBBCodeVideo($request['htmlstatus']);
69 $config = HTMLPurifier_Config::createDefault();
70 $config->set('Cache.DefinitionImpl', null);
72 $purifier = new HTMLPurifier($config);
73 $body = $purifier->purify($body);
75 $body = HTML::toBBCode($request['htmlstatus']);
77 // The imput is defined as text. So we can use Markdown for some enhancements
78 $body = Markdown::toBBCode($request['status']);
82 $item['network'] = Protocol::DFRN;
84 $item['verb'] = Activity::POST;
85 $item['contact-id'] = $owner['id'];
86 $item['author-id'] = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
87 $item['title'] = $request['title'];
88 $item['body'] = $body;
89 $item['app'] = $request['source'];
91 if (empty($item['app']) && !empty(self::getCurrentApplication()['name'])) {
92 $item['app'] = self::getCurrentApplication()['name'];
95 if (!empty($request['lat']) && !empty($request['long'])) {
96 $item['coord'] = sprintf("%s %s", $request['lat'], $request['long']);
99 $item['allow_cid'] = $owner['allow_cid'] ?? '';
100 $item['allow_gid'] = $owner['allow_gid'] ?? '';
101 $item['deny_cid'] = $owner['deny_cid'] ?? '';
102 $item['deny_gid'] = $owner['deny_gid'] ?? '';
104 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
105 $item['private'] = Item::PRIVATE;
106 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
107 $item['private'] = Item::UNLISTED;
109 $item['private'] = Item::PUBLIC;
112 if ($request['in_reply_to_status_id']) {
113 $parent = Post::selectFirst(['uri'], ['id' => $request['in_reply_to_status_id'], 'uid' => [0, $uid]]);
115 $item['thr-parent'] = $parent['uri'];
116 $item['gravity'] = GRAVITY_COMMENT;
117 $item['object-type'] = Activity\ObjectType::COMMENT;
119 self::checkThrottleLimit();
121 $item['gravity'] = GRAVITY_PARENT;
122 $item['object-type'] = Activity\ObjectType::NOTE;
125 $item = DI::contentItem()->expandTags($item);
127 if (!empty($request['media_ids'])) {
128 $ids = explode(',', $request['media_ids']);
129 } elseif (!empty($_FILES['media'])) {
130 // upload the image if we have one
131 $picture = Photo::upload($uid, $_FILES['media']);
132 if (!empty($picture)) {
133 $ids[] = $picture['id'];
138 $item['object-type'] = Activity\ObjectType::IMAGE;
139 $item['post-type'] = Item::PT_IMAGE;
140 $item['attachments'] = [];
142 foreach ($ids as $id) {
143 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
144 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
145 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
151 Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
153 $ressources[] = $media[0]['resource-id'];
154 $phototypes = Images::supportedTypes();
155 $ext = $phototypes[$media[0]['type']];
158 'type' => Post\Media::IMAGE,
159 'mimetype' => $media[0]['type'],
160 'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
161 'size' => $media[0]['datasize'],
162 'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
163 'description' => $media[0]['desc'] ?? '',
164 'width' => $media[0]['width'],
165 'height' => $media[0]['height']
168 if (count($media) > 1) {
169 $attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
170 $attachment['preview-width'] = $media[1]['width'];
171 $attachment['preview-height'] = $media[1]['height'];
173 $item['attachments'][] = $attachment;
177 $id = Item::insert($item, true);
179 $item = Post::selectFirst(['uri-id'], ['id' => $id]);
180 if (!empty($item['uri-id'])) {
181 // output the post that we just posted.
182 $status_info = DI::twitterStatus()->createFromUriId($item['uri-id'], $uid, $request['include_entities'])->toArray();
183 DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
187 DI::mstdnError()->InternalError();