diff --git a/components/Left/Left.php b/components/Left/Left.php index 6e5d65e7b9..d1fa80fde8 100644 --- a/components/Left/Left.php +++ b/components/Left/Left.php @@ -20,6 +20,7 @@ namespace Component\Left; use App\Core\Event; +use App\Core\Log; use App\Core\Module; use App\Util\Common; use Exception; @@ -31,10 +32,14 @@ class Left extends Module try { $user = Common::user(); if ($user != null) { - $vars['user_nickname'] = $user->getNickname(); - $vars['user_tags'] = $user->getActor()->getSelfTags(); + $actor = $user->getActor(); + $vars['user_nickname'] = $user->getNickname(); + $vars['user_tags'] = $actor->getSelfTags(); + $vars['user_followers'] = $actor->getFollowersCount(); + $vars['user_followed'] = $actor->getFollowedCount(); } } catch (Exception $e) { + Log::error('Got an exception while populating variables for the left panel: ' . $e); } return Event::next; } diff --git a/src/Entity/Follow.php b/src/Entity/Follow.php index 57dc690add..3412651352 100644 --- a/src/Entity/Follow.php +++ b/src/Entity/Follow.php @@ -111,17 +111,13 @@ class Follow extends Entity 'fields' => [ 'follower' => ['type' => 'int', 'not null' => true, 'description' => 'gsactor listening'], 'followed' => ['type' => 'int', 'not null' => true, 'description' => 'gsactor being listened to'], - 'uri' => ['type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier'], 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'], ], 'primary key' => ['follower', 'followed'], - 'unique keys' => [ - 'subscription_uri_key' => ['uri'], - ], - 'indexes' => [ - 'subscription_subscriber_idx' => ['follower', 'created'], - 'subscription_subscribed_idx' => ['followed', 'created'], + 'indexes' => [ + 'follow_follower_idx' => ['follower', 'created'], + 'follow_followed_idx' => ['followed', 'created'], ], ]; } diff --git a/src/Entity/GSActor.php b/src/Entity/GSActor.php index 017e5a2e8a..d91060ae71 100644 --- a/src/Entity/GSActor.php +++ b/src/Entity/GSActor.php @@ -19,6 +19,7 @@ namespace App\Entity; +use App\Core\Cache; use App\Core\DB\DB; use App\Core\Entity; use App\Core\UserRoles; @@ -219,7 +220,10 @@ class GSActor extends Entity public function getSelfTags(): array { - return DB::findBy('gsactor_tag', ['tagger' => $this->id, 'tagged' => $this->id]); + return Cache::get('selftags-' . $this->id, + function () { + return DB::findBy('gsactor_tag', ['tagger' => $this->id, 'tagged' => $this->id]); + }); } public function setSelfTags(array $tags, array $existing): void @@ -235,6 +239,25 @@ class GSActor extends Entity foreach ($pt_to_remove as $pt) { DB::remove($pt); } + Cache::delete('selftags-' . $this->id); + } + + public function getFollowersCount() + { + return Cache::get('followers-' . $this->id, + function () { + return DB::dql('select count(f) from App\Entity\Follow f where f.followed = :followed', + ['followed' => $this->id])[0][1]; + }); + } + + public function getFollowedCount() + { + return Cache::get('followed-' . $this->id, + function () { + return DB::dql('select count(f) from App\Entity\Follow f where f.follower = :follower', + ['follower' => $this->id])[0][1]; + }); } public static function schemaDef(): array diff --git a/templates/left/left.html.twig b/templates/left/left.html.twig index 997be9b789..27b13787fb 100644 --- a/templates/left/left.html.twig +++ b/templates/left/left.html.twig @@ -30,12 +30,8 @@ {% endfor %}