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.9 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Support\Social;

use App\Models\Post;
use App\Support\ThreadReplies;

/**
 * Segments of a thread already live on the network, kept in
 * posts.error_context so any retry resumes instead of re-posting.
 */
final class ThreadProgress
{
    public const string KEY = 'thread_progress';

    /**
     * A reply poll is part of the segment, so changing it is a new segment;
     * a segment without one keeps the hash it always had.
     *
     * @param  list<string>  $mediaIds
     * @param  array<string, mixed>|null  $poll
     */
    public static function hash(string $text, array $mediaIds = [], ?array $poll = null): string
    {
        return sha1((string) json_encode(blank($poll) ? [$text, $mediaIds] : [$text, $mediaIds, $poll]));
    }

    /**
     * The hash of the root already live. The root is the target's identity, so a
     * resume keeps it even when the same text would now hash differently.
     *
     * @param  array<string, mixed>|null  $context
     */
    public static function rootHash(?array $context): ?string
    {
        $root = data_get($context, self::KEY.'.0');
        $hash = data_get($root, 'hash');

        return is_string($hash) && is_string(data_get($root, 'id')) ? $hash : null;
    }

    /**
     * The failure message of a thread that stopped with part of it live.
     *
     * @param  array<string, mixed>|null  $context
     */
    public static function failureMessage(Post $post, string $message, ?array $context): string
    {
        $published = count((array) data_get($context, self::KEY, []));
        $total = 1 + (ThreadReplies::supports($post->platform) ? count(ThreadReplies::of($post->meta)) : 0);

        return $published > 0 && $published < $total
            ? __('posts.errors.thread_incomplete', ['published' => $published, 'total' => $total, 'error' => $message])
            : $message;
    }

    /**
     * The stored segments that still match the current ones, in order. The
     * first segment whose text changed ends the resumable part.
     *
     * @param  array<string, mixed>|null  $context
     * @param  list<string>  $hashes
     * @return list<array<string, mixed>>
     */
    public static function resumable(?array $context, array $hashes): array
    {
        $kept = [];

        foreach (array_values((array) data_get($context, self::KEY, [])) as $index => $entry) {
            if (! is_array($entry) || ($hashes[$index] ?? null) !== data_get($entry, 'hash') || ! is_string(data_get($entry, 'id'))) {
                break;
            }

            $kept[] = $entry;
        }

        return $kept;
    }

    /**
     * @param  list<array<string, mixed>>  $posted
     */
    public static function remember(Post $post, array $posted): void
    {
        $post->writePublication([
            'error_context' => [...($post->error_context ?? []), self::KEY => $posted],
        ]);
    }
}