Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

Read the streak's rank from the latest ranking on every page (#530)

Paulo Castellanof844a83a6c46
diff --git a/app/Actions/Analytics/ShowPostingStreak.php b/app/Actions/Analytics/ShowPostingStreak.php
index e2e4bb9c7..0deb85070 100644
--- a/app/Actions/Analytics/ShowPostingStreak.php
+++ b/app/Actions/Analytics/ShowPostingStreak.php
@@ -43,6 +43,8 @@ final class ShowPostingStreak
/**
* Null while the workspace has no streak: the sidebar shows no badge then.
* The streak is cached even at zero weeks, since the cache keeps no null.
+ * Its rank is read from the latest ranking on every call, so a new
+ * ranking shows at once without forgetting any badge.
*
* @return array{weeks: int, posted_this_week: bool, top_percent: ?int}|null
*/
@@ -53,14 +55,17 @@ final class ShowPostingStreak
$streak = Cache::remember(
"posting-streak:{$workspace->id}:{$version}:{$user->id}:{$user->timezone}:{$user->week_starts_on->value}",
now()->addMinutes(self::CACHE_MINUTES),
- fn (): array => self::build($user, $workspace),
+ fn (): array => app(BuildPostingStreak::class)->execute($workspace, $user->timezone, $user->week_starts_on),
);
if (data_get($streak, 'weeks') === 0) {
return null;
}
- return $streak;
+ return [
+ ...$streak,
+ 'top_percent' => RankPostingStreaks::topPercent(data_get($streak, 'weeks')),
+ ];
}
/** Drops every member's cached badge of the workspace, once the change that calls it is committed. */
@@ -73,17 +78,4 @@ final class ShowPostingStreak
{
return "posting-streak:version:{$workspaceId}";
}
-
- /**
- * @return array{weeks: int, posted_this_week: bool, top_percent: ?int}
- */
- private static function build(User $user, Workspace $workspace): array
- {
- $streak = app(BuildPostingStreak::class)->execute($workspace, $user->timezone, $user->week_starts_on);
-
- return [
- ...$streak,
- 'top_percent' => RankPostingStreaks::topPercent(data_get($streak, 'weeks')),
- ];
- }
}
diff --git a/tests/Feature/Analytics/PostingStreakTest.php b/tests/Feature/Analytics/PostingStreakTest.php
index e5f1e2009..0ad16c6b3 100644
--- a/tests/Feature/Analytics/PostingStreakTest.php
+++ b/tests/Feature/Analytics/PostingStreakTest.php
@@ -346,3 +346,19 @@ test('the sidebar gets no streak without a current workspace or without app acce
expect(data_get(ShowPostingStreak::forSidebar($user->fresh()), 'weeks'))->toBe(1);
});
+
+test('a new ranking reaches a badge already cached at once', function () {
+ $user = User::factory()->create(['current_workspace_id' => $this->workspace->id, 'timezone' => 'UTC', 'week_starts_on' => WeekStart::Monday]);
+ subscribeAccount($this->workspace->account);
+ postingStreakPublication($this->instagram, $this->thisWeek->addDay());
+
+ expect(ShowPostingStreak::cached($user, $this->workspace))->toBe(['weeks' => 1, 'posted_this_week' => true, 'top_percent' => null]);
+
+ foreach (range(1, RankPostingStreaks::MIN_RANKED - 1) as $_) {
+ subscribeAccount(Workspace::factory()->create()->account);
+ }
+
+ app(RankPostingStreaks::class)->execute();
+
+ expect(ShowPostingStreak::cached($user, $this->workspace))->toBe(['weeks' => 1, 'posted_this_week' => true, 'top_percent' => 10]);
+});