This commit is contained in:
deploy-bot committed 2026-12-04 15:10:03 +00:00
commit 860182098c
277 files changed
+18338

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
<?php
namespace IriksIT\Manager\Support\Sharing;
use IriksIT\Manager\Models\Post;
use IriksIT\Manager\Models\PostShare;
use Throwable;
/**
* Sharing posts on the platforms: doing it, and what came of it. The only code that writes `post_shares`.
*
* A site posts for real only with the setting `manager.sharing.live`. Without it (staging, local) a share is written down
* as done, marked "dry run", and nothing is sent. A row moves from `pending` to `sharing` before the platform is called,
* so two clicks can never post the same thing twice, and a failure stays failed until the owner presses the button again.
*/
final class PostSharing
{
public function __construct(private readonly Sharers $sharers) {}
public static function isLive(): bool
{
return config('manager.sharing.live') === true;
}
/**
* Shares a published post on one platform right now, also when it was shared there before.
*
* @throws SharingException when the post is not live or the platform is not connected
*/
public function shareNow(Post $post, string $platform): PostShare
{
if (! $post->isPublished()) {
throw new SharingException(__('Only a post that is live can be shared.'));
}
if (! $this->sharers->isConnected($platform)) {
throw new SharingException(__(':platform is not connected. Connect it under Sharing.', ['platform' => $this->sharers->for($platform)->label()]));
}
PostShare::query()->where('post_id', $post->id)->where('platform', $platform)->whereIn('status', [PostShare::PENDING, PostShare::FAILED])->delete();
return $this->share(PostShare::query()->create(['post_id' => $post->id, 'platform' => $platform, 'status' => PostShare::PENDING]));
}
/**
* How sharing stands for a post, per platform, for the manager.
*
* @return list<array{platform: string, label: string, connected: bool, preview: string, last: ?array{status: string, shared_at: ?string, url: ?string, error: ?string, dry_run: bool}}>
*/
public function overview(Post $post): array
{
$shares = PostShare::query()->where('post_id', $post->id)->orderByDesc('id')->get();
$overview = [];
foreach ($this->sharers->names() as $name) {
$sharer = $this->sharers->for($name);
$last = $shares->first(fn (PostShare $share) => $share->platform === $name && $share->status !== PostShare::PENDING);
$overview[] = [
'platform' => $name,
'label' => $sharer->label(),
'connected' => $sharer->connection() !== null,
'preview' => ShareText::for($post, $sharer->maxLength()),
'last' => $last instanceof PostShare ? [
'status' => $last->status,
'shared_at' => $last->shared_at?->toIso8601String(),
'url' => $last->external_url,
'error' => $last->error,
'dry_run' => (bool) $last->dry_run,
] : null,
];
}
return $overview;
}
private function share(PostShare $share): PostShare
{
// Claim the row: whoever changes it from pending to sharing is the one that posts.
$claimed = PostShare::query()->whereKey($share->id)->where('status', PostShare::PENDING)->update(['status' => PostShare::SHARING]);
if ($claimed !== 1) {
return $share->refresh();
}
$post = $share->post;
try {
if (! $post instanceof Post || ! $this->sharers->isConnected($share->platform)) {
throw new SharingException(__('This platform is not connected. Connect it under Sharing.'));
}
$sharer = $this->sharers->for($share->platform);
$text = ShareText::for($post, $sharer->maxLength());
$url = ShareText::url($post, $share->platform);
if ($url === null) {
throw new SharingException(__('The site did not say where its posts live (manager.sharing.post_url).'));
}
$share->update(['text' => $text]);
if (! self::isLive()) {
$share->update(['status' => PostShare::SHARED, 'dry_run' => true, 'shared_at' => now(), 'error' => null]);
return $share;
}
$posted = $sharer->publish(new ShareContent($text, $url, (string) $post->title, ShareText::description($post)));
$share->update([
'status' => PostShare::SHARED,
'external_id' => mb_substr($posted->id, 0, 255),
'external_url' => $posted->url,
'shared_at' => now(),
'error' => null,
]);
} catch (SharingException $e) {
$share->update(['status' => PostShare::FAILED, 'error' => mb_substr($e->getMessage(), 0, 500)]);
} catch (Throwable $e) {
report($e);
$share->update(['status' => PostShare::FAILED, 'error' => __('Something went wrong on this site while sharing. It is in the log.')]);
}
return $share;
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace IriksIT\Manager\Support\Sharing;
/** Everything a platform needs to post about one post of the site. */
final class ShareContent
{
/**
* @param string $text what the post says, already cut to what the platform takes
* @param string $url the address of the post on the site, with `?via=<platform>`
*/
public function __construct(
public readonly string $text,
public readonly string $url,
public readonly string $title,
public readonly string $description,
) {}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace IriksIT\Manager\Support\Sharing;
use Illuminate\Support\Str;
use IriksIT\Manager\Models\Post;
use IriksIT\Manager\Support\Markdown;
/** What a shared post says and links to. */
final class ShareText
{
/**
* The address of the post on the site, marked so the statistics know which platform a reader came from. Built from
* `manager.sharing.post_url` (for example `https://example.test/blog/{slug}`); null when the site did not say where posts live.
*/
public static function url(Post $post, string $platform): ?string
{
$pattern = (string) config('manager.sharing.post_url');
if ($pattern === '' || ! str_contains($pattern, '{slug}')) {
return null;
}
$url = str_replace('{slug}', rawurlencode((string) $post->slug), $pattern);
return $url.(str_contains($url, '?') ? '&' : '?').'via='.rawurlencode($platform);
}
public static function description(Post $post): string
{
$excerpt = trim((string) $post->excerpt);
return $excerpt !== '' ? $excerpt : Str::limit(Markdown::toPlainText((string) $post->body), 160);
}
/** The words as they are posted, cut to what the platform takes. */
public static function for(Post $post, int $limit): string
{
return Str::limit(self::description($post), max(1, $limit));
}
}
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace IriksIT\Manager\Support\Sharing;
/** What a platform answered after a post: its id for it and, when it says, where to see it. */
final class SharedPost
{
public function __construct(public readonly string $id, public readonly ?string $url = null) {}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace IriksIT\Manager\Support\Sharing;
use IriksIT\Manager\Contracts\Sharer;
/** Which code posts on which platform: `manager.sharing.platforms` (name => class of a `Sharer`). */
final class Sharers
{
/** @return list<string> the names of the platforms, as the routes and the database know them */
public function names(): array
{
return array_values(array_filter(array_keys((array) config('manager.sharing.platforms', [])), fn ($name) => is_string($name) && preg_match('/^[a-z][a-z0-9_]{0,19}$/', $name) === 1));
}
public function has(string $name): bool
{
return in_array($name, $this->names(), true);
}
public function for(string $name): Sharer
{
$class = config("manager.sharing.platforms.{$name}");
abort_unless($this->has($name) && is_string($class) && is_subclass_of($class, Sharer::class), 404);
return app($class);
}
/** The owner has connected this platform. */
public function isConnected(string $name): bool
{
return $this->has($name) && $this->for($name)->connection() !== null;
}
}
+8
View File
@@ -0,0 +1,8 @@
<?php
namespace IriksIT\Manager\Support\Sharing;
use RuntimeException;
/** Something a platform said or did that the owner can read and act on. Never holds a token. */
class SharingException extends RuntimeException {}