Merge pull request #9458 from annando/mastodon-status

More data for the Mastodon status class
This commit is contained in:
Hypolite Petovan 2020-10-25 18:37:31 -04:00 committed by GitHub
commit e06160eeeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 489 additions and 20 deletions

View file

@ -279,6 +279,22 @@ abstract class DI
return self::$dice->create(Factory\Api\Mastodon\Status::class); return self::$dice->create(Factory\Api\Mastodon\Status::class);
} }
/**
* @return Factory\Api\Mastodon\Mention
*/
public static function mstdnMention()
{
return self::$dice->create(Factory\Api\Mastodon\Mention::class);
}
/**
* @return Factory\Api\Mastodon\Tag
*/
public static function mstdnTag()
{
return self::$dice->create(Factory\Api\Mastodon\Tag::class);
}
/** /**
* @return Factory\Api\Twitter\User * @return Factory\Api\Twitter\User
*/ */

View file

@ -69,7 +69,15 @@ class Account extends BaseFactory
$apcontact = APContact::getByURL($publicContact['url'], false); $apcontact = APContact::getByURL($publicContact['url'], false);
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, new Fields(), $apcontact, $userContact); $self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]);
if (!empty($self_contact['uid'])) {
$profileFields = $this->profileField->select(['uid' => $self_contact['uid'], 'psid' => PermissionSet::PUBLIC]);
$fields = $this->mstdnField->createFromProfileFields($profileFields);
} else {
$fields = new Fields();
}
return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact, $userContact);
} }
/** /**

View file

@ -37,7 +37,7 @@ class Field extends BaseFactory
*/ */
public function createFromProfileField(ProfileField $profileField) public function createFromProfileField(ProfileField $profileField)
{ {
return new \Friendica\Api\Entity\Mastodon\Field($profileField->label, BBCode::convert($profileField->value, false, BBCode::ACTIVITYPUB)); return new \Friendica\Object\Api\Mastodon\Field($profileField->label, BBCode::convert($profileField->value, false, BBCode::ACTIVITYPUB));
} }
/** /**

View file

@ -0,0 +1,67 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Model\Contact;
use Friendica\Model\Tag;
use Friendica\Network\HTTPException;
use Friendica\Repository\ProfileField;
use Psr\Log\LoggerInterface;
class Mention extends BaseFactory
{
/** @var BaseURL */
protected $baseUrl;
/** @var ProfileField */
protected $profileField;
/** @var Field */
protected $mstdnField;
public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
{
parent::__construct($logger);
$this->baseUrl = $baseURL;
$this->profileField = $profileField;
$this->mstdnField = $mstdnField;
}
/**
* @param int $uriId Uri-ID of the item
* @return array
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public function createFromUriId(int $uriId)
{
$mentions = [];
$tags = Tag::getByURIId($uriId, [Tag::MENTION, Tag::EXCLUSIVE_MENTION, Tag::IMPLICIT_MENTION]);
foreach ($tags as $tag) {
$contact = Contact::getByURL($tag['url'], false);
$mention = new \Friendica\Object\Api\Mastodon\Mention($this->baseUrl, $tag, $contact);
$mentions[] = $mention->toArray();
}
return $mentions;
}
}

View file

@ -23,6 +23,7 @@ namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL; use Friendica\App\BaseURL;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Content\Text\BBCode;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Item; use Friendica\Model\Item;
@ -68,6 +69,22 @@ class Status extends BaseFactory
DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]) DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)])
); );
return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts); $userAttributes = new \Friendica\Object\Api\Mastodon\Status\UserAttributes(
DBA::exists('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]),
DBA::exists('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'origin' => true, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]),
DBA::exists('thread', ['iid' => $item['id'], 'uid' => $item['uid'], 'ignored' => true]),
(bool)$item['starred'],
DBA::exists('user-item', ['iid' => $item['id'], 'uid' => $item['uid'], 'pinned' => true])
);
$sensitive = DBA::exists('tag-view', ['uri-id' => $uriId, 'name' => 'nsfw']);
$application = new \Friendica\Object\Api\Mastodon\Application($item['app']);
$mentions = DI::mstdnMention()->createFromUriId($uriId);
$tags = DI::mstdnTag()->createFromUriId($uriId);
$attachment = BBCode::getAttachmentData($item['body']);
$card = new \Friendica\Object\Api\Mastodon\Card($attachment);
return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card);
} }
} }

View file

@ -0,0 +1,65 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Factory\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseFactory;
use Friendica\Model\Tag as TagModel;
use Friendica\Network\HTTPException;
use Friendica\Repository\ProfileField;
use Psr\Log\LoggerInterface;
class Tag extends BaseFactory
{
/** @var BaseURL */
protected $baseUrl;
/** @var ProfileField */
protected $profileField;
/** @var Field */
protected $mstdnField;
public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
{
parent::__construct($logger);
$this->baseUrl = $baseURL;
$this->profileField = $profileField;
$this->mstdnField = $mstdnField;
}
/**
* @param int $uriId Uri-ID of the item
* @return array
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public function createFromUriId(int $uriId)
{
$hashtags = [];
$tags = TagModel::getByURIId($uriId, [TagModel::HASHTAG]);
foreach ($tags as $tag) {
$hashtag = new \Friendica\Object\Api\Mastodon\Tag($this->baseUrl, $tag);
$hashtags[] = $hashtag->toArray();
}
return $hashtags;
}
}

View file

@ -99,7 +99,7 @@ class Account extends BaseEntity
$publicContact['nick'] : $publicContact['nick'] :
$publicContact['addr']; $publicContact['addr'];
$this->display_name = $publicContact['name']; $this->display_name = $publicContact['name'];
$this->locked = $publicContact['manually-approve'] ?? !empty($apcontact['manually-approve']); $this->locked = (bool)$publicContact['manually-approve'] ?? !empty($apcontact['manually-approve']);
$this->bot = ($publicContact['contact-type'] == Contact::TYPE_NEWS); $this->bot = ($publicContact['contact-type'] == Contact::TYPE_NEWS);
$this->discoverable = !$publicContact['unsearchable']; $this->discoverable = !$publicContact['unsearchable'];
$this->group = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY); $this->group = ($publicContact['contact-type'] == Contact::TYPE_COMMUNITY);
@ -132,4 +132,20 @@ class Account extends BaseEntity
$this->fields = $fields->getArrayCopy(); $this->fields = $fields->getArrayCopy();
} }
/**
* Returns the current entity as an array
*
* @return array
*/
public function toArray()
{
$account = parent::toArray();
if (empty($account['moved'])) {
unset($account['moved']);
}
return $account;
}
} }

