Loading modules/Fediverse/Config/Routes.php +5 −0 Original line number Diff line number Diff line Loading @@ -29,6 +29,11 @@ $routes->group('', [ 'as' => 'webfinger', ]); // nodeInfo2 $routes->get('.well-known/x-nodeinfo2', 'NodeInfo2Controller', [ 'as' => 'nodeInfo2', ]); // Actor $routes->group('@(:actorUsername)', function ($routes): void { // Actor Loading modules/Fediverse/Controllers/NodeInfo2Controller.php 0 → 100644 +53 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); /** * @copyright 2021 Podlibre * @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3 * @link https://castopod.org/ */ namespace Modules\Fediverse\Controllers; use CodeIgniter\Controller; use CodeIgniter\HTTP\ResponseInterface; class NodeInfo2Controller extends Controller { public function index(): ResponseInterface { $totalUsers = model('ActorModel') ->getTotalLocalActors(); $totalPosts = model('PostModel') ->getTotalLocalPosts(); $activeMonth = model('ActorModel') ->getActiveLocalActors(1); $activeHalfyear = model('ActorModel') ->getActiveLocalActors(6); $nodeInfo2 = [ 'version' => '1.0', 'server' => [ 'baseUrl' => base_url(), 'name' => service('settings') ->get('App.siteName'), 'software' => 'Castopod Host', 'version' => CP_VERSION, ], 'protocols' => ['activitypub'], 'openRegistrations' => config('Auth') ->allowRegistration, 'usage' => [ 'users' => [ 'total' => $totalUsers, 'activeMonth' => $activeMonth, 'activeHalfyear' => $activeHalfyear, ], 'localPosts' => $totalPosts, ], ]; return $this->response->setJSON($nodeInfo2); } } modules/Fediverse/Models/ActorModel.php +67 −0 Original line number Diff line number Diff line Loading @@ -205,6 +205,73 @@ class ActorModel extends BaseModel Events::trigger('on_unblock_actor', $actorId); } public function getTotalLocalActors(): int { helper('fediverse'); $cacheName = config('Fediverse') ->cachePrefix . 'blocked_actors'; if (! ($found = cache($cacheName))) { $result = $this->select('COUNT(*) as total_local_actors') ->where('domain', get_current_domain()) ->get() ->getResultArray(); $found = (int) $result[0]['total_local_actors']; cache() ->save($cacheName, $found, DECADE); } return $found; } public function getActiveLocalActors(int $lastNumberOfMonths = 1): int { helper('fediverse'); $cacheName = config('Fediverse') ->cachePrefix . 'blocked_actors'; if (! ($found = cache($cacheName))) { $tablePrefix = config('Database') ->default['DBPrefix'] . config('Fediverse') ->tablesPrefix; $result = $this->select('COUNT(DISTINCT `cp_fediverse_actors`.`id`) as `total_active_actors`', false) ->join( $tablePrefix . 'posts', $tablePrefix . 'actors.id = ' . $tablePrefix . 'posts.actor_id', 'left outer' ) ->join( $tablePrefix . 'favourites', $tablePrefix . 'actors.id = ' . $tablePrefix . 'favourites.actor_id', 'left outer' ) ->where($tablePrefix . 'actors.domain', get_current_domain()) ->groupStart() ->where( "`{$tablePrefix}posts`.`created_at` >= NOW() - INTERVAL {$lastNumberOfMonths} month", null, false ) ->orWhere( "`{$tablePrefix}favourites`.`created_at` >= NOW() - INTERVAL {$lastNumberOfMonths} month", null, false ) ->groupEnd() ->get() ->getResultArray(); $found = (int) $result[0]['total_active_actors']; cache() ->save($cacheName, $found, DAY); } return $found; } public function clearCache(Actor $actor): void { $cachePrefix = config('Fediverse') Loading modules/Fediverse/Models/PostModel.php +26 −0 Original line number Diff line number Diff line Loading @@ -470,6 +470,7 @@ class PostModel extends BaseUuidModel 'actor_id' => $actor->id, 'reblog_of_id' => $post->id, 'published_at' => Time::now(), 'created_by' => user_id(), ]); // add reblog Loading Loading @@ -593,6 +594,31 @@ class PostModel extends BaseUuidModel } } public function getTotalLocalPosts(): int { helper('fediverse'); $cacheName = config('Fediverse') ->cachePrefix . 'blocked_actors'; if (! ($found = cache($cacheName))) { $tablePrefix = config('Fediverse') ->tablesPrefix; $result = $this->select('COUNT(*) as total_local_posts') ->join($tablePrefix . 'actors', $tablePrefix . 'actors.id = ' . $tablePrefix . 'posts.actor_id') ->where($tablePrefix . 'actors.domain', get_current_domain()) ->where('`published_at` <= NOW()', null, false) ->get() ->getResultArray(); $found = (int) $result[0]['total_local_posts']; cache() ->save($cacheName, $found, DECADE); } return $found; } public function clearCache(Post $post): void { $cachePrefix = config('Fediverse') Loading modules/Fediverse/Objects/ActorObject.php +3 −0 Original line number Diff line number Diff line Loading @@ -36,6 +36,8 @@ class ActorObject extends ObjectType protected string $url; protected string $nodeInfo2Url; /** * @var array<string, string> */ Loading @@ -59,6 +61,7 @@ class ActorObject extends ObjectType $this->preferredUsername = $actor->username; $this->summary = $actor->summary; $this->url = $actor->uri; $this->nodeInfo2Url = url_to('nodeInfo2'); $this->inbox = $actor->inbox_url; $this->outbox = $actor->outbox_url; Loading Loading
modules/Fediverse/Config/Routes.php +5 −0 Original line number Diff line number Diff line Loading @@ -29,6 +29,11 @@ $routes->group('', [ 'as' => 'webfinger', ]); // nodeInfo2 $routes->get('.well-known/x-nodeinfo2', 'NodeInfo2Controller', [ 'as' => 'nodeInfo2', ]); // Actor $routes->group('@(:actorUsername)', function ($routes): void { // Actor Loading
modules/Fediverse/Controllers/NodeInfo2Controller.php 0 → 100644 +53 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); /** * @copyright 2021 Podlibre * @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3 * @link https://castopod.org/ */ namespace Modules\Fediverse\Controllers; use CodeIgniter\Controller; use CodeIgniter\HTTP\ResponseInterface; class NodeInfo2Controller extends Controller { public function index(): ResponseInterface { $totalUsers = model('ActorModel') ->getTotalLocalActors(); $totalPosts = model('PostModel') ->getTotalLocalPosts(); $activeMonth = model('ActorModel') ->getActiveLocalActors(1); $activeHalfyear = model('ActorModel') ->getActiveLocalActors(6); $nodeInfo2 = [ 'version' => '1.0', 'server' => [ 'baseUrl' => base_url(), 'name' => service('settings') ->get('App.siteName'), 'software' => 'Castopod Host', 'version' => CP_VERSION, ], 'protocols' => ['activitypub'], 'openRegistrations' => config('Auth') ->allowRegistration, 'usage' => [ 'users' => [ 'total' => $totalUsers, 'activeMonth' => $activeMonth, 'activeHalfyear' => $activeHalfyear, ], 'localPosts' => $totalPosts, ], ]; return $this->response->setJSON($nodeInfo2); } }
modules/Fediverse/Models/ActorModel.php +67 −0 Original line number Diff line number Diff line Loading @@ -205,6 +205,73 @@ class ActorModel extends BaseModel Events::trigger('on_unblock_actor', $actorId); } public function getTotalLocalActors(): int { helper('fediverse'); $cacheName = config('Fediverse') ->cachePrefix . 'blocked_actors'; if (! ($found = cache($cacheName))) { $result = $this->select('COUNT(*) as total_local_actors') ->where('domain', get_current_domain()) ->get() ->getResultArray(); $found = (int) $result[0]['total_local_actors']; cache() ->save($cacheName, $found, DECADE); } return $found; } public function getActiveLocalActors(int $lastNumberOfMonths = 1): int { helper('fediverse'); $cacheName = config('Fediverse') ->cachePrefix . 'blocked_actors'; if (! ($found = cache($cacheName))) { $tablePrefix = config('Database') ->default['DBPrefix'] . config('Fediverse') ->tablesPrefix; $result = $this->select('COUNT(DISTINCT `cp_fediverse_actors`.`id`) as `total_active_actors`', false) ->join( $tablePrefix . 'posts', $tablePrefix . 'actors.id = ' . $tablePrefix . 'posts.actor_id', 'left outer' ) ->join( $tablePrefix . 'favourites', $tablePrefix . 'actors.id = ' . $tablePrefix . 'favourites.actor_id', 'left outer' ) ->where($tablePrefix . 'actors.domain', get_current_domain()) ->groupStart() ->where( "`{$tablePrefix}posts`.`created_at` >= NOW() - INTERVAL {$lastNumberOfMonths} month", null, false ) ->orWhere( "`{$tablePrefix}favourites`.`created_at` >= NOW() - INTERVAL {$lastNumberOfMonths} month", null, false ) ->groupEnd() ->get() ->getResultArray(); $found = (int) $result[0]['total_active_actors']; cache() ->save($cacheName, $found, DAY); } return $found; } public function clearCache(Actor $actor): void { $cachePrefix = config('Fediverse') Loading
modules/Fediverse/Models/PostModel.php +26 −0 Original line number Diff line number Diff line Loading @@ -470,6 +470,7 @@ class PostModel extends BaseUuidModel 'actor_id' => $actor->id, 'reblog_of_id' => $post->id, 'published_at' => Time::now(), 'created_by' => user_id(), ]); // add reblog Loading Loading @@ -593,6 +594,31 @@ class PostModel extends BaseUuidModel } } public function getTotalLocalPosts(): int { helper('fediverse'); $cacheName = config('Fediverse') ->cachePrefix . 'blocked_actors'; if (! ($found = cache($cacheName))) { $tablePrefix = config('Fediverse') ->tablesPrefix; $result = $this->select('COUNT(*) as total_local_posts') ->join($tablePrefix . 'actors', $tablePrefix . 'actors.id = ' . $tablePrefix . 'posts.actor_id') ->where($tablePrefix . 'actors.domain', get_current_domain()) ->where('`published_at` <= NOW()', null, false) ->get() ->getResultArray(); $found = (int) $result[0]['total_local_posts']; cache() ->save($cacheName, $found, DECADE); } return $found; } public function clearCache(Post $post): void { $cachePrefix = config('Fediverse') Loading
modules/Fediverse/Objects/ActorObject.php +3 −0 Original line number Diff line number Diff line Loading @@ -36,6 +36,8 @@ class ActorObject extends ObjectType protected string $url; protected string $nodeInfo2Url; /** * @var array<string, string> */ Loading @@ -59,6 +61,7 @@ class ActorObject extends ObjectType $this->preferredUsername = $actor->username; $this->summary = $actor->summary; $this->url = $actor->uri; $this->nodeInfo2Url = url_to('nodeInfo2'); $this->inbox = $actor->inbox_url; $this->outbox = $actor->outbox_url; Loading