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 */ 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; } }