View file

@ -0,0 +1,76 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Object\Api\Mastodon;
use Friendica\BaseEntity;
use Friendica\Content\Text\BBCode;
use Friendica\Object\Api\Mastodon\Status\Counts;
use Friendica\Object\Api\Mastodon\Status\UserAttributes;
use Friendica\Util\DateTimeFormat;
/**
* Class Card
*
* @see https://docs.joinmastodon.org/entities/card
*/
class Card extends BaseEntity
{
/** @var string */
protected $url;
/** @var string */
protected $title;
/** @var string */
protected $description;
/** @var string */
protected $type;
/** @var string */
protected $image;
/**
* Creates a status record from an item record.
*
* @param array $attachment Attachment record
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(array $attachment)
{
$this->url = $attachment['url'] ?? '';
$this->title = $attachment['title'] ?? '';
$this->description = $attachment['description'] ?? '';
$this->type = $attachment['type'] ?? '';
$this->image = $attachment['image'] ?? '';
}
/**
* Returns the current entity as an array
*
* @return array
*/
public function toArray()
{
if (empty($this->url)) {
return null;
}
return parent::toArray();
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Object\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseEntity;
/**
* Class Mention
*
* @see https://docs.joinmastodon.org/entities/mention
*/
class Mention extends BaseEntity
{
/** @var string */
protected $id;
/** @var string */
protected $username;
/** @var string */
protected $url = null;
/** @var string */
protected $acct = null;
/**
* Creates a mention record from an tag-view record.
*
* @param BaseURL $baseUrl
* @param array $tag tag-view record
* @param array $contact contact table record
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(BaseURL $baseUrl, array $tag, array $contact)
{
$this->id = $contact['id'] ?? 0;
$this->username = $tag['name'];
$this->url = $tag['url'];
if (!empty($contact)) {
$this->acct =
strpos($contact['url'], $baseUrl->get() . '/') === 0 ?
$contact['nick'] :
$contact['addr'];
} else {
$this->acct = '';
}
}
}

View file

@ -24,6 +24,7 @@ namespace Friendica\Object\Api\Mastodon;
use Friendica\BaseEntity; use Friendica\BaseEntity;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\Object\Api\Mastodon\Status\Counts; use Friendica\Object\Api\Mastodon\Status\Counts;
use Friendica\Object\Api\Mastodon\Status\UserAttributes;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
/** /**
@ -96,7 +97,7 @@ class Status extends BaseEntity
* @param array $item * @param array $item
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public function __construct(array $item, Account $account, Counts $counts) public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card)
{ {
$this->id = (string)$item['uri-id']; $this->id = (string)$item['uri-id'];
$this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM); $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM);
@ -106,32 +107,54 @@ class Status extends BaseEntity
$this->in_reply_to_account_id = (string)$item['parent-author-id']; $this->in_reply_to_account_id = (string)$item['parent-author-id'];
} }
$this->sensitive = false; $this->sensitive = $sensitive;
$this->spoiler_text = $item['title']; $this->spoiler_text = $item['title'];
$visibility = ['public', 'private', 'unlisted']; $visibility = ['public', 'private', 'unlisted'];
$this->visibility = $visibility[$item['private']]; $this->visibility = $visibility[$item['private']];
$this->language = null; $languages = json_decode($item['language'], true);
$this->language = is_array($languages) ? array_key_first($languages) : null;
$this->uri = $item['uri']; $this->uri = $item['uri'];
$this->url = $item['plink'] ?? null; $this->url = $item['plink'] ?? null;
$this->replies_count = $counts->replies; $this->replies_count = $counts->replies;
$this->reblogs_count = $counts->reblogs; $this->reblogs_count = $counts->reblogs;
$this->favourites_count = $counts->favourites; $this->favourites_count = $counts->favourites;
$this->favourited = false; $this->favourited = $userAttributes->favourited;
$this->reblogged = false; $this->reblogged = $userAttributes->reblogged;
$this->muted = false; $this->muted = $userAttributes->muted;
$this->bookmarked = false; $this->bookmarked = $userAttributes->bookmarked;
$this->pinned = false; $this->pinned = $userAttributes->pinned;
$this->content = BBCode::convert($item['body'], false); $this->content = BBCode::convert($item['body'], false);
$this->reblog = null; $this->reblog = null; /// @todo
$this->application = null; $this->application = $application->toArray();
$this->account = $account->toArray(); $this->account = $account->toArray();
$this->media_attachments = []; $this->media_attachments = []; /// @todo
$this->mentions = []; $this->mentions = $mentions;
$this->tags = []; $this->tags = $tags;
$this->emojis = []; $this->emojis = [];
$this->card = null; $this->card = $card->toArray();
$this->poll = null; $this->poll = null;
} }
/**
* Returns the current entity as an array
*
* @return array
*/
public function toArray()
{
$status = parent::toArray();
if (!$status['pinned']) {
unset($status['pinned']);
}
if (empty($status['application']['name'])) {
unset($status['application']);
}
return $status;
}
} }

View file

@ -51,6 +51,6 @@ class Counts
} }
public function __get($name) { public function __get($name) {
return $this->$name; return $this->$name;
} }
} }

