-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Revolution\Bluesky\FeedGenerator; | ||
|
||
use Illuminate\Http\Request; | ||
|
||
final class FeedGenerator | ||
{ | ||
protected static array $algos; | ||
|
||
/** | ||
* Register FeedGenerator algorithm. | ||
* | ||
* ``` | ||
* // Register in your AppServiceProvider::boot() | ||
* | ||
* FeedGenerator::register(name: 'artisan', algo: function(int $limit, string $cursor): array { | ||
* // The implementation is entirely up to you. | ||
* | ||
* $response = Bluesky::searchPosts(q: '#laravel', limit: $limit, cursor: $cursor); | ||
* | ||
* $cursor = $response->json('cursor'); | ||
* $feed = $response->collect('posts')->map(function(array $post) { | ||
* return ['post' => data_get($post, 'uri')]; | ||
* })->toArray(); | ||
* | ||
* return compact('cursor', 'feed'); | ||
* }); | ||
* ``` | ||
* | ||
* @param string $name short name. Used in generator url. `at://did:.../app.bsky.feed.generator/{name}` | ||
* @param callable(int $limit, string $cursor, string $feed): array $algo | ||
*/ | ||
public static function register(string $name, callable $algo): void | ||
{ | ||
self::$algos[$name] = $algo; | ||
} | ||
|
||
public static function getFeedSkeleton(string $name, int $limit, string $cursor, Request $request): mixed | ||
{ | ||
return call_user_func(self::$algos[$name], $limit, $cursor, $request); | ||
} | ||
|
||
public static function has(string $name): bool | ||
{ | ||
return isset(self::$algos[$name]); | ||
} | ||
|
||
public static function missing(string $name): bool | ||
{ | ||
return ! self::has($name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Revolution\Bluesky\FeedGenerator\Http; | ||
|
||
use Illuminate\Http\Request; | ||
use Revolution\AtProto\Lexicon\Enum\Feed; | ||
use Revolution\Bluesky\FeedGenerator\FeedGenerator; | ||
use Revolution\Bluesky\Support\AtUri; | ||
|
||
class FeedSkeletonController | ||
{ | ||
public function __invoke(Request $request): mixed | ||
{ | ||
$at = AtUri::parse($request->input('feed')); | ||
|
||
if ($at->collection() !== Feed::Generator->value || FeedGenerator::missing($at->rkey())) { | ||
abort(404); | ||
} | ||
|
||
return FeedGenerator::getFeedSkeleton( | ||
name: $at->rkey(), | ||
limit: $request->input('limit', 50), | ||
cursor: $request->input('cursor', ''), | ||
request: $request, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Revolution\Bluesky\Record; | ||
|
||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Support\Traits\Conditionable; | ||
use Illuminate\Support\Traits\Macroable; | ||
use Illuminate\Support\Traits\Tappable; | ||
use Revolution\AtProto\Lexicon\Record\App\Bsky\Feed\AbstractGenerator; | ||
use Revolution\Bluesky\Contracts\Recordable; | ||
use Revolution\Bluesky\Types\BlobRef; | ||
use Revolution\Bluesky\Types\SelfLabels; | ||
|
||
final class Generator extends AbstractGenerator implements Arrayable, Recordable | ||
{ | ||
use HasRecord; | ||
use Macroable; | ||
use Conditionable; | ||
use Tappable; | ||
|
||
public function __construct(string $did, string $displayName) | ||
{ | ||
$this->did = $did; | ||
$this->displayName = $displayName; | ||
} | ||
|
||
public static function create(string $did, string $displayName): self | ||
{ | ||
return new self($did, $displayName); | ||
} | ||
|
||
public function did(string $did): self | ||
{ | ||
$this->did = $did; | ||
|
||
return $this; | ||
} | ||
|
||
public function displayName(string $displayName): self | ||
{ | ||
$this->displayName = $displayName; | ||
|
||
return $this; | ||
} | ||
|
||
public function description(?string $description = null): self | ||
{ | ||
$this->description = $description; | ||
|
||
return $this; | ||
} | ||
|
||
public function avatar(null|BlobRef|array|callable $avatar = null): self | ||
{ | ||
if (is_null($avatar)) { | ||
$this->avatar = $avatar; | ||
|
||
return $this; | ||
} | ||
|
||
if (is_callable($avatar)) { | ||
$avatar = call_user_func($avatar); | ||
} | ||
|
||
if ($avatar instanceof BlobRef) { | ||
$avatar = $avatar->toArray(); | ||
} | ||
|
||
$this->avatar = $avatar; | ||
|
||
return $this; | ||
} | ||
|
||
public function labels(?SelfLabels $labels = null): self | ||
{ | ||
$this->labels = $labels?->toArray(); | ||
|
||
return $this; | ||
} | ||
} |