Merge pull request #7464 from pixelfed/fix/story-recent-deterministic-selection

Select newest active story per author deterministically
pull/7465/head
Shlee 4 days ago committed by GitHub
commit 2d263507ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -34,36 +34,25 @@ class StoryController extends StoryComposeController
}
$pid = $user->profile_id;
if (db_is_pgsql()) {
$s = Cache::remember('pf:stories:recent-by-id:'.$pid, 900, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid)
->where('stories.active', true)
->get()
->map(function ($s) {
$r = new \StdClass;
$r->id = $s->id;
$r->profile_id = $s->profile_id;
$r->type = $s->type;
$r->path = $s->path;
return $r;
})
->unique('profile_id');
});
} else {
$s = Cache::remember('pf:stories:recent-by-id:'.$pid, 900, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid)
->where('stories.active', true)
->groupBy('followers.following_id')
->orderByDesc('id')
->get();
});
}
// One row per followed author: the author's newest active story
// (MAX(id), matching StoryService::latest()). Collapsing in SQL via a
// correlated MAX(id) is deterministic and portable across
// mysql/mariadb/pgsql/sqlite — the previous groupBy(stories.*) and
// unique() without ordering both left the kept row unspecified.
$s = Cache::remember('pf:stories:recent-by-id:'.$pid, 900, function () use ($pid) {
return Story::select('stories.*')
->join('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid)
->where('stories.active', true)
->whereRaw('stories.id = (
select max(s2.id)
from stories as s2
where s2.profile_id = stories.profile_id
and s2.active = ?
)', [true])
->orderByDesc('stories.id')
->get();
});
$self = Cache::remember('pf:stories:recent-self:'.$pid, 21600, function () use ($pid) {
return Story::whereProfileId($pid)
@ -219,7 +208,10 @@ class StoryController extends StoryComposeController
}
}
Cache::forget('stories:recent:by_id:'.$authed->id);
// Match the key recent() writes (pf:stories:recent-by-id:{viewer_pid});
// the old key never matched, so the viewer's carousel snapshot (and its
// seen flags) stayed pinned for the full TTL.
Cache::forget('pf:stories:recent-by-id:'.$authed->id);
StoryService::addSeen($authed->id, $story->id);
return ['code' => 200];

@ -0,0 +1,99 @@
<?php
use App\Models\Follower;
use App\Models\Profile;
use App\Models\Story;
use App\Models\User;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Facades\Cache;
uses(LazilyRefreshDatabase::class);
/*
|--------------------------------------------------------------------------
| StoryController::recent deterministic per-author selection
|--------------------------------------------------------------------------
|
| recent() returns one card per followed author. It used to pick that row
| indeterminately (groupBy(stories.*) with post-group ordering, or unique()
| without ordering), so an author with two active stories could show either
| one's preview. The card must reflect the author's newest active story
| (MAX(id)), matching StoryService::latest().
|
*/
beforeEach(function () {
config(['instance.stories.enabled' => true, 'image.driver' => 'gd']);
Cache::flush();
});
function makeActiveStory(Profile $author, int $durationHours = 4): Story
{
$story = new Story;
$story->profile_id = $author->id;
$story->type = 'photo';
$story->duration = 3;
$story->path = 'public/story/'.uniqid().'.jpg';
$story->local = true;
$story->active = true;
$story->public = false;
$story->view_count = 0;
$story->expires_at = now()->addHours($durationHours);
$story->save();
return $story;
}
function followedAuthorWithStories(User $viewer, int $count): array
{
$author = Profile::factory()->create(['user_id' => null, 'domain' => 'remote.example']);
Follower::create([
'profile_id' => $viewer->profile_id,
'following_id' => $author->id,
'local_profile' => true,
]);
$stories = [];
foreach (range(1, $count) as $i) {
$stories[] = makeActiveStory($author, 4 + $i);
}
return [$author, $stories];
}
it('shows the newest active story for a followed author with two active stories', function () {
$viewer = User::factory()->create();
$viewer->refresh();
[$author, $stories] = followedAuthorWithStories($viewer, 2);
// "Newest" is the max id, matching StoryService::latest(); snowflake ids
// are not guaranteed to increase with creation order within a tick.
$newestId = collect($stories)->max(fn ($s) => (int) $s->id);
$row = collect($this->actingAs($viewer, 'web')
->getJson('/api/web/stories/v1/recent')
->assertOk()
->json())
->firstWhere('pid', $author->id);
expect($row)->not->toBeNull()
->and((int) $row['latest']['id'])->toBe($newestId)
->and((int) $row['sid'])->toBe($newestId);
});
it('returns exactly one card per followed author', function () {
$viewer = User::factory()->create();
$viewer->refresh();
[$author] = followedAuthorWithStories($viewer, 3);
$rows = collect($this->actingAs($viewer, 'web')
->getJson('/api/web/stories/v1/recent')
->assertOk()
->json())
->where('pid', $author->id);
expect($rows)->toHaveCount(1);
});
Loading…
Cancel
Save