- /**
- * 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
- {
- return BBCode::fetchShareAttributes($item['body']);
- }
-
- /**
- * Fetch item information for shared items from the original items and adds it.
- *
- * @param array $item
- *
- * @return array item array with data from the original item
- */
- public static function addShareDataFromOriginal(array $item): array
- {
- $shared = self::getShareArray($item);
- if (empty($shared)) {
- return $item;
- }
-
- // Real reshares always have got a GUID.
- if (empty($shared['guid'])) {
- return $item;
- }
-
- $uid = $item['uid'] ?? 0;
-
- // first try to fetch the item via the GUID. This will work for all reshares that had been created on this system
- $shared_item = Post::selectFirst(['title', 'body'], ['guid' => $shared['guid'], 'uid' => [0, $uid]]);
- if (!DBA::isResult($shared_item)) {
- if (empty($shared['link'])) {
- return $item;
- }
-
- // Otherwhise try to find (and possibly fetch) the item via the link. This should work for Diaspora and ActivityPub posts
- $id = self::fetchByLink($shared['link'] ?? '', $uid);
- if (empty($id)) {
- Logger::info('Original item not found', ['url' => $shared['link'] ?? '', 'callstack' => System::callstack()]);
- return $item;
- }
-
- $shared_item = Post::selectFirst(['title', 'body'], ['id' => $id]);
- if (!DBA::isResult($shared_item)) {
- return $item;
- }
- Logger::info('Got shared data from url', ['url' => $shared['link'], 'callstack' => System::callstack()]);
- } else {
- Logger::info('Got shared data from guid', ['guid' => $shared['guid'], 'callstack' => System::callstack()]);
- }
-
- if (!empty($shared_item['title'])) {
- $body = '[h3]' . $shared_item['title'] . "[/h3]\n" . $shared_item['body'];
- unset($shared_item['title']);
- } else {
- $body = $shared_item['body'];
- }
-
- $item['body'] = preg_replace("/\[share ([^\[\]]*)\].*\[\/share\]/ism", '[share $1]' . str_replace('$', '\$', $body) . '[/share]', $item['body']);
- unset($shared_item['body']);
-
- return array_merge($item, $shared_item);
- }
-