v0.8.0
This commit is contained in:
commit
bf5c31169a
221 files changed
+16292
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user