Something went wrong. Try again.
Laravel AT Protocol Client (alpha & unstable)
Something went wrong. Try again.
4.6 kB · 139 lines
PHP
at dev
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140<?php
namespace SocialDept\AtpClient\Client\Records;
use DateTimeInterface;use SocialDept\AtpClient\Attributes\ScopedEndpoint;use SocialDept\AtpClient\Builders\PostBuilder;use SocialDept\AtpClient\Client\Requests\Request;use SocialDept\AtpClient\Contracts\Recordable;use SocialDept\AtpClient\Data\Record;use SocialDept\AtpClient\Data\Responses\Atproto\Repo\CreateRecordResponse;use SocialDept\AtpClient\Data\Responses\Atproto\Repo\DeleteRecordResponse;use SocialDept\AtpClient\Data\Responses\Atproto\Repo\PutRecordResponse;use SocialDept\AtpClient\Data\StrongRef;use SocialDept\AtpClient\Enums\Nsid\BskyFeed;use SocialDept\AtpClient\Enums\Scope;use SocialDept\AtpClient\RichText\TextBuilder;use SocialDept\AtpSchema\Generated\App\Bsky\Feed\Defs\PostView;
class PostRecordClient extends Request{ /** * Create a new post builder bound to this client */ public function build(): PostBuilder { return PostBuilder::make()->for($this); }
/** * Create a post * * @requires transition:generic OR (rpc:com.atproto.repo.createRecord AND repo:app.bsky.feed.post?action=create) */ #[ScopedEndpoint(Scope::TransitionGeneric, granular: 'rpc:com.atproto.repo.createRecord')] #[ScopedEndpoint(Scope::TransitionGeneric, granular: 'repo:app.bsky.feed.post?action=create')] public function create( string|array|Recordable $content, ?array $facets = null, ?array $embed = null, ?array $reply = null, ?array $langs = null, ?DateTimeInterface $createdAt = null ): CreateRecordResponse { // Handle different input types if (is_string($content)) { $record = [ 'text' => $content, 'facets' => $facets ?? TextBuilder::parse($content)['facets'], ]; } elseif ($content instanceof Recordable) { $record = $content->toArray(); } else { $record = $content; }
// Add optional fields (only for non-Recordable inputs) if (! ($content instanceof Recordable)) { if ($embed) { $record['embed'] = $embed; } if ($reply) { $record['reply'] = $reply; } if ($langs) { $record['langs'] = $langs; } }
if (! isset($record['createdAt'])) { $record['createdAt'] = ($createdAt ?? now())->format('c'); }
// Ensure $type is set if (! isset($record['$type'])) { $record['$type'] = BskyFeed::Post->value; }
return $this->atp->atproto->repo->createRecord( collection: BskyFeed::Post, record: $record ); }
/** * Update a post * * @requires transition:generic OR (rpc:com.atproto.repo.putRecord AND repo:app.bsky.feed.post?action=update) */ #[ScopedEndpoint(Scope::TransitionGeneric, granular: 'rpc:com.atproto.repo.putRecord')] #[ScopedEndpoint(Scope::TransitionGeneric, granular: 'repo:app.bsky.feed.post?action=update')] public function update(string $rkey, array $record): PutRecordResponse { // Ensure $type is set if (! isset($record['$type'])) { $record['$type'] = BskyFeed::Post->value; }
return $this->atp->atproto->repo->putRecord( collection: BskyFeed::Post, rkey: $rkey, record: $record ); }
/** * Delete a post * * @requires transition:generic OR (rpc:com.atproto.repo.deleteRecord AND repo:app.bsky.feed.post?action=delete) */ #[ScopedEndpoint(Scope::TransitionGeneric, granular: 'rpc:com.atproto.repo.deleteRecord')] #[ScopedEndpoint(Scope::TransitionGeneric, granular: 'repo:app.bsky.feed.post?action=delete')] public function delete(string $rkey): DeleteRecordResponse { return $this->atp->atproto->repo->deleteRecord( collection: BskyFeed::Post, rkey: $rkey ); }
/** * Get a post * * @requires transition:generic (rpc:com.atproto.repo.getRecord) */ #[ScopedEndpoint(Scope::TransitionGeneric, granular: 'rpc:com.atproto.repo.getRecord')] public function get(string $rkey, ?string $cid = null): Record { $response = $this->atp->atproto->repo->getRecord( repo: $this->atp->client->session()->did(), collection: BskyFeed::Post, rkey: $rkey, cid: $cid );
return Record::fromArrayRaw($response->toArray()); }
}