diff --git a/CHANGELOG.md b/CHANGELOG.md index 87635ac00..0c3bec20c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ - Updated Network Timeline, use existing Timeline component. ([0deaafc0](https://github.com/pixelfed/pixelfed/commit/0deaafc0)) - Updated PostComponent, show like count to owner using MomentUI. ([e9c46bab](https://github.com/pixelfed/pixelfed/commit/e9c46bab)) - Updated ContextMenu, add missing statusUrl method. ([3cffdb11](https://github.com/pixelfed/pixelfed/commit/3cffdb11)) +- Updated PublicApiController, add LikeService to Network timeline. ([82895591](https://github.com/pixelfed/pixelfed/commit/82895591)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.11.0 (2021-06-01)](https://github.com/pixelfed/pixelfed/compare/v0.10.10...v0.11.0) diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index 2eafe7044..50f0e9e3e 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/app/Http/Controllers/PublicApiController.php @@ -26,6 +26,7 @@ use App\Transformer\Api\{ }; use App\Services\{ AccountService, + LikeService, PublicTimelineService, StatusService, SnowflakeService, @@ -527,8 +528,10 @@ class PublicApiController extends Controller ->orderBy('created_at', 'desc') ->limit($limit) ->get() - ->map(function($s) { - return StatusService::get($s->id); + ->map(function($s) use ($user) { + $status = StatusService::get($s->id); + $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); + return $status; }); $res = $timeline->toArray(); } else { @@ -543,11 +546,13 @@ class PublicApiController extends Controller ->whereScope('public') ->where('id', '>', $amin) ->orderBy('created_at', 'desc') - ->limit($limit) - ->get() - ->map(function($s) { - return StatusService::get($s->id); - }); + ->limit($limit) + ->get() + ->map(function($s) use ($user) { + $status = StatusService::get($s->id); + $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); + return $status; + }); $res = $timeline->toArray(); } diff --git a/app/Observers/LikeObserver.php b/app/Observers/LikeObserver.php new file mode 100644 index 000000000..d76140064 --- /dev/null +++ b/app/Observers/LikeObserver.php @@ -0,0 +1,64 @@ +profile_id, $like->status_id); + } + + /** + * Handle the Like "updated" event. + * + * @param \App\Models\Like $like + * @return void + */ + public function updated(Like $like) + { + // + } + + /** + * Handle the Like "deleted" event. + * + * @param \App\Models\Like $like + * @return void + */ + public function deleted(Like $like) + { + LikeService::remove($like->profile_id, $like->status_id); + } + + /** + * Handle the Like "restored" event. + * + * @param \App\Models\Like $like + * @return void + */ + public function restored(Like $like) + { + LikeService::add($like->profile_id, $like->status_id); + } + + /** + * Handle the Like "force deleted" event. + * + * @param \App\Models\Like $like + * @return void + */ + public function forceDeleted(Like $like) + { + LikeService::remove($like->profile_id, $like->status_id); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 7364745a8..a4dfbe27b 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,22 +3,24 @@ namespace App\Providers; use App\Observers\{ - AvatarObserver, - NotificationObserver, - ModLogObserver, - ProfileObserver, - StatusHashtagObserver, - UserObserver, - UserFilterObserver, + AvatarObserver, + LikeObserver, + NotificationObserver, + ModLogObserver, + ProfileObserver, + StatusHashtagObserver, + UserObserver, + UserFilterObserver, }; use App\{ - Avatar, - Notification, - ModLog, - Profile, - StatusHashtag, - User, - UserFilter + Avatar, + Like, + Notification, + ModLog, + Profile, + StatusHashtag, + User, + UserFilter }; use Auth, Horizon, URL; use Illuminate\Support\Facades\Blade; @@ -28,54 +30,36 @@ use Illuminate\Pagination\Paginator; class AppServiceProvider extends ServiceProvider { - /** - * Bootstrap any application services. - * - * @return void - */ - public function boot() - { - URL::forceScheme('https'); - Schema::defaultStringLength(191); + /** + * Bootstrap any application services. + * + * @return void + */ + public function boot() + { + URL::forceScheme('https'); + Schema::defaultStringLength(191); + Paginator::useBootstrap(); + Avatar::observe(AvatarObserver::class); + Like::observe(LikeObserver::class); + Notification::observe(NotificationObserver::class); + ModLog::observe(ModLogObserver::class); + Profile::observe(ProfileObserver::class); + StatusHashtag::observe(StatusHashtagObserver::class); + User::observe(UserObserver::class); + UserFilter::observe(UserFilterObserver::class); + Horizon::auth(function ($request) { + return Auth::check() && $request->user()->is_admin; + }); + } - Paginator::useBootstrap(); - - Avatar::observe(AvatarObserver::class); - Notification::observe(NotificationObserver::class); - ModLog::observe(ModLogObserver::class); - Profile::observe(ProfileObserver::class); - StatusHashtag::observe(StatusHashtagObserver::class); - User::observe(UserObserver::class); - UserFilter::observe(UserFilterObserver::class); - - Horizon::auth(function ($request) { - return Auth::check() && $request->user()->is_admin; - }); - - Blade::directive('prettyNumber', function ($expression) { - $num = \App\Util\Lexer\PrettyNumber::convert($expression); - return ""; - }); - - Blade::directive('prettySize', function ($expression) { - $size = \App\Util\Lexer\PrettyNumber::size($expression); - return ""; - }); - - Blade::directive('maxFileSize', function () { - $value = config('pixelfed.max_photo_size'); - - return \App\Util\Lexer\PrettyNumber::size($value, true); - }); - } - - /** - * Register any application services. - * - * @return void - */ - public function register() - { - // - } + /** + * Register any application services. + * + * @return void + */ + public function register() + { + // + } } diff --git a/app/Services/LikeService.php b/app/Services/LikeService.php index 20ccac380..8b2ef467c 100644 --- a/app/Services/LikeService.php +++ b/app/Services/LikeService.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Util\ActivityPub\Helpers; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Redis; use App\Like; @@ -10,42 +11,27 @@ class LikeService { const CACHE_KEY = 'pf:services:likes:ids:'; - public static function getUser($profile_id) + public static function add($profileId, $statusId) { - return self::get($profile_id); + $key = self::CACHE_KEY . $profileId . ':' . $statusId; + $ttl = now()->addHours(2); + return Cache::put($key, true, $ttl); } - protected static function get($profile_id) + public static function remove($profileId, $statusId) { - $key = self::CACHE_KEY . $profile_id; - if(Redis::zcard($key) == 0) { - self::warmCache($profile_id); - } else { - return Redis::zrevrange($key, 0, 40); - } - } - - protected static function set($profile_id, $status_id) - { - $key = self::CACHE_KEY . $profile_id; - Redis::zadd($key, $status_id, $status_id); - } - - public static function warmCache($profile_id) - { - Like::select('id', 'profile_id', 'status_id') - ->whereProfileId($profile_id) - ->latest() - ->get() - ->each(function($like) use ($profile_id) { - self::set($profile_id, $like->status_id); - }); + $key = self::CACHE_KEY . $profileId . ':' . $statusId; + $ttl = now()->addHours(2); + return Cache::put($key, false, $ttl); } public static function liked($profileId, $statusId) { - $key = self::CACHE_KEY . $profileId; - return (bool) Redis::zrank($key, $statusId); + $key = self::CACHE_KEY . $profileId . ':' . $statusId; + $ttl = now()->addMinutes(30); + return Cache::remember($key, $ttl, function() use($profileId, $statusId) { + return Like::whereProfileId($profileId)->whereStatusId($statusId)->exists(); + }); } public static function likedBy($status) diff --git a/app/Services/PublicTimelineService.php b/app/Services/PublicTimelineService.php index 56990c9af..dac552f0e 100644 --- a/app/Services/PublicTimelineService.php +++ b/app/Services/PublicTimelineService.php @@ -28,7 +28,8 @@ class PublicTimelineService { public static function add($val) { - return Redis::zadd(self::CACHE_KEY, 1, $val); + // return Redis::zadd(self::CACHE_KEY, $val, $val); + return; } public static function rem($val) @@ -64,4 +65,4 @@ class PublicTimelineService { } return 0; } -} \ No newline at end of file +} diff --git a/public/js/timeline.js b/public/js/timeline.js index 0a4fe39de..5984660c5 100644 Binary files a/public/js/timeline.js and b/public/js/timeline.js differ diff --git a/public/mix-manifest.json b/public/mix-manifest.json index 56d3da671..3cb89bcd8 100644 Binary files a/public/mix-manifest.json and b/public/mix-manifest.json differ diff --git a/resources/assets/js/components/Timeline.vue b/resources/assets/js/components/Timeline.vue index eed3f5c9d..a266b3a8a 100644 --- a/resources/assets/js/components/Timeline.vue +++ b/resources/assets/js/components/Timeline.vue @@ -709,7 +709,7 @@ recentFeed: this.scope === 'home' ? true : false, recentFeedMin: null, recentFeedMax: null, - reactionBar: this.scope === 'network' ? false : true + reactionBar: true } },