mirror of https://github.com/pixelfed/pixelfed
Extract following-ids lookup into FollowerService::getFollowingIds
The Cache::remember('profile:following:'.$pid, ...) block that plucks
following_id and appends the caller's own id was copy-pasted across four
call sites, with inconsistent TTLs (1440 minutes vs 1209600 seconds).
Add FollowerService::getFollowingIds($pid), which owns the cache key that
add()/remove() already invalidate, and use it from InternalApiController,
PublicApiController, ApiV1Controller and HashtagUnfollowPipeline. Removes
the now-unused Follower/Cache imports left behind.
Adds a test covering the followed-ids-plus-self result and the
follows-nobody case.
pull/7045/head
parent
ed07f9bfec
commit
667f6e2fc9
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Follower;
|
||||
use App\Models\User;
|
||||
use App\Services\FollowerService;
|
||||
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
uses(LazilyRefreshDatabase::class);
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| FollowerService::getFollowingIds
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Extracted from four inline copies of the same
|
||||
| Cache::remember('profile:following:'.$pid, ...) pattern. Verifies the
|
||||
| method returns the followed profile ids plus the caller's own id.
|
||||
|
|
||||
*/
|
||||
|
||||
beforeEach(function () {
|
||||
Cache::flush();
|
||||
});
|
||||
|
||||
it('returns followed ids plus the callers own id', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->refresh();
|
||||
$a = User::factory()->create();
|
||||
$a->refresh();
|
||||
$b = User::factory()->create();
|
||||
$b->refresh();
|
||||
|
||||
foreach ([$a->profile_id, $b->profile_id] as $targetId) {
|
||||
$f = new Follower;
|
||||
$f->profile_id = $user->profile_id;
|
||||
$f->following_id = $targetId;
|
||||
$f->save();
|
||||
}
|
||||
|
||||
$ids = FollowerService::getFollowingIds($user->profile_id);
|
||||
|
||||
expect($ids)->toContain($a->profile_id)
|
||||
->toContain($b->profile_id)
|
||||
->toContain($user->profile_id);
|
||||
});
|
||||
|
||||
it('returns only the callers own id when following nobody', function () {
|
||||
$user = User::factory()->create();
|
||||
$user->refresh();
|
||||
|
||||
$ids = FollowerService::getFollowingIds($user->profile_id);
|
||||
|
||||
expect($ids)->toBe([$user->profile_id]);
|
||||
});
|
||||
Loading…
Reference in New Issue