Skip to content

Commit

Permalink
labeler
Browse files Browse the repository at this point in the history
Update Labeler.php

Update LabelerServer.php

Update HttpServer.php

Update HttpServer.php

Update AbstractLabeler.php

Update AtpClient.php

Update HasShortHand.php

Update HasShortHand.php

wip

Update AtBytesObject.php

wip

wip

Update LabelerServer.php

wip

Update LabelerServer.php

Create HttpServer.php

Update ArtisanLabeler.php

Update LabelDefinition.php

wip

Update HasShortHand.php

Update HasShortHand.php

Update AtpClient.php

wip

Update UnsignedLabel.php

Update UnsignedLabel.php

wip

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update DidDocument.php

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update SignedLabel.php

Update SignedLabel.php

wip

Update AbstractLabeler.php

wip

wip

wip

labeler

labeler

Update LabelerServer.php

wip

wip

Create LabelerTest.php

Update Encoder.php

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update Labeler.php

Update Labeler.php

Update LabelerServer.php

Create AtBytes.php

Update BytesWrapper.php

Update LabelerServer.php

Update LabelerController.php

Update LabelerServer.php

Update Labeler.php

Update Labeler.php

Update LabelerServer.php

Update BytesWrapper.php

Update LabelerServer.php

Update EmitEventResponse.php

Update LabelerServer.php

Update LabelerServer.php

Update Labeler.php

Update HasShortHand.php

Update LabelerServer.php

Update HasShortHand.php

Update LabelerServer.php

Update Labeler.php

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update LabelerServer.php

Update HasShortHand.php

Update HasShortHand.php

Update EmitEventResponse.php

Update HasShortHand.php

Update HasShortHand.php

Update HasShortHand.php

Update LabelerServer.php

Update LabelerServer.php

LabelerServer

Update HasShortHand.php

Update AbstractLabeler.php
  • Loading branch information
puklipo committed Dec 26, 2024
1 parent 6212b4d commit 140397c
Show file tree
Hide file tree
Showing 32 changed files with 929 additions and 102 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"require-dev": {
"orchestra/testbench": "^9.0",
"larastan/larastan": "^3.0",
"phpstan/extension-installer": "^1.4"
"phpstan/extension-installer": "^1.4",
"workerman/workerman": "^5.0-rc.3",
"revolt/event-loop": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
32 changes: 32 additions & 0 deletions src/Casts/AtBytesObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Revolution\Bluesky\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Revolution\Bluesky\Core\CBOR\AtBytes;

class AtBytesObject implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): AtBytes
{
return new AtBytes($value);
}

/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): string
{
return $value instanceof AtBytes ? $value->toBytes() : $value;
}
}
52 changes: 52 additions & 0 deletions src/Console/Labeler/LabelerServeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Revolution\Bluesky\Console\Labeler;

use Illuminate\Console\Command;
use Revolution\Bluesky\Labeler\Server\LabelerServer;
use Workerman\Worker;

/**
* ```
* php artisan bluesky:labeler:server start
* ```
*/
class LabelerServeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bluesky:labeler:server {cmd} {--H|host=127.0.0.1} {--P|port=7000} {--d|daemon=}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Labeler WebSocket server';

/**
* Execute the console command.
*
* @return int
*/
public function handle(LabelerServer $server): int
{
if (! class_exists(Worker::class)) {
$this->error('Please install workerman/workerman');

return 1;
}

$host = (string) $this->option('host');
$port = (int) $this->option('port');

$server->start($host, $port);

return 0;
}
}
48 changes: 48 additions & 0 deletions src/Core/CBOR/AtBytes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Revolution\Bluesky\Core\CBOR;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;

