From c65fff6f93e4e33901510343faef69654fd1a97b Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 26 Oct 2022 17:00:55 +0000 Subject: [PATCH 1/3] Some more improvements for posts with shares --- mod/display.php | 6 +-- mod/share.php | 6 +-- src/Content/Item.php | 38 +++++++++++++++++- src/Content/Text/BBCode.php | 26 +++++++++++- src/Factory/Api/Mastodon/Status.php | 10 ++--- src/Factory/Api/Twitter/Status.php | 9 ++--- src/Model/Item.php | 51 +++--------------------- src/Model/Post/Media.php | 6 +-- src/Protocol/ActivityPub/Transmitter.php | 28 +++++-------- src/Protocol/Diaspora.php | 19 +++------ 10 files changed, 100 insertions(+), 99 deletions(-) diff --git a/mod/display.php b/mod/display.php index 41c4cc3b9a..62e180874d 100644 --- a/mod/display.php +++ b/mod/display.php @@ -122,9 +122,9 @@ function display_init(App $a) function display_fetchauthor($item) { - $shared = Item::getShareArray($item); - if (empty($shared['comment']) && !empty($shared['guid']) && !empty($shared['profile'])) { - $contact = Contact::getByURLForUser($shared['profile'], DI::userSession()->getLocalUserId()); + $shared = DI::contentItem()->getSharedPost($item, ['author-link']); + if (!empty($shared) && empty($shared['comment'])) { + $contact = Contact::getByURLForUser($shared['post']['author-link'], DI::userSession()->getLocalUserId()); } if (empty($contact)) { diff --git a/mod/share.php b/mod/share.php index 9f94d9ddc9..f0a2170b5e 100644 --- a/mod/share.php +++ b/mod/share.php @@ -41,9 +41,9 @@ function share_init(App $a) { System::exit(); } - $shared = Item::getShareArray($item); - if (empty($shared['comment']) && (!empty($shared['message_id']) || !empty($shared['link']))) { - $content = '[share]' . ($shared['message_id'] ?: $shared['link']) . '[/share]'; + $shared = DI::contentItem()->getSharedPost($item, ['uri']); + if (!empty($shared) && empty($shared['comment'])) { + $content = '[share]' . $shared['post']['uri'] . '[/share]'; } else { $content = '[share]' . $item['uri'] . '[/share]'; } diff --git a/src/Content/Item.php b/src/Content/Item.php index f03e42a088..f72493c17e 100644 --- a/src/Content/Item.php +++ b/src/Content/Item.php @@ -680,11 +680,11 @@ class Item $shared_content .= '[h3]' . $item['title'] . "[/h3]\n"; } - $shared = ItemModel::getShareArray($item); + $shared = $this->getSharedPost($item, ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network']); // If it is a reshared post then reformat it to avoid display problems with two share elements if (!empty($shared)) { - if (!empty($shared['guid']) && ($encaspulated_share = self::createSharedPostByGuid($shared['guid'], 0, '', $add_media))) { + if (($encaspulated_share = $this->createSharedBlockByArray($shared['post'], $add_media))) { $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']); } @@ -695,4 +695,38 @@ class Item return $shared_content; } + + /** + * Return the shared post from an item array (if the item is shared item) + * + * @param array $item + * @param array $fields + * + * @return array with the shared post + */ + public function getSharedPost(array $item, array $fields = []): array + { + if (!empty($item['quote-uri-id'])) { + $shared = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id'], 'uid' => [0, $item['uid'] ?? 0]]); + if (is_array($shared)) { + return [ + 'comment' => BBCode::removeSharedData($item['body'] ?? ''), + 'post' => $shared + ]; + } + } + + $attributes = BBCode::fetchShareAttributes($item['body'] ?? ''); + if (!empty($attributes)) { + $shared = Post::selectFirst($fields, ['guid' => $attributes['guid'], 'uid' => [0, $item['uid'] ?? 0]]); + if (is_array($shared)) { + return [ + 'comment' => $attributes['comment'], + 'post' => $shared + ]; + } + } + + return []; + } } diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 7394f5fd7b..aa413cd022 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1070,6 +1070,30 @@ class BBCode return $attributes; } + /** + * Checks, if the provided body contains a native reshare + * + * @param string $body + * @return boolean + */ + public static function isNativeReshare(string $body): bool + { + $shared = BBCode::fetchShareAttributes($body); + return !empty($shared['guid'] ?? ''); + } + + /** + * Checks if the provided body contains a "share" element + * + * @param string $body + * @return boolean + */ + public static function existsShare(string $body): bool + { + $shared = BBCode::fetchShareAttributes($body); + return !empty($shared['link'] ?? ''); + } + /** * Replace the share block with a link * @@ -1094,7 +1118,7 @@ class BBCode */ public static function removeSharedData(string $body): string { - return preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body); + return trim(preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body)); } /** diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 4e1f790f50..caa219a237 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -23,9 +23,9 @@ namespace Friendica\Factory\Api\Mastodon; use Friendica\BaseFactory; use Friendica\Content\ContactSelector; -use Friendica\Content\Text\BBCode; use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\DI; use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\Tag as TagModel; @@ -155,11 +155,9 @@ class Status extends BaseFactory $poll = null; } - $shared = Item::getShareArray($item); - if (!empty($shared['guid'])) { - $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]); - - $shared_uri_id = $shared_item['uri-id'] ?? 0; + $shared = DI::contentItem()->getSharedPost($item, ['uri-id']); + if (!empty($shared)) { + $shared_uri_id = $shared['post']['uri-id']; $mentions = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy()); $tags = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id)); diff --git a/src/Factory/Api/Twitter/Status.php b/src/Factory/Api/Twitter/Status.php index ac03172e89..33abfdbcc5 100644 --- a/src/Factory/Api/Twitter/Status.php +++ b/src/Factory/Api/Twitter/Status.php @@ -25,6 +25,7 @@ use Friendica\BaseFactory; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; use Friendica\Database\Database; +use Friendica\DI; use Friendica\Factory\Api\Friendica\Activities; use Friendica\Factory\Api\Twitter\User as TwitterUser; use Friendica\Model\Item; @@ -178,11 +179,9 @@ class Status extends BaseFactory $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid); - $shared = Item::getShareArray($item); - if (!empty($shared['guid'])) { - $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]); - - $shared_uri_id = $shared_item['uri-id'] ?? 0; + $shared = DI::contentItem()->getSharedPost($item, ['uri-id']); + if (!empty($shared)) { + $shared_uri_id = $shared['post']['uri-id']; if ($include_entities) { $hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text)); diff --git a/src/Model/Item.php b/src/Model/Item.php index 5eb3ba6a5d..ced18fc8a6 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2949,20 +2949,18 @@ class Item $item['mentions'] = $tags['mentions']; $body = $item['body'] ?? ''; - $shared = self::getShareArray($item); - if (!empty($shared['guid'])) { - $shared_item = Post::selectFirst(['uri-id', 'guid', 'plink', 'has-media'], ['guid' => $shared['guid'], 'uid' => [$item['uid'], 0]]); - } $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'has-media']; $shared_uri_id = 0; $shared_links = []; - if (empty($shared_item['uri-id']) && !empty($item['quote-uri-id'])) { - $shared_item = Post::selectFirst($fields, ['uri-id' => $item['quote-uri-id']]); - $quote_uri_id = $item['quote-uri-id'] ?? 0; - $shared_links[] = strtolower($item['quote-uri']); + $shared = DI::contentItem()->getSharedPost($item, $fields); + if (!empty($shared['post'])) { + $shared_item = $shared['post']; + $quote_uri_id = $shared['post']['uri-id']; + $shared_links[] = strtolower($shared['post']['uri']); + $item['body'] = BBCode::removeSharedData($item['body']); } elseif (empty($shared_item['uri-id']) && empty($item['quote-uri-id'])) { $media = Post\Media::getByURIId($item['uri-id'], [Post\Media::ACTIVITY]); if (!empty($media)) { @@ -3588,43 +3586,6 @@ class Item return 0; } - /** - * Return share data from an item array (if the item is shared item) - * We are providing the complete Item array, because at some time in the future - * we hopefully will define these values not in the body anymore but in some item fields. - * This function is meant to replace all similar functions in the system. - * - * @param array $item - * - * @return array with share information - */ - public static function getShareArray(array $item): array - { - $attributes = BBCode::fetchShareAttributes($item['body'] ?? ''); - if (!empty($attributes)) { - return $attributes; - } - - if (!empty($item['quote-uri-id'])) { - $shared = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'uri', 'body'], ['uri-id' => $item['quote-uri-id']]); - if (!empty($shared)) { - return [ - 'author' => $shared['author-name'], - 'profile' => $shared['author-link'], - 'avatar' => $shared['author-avatar'], - 'link' => $shared['plink'], - 'posted' => $shared['created'], - 'guid' => $shared['guid'], - 'message_id' => $shared['uri'], - 'comment' => $item['body'], - 'shared' => $shared['body'], - ]; - } - } - - return []; - } - /** * Check a prospective item array against user-level permissions * diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index 8d86bc757c..37603ad372 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -404,8 +404,7 @@ class Media $unshared_body = $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body); // Only remove the shared data from "real" reshares - $shared = BBCode::fetchShareAttributes($body); - if (!empty($shared['guid'])) { + if (BBCode::isNativeReshare($body)) { $unshared_body = BBCode::removeSharedData($body); } @@ -486,8 +485,7 @@ class Media public static function insertFromRelevantUrl(int $uriid, string $body) { // Only remove the shared data from "real" reshares - $shared = BBCode::fetchShareAttributes($body); - if (!empty($shared['guid'])) { + if (BBCode::isNativeReshare($body)) { // Don't look at the shared content $body = BBCode::removeSharedData($body); } diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index cc7774b23a..7cb1eecf50 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1664,18 +1664,18 @@ class Transmitter $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body); } - $body = BBCode::setMentionsToNicknames($body); - $shared = BBCode::fetchShareAttributes($body); + $body = BBCode::setMentionsToNicknames($body); + $exists_reshare = BBCode::existsShare($body); if (!empty($item['quote-uri']) && Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) { $real_quote = true; - if (!empty($shared['link'])) { + if ($exists_reshare) { $body = BBCode::replaceSharedData($body); } elseif (strpos($body, $item['quote-uri']) === false) { $body .= "\n♲ " . $item['quote-uri']; } $data['quoteUrl'] = $item['quote-uri']; - } elseif (!empty($item['quote-uri']) && empty($shared)) { + } elseif (!empty($item['quote-uri']) && !$exists_reshare) { $body .= "\n" . DI::contentItem()->createSharedPostByUriId($item['quote-uri-id'], $item['uid'], true); $item['body'] = Item::improveSharedDataInBody($item, true); } @@ -1691,8 +1691,7 @@ class Transmitter $richbody = BBCode::setMentionsToNicknames($item['body'] ?? ''); if ($real_quote) { - $shared = BBCode::fetchShareAttributes($richbody); - if (!empty($shared['link'])) { + if (BBCode::existsShare($richbody)) { $richbody = BBCode::replaceSharedData($richbody); } elseif (strpos($richbody, $item['quote-uri']) === false) { $richbody .= "\n♲ " . $item['quote-uri']; @@ -1821,28 +1820,23 @@ class Transmitter * @param array $item * @return array Announcement array */ - public static function getAnnounceArray(array $item): array + private static function getAnnounceArray(array $item): array { - $reshared = Item::getShareArray($item); - if (empty($reshared['guid'])) { + $reshared = DI::contentItem()->getSharedPost($item, Item::DELIVER_FIELDLIST); + if (empty($reshared)) { return []; } - $reshared_item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['guid' => $reshared['guid']]); - if (!DBA::isResult($reshared_item)) { + if (!in_array($reshared['post']['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) { return []; } - if (!in_array($reshared_item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) { - return []; - } - - $profile = APContact::getByURL($reshared_item['author-link'], false); + $profile = APContact::getByURL($reshared['post']['author-link'], false); if (empty($profile)) { return []; } - return ['object' => $reshared_item, 'actor' => $profile, 'comment' => $reshared['comment']]; + return ['object' => $reshared['post'], 'actor' => $profile, 'comment' => $reshared['comment']]; } /** diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 389bbc55d2..bd5da5445a 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -3181,27 +3181,20 @@ class Diaspora */ public static function getReshareDetails(array $item): array { - $reshared = Item::getShareArray($item); + $reshared = DI::contentItem()->getSharedPost($item, ['network', 'author-addr']); if (empty($reshared)) { return []; } // Skip if it isn't a pure repeated messages or not a real reshare - if (!empty($reshared['comment']) || empty($reshared['guid'])) { + if (!empty($reshared['comment']) || !in_array($reshared['post']['network'], [Protocol::DFRN, Protocol::DIASPORA])) { return []; } - $condition = ['guid' => $reshared['guid'], 'network' => [Protocol::DFRN, Protocol::DIASPORA]]; - $item = Post::selectFirst(['author-addr'], $condition); - if (DBA::isResult($item)) { - return [ - 'root_handle' => strtolower($item['author-addr']), - 'root_guid' => $reshared['guid'] - ]; - } - - // We are resharing something that isn't a DFRN or Diaspora post. - return []; + return [ + 'root_handle' => strtolower($reshared['post']['author-addr']), + 'root_guid' => $reshared['guid'] + ]; } /** From deccea02289354d2d870d4f5865f0171a58011c0 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 27 Oct 2022 05:44:44 +0000 Subject: [PATCH 2/3] More functions moved to content class --- mod/item.php | 4 +- src/Content/Item.php | 71 +++++++++++++++++++++++- src/Database/PostUpdate.php | 2 +- src/Factory/Api/Mastodon/Status.php | 10 +++- src/Factory/Api/Twitter/Status.php | 9 ++- src/Model/Item.php | 30 ---------- src/Model/Tag.php | 40 ++++++++----- src/Protocol/ActivityPub/Processor.php | 2 +- src/Protocol/ActivityPub/Transmitter.php | 2 +- src/Protocol/DFRN.php | 2 +- 10 files changed, 113 insertions(+), 59 deletions(-) diff --git a/mod/item.php b/mod/item.php index f30798fa38..a0bab514ec 100644 --- a/mod/item.php +++ b/mod/item.php @@ -611,7 +611,7 @@ function item_post(App $a) { $datarray['author-uri-id'] = ItemURI::getIdByURI($datarray['author-link']); $datarray['owner-updated'] = ''; $datarray['has-media'] = false; - $datarray['body'] = Item::improveSharedDataInBody($datarray); + $datarray['body'] = DI::contentItem()->improveSharedDataInBody($datarray); $o = DI::conversation()->create([array_merge($contact_record, $datarray)], 'search', false, true); @@ -652,7 +652,7 @@ function item_post(App $a) { } $datarray['uri-id'] = ItemURI::getIdByURI($datarray['uri']); - $datarray['body'] = Item::improveSharedDataInBody($datarray); + $datarray['body'] = DI::contentItem()->improveSharedDataInBody($datarray); if ($orig_post) { $fields = [ diff --git a/src/Content/Item.php b/src/Content/Item.php index f72493c17e..510bae9703 100644 --- a/src/Content/Item.php +++ b/src/Content/Item.php @@ -680,11 +680,11 @@ class Item $shared_content .= '[h3]' . $item['title'] . "[/h3]\n"; } - $shared = $this->getSharedPost($item, ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network']); + $shared = $this->getShareArray($item); // If it is a reshared post then reformat it to avoid display problems with two share elements if (!empty($shared)) { - if (($encaspulated_share = $this->createSharedBlockByArray($shared['post'], $add_media))) { + if (!empty($shared['guid']) && ($encaspulated_share = $this->createSharedPostByGuid($shared['guid'], 0, '', $add_media))) { $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $encaspulated_share, $item['body']); } @@ -729,4 +729,71 @@ class Item return []; } + + /** + * Improve the data in shared posts + * + * @param array $item + * @param bool $add_media + * @return string body + */ + public function improveSharedDataInBody(array $item, bool $add_media = false): string + { + $shared = BBCode::fetchShareAttributes($item['body']); + if (empty($shared['guid']) && empty($shared['message_id'])) { + return $item['body']; + } + + $link = $shared['link'] ?: $shared['message_id']; + + if (empty($shared_content)) { + $shared_content = $this->createSharedPostByUrl($link, $item['uid'] ?? 0, $add_media); + } + + if (empty($shared_content)) { + return $item['body']; + } + + $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content, $item['body']); + + Logger::debug('New shared data', ['uri-id' => $item['uri-id'], 'link' => $link, 'guid' => $item['guid']]); + return $item['body']; + } + + /** + * Return share data from an item array (if the item is shared item) + * We are providing the complete Item array, because at some time in the future + * we hopefully will define these values not in the body anymore but in some item fields. + * This function is meant to replace all similar functions in the system. + * + * @param array $item + * + * @return array with share information + */ + private function getShareArray(array $item): array + { + $attributes = BBCode::fetchShareAttributes($item['body'] ?? ''); + if (!empty($attributes)) { + return $attributes; + } + + if (!empty($item['quote-uri-id'])) { + $shared = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'uri', 'body'], ['uri-id' => $item['quote-uri-id']]); + if (!empty($shared)) { + return [ + 'author' => $shared['author-name'], + 'profile' => $shared['author-link'], + 'avatar' => $shared['author-avatar'], + 'link' => $shared['plink'], + 'posted' => $shared['created'], + 'guid' => $shared['guid'], + 'message_id' => $shared['uri'], + 'comment' => $item['body'], + 'shared' => $shared['body'], + ]; + } + } + + return []; + } } diff --git a/src/Database/PostUpdate.php b/src/Database/PostUpdate.php index 04c218ca00..adc88b13e8 100644 --- a/src/Database/PostUpdate.php +++ b/src/Database/PostUpdate.php @@ -359,7 +359,7 @@ class PostUpdate } } - Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url'], false); + Tag::store($term['uri-id'], $term['type'], $term['term'], $term['url']); $id = $term['tid']; ++$rows; diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index caa219a237..9ff2211907 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -23,9 +23,9 @@ namespace Friendica\Factory\Api\Mastodon; use Friendica\BaseFactory; use Friendica\Content\ContactSelector; +use Friendica\Content\Item as ContentItem; use Friendica\Database\Database; use Friendica\Database\DBA; -use Friendica\DI; use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\Tag as TagModel; @@ -54,11 +54,14 @@ class Status extends BaseFactory private $mstdnErrorFactory; /** @var Poll */ private $mstdnPollFactory; + /** @var ContentItem */ + private $contentItem; public function __construct(LoggerInterface $logger, Database $dba, Account $mstdnAccountFactory, Mention $mstdnMentionFactory, Tag $mstdnTagFactory, Card $mstdnCardFactory, - Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory, Poll $mstdnPollFactory) + Attachment $mstdnAttachementFactory, Error $mstdnErrorFactory, + Poll $mstdnPollFactory, ContentItem $contentItem) { parent::__construct($logger); $this->dba = $dba; @@ -69,6 +72,7 @@ class Status extends BaseFactory $this->mstdnAttachementFactory = $mstdnAttachementFactory; $this->mstdnErrorFactory = $mstdnErrorFactory; $this->mstdnPollFactory = $mstdnPollFactory; + $this->contentItem = $contentItem; } /** @@ -155,7 +159,7 @@ class Status extends BaseFactory $poll = null; } - $shared = DI::contentItem()->getSharedPost($item, ['uri-id']); + $shared = $this->contentItem->getSharedPost($item, ['uri-id']); if (!empty($shared)) { $shared_uri_id = $shared['post']['uri-id']; diff --git a/src/Factory/Api/Twitter/Status.php b/src/Factory/Api/Twitter/Status.php index 33abfdbcc5..55f843ed28 100644 --- a/src/Factory/Api/Twitter/Status.php +++ b/src/Factory/Api/Twitter/Status.php @@ -22,10 +22,10 @@ namespace Friendica\Factory\Api\Twitter; use Friendica\BaseFactory; +use Friendica\Content\Item as ContentItem; use Friendica\Content\Text\BBCode; use Friendica\Content\Text\HTML; use Friendica\Database\Database; -use Friendica\DI; use Friendica\Factory\Api\Friendica\Activities; use Friendica\Factory\Api\Twitter\User as TwitterUser; use Friendica\Model\Item; @@ -54,8 +54,10 @@ class Status extends BaseFactory private $activities; /** @var Activities entity */ private $attachment; + /** @var ContentItem */ + private $contentItem; - public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities, Attachment $attachment) + public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities, Attachment $attachment, ContentItem $contentItem) { parent::__construct($logger); $this->dba = $dba; @@ -66,6 +68,7 @@ class Status extends BaseFactory $this->mention = $mention; $this->activities = $activities; $this->attachment = $attachment; + $this->contentItem = $contentItem; } /** @@ -179,7 +182,7 @@ class Status extends BaseFactory $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid); - $shared = DI::contentItem()->getSharedPost($item, ['uri-id']); + $shared = $this->contentItem->getSharedPost($item, ['uri-id']); if (!empty($shared)) { $shared_uri_id = $shared['post']['uri-id']; diff --git a/src/Model/Item.php b/src/Model/Item.php index ced18fc8a6..e1e4883da5 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3623,36 +3623,6 @@ class Item return true; } - /** - * Improve the data in shared posts - * - * @param array $item - * @param bool $add_media - * @return string body - */ - public static function improveSharedDataInBody(array $item, bool $add_media = false): string - { - $shared = BBCode::fetchShareAttributes($item['body']); - if (empty($shared['guid']) && empty($shared['message_id'])) { - return $item['body']; - } - - $link = $shared['link'] ?: $shared['message_id']; - - if (empty($shared_content)) { - $shared_content = DI::contentItem()->createSharedPostByUrl($link, $item['uid'] ?? 0, $add_media); - } - - if (empty($shared_content)) { - return $item['body']; - } - - $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content, $item['body']); - - Logger::debug('New shared data', ['uri-id' => $item['uri-id'], 'link' => $link, 'guid' => $item['guid']]); - return $item['body']; - } - /** * Fetch the uri-id of a quote * diff --git a/src/Model/Tag.php b/src/Model/Tag.php index 6611245a0c..2f034e92b7 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -257,17 +257,16 @@ class Tag * @param string $hash * @param string $name * @param string $url - * @param boolean $probing Whether probing is active * @return void */ - public static function storeByHash(int $uriId, string $hash, string $name, string $url = '', bool $probing = true) + public static function storeByHash(int $uriId, string $hash, string $name, string $url = '') { $type = self::getTypeForHash($hash); if ($type == self::UNKNOWN) { return; } - self::store($uriId, $type, $name, $url, $probing); + self::store($uriId, $type, $name, $url); } /** @@ -297,32 +296,43 @@ class Tag * @param integer $uriId URI-Id * @param string $body Body of the post * @param string $tags Accepted tags - * @param boolean $probing Perform a probing for contacts, adding them if needed * @return void */ - public static function storeFromBody(int $uriId, string $body, string $tags = null, bool $probing = true) + public static function storeFromBody(int $uriId, string $body, string $tags = null) { - Logger::info('Check for tags', ['uri-id' => $uriId, 'hash' => $tags, 'callstack' => System::callstack()]); + $item = ['uri-id' => $uriId, 'body' => $body, 'quote-uri-id' => null]; + self::storeFromArray($item, $tags); + } + + /** + * Store tags and mentions from the item array + * + * @param array $item Item array + * @param string $tags Accepted tags + * @return void + */ + public static function storeFromArray(array $item, string $tags = null) + { + Logger::info('Check for tags', ['uri-id' => $item['uri-id'], 'hash' => $tags, 'callstack' => System::callstack()]); if (is_null($tags)) { $tags = self::TAG_CHARACTER[self::HASHTAG] . self::TAG_CHARACTER[self::MENTION] . self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]; } // Only remove the shared data from "real" reshares - $shared = BBCode::fetchShareAttributes($body); - if (!empty($shared['guid'])) { - $share_body = $shared['shared']; - $body = BBCode::removeSharedData($body); + $shared = DI::contentItem()->getSharedPost($item, ['uri-id']); + if (!empty($shared)) { + $item['body'] = BBCode::removeSharedData($item['body']); } - foreach (self::getFromBody($body, $tags) as $tag) { - self::storeByHash($uriId, $tag[1], $tag[3], $tag[2], $probing); + foreach (self::getFromBody($item['body'], $tags) as $tag) { + self::storeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]); } // Search for hashtags in the shared body (but only if hashtags are wanted) - if (!empty($share_body) && (strpos($tags, self::TAG_CHARACTER[self::HASHTAG]) !== false)) { - foreach (self::getFromBody($share_body, self::TAG_CHARACTER[self::HASHTAG]) as $tag) { - self::storeByHash($uriId, $tag[1], $tag[3], $tag[2], $probing); + if (!empty($shared) && (strpos($tags, self::TAG_CHARACTER[self::HASHTAG]) !== false)) { + foreach (self::getByURIId($shared['post']['uri-id'], [self::HASHTAG]) as $tag) { + self::store($item['uri-id'], $tag['type'], $tag['name'], $tag['url']); } } } diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 79626abbb8..38361ec069 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -830,7 +830,7 @@ class Processor if (!empty($activity['source'])) { $item['body'] = $activity['source']; $item['raw-body'] = $content; - $item['body'] = Item::improveSharedDataInBody($item); + $item['body'] = DI::contentItem()->improveSharedDataInBody($item); } else { $parent_uri = $item['parent-uri'] ?? $item['thr-parent']; if (empty($activity['directmessage']) && ($parent_uri != $item['uri']) && ($item['gravity'] == Item::GRAVITY_COMMENT)) { diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 7cb1eecf50..afc2d5eac0 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1677,7 +1677,7 @@ class Transmitter $data['quoteUrl'] = $item['quote-uri']; } elseif (!empty($item['quote-uri']) && !$exists_reshare) { $body .= "\n" . DI::contentItem()->createSharedPostByUriId($item['quote-uri-id'], $item['uid'], true); - $item['body'] = Item::improveSharedDataInBody($item, true); + $item['body'] = DI::contentItem()->improveSharedDataInBody($item, true); } $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB); diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 8fe3c5556e..f626582bab 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -1838,7 +1838,7 @@ class DFRN $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]); - $item['body'] = Item::improveSharedDataInBody($item); + $item['body'] = DI::contentItem()->improveSharedDataInBody($item); Tag::storeFromBody($item['uri-id'], $item['body']); From e31533d0d3a620704dc94dc16fbe6a1b06d72943 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 27 Oct 2022 06:31:09 +0000 Subject: [PATCH 3/3] Possibly fix test --- tests/src/Factory/Api/Twitter/StatusTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Factory/Api/Twitter/StatusTest.php b/tests/src/Factory/Api/Twitter/StatusTest.php index f608b66bb1..6c2e856cc8 100644 --- a/tests/src/Factory/Api/Twitter/StatusTest.php +++ b/tests/src/Factory/Api/Twitter/StatusTest.php @@ -49,7 +49,7 @@ class StatusTest extends FixtureTest new Url(DI::logger()), new Mention(DI::logger(), DI::baseUrl()), new Activities(DI::logger(), DI::twitterUser()), - new Attachment(DI::logger())); + new Attachment(DI::logger()), DI::contentItem()); } /**