]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Api/Mastodon/Statuses.php
Fix tag and mention updates in Mastodon status edit endpoint
[friendica.git] / src / Module / Api / Mastodon / Statuses.php
index 943fc7514f45f5d0786d6c41537dd4462d6e03a4..87840eef9704b0e615758ea24beb95afc7266462 100644 (file)
@@ -21,6 +21,8 @@
 
 namespace Friendica\Module\Api\Mastodon;
 
+use Friendica\Content\PageInfo;
+use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\Markdown;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
@@ -32,10 +34,12 @@ use Friendica\Model\Group;
 use Friendica\Model\Item;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
+use Friendica\Model\Tag;
 use Friendica\Model\User;
 use Friendica\Module\BaseApi;
 use Friendica\Network\HTTPException;
 use Friendica\Protocol\Activity;
+use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Images;
 
 /**
@@ -54,6 +58,7 @@ class Statuses extends BaseApi
                        'in_reply_to_id' => 0,     // ID of the status being replied to, if status is a reply
                        'spoiler_text'   => '',    // Text to be shown as a warning or subject before the actual content. Statuses are generally collapsed behind this field.
                        'language'       => '',    // ISO 639 language code for this status.
+                       'friendica'      => [],
                ], $request);
 
                $owner = User::getOwnerDataById($uid);
@@ -66,26 +71,45 @@ class Statuses extends BaseApi
                        'origin'     => true,
                ];
 
-               $post = Post::selectFirst(['uri-id', 'id', 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'], $condition);
+               $post = Post::selectFirst(['uri-id', 'id', 'gravity', 'uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'network'], $condition);
                if (empty($post['id'])) {
                        throw new HTTPException\NotFoundException('Item with URI ID ' . $this->parameters['id'] . ' not found for user ' . $uid . '.');
                }
 
                // The imput is defined as text. So we can use Markdown for some enhancements
-               $item = ['body' => Markdown::toBBCode($request['status']), 'app' => $this->getApp(), 'title' => ''];
+               $body = Markdown::toBBCode($request['status']);
+
+               if (DI::pConfig()->get($uid, 'system', 'api_auto_attach', false) && preg_match("/\[url=[^\[\]]*\](.*)\[\/url\]\z/ism", $body, $matches)) {
+                       $body = preg_replace("/\[url=[^\[\]]*\].*\[\/url\]\z/ism", PageInfo::getFooterFromUrl($matches[1]), $body);
+               }
+
+               $item['title']      = '';
+               $item['uid']        = $post['uid'];
+               $item['body']       = $body;
+               $item['network']    = $post['network'];
+               $item['app']        = $this->getApp();
 
                if (!empty($request['language'])) {
                        $item['language'] = json_encode([$request['language'] => 1]);
                }
 
-               if (!empty($request['spoiler_text'])) {
-                       if (($request['in_reply_to_id'] == $post['uri-id']) && DI::pConfig()->get($uid, 'system', 'api_spoiler_title', true)) {
-                               $item['title'] = $request['spoiler_text'];
+               if ($post['gravity'] == Item::GRAVITY_PARENT) {
+                       $item['title'] = $request['friendica']['title'] ?? '';
+               }
+
+               $spoiler_text = $request['spoiler_text'];
+
+               if (!empty($spoiler_text)) {
+                       if (!isset($request['friendica']['title']) && $post['gravity'] == Item::GRAVITY_PARENT && DI::pConfig()->get($uid, 'system', 'api_spoiler_title', true)) {
+                               $item['title'] = $spoiler_text;
                        } else {
-                               $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $request['spoiler_text'] . "[/abstract]\n" . $item['body'];
+                               $item['body'] = '[abstract=' . Protocol::ACTIVITYPUB . ']' . $spoiler_text . "[/abstract]\n" . $item['body'];
+                               $item['content-warning'] = BBCode::toPlaintext($spoiler_text);
                        }
                }
 
+               $item = DI::contentItem()->expandTags($item, $request['visibility'] == 'direct');
+
                if (!empty($request['media_ids'])) {
                        /*
                        The provided ids in the request value consists of these two sources:
@@ -123,8 +147,19 @@ class Statuses extends BaseApi
                        }
                        unset($item['attachments']);
                }
+               if (!Item::isValid($item)) {
+                       throw new \Exception('Missing parameters in definition');
+               }
 
                Item::update($item, ['id' => $post['id']]);
+
+               foreach (Tag::getByURIId($post['uri-id']) as $tagToRemove) {
+                       Tag::remove($post['uri-id'], $tagToRemove['type'], $tagToRemove['name'], $tagToRemove['url']);
+               }
+               // Store tags from the body if this hadn't been handled previously in the protocol classes
+
+               Tag::storeFromBody($post['uri-id'], Item::setHashtags($item['body']));
+
                Item::updateDisplayCache($post['uri-id']);
 
                System::jsonExit(DI::mstdnStatus()->createFromUriId($post['uri-id'], $uid, self::appSupportsQuotes()));
@@ -154,6 +189,10 @@ class Statuses extends BaseApi
                // The imput is defined as text. So we can use Markdown for some enhancements
                $body = Markdown::toBBCode($request['status']);
 
+               if (DI::pConfig()->get($uid, 'system', 'api_auto_attach', false) && preg_match("/\[url=[^\[\]]*\](.*)\[\/url\]\z/ism", $body, $matches)) {
+                       $body = preg_replace("/\[url=[^\[\]]*\].*\[\/url\]\z/ism", PageInfo::getFooterFromUrl($matches[1]), $body);
+               }
+
                $item               = [];
                $item['network']    = Protocol::DFRN;
                $item['uid']        = $uid;
@@ -267,7 +306,7 @@ class Statuses extends BaseApi
                if (!empty($request['scheduled_at'])) {
                        $item['guid'] = Item::guid($item, true);
                        $item['uri'] = Item::newURI($item['guid']);
-                       $id = Post\Delayed::add($item['uri'], $item, Worker::PRIORITY_HIGH, Post\Delayed::PREPARED, $request['scheduled_at']);
+                       $id = Post\Delayed::add($item['uri'], $item, Worker::PRIORITY_HIGH, Post\Delayed::PREPARED, DateTimeFormat::utc($request['scheduled_at']));
                        if (empty($id)) {
                                DI::mstdnError()->InternalError();
                        }