/**
* @link https://github.com/mary-ext/atcute/blob/trunk/packages/utilities/cbor/lib/bytes.ts
*/
final readonly class AtBytes implements Arrayable, Jsonable
{
public function __construct(protected string $bytes)
{
}

/**
* ```
* $bytes = AtBytes::fromArray(['$bytes' => 'base64'])->toBytes();
* ```
*/
public static function fromArray(array $array): self
{
return new self(base64_decode($array['$bytes']));
}

public function toBytes(): string
{
return $this->bytes;
}

public function toArray(): array
{
return ['$bytes' => $this->encode()];
}

public function toJson($options = 0): string
{
return json_encode($this->toArray(), $options);
}

protected function encode(): string
{
return base64_encode($this->bytes);
}
}
34 changes: 0 additions & 34 deletions src/Core/CBOR/BytesWrapper.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Core/CBOR/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ private function readString(int $length): string
return $this->stream->read(min($length, $this->stream_size));
}

private function readBytes(int $length): BytesWrapper
private function readBytes(int $length): AtBytes
{
return new BytesWrapper($this->stream->read(min($length, $this->stream_size)));
return new AtBytes($this->stream->read(min($length, $this->stream_size)));
}

private function readCid(int $length): CIDLinkWrapper
Expand Down
7 changes: 4 additions & 3 deletions src/Core/CBOR/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,10 @@ private function writeValue(mixed $val): void
return;
}

$filtered = collect((array) $val)
->reject(fn ($val, $key) => $key !== 'prev' && is_null($val))
->toArray();
$filtered = $val;
// $filtered = collect((array) $val)
// ->reject(fn ($val, $key) => $key !== 'prev' && is_null($val))
// ->toArray();

uksort($filtered, new MapKeySort());

Expand Down
6 changes: 3 additions & 3 deletions src/Core/CBOR/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function __invoke(mixed $data): mixed
}

// Only here is in ['$bytes' => 'base64'] format
if ($key === 'sig' && $item instanceof BytesWrapper) {
if ($key === 'sig' && $item instanceof AtBytes) {
return $item->toArray();
}

if ($item instanceof BytesWrapper) {
return $item->bytes();
if ($item instanceof AtBytes) {
return $item->toBytes();
}

return $this($item);
Expand Down
53 changes: 52 additions & 1 deletion src/HasShortHand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use BackedEnum;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Client\Response;
use InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;
Expand Down Expand Up @@ -33,6 +34,7 @@
use Revolution\Bluesky\Record\Repost;
use Revolution\Bluesky\Record\ThreadGate;
use Revolution\Bluesky\Support\AtUri;
use Revolution\Bluesky\Types\RepoRef;
use Revolution\Bluesky\Types\StrongRef;

use function Illuminate\Support\enum_value;
Expand Down Expand Up @@ -238,7 +240,6 @@ public function post(Post|string|array $text): Response
);
}


/**
* @param string $uri at://did:plc:.../app.bsky.feed.post/{rkey}
*/
Expand Down Expand Up @@ -648,4 +649,54 @@ public function deleteLabelDefinitions(): Response
rkey: 'self',
);
}

/**
* ```
* $response = Bluesky::login(config('bluesky.labeler.identifier'), config('bluesky.labeler.password'))
* ->createLabels(RepoRef::to('did'), ['label1', 'label2']);
* ```
*/
public function createLabels(RepoRef|StrongRef|array $subject, array $labels): Response
{
$event = [
'$type' => 'tools.ozone.moderation.defs#modEventLabel',
'createLabelVals' => $labels,
'negateLabelVals' => [],
];

return $this->emitEventLabel(
event: $event,
subject: $subject,
);
}

public function deleteLabels(RepoRef|StrongRef|array $subject, array $labels): Response
{
$event = [
'$type' => 'tools.ozone.moderation.defs#modEventLabel',
'createLabelVals' => [],
'negateLabelVals' => $labels,
];

return $this->emitEventLabel(
event: $event,
subject: $subject,
);
}

