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.

2.2 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Support\Analytics;

use App\Enums\SocialAccount\Platform;
use Carbon\CarbonImmutable;

/**
 * How often Insights data is refreshed.
 */
class SyncCadence
{
    public const int METRICS_WINDOW_DAYS = 30;

    /**
     * X bills every read, so after the first read a post is read again only at these ages
     * (days since its UTC publish date). The last one closes the window.
     *
     * @var list<int>
     */
    public const array X_METRICS_DAYS = [2, 3, 7, 14, 28];

    /**
     * A Mastodon poll may run 30 days (`MastodonPoll::MAX_DURATION`), so a
     * Mastodon post is read one day longer: the daily read after the poll ends
     * stores its final result (and the votes a poll hid until then).
     */
    public const int MASTODON_METRICS_WINDOW_DAYS = 31;

    /**
     * A post just published has nothing to measure yet, so its first read waits this long.
     */
    public const int FIRST_READ_DELAY_MINUTES = 60;

    public static function metricsWindowDays(Platform $platform): int
    {
        if ($platform === Platform::X) {
            return max(self::X_METRICS_DAYS);
        }

        if ($platform === Platform::Mastodon) {
            return self::MASTODON_METRICS_WINDOW_DAYS;
        }

        return self::METRICS_WINDOW_DAYS;
    }

    /**
     * The oldest publish time still inside the platform's metrics window on that day.
     */
    public static function metricsWindowStart(Platform $platform, CarbonImmutable $on): CarbonImmutable
    {
        return $on->subDays(self::metricsWindowDays($platform))->startOfDay();
    }

    /**
     * Ages the daily run reads; null means every day of the window.
     *
     * @return list<int>|null
     */
    public static function metricsDays(Platform $platform): ?array
    {
        return $platform === Platform::X ? self::X_METRICS_DAYS : null;
    }

    /**
     * The page reads the last sync time from the report's coverage rows, so a coverage poll keeps it current.
     *
     * @return array{metrics_days: int}
     */
    public static function toArray(): array
    {
        return [
            'metrics_days' => self::METRICS_WINDOW_DAYS,
        ];
    }
}