From db6282b5e2be2993a83876e7b98c7f04c460ca1a Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 6 Sep 2020 07:12:04 +0000 Subject: [PATCH 1/6] API: Counts added, local query improved --- src/Factory/Api/Mastodon/Status.php | 9 ++- .../Api/Mastodon/Timelines/PublicTimeline.php | 8 +-- src/Object/Api/Mastodon/Activity.php | 55 +++++++++++++++++++ src/Object/Api/Mastodon/Application.php | 46 ++++++++++++++++ src/Object/Api/Mastodon/Status.php | 8 +-- 5 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 src/Object/Api/Mastodon/Activity.php create mode 100644 src/Object/Api/Mastodon/Application.php diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 9a7589a6e..6295b4469 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -23,9 +23,12 @@ namespace Friendica\Factory\Api\Mastodon; use Friendica\App\BaseURL; use Friendica\BaseFactory; +use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Item; +use Friendica\Model\Verb; use Friendica\Network\HTTPException; +use Friendica\Protocol\Activity; use Friendica\Repository\ProfileField; use Psr\Log\LoggerInterface; @@ -59,6 +62,10 @@ class Status extends BaseFactory $item = Item::selectFirst([], ['uri-id' => $uriId, 'uid' => $uid]); $account = DI::mstdnAccount()->createFromContactId($item['author-id']); - return new \Friendica\Object\Api\Mastodon\Status($item, $account); + $count['replies'] = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]); + $count['reblogs'] = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]); + $count['favourites'] = 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, $count); } } diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 9a42521d9..598cfe5c6 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -57,11 +57,11 @@ class PublicTimeline extends BaseApi $params = ['order' => ['uri-id' => true], 'limit' => $limit]; - $condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => Item::PUBLIC, 'network' => Protocol::FEDERATED]; + $condition = ['gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => Item::PUBLIC, + 'uid' => 0, 'network' => Protocol::FEDERATED]; + if ($local) { - $condition['origin'] = true; - } else { - $condition['uid'] = 0; + $condition = DBA::mergeConditions($condition, ["`uri-id` IN (SELECT `uri-id` FROM `item` WHERE `origin`)"]); } if ($remote) { diff --git a/src/Object/Api/Mastodon/Activity.php b/src/Object/Api/Mastodon/Activity.php new file mode 100644 index 000000000..a73307eb4 --- /dev/null +++ b/src/Object/Api/Mastodon/Activity.php @@ -0,0 +1,55 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon; + +use Friendica\BaseEntity; + +/** + * Class Activity + * + * @see https://docs.joinmastodon.org/entities/activity + */ +class Activity extends BaseEntity +{ + /** @var string (UNIX Timestamp) */ + protected $week; + /** @var string */ + protected $statuses; + /** @var string */ + protected $logins; + /** @var string */ + protected $registrations; + + /** + * Creates an activity + * + * @param array $item + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public function __construct(int $week, int $statuses, int $logins, int $registrations) + { + $this->week = (string)$week; + $this->statuses = (string)$statuses; + $this->logins = (string)$logins; + $this->registrations = (string)$registrations; + } +} diff --git a/src/Object/Api/Mastodon/Application.php b/src/Object/Api/Mastodon/Application.php new file mode 100644 index 000000000..d26d270d9 --- /dev/null +++ b/src/Object/Api/Mastodon/Application.php @@ -0,0 +1,46 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon; + +use Friendica\BaseEntity; + +/** + * Class Application + * + * @see https://docs.joinmastodon.org/entities/application + */ +class Application extends BaseEntity +{ + /** @var string */ + protected $name; + + /** + * Creates an application entry + * + * @param array $item + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public function __construct(string $name) + { + $this->name = $name; + } +} diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index 5ebddf469..ea9326caf 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -95,7 +95,7 @@ class Status extends BaseEntity * @param array $item * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function __construct(array $item, Account $account) + public function __construct(array $item, Account $account, array $count) { $this->id = (string)$item['uri-id']; $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM); @@ -114,9 +114,9 @@ class Status extends BaseEntity $this->language = null; $this->uri = $item['uri']; $this->url = $item['plink'] ?? null; - $this->replies_count = 0; - $this->reblogs_count = 0; - $this->favourites_count = 0; + $this->replies_count = $count['replies']; + $this->reblogs_count = $count['reblogs']; + $this->favourites_count = $count['favourites']; $this->favourited = false; $this->reblogged = false; $this->muted = false; From 1bca280eae2936750d94e137cb84a480c8131be7 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 7 Sep 2020 14:34:05 +0000 Subject: [PATCH 2/6] StdClass instead of arrays --- src/Factory/Api/Mastodon/Status.php | 8 +++++--- src/Object/Api/Mastodon/Status.php | 9 +++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 6295b4469..ccb131aa8 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -31,6 +31,7 @@ use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Repository\ProfileField; use Psr\Log\LoggerInterface; +use stdClass; class Status extends BaseFactory { @@ -62,9 +63,10 @@ class Status extends BaseFactory $item = Item::selectFirst([], ['uri-id' => $uriId, 'uid' => $uid]); $account = DI::mstdnAccount()->createFromContactId($item['author-id']); - $count['replies'] = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]); - $count['reblogs'] = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]); - $count['favourites'] = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]); + $count = new stdClass; + $count->replies = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]); + $count->reblogs = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]); + $count->favourites = 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, $count); } diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index ea9326caf..a98378588 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -24,6 +24,7 @@ namespace Friendica\Object\Api\Mastodon; use Friendica\BaseEntity; use Friendica\Content\Text\BBCode; use Friendica\Util\DateTimeFormat; +use stdClass; /** * Class Status @@ -95,7 +96,7 @@ class Status extends BaseEntity * @param array $item * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function __construct(array $item, Account $account, array $count) + public function __construct(array $item, Account $account, stdClass $count) { $this->id = (string)$item['uri-id']; $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM); @@ -114,9 +115,9 @@ class Status extends BaseEntity $this->language = null; $this->uri = $item['uri']; $this->url = $item['plink'] ?? null; - $this->replies_count = $count['replies']; - $this->reblogs_count = $count['reblogs']; - $this->favourites_count = $count['favourites']; + $this->replies_count = $count->replies; + $this->reblogs_count = $count->reblogs; + $this->favourites_count = $count->favourites; $this->favourited = false; $this->reblogged = false; $this->muted = false; From 59374eb6c6e9c81c9079cd140e72d3c48a1532f4 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 7 Sep 2020 18:24:11 +0000 Subject: [PATCH 3/6] Use "StatusCounts" class --- src/Factory/Api/Mastodon/Status.php | 9 ++- src/Object/Api/Mastodon/Status.php | 10 ++-- .../Api/Mastodon/Status/StatusCounts.php | 56 +++++++++++++++++++ 3 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 src/Object/Api/Mastodon/Status/StatusCounts.php diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index ccb131aa8..db7c7203d 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -31,7 +31,6 @@ use Friendica\Network\HTTPException; use Friendica\Protocol\Activity; use Friendica\Repository\ProfileField; use Psr\Log\LoggerInterface; -use stdClass; class Status extends BaseFactory { @@ -63,10 +62,10 @@ class Status extends BaseFactory $item = Item::selectFirst([], ['uri-id' => $uriId, 'uid' => $uid]); $account = DI::mstdnAccount()->createFromContactId($item['author-id']); - $count = new stdClass; - $count->replies = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]); - $count->reblogs = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]); - $count->favourites = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]); + $count = new \Friendica\Object\Api\Mastodon\Status\StatusCounts( + DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]), + DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]), + 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, $count); } diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index a98378588..a82d100c9 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -23,8 +23,8 @@ namespace Friendica\Object\Api\Mastodon; use Friendica\BaseEntity; use Friendica\Content\Text\BBCode; +use Friendica\Object\Api\Mastodon\Status\StatusCounts; use Friendica\Util\DateTimeFormat; -use stdClass; /** * Class Status @@ -96,7 +96,7 @@ class Status extends BaseEntity * @param array $item * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function __construct(array $item, Account $account, stdClass $count) + public function __construct(array $item, Account $account, StatusCounts $count) { $this->id = (string)$item['uri-id']; $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM); @@ -115,9 +115,9 @@ class Status extends BaseEntity $this->language = null; $this->uri = $item['uri']; $this->url = $item['plink'] ?? null; - $this->replies_count = $count->replies; - $this->reblogs_count = $count->reblogs; - $this->favourites_count = $count->favourites; + $this->replies_count = $count->__get('replies'); + $this->reblogs_count = $count->__get('reblogs'); + $this->favourites_count = $count->__get('favourites'); $this->favourited = false; $this->reblogged = false; $this->muted = false; diff --git a/src/Object/Api/Mastodon/Status/StatusCounts.php b/src/Object/Api/Mastodon/Status/StatusCounts.php new file mode 100644 index 000000000..eb62326de --- /dev/null +++ b/src/Object/Api/Mastodon/Status/StatusCounts.php @@ -0,0 +1,56 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\Status; + +/** + * Class StatusCounts + * + * @see https://docs.joinmastodon.org/entities/status + */ +class StatusCounts +{ + /** @var int */ + protected $replies; + /** @var int */ + protected $reblogs; + /** @var int */ + protected $favourites; + + /** + * Creates a status count object + * + * @param int $replies + * @param int $reblogs + * @param int $favourites + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public function __construct(int $replies, int $reblogs, int $favourites) + { + $this->replies = $replies; + $this->reblogs = $reblogs; + $this->favourites = $favourites; + } + + public function __get($name) { + return $this->$name; + } +} From 2bb725fa3091e95c97f3a557a966d660045050c6 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Tue, 8 Sep 2020 03:39:51 +0200 Subject: [PATCH 4/6] Apply suggestions from code review Co-authored-by: Hypolite Petovan --- src/Factory/Api/Mastodon/Status.php | 7 ++++--- src/Object/Api/Mastodon/Status.php | 10 +++++----- src/Object/Api/Mastodon/Status/StatusCounts.php | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index db7c7203d..c31c211a5 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -62,11 +62,12 @@ class Status extends BaseFactory $item = Item::selectFirst([], ['uri-id' => $uriId, 'uid' => $uid]); $account = DI::mstdnAccount()->createFromContactId($item['author-id']); - $count = new \Friendica\Object\Api\Mastodon\Status\StatusCounts( + $counts = new \Friendica\Object\Api\Mastodon\Status\Counts( DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]), DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]), - 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, $count); + return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts); } } diff --git a/src/Object/Api/Mastodon/Status.php b/src/Object/Api/Mastodon/Status.php index a82d100c9..aa26fa198 100644 --- a/src/Object/Api/Mastodon/Status.php +++ b/src/Object/Api/Mastodon/Status.php @@ -23,7 +23,7 @@ namespace Friendica\Object\Api\Mastodon; use Friendica\BaseEntity; use Friendica\Content\Text\BBCode; -use Friendica\Object\Api\Mastodon\Status\StatusCounts; +use Friendica\Object\Api\Mastodon\Status\Counts; use Friendica\Util\DateTimeFormat; /** @@ -96,7 +96,7 @@ class Status extends BaseEntity * @param array $item * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function __construct(array $item, Account $account, StatusCounts $count) + public function __construct(array $item, Account $account, Counts $counts) { $this->id = (string)$item['uri-id']; $this->created_at = DateTimeFormat::utc($item['created'], DateTimeFormat::ATOM); @@ -115,9 +115,9 @@ class Status extends BaseEntity $this->language = null; $this->uri = $item['uri']; $this->url = $item['plink'] ?? null; - $this->replies_count = $count->__get('replies'); - $this->reblogs_count = $count->__get('reblogs'); - $this->favourites_count = $count->__get('favourites'); + $this->replies_count = $counts->replies; + $this->reblogs_count = $counts->reblogs; + $this->favourites_count = $counts->favourites; $this->favourited = false; $this->reblogged = false; $this->muted = false; diff --git a/src/Object/Api/Mastodon/Status/StatusCounts.php b/src/Object/Api/Mastodon/Status/StatusCounts.php index eb62326de..feed5cc54 100644 --- a/src/Object/Api/Mastodon/Status/StatusCounts.php +++ b/src/Object/Api/Mastodon/Status/StatusCounts.php @@ -26,7 +26,7 @@ namespace Friendica\Object\Api\Mastodon\Status; * * @see https://docs.joinmastodon.org/entities/status */ -class StatusCounts +class Counts { /** @var int */ protected $replies; From 24f1bb4ea15ad09521dcb8a554ed9f1ac9a45264 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 8 Sep 2020 01:44:49 +0000 Subject: [PATCH 5/6] Class file renamed --- src/Object/Api/Mastodon/Status/{StatusCounts.php => Counts.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/Object/Api/Mastodon/Status/{StatusCounts.php => Counts.php} (98%) diff --git a/src/Object/Api/Mastodon/Status/StatusCounts.php b/src/Object/Api/Mastodon/Status/Counts.php similarity index 98% rename from src/Object/Api/Mastodon/Status/StatusCounts.php rename to src/Object/Api/Mastodon/Status/Counts.php index feed5cc54..caf839dbb 100644 --- a/src/Object/Api/Mastodon/Status/StatusCounts.php +++ b/src/Object/Api/Mastodon/Status/Counts.php @@ -22,7 +22,7 @@ namespace Friendica\Object\Api\Mastodon\Status; /** - * Class StatusCounts + * Class Counts * * @see https://docs.joinmastodon.org/entities/status */ From e45ccea0f2b18fcf12552b4e36a5c8bf4059f3b1 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 8 Sep 2020 01:45:59 +0000 Subject: [PATCH 6/6] Tabs instead of spaces --- src/Object/Api/Mastodon/Status/Counts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Object/Api/Mastodon/Status/Counts.php b/src/Object/Api/Mastodon/Status/Counts.php index caf839dbb..2c446c36c 100644 --- a/src/Object/Api/Mastodon/Status/Counts.php +++ b/src/Object/Api/Mastodon/Status/Counts.php @@ -31,7 +31,7 @@ class Counts /** @var int */ protected $replies; /** @var int */ - protected $reblogs; + protected $reblogs; /** @var int */ protected $favourites;