From 670bbe58a1cddabb622387a8926eb39023cc8a00 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 22 Nov 2021 07:28:02 +0000 Subject: [PATCH] API: The status is now an object --- database.sql | 3 +- src/DI.php | 8 ++ src/Factory/Api/Twitter/Status.php | 108 +++++++++++++++++++ src/Object/Api/Twitter/Status.php | 160 +++++++++++++++++++++++++++++ static/dbstructure.config.php | 2 +- static/dbview.config.php | 1 + 6 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 src/Factory/Api/Twitter/Status.php create mode 100644 src/Object/Api/Twitter/Status.php diff --git a/database.sql b/database.sql index fbccddcb28..ef9d08d217 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.12-dev (Siberian Iris) --- DB_UPDATE_VERSION 1443 +-- DB_UPDATE_VERSION 1444 -- ------------------------------------------ @@ -1701,6 +1701,7 @@ CREATE VIEW `post-user-view` AS SELECT `parent-post`.`author-id` AS `parent-author-id`, `parent-post-author`.`url` AS `parent-author-link`, `parent-post-author`.`name` AS `parent-author-name`, + `parent-post-author`.`nick` AS `parent-author-nick`, `parent-post-author`.`network` AS `parent-author-network`, `parent-post-author`.`blocked` AS `parent-author-blocked`, `parent-post-author`.`hidden` AS `parent-author-hidden` diff --git a/src/DI.php b/src/DI.php index 0c134f37b4..1531988aaf 100644 --- a/src/DI.php +++ b/src/DI.php @@ -382,6 +382,14 @@ abstract class DI return self::$dice->create(Factory\Api\Mastodon\Notification::class); } + /** + * @return Factory\Api\Twitter\Status + */ + public static function twitterStatus() + { + return self::$dice->create(Factory\Api\Twitter\Status::class); + } + /** * @return Factory\Api\Twitter\User */ diff --git a/src/Factory/Api/Twitter/Status.php b/src/Factory/Api/Twitter/Status.php new file mode 100644 index 0000000000..96114eb545 --- /dev/null +++ b/src/Factory/Api/Twitter/Status.php @@ -0,0 +1,108 @@ +. + * + */ + +namespace Friendica\Factory\Api\Twitter; + +use Friendica\BaseFactory; +use Friendica\Content\Text\BBCode; +use Friendica\Database\Database; +use Friendica\Factory\Api\Twitter\User as TwitterUser; +use Friendica\Model\Post; +use Friendica\Model\Verb; +use Friendica\Network\HTTPException; +use Friendica\Protocol\Activity; +use ImagickException; +use Psr\Log\LoggerInterface; + +class Status extends BaseFactory +{ + /** @var Database */ + private $dba; + /** @var TwitterUser */ + private $twitterUser; + + public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser) + { + parent::__construct($logger); + $this->dba = $dba; + $this->twitterUser = $twitteruser; + } + + /** + * @param int $uriId Uri-ID of the item + * @param int $uid Item user + * + * @return \Friendica\Object\Api\Mastodon\Status + * @throws HTTPException\InternalServerErrorException + * @throws ImagickException|HTTPException\NotFoundException + */ + public function createFromUriId(int $uriId, $uid = 0): \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']; + $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 : '.')); + } + + $author = $this->twitterUser->createFromContactId($item['author-id'], $item['uid']); + $owner = $this->twitterUser->createFromContactId($item['owner-id'], $item['uid']); + + $friendica_comments = Post::countPosts(['thr-parent-id' => $item['uri-id'], 'deleted' => false, 'gravity' => GRAVITY_COMMENT]); + + $geo = []; + + //$mentions = $this->mstdnMentionFactory->createFromUriId($uriId)->getArrayCopy(); + //$tags = $this->mstdnTagFactory->createFromUriId($uriId); + //$attachments = $this->mstdnAttachementFactory->createFromUriId($uriId); + $entities = []; + $attachments = []; + $friendica_activities = []; + + $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; + + //$mentions = array_merge($mentions, $this->mstdnMentionFactory->createFromUriId($shared_uri_id)->getArrayCopy()); + //$tags = array_merge($tags, $this->mstdnTagFactory->createFromUriId($shared_uri_id)); + //$attachments = array_merge($attachments, $this->mstdnAttachementFactory->createFromUriId($shared_uri_id)); + $entities = []; + $attachments = []; + $friendica_activities = []; + } + + 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]]); + $item['title'] = $retweeted_item['title'] ?? $item['title']; + $item['body'] = $retweeted_item['body'] ?? $item['body']; + $author = $this->twitterUser->createFromContactId($retweeted_item['author-id'], $item['uid']); + } else { + $retweeted = []; + } + + $quoted = []; + + return new \Friendica\Object\Api\Twitter\Status($item, $author, $owner, $retweeted, $quoted, $attachments, $geo, $friendica_activities, $entities, $friendica_comments); + } +} diff --git a/src/Object/Api/Twitter/Status.php b/src/Object/Api/Twitter/Status.php new file mode 100644 index 0000000000..ac4d467d55 --- /dev/null +++ b/src/Object/Api/Twitter/Status.php @@ -0,0 +1,160 @@ +. + * + */ + +namespace Friendica\Object\Api\Twitter; + +use Friendica\BaseDataTransferObject; +use Friendica\Content\ContactSelector; +use Friendica\Content\Text\BBCode; +use Friendica\Content\Text\HTML; +use Friendica\Model\Item; +use Friendica\Util\DateTimeFormat; + +/** + * Class Status + * + * @see https://docs.joinmastodon.org/entities/status + */ +class Status extends BaseDataTransferObject +{ + /** @var int */ + protected $id; + /** @var string */ + protected $id_str; + /** @var string (Datetime) */ + protected $created_at; + /** @var int|null */ + protected $in_reply_to_status_id = null; + /** @var string|null */ + protected $in_reply_to_status_id_str = null; + /** @var int|null */ + protected $in_reply_to_user_id = null; + /** @var string|null */ + protected $in_reply_to_user_id_str = null; + /** @var string|null */ + protected $in_reply_to_screen_name = null; + /** @var User */ + protected $user; + /** @var User */ + protected $friendica_author; + /** @var User */ + protected $friendica_owner; + /** @var bool */ + protected $favorited = false; + /** @var Status|null */ + protected $retweeted_status = null; + /** @var Status|null */ + protected $quoted_status = null; + /** @var string */ + protected $text; + /** @var string */ + protected $statusnet_html; + /** @var string */ + protected $friendica_html; + /** @var string */ + protected $friendica_title; + /** @var bool */ + protected $truncated; + /** @var int */ + protected $friendica_comments; + /** @var string */ + protected $source; + /** @var string */ + protected $external_url; + /** @var int */ + protected $statusnet_conversation_id; + /** @var bool */ + protected $friendica_private; + /** @var Attachment */ + protected $attachments = []; + protected $geo; + protected $friendica_activities; + protected $entities; + + /** + * Creates a status record from an item record. + * + * @param array $item + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public function __construct(array $item, User $author, User $owner, array $retweeted, array $quoted, array $attachments, array $geo, array $friendica_activities, array $entities, int $friendica_comments) + { + $this->id = (int)$item['id']; + $this->id_str = (string)$item['id']; + $this->statusnet_conversation_id = (int)$item['parent']; + + $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::API); + + if ($item['gravity'] == GRAVITY_COMMENT) { + $this->in_reply_to_status_id = (int)$item['thr-parent-id']; + $this->in_reply_to_status_id_str = (string)$item['thr-parent-id']; + $this->in_reply_to_user_id = (int)$item['parent-author-id']; + $this->in_reply_to_user_id_str = (string)$item['parent-author-id']; + $this->in_reply_to_screen_name = $item['parent-author-nick']; + } + + $this->text = trim(HTML::toPlaintext(BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::API), 0)); + $this->friendica_title = $item['title']; + $this->statusnet_html = BBCode::convertForUriId($item['uri-id'], BBCode::setMentionsToNicknames($item['raw-body'] ?? $item['body']), BBCode::API); + $this->friendica_html = BBCode::convertForUriId($item['uri-id'], $item['body'], BBCode::EXTERNAL); + $this->user = $author->toArray(); + $this->friendica_author = $author->toArray(); + $this->friendica_owner = $owner->toArray(); + $this->truncated = false; + $this->friendica_private = $item['private'] == Item::PRIVATE; + $this->retweeted_status = $retweeted; + $this->quoted_status = $quoted; + $this->external_url = $item['plink']; + $this->favorited = (bool)$item['starred']; + $this->friendica_comments = $friendica_comments; + $this->source = $item['app'] ?: 'web'; + $this->attachments = $attachments; + $this->geo = $geo; + $this->friendica_activities = $friendica_activities; + $this->entities = $entities; + + if ($this->source == 'web') { + $this->source = ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']); + } elseif (ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']) != $this->source) { + $this->source = trim($this->source. ' (' . ContactSelector::networkToName($item['author-network'], $item['author-link'], $item['network']) . ')'); + } + } + + /** + * Returns the current entity as an array + * + * @return array + */ + public function toArray(): array + { + $status = parent::toArray(); + + if (empty($status['retweeted_status'])) { + unset($status['retweeted_status']); + } + + if (empty($status['quoted_status'])) { + unset($status['quoted_status']); + } + + return $status; + } +} diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index b52f670648..5428b0bde3 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1443); + define('DB_UPDATE_VERSION', 1444); } return [ diff --git a/static/dbview.config.php b/static/dbview.config.php index a12d5747a5..3caa7c0877 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -193,6 +193,7 @@ "parent-author-id" => ["parent-post", "author-id"], "parent-author-link" => ["parent-post-author", "url"], "parent-author-name" => ["parent-post-author", "name"], + "parent-author-nick" => ["parent-post-author", "nick"], "parent-author-network" => ["parent-post-author", "network"], "parent-author-blocked" => ["parent-post-author", "blocked"], "parent-author-hidden" => ["parent-post-author", "hidden"],