|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Jobs\Twitter; |
| 6 | + |
| 7 | +use App\Models\TwitterAccount; |
| 8 | +use App\Models\TwitterFollower; |
| 9 | +use Illuminate\Bus\Queueable; |
| 10 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 11 | +use Illuminate\Foundation\Bus\Dispatchable; |
| 12 | +use Illuminate\Queue\InteractsWithQueue; |
| 13 | +use Illuminate\Queue\SerializesModels; |
| 14 | +use Illuminate\Support\Carbon; |
| 15 | +use Illuminate\Support\Facades\Http; |
| 16 | +use Illuminate\Support\Facades\Log; |
| 17 | + |
| 18 | +class PollFollowers implements ShouldQueue |
| 19 | +{ |
| 20 | + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| 21 | + |
| 22 | + public function __construct(public TwitterAccount $twitterAccount) |
| 23 | + { |
| 24 | + $this->connection = config('twitter.queues.connection', 'redis'); |
| 25 | + $this->queue = config('twitter.queues.poll_queue', 'twitter-polling'); |
| 26 | + $this->onConnection($this->connection); |
| 27 | + $this->onQueue($this->queue); |
| 28 | + } |
| 29 | + |
| 30 | + public function handle(): void |
| 31 | + { |
| 32 | + $account = $this->twitterAccount->fresh(); |
| 33 | + if ($account === null || $account->disconnected_at !== null) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $token = $account->access_token; |
| 38 | + if ($token === null) { |
| 39 | + Log::warning('Twitter polling skipped due to missing access token', [ |
| 40 | + 'twitter_account_id' => $account->id, |
| 41 | + ]); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + $url = sprintf('%s/2/users/%s/followers', config('twitter.base_url'), $account->twitter_id); |
| 46 | + $paginationToken = null; |
| 47 | + $welcomeMessage = config('twitter.dm.welcome_message'); |
| 48 | + $queueConnection = config('twitter.queues.connection', 'redis'); |
| 49 | + $dmQueue = config('twitter.queues.dm_queue', 'twitter-dm'); |
| 50 | + |
| 51 | + do { |
| 52 | + $query = [ |
| 53 | + 'max_results' => config('twitter.polling.page_size'), |
| 54 | + 'user.fields' => 'id,name,username', |
| 55 | + ]; |
| 56 | + |
| 57 | + if ($paginationToken !== null) { |
| 58 | + $query['pagination_token'] = $paginationToken; |
| 59 | + } |
| 60 | + |
| 61 | + $response = Http::withToken($token) |
| 62 | + ->acceptJson() |
| 63 | + ->get($url, $query); |
| 64 | + |
| 65 | + if ($response->failed()) { |
| 66 | + Log::error('Twitter followers API request failed', [ |
| 67 | + 'twitter_account_id' => $account->id, |
| 68 | + 'status' => $response->status(), |
| 69 | + 'body' => $response->json(), |
| 70 | + ]); |
| 71 | + |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $followers = $response->json('data', []); |
| 76 | + if (! is_array($followers)) { |
| 77 | + Log::warning('Twitter followers API returned unexpected payload', [ |
| 78 | + 'twitter_account_id' => $account->id, |
| 79 | + ]); |
| 80 | + |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + foreach ($followers as $follower) { |
| 85 | + $followerId = $follower['id'] ?? null; |
| 86 | + if ($followerId === null) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + $record = TwitterFollower::query()->firstOrCreate( |
| 91 | + [ |
| 92 | + 'twitter_account_id' => $account->id, |
| 93 | + 'follower_id' => $followerId, |
| 94 | + ], |
| 95 | + [ |
| 96 | + 'username' => $follower['username'] ?? null, |
| 97 | + 'name' => $follower['name'] ?? null, |
| 98 | + 'seen_at' => Carbon::now(), |
| 99 | + ], |
| 100 | + ); |
| 101 | + |
| 102 | + if (! $record->wasRecentlyCreated) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + SendDirectMessage::dispatch( |
| 107 | + $account, |
| 108 | + $followerId, |
| 109 | + $welcomeMessage, |
| 110 | + )->onConnection($queueConnection) |
| 111 | + ->onQueue($dmQueue); |
| 112 | + } |
| 113 | + |
| 114 | + $paginationToken = $response->json('meta.next_token'); |
| 115 | + } while ($paginationToken !== null); |
| 116 | + |
| 117 | + $account->forceFill([ |
| 118 | + 'last_polled_at' => Carbon::now(), |
| 119 | + ])->save(); |
| 120 | + } |
| 121 | +} |
0 commit comments