private function emitEventLabel(array $event, RepoRef|StrongRef|array $subject): Response
{
$labeler = $this->assertDid();

$subject = $subject instanceof Arrayable ? $subject->toArray() : $subject;

return $this->client(auth: true)
->ozone()
->withServiceProxy($labeler.'#atproto_labeler')
->emitEvent(
event: $event,
subject: $subject,
createdBy: $labeler,
);
}
}
41 changes: 29 additions & 12 deletions src/Labeler/AbstractLabeler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Revolution\Bluesky\Labeler;

use Revolution\AtProto\Lexicon\Contracts\Com\Atproto\Label;
use Revolution\AtProto\Lexicon\Contracts\Com\Atproto\Moderation;
use Illuminate\Http\Request;

abstract class AbstractLabeler implements Label, Moderation
abstract class AbstractLabeler
{
/**
* Label definitions.
Expand All @@ -20,9 +19,15 @@ abstract class AbstractLabeler implements Label, Moderation
* return [
* new LabelDefinition(
* identifier: 'artisan',
* locales: [
* new LabelLocale(
* lang: 'en',
* name: 'artisan',
* description: 'Web artisan',
* ),
* ],
* severity: 'inform',
* blurs: 'none',
* locales: [new LabelLocale(lang: 'en', name: 'artisan', description: 'Web artisan')],
* ),
* ];
* }
Expand All @@ -33,25 +38,37 @@ abstract class AbstractLabeler implements Label, Moderation
abstract public function labels(): array;

/**
* Take a moderation action on an actor.
* @return iterable<null|SubscribeLabelMessage>
*
* @return array{id: int, event: array, subject: array, subjectBlobCids: array, createdBy: string, createdAt: string, creatorHandle?: string, subjectHandle?: string}
* @throw LabelerException
*/
abstract public function subscribeLabels(?int $cursor): iterable;

/**
* @return iterable<UnsignedLabel>
*
* @throw LabelerException
*
* @link https://docs.bsky.app/docs/api/tools-ozone-moderation-emit-event
*/
abstract public function emitEvent(array $event, array $subject, string $createdBy, ?array $subjectBlobCids = null): array;
abstract public function emitEvent(Request $request, ?string $user, ?string $token): iterable;

/**
* @return array{cursor: string, labels: array{ver?: int, src: string, uri: string, cid?: string, val: string, neg?: bool, cts: string, exp?: string, sig?: mixed}}
*
* @link https://docs.bsky.app/docs/api/com-atproto-label-query-labels
* @throw LabelerException
*/
abstract public function queryLabels(array $uriPatterns, ?array $sources = null, ?int $limit = 50, ?string $cursor = null): array;
abstract public function saveLabel(SignedLabel $label, string $sign): ?SavedLabel;

/**
* @return array{id: int, reasonType: string, reason: string, subject: array, reportedBy: string, createdAt: string}
*
* @link https://docs.bsky.app/docs/api/com-atproto-moderation-create-report
*/
abstract public function createReport(string $reasonType, array $subject, ?string $reason = null): array;
abstract public function createReport(Request $request): array;

/**
* @return array{cursor: string, labels: array{ver?: int, src: string, uri: string, cid?: string, val: string, neg?: bool, cts: string, exp?: string, sig?: mixed}}
*
* @link https://docs.bsky.app/docs/api/com-atproto-label-query-labels
*/
abstract public function queryLabels(Request $request): array;
}
2 changes: 2 additions & 0 deletions src/Labeler/Actions/DeclareLabelDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use RuntimeException;

/**
* @internal
*
* @link https://github.com/skyware-js/labeler/blob/main/src/scripts/declareLabeler.ts
*/
class DeclareLabelDefinitions
Expand Down
2 changes: 2 additions & 0 deletions src/Labeler/Actions/DeleteLabelDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Revolution\Bluesky\Facades\Bluesky;

/**
* @internal
*
* @link https://github.com/skyware-js/labeler/blob/main/src/scripts/declareLabeler.ts
*/
class DeleteLabelDefinitions
Expand Down
Loading

0 comments on commit 140397c

Please sign in to comment.