askvortsov johnlewissims or to make this project simpler on the PHP end since you're just adding a new user attribute and a little Eloquent , you can throw it all in your extend.php
file like so :
<?php
/*
* This file is part of ejin/like-counter.
*
* Copyright (c) 2020 John Lewis Sims.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Ejin\LikeCounter;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Extend;
use Flarum\Post\Post;
use Flarum\Api\Event\Serializing;
return [
(new Extend\Frontend('forum'))
->js(__DIR__ . '/js/dist/forum.js'),
new Extend\Locales(__DIR__ . '/resources/locale'),
function (Serializing $event) {
if ($event->isSerializer(UserSerializer::class)) {
$event->attributes['ejinLikeCount'] =
Post::where('user_id')->select('id')->withCount('likes')->get()->sum('likes_count');
}
}
];
You can drop the rest of the classes (and src folder entirely), migrations, delete the LESS directory (and contents), and just add a simple translation to your translation file for the likes counter text 🙂
I believe this can also all be done through JS as well by doing some work with frontend models but I haven't dug into much of that yet.
Edit: Yeah, not sure about frontend models in regards to performance or the likes (no pun intended 😉), will have to experiment with frontend models some more...
Edit 2: I also see that your JS is pretty jacked up...might want to fix that and only include a forum.js
in your js
directory pointing to the src/forum
directory and just include an index.js
file with your JS code in there if that made any sense 🙂