- /**
- * Fetches a message with a given guid
- *
- * @param string $guid message guid
- * @param string $orig_author handle of the original post
- * @return array|bool The fetched item or false on failure
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- * @throws \ImagickException
- */
- public static function originalItem(string $guid, string $orig_author)
- {
- if (empty($guid)) {
- Logger::notice('Empty guid. Quitting.');
- return false;
- }
-
- // Do we already have this item?
- $fields = ['body', 'title', 'app', 'created', 'object-type', 'uri', 'guid',
- 'author-name', 'author-link', 'author-avatar', 'plink', 'uri-id'];
- $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => [Item::PUBLIC, Item::UNLISTED]];
- $item = Post::selectFirst($fields, $condition);
-
- if (DBA::isResult($item)) {
- Logger::notice("reshared message " . $guid . " already exists on system.");
-
- // Maybe it is already a reshared item?
- // Then refetch the content, if it is a reshare from a reshare.
- // If it is a reshared post from another network then reformat to avoid display problems with two share elements
- if (self::isReshare($item['body'], true)) {
- $item = [];
- } elseif (self::isReshare($item['body'], false) || strstr($item['body'], '[share')) {
- $item['body'] = Markdown::toBBCode(BBCode::toMarkdown($item['body']));
-
- $item['body'] = self::replacePeopleGuid($item['body'], $item['author-link']);
-
- return $item;
- } else {
- return $item;
- }
- }
-
- if (!DBA::isResult($item)) {
- if (empty($orig_author)) {
- Logger::notice('Empty author for guid ' . $guid . '. Quitting.');
- return false;
- }
-
- $server = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1);
- Logger::notice('1st try: reshared message ' . $guid . ' will be fetched via SSL from the server ' . $server);
- $stored = self::storeByGuid($guid, $server, true);
-
- if (!$stored) {
- $server = 'http://' . substr($orig_author, strpos($orig_author, '@') + 1);
- Logger::notice('2nd try: reshared message ' . $guid . ' will be fetched without SSL from the server ' . $server);
- $stored = self::storeByGuid($guid, $server, true);
- }
-
- if ($stored) {
- $fields = ['body', 'title', 'app', 'created', 'object-type', 'uri', 'guid',
- 'author-name', 'author-link', 'author-avatar', 'plink', 'uri-id'];
- $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => [Item::PUBLIC, Item::UNLISTED]];
- $item = Post::selectFirst($fields, $condition);
-
- if (DBA::isResult($item)) {
- // If it is a reshared post from another network then reformat to avoid display problems with two share elements
- if (self::isReshare($item['body'], false)) {
- $item['body'] = Markdown::toBBCode(BBCode::toMarkdown($item['body']));
- $item['body'] = self::replacePeopleGuid($item['body'], $item['author-link']);
- }
-
- return $item;
- }
- }
- }
- return false;
- }
-
- /**
- * Stores a reshare activity
- *
- * @param array $item Array of reshare post
- * @param integer $parent_message_id Id of the parent post
- * @param string $guid GUID string of reshare action
- * @param string $author Author handle
- */
- private static function addReshareActivity(array $item, int $parent_message_id, string $guid, string $author)
- {
- $parent = Post::selectFirst(['uri', 'guid'], ['id' => $parent_message_id]);
-
- $datarray = [];
-
- $datarray['uid'] = $item['uid'];
- $datarray['contact-id'] = $item['contact-id'];
- $datarray['network'] = $item['network'];
-
- $datarray['author-link'] = $item['author-link'];
- $datarray['author-id'] = $item['author-id'];
-
- $datarray['owner-link'] = $datarray['author-link'];
- $datarray['owner-id'] = $datarray['author-id'];
-
- $datarray['guid'] = $parent['guid'] . '-' . $guid;
- $datarray['uri'] = self::getUriFromGuid($author, $datarray['guid']);
- $datarray['thr-parent'] = $parent['uri'];
-
- $datarray['verb'] = $datarray['body'] = Activity::ANNOUNCE;
- $datarray['gravity'] = GRAVITY_ACTIVITY;
- $datarray['object-type'] = Activity\ObjectType::NOTE;
-
- $datarray['protocol'] = $item['protocol'];
- $datarray['source'] = $item['source'];
- $datarray['direction'] = $item['direction'];
- $datarray['post-reason'] = $item['post-reason'];
-
- $datarray['plink'] = self::plink($author, $datarray['guid']);
- $datarray['private'] = $item['private'];
- $datarray['changed'] = $datarray['created'] = $datarray['edited'] = $item['created'];
-
- if (Item::isTooOld($datarray)) {
- Logger::info('Reshare activity is too old', ['created' => $datarray['created'], 'uid' => $datarray['uid'], 'guid' => $datarray['guid']]);
- return false;
- }
-
- $message_id = Item::insert($datarray);
-
- if ($message_id) {
- Logger::info('Stored reshare activity.', ['guid' => $guid, 'id' => $message_id]);
- if ($datarray['uid'] == 0) {
- Item::distribute($message_id);
- }
- }
- }
-