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.

1.7 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Models\Scopes;

use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Query\Builder as QueryBuilder;

/**
 * Default channel order: position, then created_at and id. Skipped when the
 * query already orders, groups, is DISTINCT, unions or selects only raw
 * expressions (relation count sub-queries); dropped again for aggregates,
 * since PostgreSQL rejects an ORDER BY column that is neither grouped nor
 * aggregated.
 */
class SocialAccountOrderScope implements Scope
{
    public function apply(Builder $builder, Model $model): void
    {
        $query = $builder->getQuery();

        if (! $this->acceptsDefaultOrder($query)) {
            return;
        }

        $builder
            ->orderBy($model->qualifyColumn('position'))
            ->orderBy($model->qualifyColumn('created_at'))
            ->orderBy($model->qualifyColumn($model->getKeyName()));

        $query->beforeQuery(function (QueryBuilder $query): void {
            if ($query->aggregate !== null) {
                $query->orders = null;
                $query->bindings['order'] = [];
            }
        });
    }

    private function acceptsDefaultOrder(QueryBuilder $query): bool
    {
        if ($query->aggregate !== null || filled($query->orders) || filled($query->groups) || $query->distinct !== false || filled($query->unions)) {
            return false;
        }

        $columns = $query->columns ?? [];

        return $columns === [] || array_filter($columns, fn (mixed $column): bool => ! $column instanceof Expression) !== [];
    }
}