Skip to content

Commit

Permalink
Add check() and logout()
Browse files Browse the repository at this point in the history
  • Loading branch information
kawax committed Apr 2, 2024
1 parent f502b4e commit 299324a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
20 changes: 20 additions & 0 deletions docs/bluesky-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,23 @@ Bluesky::login(identifier: config('bluesky.identifier'), password: config('blues

dump($response);
```

## Login
```php
use Revolution\Bluesky\Facades\Bluesky;

Bluesky::login(identifier: config('bluesky.identifier'), password: config('bluesky.password'));

dump(Bluesky::check());
// true
```

## Logout
```php
use Revolution\Bluesky\Facades\Bluesky;

Bluesky::logout();

dump(Bluesky::check());
// false
```
14 changes: 14 additions & 0 deletions src/BlueskyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Illuminate\Http\Client\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Revolution\Bluesky\Contracts\Factory;
use Revolution\Bluesky\Enums\AtProto;

class BlueskyClient implements Factory
{
use Macroable;
use Conditionable;

protected ?Collection $session = null;

Expand Down Expand Up @@ -96,6 +98,18 @@ public function post(string $text): Response
]);
}

public function check(): bool
{
return ! empty($this->session['accessJwt']);
}

public function logout(): static
{
$this->session = null;

return $this;
}

private function baseUrl(): string
{
return $this->service.'/xrpc/';
Expand Down
7 changes: 7 additions & 0 deletions src/Facades/Bluesky.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
* @method static static service(string $service)
* @method static mixed session(string $key)
* @method static static login(string $identifier, string $password)
* @method static static logout()
* @method static bool check()
* @method static Response feed(int $limit = 50, string $cursor = '', string $filter = 'posts_with_replies')
* @method static Response timeline(int $limit = 50, string $cursor = '')
* @method static Response post(string $text)
* @method static void macro(string $name, object|callable $macro)
* @method static \Revolution\Bluesky\BlueskyClient|mixed when(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
* @method static \Revolution\Bluesky\BlueskyClient|mixed unless(\Closure|mixed|null $value = null, callable|null $callback = null, callable|null $default = null)
*
* @see \Revolution\Bluesky\BlueskyClient
*/
class Bluesky extends Facade
{
Expand Down
24 changes: 23 additions & 1 deletion tests/Feature/Client/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ public function test_login()
});

$this->assertSame('test', $client->session('accessJwt'));
$this->assertTrue($client->check());
}

public function test_logout()
{
Http::fake(fn () => ['accessJwt' => 'test', 'did' => 'test']);

$client = new BlueskyClient();

$client->service('https://bsky.social')
->login(identifier: 'identifier', password: 'password');

Http::assertSent(function (Request $request) {
return $request['identifier'] === 'identifier';
});

$client->logout();

$this->assertNull($client->session());
$this->assertFalse($client->check());
}

public function test_session()
Expand All @@ -48,7 +68,9 @@ public function test_feed()
->push(['feed' => ['post' => []]]);

$response = Bluesky::login(identifier: 'identifier', password: 'password')
->feed(limit: 10, cursor: '2024', filter: 'posts_with_media');
->when(Bluesky::check(), function () {
return Bluesky::feed(limit: 10, cursor: '2024', filter: 'posts_with_media');
});

$this->assertTrue($response->collect()->has('feed'));
}
Expand Down

0 comments on commit 299324a

Please sign in to comment.