3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Factory\Api\Twitter;
24 use Friendica\BaseFactory;
25 use Friendica\Content\Item as ContentItem;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Content\Text\HTML;
28 use Friendica\Database\Database;
29 use Friendica\Factory\Api\Friendica\Activities;
30 use Friendica\Factory\Api\Twitter\User as TwitterUser;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post;
33 use Friendica\Model\Verb;
34 use Friendica\Network\HTTPException;
35 use Friendica\Protocol\Activity;
37 use Psr\Log\LoggerInterface;
39 class Status extends BaseFactory
43 /** @var twitterUser entity */
45 /** @var Hashtag entity */
47 /** @var Media entity */
49 /** @var Url entity */
51 /** @var Mention entity */
53 /** @var Activities entity */
55 /** @var Activities entity */
57 /** @var ContentItem */
60 public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser, Hashtag $hashtag, Media $media, Url $url, Mention $mention, Activities $activities, Attachment $attachment, ContentItem $contentItem)
62 parent::__construct($logger);
64 $this->twitterUser = $twitteruser;
65 $this->hashtag = $hashtag;
66 $this->media = $media;
68 $this->mention = $mention;
69 $this->activities = $activities;
70 $this->attachment = $attachment;
71 $this->contentItem = $contentItem;
75 * @param int $uriId Uri-ID of the item
76 * @param int $uid Item user
77 * @param bool $include_entities Whether to include entities
79 * @return \Friendica\Object\Api\Twitter\Status
80 * @throws HTTPException\InternalServerErrorException
81 * @throws ImagickException|HTTPException\NotFoundException
83 public function createFromItemId(int $id, int $uid, bool $include_entities = false): \Friendica\Object\Api\Twitter\Status
85 $fields = ['parent-uri-id', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'causer-id',
86 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network','post-reason', 'language', 'gravity',
87 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'uri', 'plink', 'private', 'vid', 'coord', 'quote-uri-id'];
88 $item = Post::selectFirst($fields, ['id' => $id], ['order' => ['uid' => true]]);
90 throw new HTTPException\NotFoundException('Item with ID ' . $id . ' not found.');
92 return $this->createFromArray($item, $uid, $include_entities);
96 * @param int $uriId Uri-ID of the item
97 * @param int $uid Item user
98 * @param bool $include_entities Whether to include entities
100 * @return \Friendica\Object\Api\Twitter\Status
101 * @throws HTTPException\InternalServerErrorException
102 * @throws ImagickException|HTTPException\NotFoundException
104 public function createFromUriId(int $uriId, int $uid = 0, bool $include_entities = false): \Friendica\Object\Api\Twitter\Status
106 $fields = ['parent-uri-id', 'uri-id', 'uid', 'author-id', 'author-link', 'author-network', 'owner-id', 'causer-id',
107 'starred', 'app', 'title', 'body', 'raw-body', 'created', 'network','post-reason', 'language', 'gravity',
108 'thr-parent-id', 'parent-author-id', 'parent-author-nick', 'uri', 'plink', 'private', 'vid', 'coord'];
109 $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]);
111 throw new HTTPException\NotFoundException('Item with URI ID ' . $uriId . ' not found' . ($uid ? ' for user ' . $uid : '.'));
113 return $this->createFromArray($item, $uid, $include_entities);
117 * @param array $item item array
118 * @param int $uid Item user
119 * @param bool $include_entities Whether to include entities
121 * @return \Friendica\Object\Api\Twitter\Status
122 * @throws HTTPException\InternalServerErrorException
123 * @throws ImagickException|HTTPException\NotFoundException
125 private function createFromArray(array $item, int $uid, bool $include_entities): \Friendica\Object\Api\Twitter\Status
127 $item = Post\Media::addHTMLAttachmentToItem($item);
128 $author = $this->twitterUser->createFromContactId($item['author-id'], $uid, true);
130 if (!empty($item['causer-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT)) {
131 $owner = $this->twitterUser->createFromContactId($item['causer-id'], $uid, true);
133 $owner = $this->twitterUser->createFromContactId($item['owner-id'], $uid, true);
136 $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => Item::GRAVITY_COMMENT]);
141 // Add the title to text / html if set
142 if (!empty($item['title'])) {
143 $text .= $item['title'] . ' ';
144 $title = sprintf("[h4]%s[/h4]\n", $item['title']);
147 $statusnetHtml = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($title . ($item['raw-body'] ?? $item['body'])), BBCode::TWITTER_API);
148 $friendicaHtml = BBCode::convertForUriId($item['uri-id'], $title . $item['body'], BBCode::EXTERNAL);
150 $text .= Post\Media::addAttachmentsToBody($item['uri-id'], $this->contentItem->addSharedPost($item));
152 $text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $text, BBCode::TWITTER_API), 0));
156 if ($item['coord'] != '') {
157 $coords = explode(' ', $item["coord"]);
158 if (count($coords) == 2) {
161 'coordinates' => [(float) $coords[0], (float) $coords[1]]
166 $liked = Post::exists([
167 'thr-parent-id' => $item['uri-id'],
170 'gravity' => Item::GRAVITY_ACTIVITY,
171 'vid' => Verb::getID(Activity::LIKE),
175 if ($include_entities) {
176 $hashtags = $this->hashtag->createFromUriId($item['uri-id'], $text);
177 $medias = $this->media->createFromUriId($item['uri-id'], $text);
178 $urls = $this->url->createFromUriId($item['uri-id'], $text);
179 $mentions = $this->mention->createFromUriId($item['uri-id'], $text);
181 $attachments = $this->attachment->createFromUriId($item['uri-id'], $text);
184 $friendica_activities = $this->activities->createFromUriId($item['uri-id'], $uid);
186 $shared = $this->contentItem->getSharedPost($item, ['uri-id']);
187 if (!empty($shared)) {
188 $shared_uri_id = $shared['post']['uri-id'];
190 if ($include_entities) {
191 $hashtags = array_merge($hashtags, $this->hashtag->createFromUriId($shared_uri_id, $text));
192 $medias = array_merge($medias, $this->media->createFromUriId($shared_uri_id, $text));
193 $urls = array_merge($urls, $this->url->createFromUriId($shared_uri_id, $text));
194 $mentions = array_merge($mentions, $this->mention->createFromUriId($shared_uri_id, $text));
196 $attachments = array_merge($attachments, $this->attachment->createFromUriId($shared_uri_id, $text));
200 if ($item['vid'] == Verb::getID(Activity::ANNOUNCE)) {
201 $retweeted = $this->createFromUriId($item['thr-parent-id'], $uid)->toArray();
202 $retweeted_item = Post::selectFirst(['title', 'body', 'author-id'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $uid]]);
203 $item['title'] = $retweeted_item['title'] ?? $item['title'];
204 $item['body'] = $retweeted_item['body'] ?? $item['body'];
205 $author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $uid, true);
210 $quoted = []; // @todo
212 if ($include_entities) {
213 $entities = ['hashtags' => $hashtags, 'media' => $medias, 'urls' => $urls, 'user_mentions' => $mentions];
219 return new \Friendica\Object\Api\Twitter\Status($text, $statusnetHtml, $friendicaHtml, $item, $author, $owner, $retweeted, $quoted, $geo, $friendica_activities, $entities, $attachments, $friendica_comments, $liked);