Skip to content

Commit

Permalink
polling
Browse files Browse the repository at this point in the history
  • Loading branch information
puklipo committed Dec 21, 2024
1 parent de74fc3 commit 2a9164e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/Console/Labeler/LabelerPollingCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Revolution\Bluesky\Console\Labeler;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Revolution\Bluesky\Events\Labeler\NotificationReceived;
use Revolution\Bluesky\Facades\Bluesky;

/**
* Execute this command periodically with the task scheduler.
*/
class LabelerPollingCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bluesky:labeler:polling {--L|limit=50}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Polling notifications for Labeler';

protected const CACHE_KEY = 'bluesky:labeler:polling:cursor';

protected const REASONS = [
'like',
'repost',
'follow',
'mention',
'reply',
'quote',
'starterpack-joined',
];

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$limit = (int) $this->option('limit');

$cursor = Cache::get(self::CACHE_KEY);

$response = Bluesky::login(Config::string('bluesky.labeler.identifier'), Config::string('bluesky.labeler.password'))
->listNotifications(limit: $limit, cursor: $cursor);

$cursor = $response->json('cursor');
Cache::forever(self::CACHE_KEY, $cursor);

/** @var array $notifications */
$notifications = $response->json('notifications');

foreach ($notifications as $notification) {
$reason = data_get($notification, 'reason');

if (in_array($reason, self::REASONS, true)) {
event(new NotificationReceived($reason, $notification));
}
}

return 0;
}
}
14 changes: 14 additions & 0 deletions src/Events/Labeler/NotificationReceived.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Revolution\Bluesky\Events\Labeler;

class NotificationReceived
{
public function __construct(
public string $reason,
public array $notification,
) {
}
}

0 comments on commit 2a9164e

Please sign in to comment.