[UI][CACHE][DB] Add follow counts to left panel, caching the results; change follow table

This commit is contained in:
Hugo Sales 2020-08-15 07:03:58 +00:00 committed by Hugo Sales
parent 3c67773e59
commit 65a129aac6
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 36 additions and 16 deletions

View File

@ -20,6 +20,7 @@
namespace Component\Left; namespace Component\Left;
use App\Core\Event; use App\Core\Event;
use App\Core\Log;
use App\Core\Module; use App\Core\Module;
use App\Util\Common; use App\Util\Common;
use Exception; use Exception;
@ -31,10 +32,14 @@ class Left extends Module
try { try {
$user = Common::user(); $user = Common::user();
if ($user != null) { if ($user != null) {
$actor = $user->getActor();
$vars['user_nickname'] = $user->getNickname(); $vars['user_nickname'] = $user->getNickname();
$vars['user_tags'] = $user->getActor()->getSelfTags(); $vars['user_tags'] = $actor->getSelfTags();
$vars['user_followers'] = $actor->getFollowersCount();
$vars['user_followed'] = $actor->getFollowedCount();
} }
} catch (Exception $e) { } catch (Exception $e) {
Log::error('Got an exception while populating variables for the left panel: ' . $e);
} }
return Event::next; return Event::next;
} }

View File

@ -111,17 +111,13 @@ class Follow extends Entity
'fields' => [ 'fields' => [
'follower' => ['type' => 'int', 'not null' => true, 'description' => 'gsactor listening'], 'follower' => ['type' => 'int', 'not null' => true, 'description' => 'gsactor listening'],
'followed' => ['type' => 'int', 'not null' => true, 'description' => 'gsactor being listened to'], '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'], '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'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
], ],
'primary key' => ['follower', 'followed'], 'primary key' => ['follower', 'followed'],
'unique keys' => [
'subscription_uri_key' => ['uri'],
],
'indexes' => [ 'indexes' => [
'subscription_subscriber_idx' => ['follower', 'created'], 'follow_follower_idx' => ['follower', 'created'],
'subscription_subscribed_idx' => ['followed', 'created'], 'follow_followed_idx' => ['followed', 'created'],
], ],
]; ];
} }

View File

@ -19,6 +19,7 @@
namespace App\Entity; namespace App\Entity;
use App\Core\Cache;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\UserRoles; use App\Core\UserRoles;
@ -219,7 +220,10 @@ class GSActor extends Entity
public function getSelfTags(): array public function getSelfTags(): array
{ {
return Cache::get('selftags-' . $this->id,
function () {
return DB::findBy('gsactor_tag', ['tagger' => $this->id, 'tagged' => $this->id]); return DB::findBy('gsactor_tag', ['tagger' => $this->id, 'tagged' => $this->id]);
});
} }
public function setSelfTags(array $tags, array $existing): void public function setSelfTags(array $tags, array $existing): void
@ -235,6 +239,25 @@ class GSActor extends Entity
foreach ($pt_to_remove as $pt) { foreach ($pt_to_remove as $pt) {
DB::remove($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 public static function schemaDef(): array

View File

@ -30,12 +30,8 @@
{% endfor %} {% endfor %}
</div> </div>
<div class="stats"> <div class="stats">
<span> <span> <b> {{ user_followers }} </b> {{'Followers' | trans}} </span>
<b>1337</b> <span> <b> {{ user_followed }} </b> {{'Followed' | trans}} </span>
Followers</span>
<span>
<b>42</b>
Following</span>
</div> </div>
</div> </div>
</a> </a>