X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FFactory%2FApi%2FTwitter%2FStatus.php;h=810b421cab68868f28bec257d158c0fd76e19560;hb=d402e666440248fbf139dcbde268609407666c7f;hp=cea2f7f92c3e36c533afdee69c83cf6ce83ae554;hpb=452cd57437f296ec048c66cb493ec02688ff629b;p=friendica.git diff --git a/src/Factory/Api/Twitter/Status.php b/src/Factory/Api/Twitter/Status.php index cea2f7f92c..810b421cab 100644 --- a/src/Factory/Api/Twitter/Status.php +++ b/src/Factory/Api/Twitter/Status.php @@ -1,6 +1,6 @@ dba = $dba; @@ -64,62 +68,88 @@ class Status extends BaseFactory $this->mention = $mention; $this->activities = $activities; $this->attachment = $attachment; + $this->contentItem = $contentItem; } /** * @param int $uriId Uri-ID of the item * @param int $uid Item user + * @param bool $include_entities Whether to include entities * - * @return \Friendica\Object\Api\Mastodon\Status + * @return \Friendica\Object\Api\Twitter\Status * @throws HTTPException\InternalServerErrorException * @throws ImagickException|HTTPException\NotFoundException */ - public function createFromItemId(int $id): \Friendica\Object\Api\Twitter\Status + public function createFromItemId(int $id, int $uid, bool $include_entities = false): \Friendica\Object\Api\Twitter\Status { - $fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network', - 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord']; + $fields = ['parent-uri-id', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'causer-id', + 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network','post-reason', 'language', 'gravity', + 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'uri', 'plink', 'private', 'vid', 'coord', 'quote-uri-id']; $item = Post::selectFirst($fields, ['id' => $id], ['order' => ['uid' => true]]); if (!$item) { throw new HTTPException\NotFoundException('Item with ID ' . $id . ' not found.'); } - return $this->createFromArray($item); + return $this->createFromArray($item, $uid, $include_entities); } /** * @param int $uriId Uri-ID of the item * @param int $uid Item user + * @param bool $include_entities Whether to include entities * - * @return \Friendica\Object\Api\Mastodon\Status + * @return \Friendica\Object\Api\Twitter\Status * @throws HTTPException\InternalServerErrorException * @throws ImagickException|HTTPException\NotFoundException */ - public function createFromUriId(int $uriId, $uid = 0): \Friendica\Object\Api\Twitter\Status + public function createFromUriId(int $uriId, int $uid = 0, bool $include_entities = false): \Friendica\Object\Api\Twitter\Status { - $fields = ['id', 'parent', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network', - 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'coord']; + $fields = ['parent-uri-id', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'causer-id', + 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network','post-reason', 'language', 'gravity', + 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'uri', 'plink', 'private', 'vid', 'coord']; $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]); if (!$item) { throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.')); } - return $this->createFromArray($item); + return $this->createFromArray($item, $uid, $include_entities); } /** * @param array $item item array * @param int $uid Item user + * @param bool $include_entities Whether to include entities * - * @return \Friendica\Object\Api\Mastodon\Status + * @return \Friendica\Object\Api\Twitter\Status * @throws HTTPException\InternalServerErrorException * @throws ImagickException|HTTPException\NotFoundException */ - private function createFromArray(array $item, $uid = 0): \Friendica\Object\Api\Twitter\Status + private function createFromArray(array $item, int $uid, bool $include_entities): \Friendica\Object\Api\Twitter\Status { - $author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid']); - $owner = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid']); + $item = Post\Media::addHTMLAttachmentToItem($item); + $author = $this->twitterUser->createFromContactId($item['author-id'], $uid, true); - $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]); + if (!empty($item['causer-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT)) { + $owner = $this->twitterUser->createFromContactId($item['causer-id'], $uid, true); + } else { + $owner = $this->twitterUser->createFromContactId($item['owner-id'], $uid, true); + } + + $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => Item::GRAVITY_COMMENT]); + + $text = ''; + $title = ''; + + // Add the title to text / html if set + if (!empty($item['title'])) { + $text .= $item['title'] . ' '; + $title = sprintf("[h4]%s[/h4]\n", $item['title']); + } + + $statusnetHtml = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($title . ($item['raw-body'] ?? $item['body'])), BBCode::TWITTER_API); + $friendicaHtml = BBCode::convertForUriId($item['uri-id'], $title . $item['body'], BBCode::EXTERNAL); + + $text .= Post\Media::addAttachmentsToBody($item['uri-id'], $this->contentItem->addSharedPost($item)); - $text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API), 0)); + $text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $text, BBCode::TWITTER_API), 0)); $geo = []; @@ -133,44 +163,59 @@ class Status extends BaseFactory } } - $hashtags = $this->hashtag->createFromUriId($item['uri-id'], $text); - $medias = $this->media->createFromUriId($item['uri-id'], $text); - $urls = $this->url->createFromUriId($item['uri-id'], $text); - $mentions = $this->mention->createFromUriId($item['uri-id'], $text); + $liked = Post::exists([ + 'thr-parent-id' => $item['uri-id'], + 'uid' => $uid, + 'origin' => true, + 'gravity' => Item::GRAVITY_ACTIVITY, + 'vid' => Verb::getID(Activity::LIKE), + 'deleted' => false + ]); + + if ($include_entities) { + $hashtags = $this->hashtag->createFromUriId($item['uri-id'], $text); + $medias = $this->media->createFromUriId($item['uri-id'], $text); + $urls = $this->url->createFromUriId($item['uri-id'], $text); + $mentions = $this->mention->createFromUriId($item['uri-id'], $text); + } else { + $attachments = $this->attachment->createFromUriId($item['uri-id'], $text); + } $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid); - $attachments = $this->attachment->createFromUriId($item['uri-id'], $text); - - $shared = BBCode::fetchShareAttributes($item['body']); - if (!empty($shared['guid'])) { - $shared_item = Post::selectFirst(['uri-id', 'plink'], ['guid' => $shared['guid']]); - - $shared_uri_id = $shared_item['uri-id'] ?? 0; - $hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text)); - $medias = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text)); - $urls = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text)); - $mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text)); - $attachments = array_merge($attachments, $this->attachment->createFromUriId($item['uri-id'], $text)); + $shared = $this->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)); + $medias = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text)); + $urls = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text)); + $mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text)); + } else { + $attachments = array_merge($attachments, $this->attachment->createFromUriId($shared_uri_id, $text)); + } } if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) { $retweeted = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray(); - $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'],'uid' => [0, $uid]]); + $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $uid]]); $item['title'] = $retweeted_item['title'] ?? $item['title']; $item['body'] = $retweeted_item['body'] ?? $item['body']; - $author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid']); + $author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $uid, true); } else { $retweeted = []; } $quoted = []; // @todo - $entities = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions]; - - // Attachments are currently deactivated for testing purposes - $attachments = []; + if ($include_entities) { + $entities = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions]; + $attachments = []; + } else { + $entities = []; + } - return new \Friendica\Object\Api\Twitter\Status($text, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments); + return new \Friendica\Object\Api\Twitter\Status($text, $statusnetHtml, $friendicaHtml, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments, $liked); } }