View file

@ -0,0 +1,64 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Object\Api\Mastodon\Status;
/**
* Class UserAttributes
*
* @see https://docs.joinmastodon.org/entities/status
*/
class UserAttributes
{
/** @var bool */
protected $favourited;
/** @var bool */
protected $reblogged;
/** @var bool */
protected $muted;
/** @var bool */
protected $bookmarked;
/** @var bool */
protected $pinned;
/**
* Creates a authorized user attributes object
*
* @param bool $favourited
* @param bool $reblogged
* @param bool $muted
* @param bool $bookmarked
* @param bool $pinned
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(bool $favourited, bool $reblogged, bool $muted, bool $bookmarked, bool $pinned)
{
$this->favourited = $favourited;
$this->reblogged = $reblogged;
$this->muted = $muted;
$this->bookmarked = $bookmarked;
$this->pinned = $pinned;
}
public function __get($name) {
return $this->$name;
}
}

View file

@ -0,0 +1,51 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Object\Api\Mastodon;
use Friendica\App\BaseURL;
use Friendica\BaseEntity;
/**
* Class Tag
*
* @see https://docs.joinmastodon.org/entities/tag
*/
class Tag extends BaseEntity
{
/** @var string */
protected $name;
/** @var string */
protected $url = null;
/**
* Creates a hashtag record from an tag-view record.
*
* @param BaseURL $baseUrl
* @param array $tag tag-view record
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public function __construct(BaseURL $baseUrl, array $tag)
{
$this->name = strtolower($tag['name']);
$this->url = $baseUrl . '/search?tag=' . urlencode($this->name);
}
}