Skip to content

Commit acd35c1

Browse files
committedMar 31, 2025
Unsync discord on invalid grants
1 parent e2438f3 commit acd35c1

File tree

7 files changed

+86
-23
lines changed

7 files changed

+86
-23
lines changed
 

‎app/Console/Commands/Users/RegenerateDiscordToken.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
<?php
22

3-
namespace App\Console\Commands;
3+
namespace App\Console\Commands\Users;
44

5+
use App\Jobs\Users\UnsyncDiscord;
56
use App\Services\DiscordService;
6-
use App\Models\User;
77
use Carbon\Carbon;
88
use App\Models\UserApp;
9+
use GuzzleHttp\Exception\ClientException;
910
use Illuminate\Console\Command;
10-
use Illuminate\Support\Facades\Log;
11+
use Illuminate\Support\Str;
1112

1213
class RegenerateDiscordToken extends Command
1314
{
1415
/**
1516
* The name and signature of the console command.
16-
*
17-
* @var string
1817
*/
1918
protected $signature = 'users:renew-discord-tokens';
2019

2120
/**
2221
* The console command description.
23-
*
24-
* @var string
2522
*/
2623
protected $description = 'Renew a user\'s discord api token.';
2724

@@ -34,7 +31,6 @@ class RegenerateDiscordToken extends Command
3431
*/
3532
public function handle()
3633
{
37-
//$userID = $this->argument('user');
3834
$this->service = app()->make(DiscordService::class);
3935

4036
$tokens = UserApp::select(['id', 'user_id', 'access_token', 'refresh_token', 'expires_at', 'updated_at', 'settings'])
@@ -51,8 +47,13 @@ public function handle()
5147
$count = 0;
5248
foreach ($tokens as $token) {
5349
try {
54-
$this->service->user($token->user)->refresh();
50+
$this->service->app($token)->refresh();
5551
$count++;
52+
} catch (ClientException $e) {
53+
if (Str::contains($e->getResponse()->getBody()->getContents(), 'invalid_grant')) {
54+
UnsyncDiscord::dispatch($token);
55+
}
56+
throw $e;
5657
} catch (\Exception $e) {
5758
// Silence errors and ignore
5859
}

‎app/Http/Controllers/Layout/NavigationController.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77

88
class NavigationController extends Controller
99
{
10-
protected NavigationService $service;
11-
12-
public function __construct(NavigationService $navigationService)
10+
public function __construct(protected NavigationService $navigationService)
1311
{
14-
$this->service = $navigationService;
1512
$this->middleware('auth');
1613
}
1714

1815
public function index()
1916
{
20-
$data = $this->service
17+
$data = $this->navigationService
2118
->user(auth()->user())
2219
->data();
2320

‎app/Jobs/Users/UnsyncDiscord.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Jobs\Users;
4+
5+
use App\Models\UserApp;
6+
use App\Notifications\Header;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Queue\Queueable;
9+
10+
class UnsyncDiscord implements ShouldQueue
11+
{
12+
use Queueable;
13+
14+
public int $id;
15+
/**
16+
* Create a new job instance.
17+
*/
18+
public function __construct(UserApp $app)
19+
{
20+
$this->id = $app->id;
21+
}
22+
23+
/**
24+
* Execute the job.
25+
*/
26+
public function handle(): void
27+
{
28+
$app = UserApp::find($this->id);
29+
if (empty($app)) {
30+
return;
31+
}
32+
33+
$app->delete();
34+
$app->user->notify(
35+
new Header(
36+
'apps.discord.invalid',
37+
'brands fa-discord',
38+
'warning',
39+
['route' => 'settings.apps']
40+
));
41+
}
42+
}

‎app/Services/DiscordService.php

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public function user(User $user): self
4040
return $this;
4141
}
4242

43+
public function app(UserApp $app): self
44+
{
45+
$this->app = $app;
46+
$this->user = $app->user;
47+
48+
return $this;
49+
}
50+
4351
public function validate(string $code): self
4452
{
4553
$body = [

‎app/Services/Layout/NavigationService.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use App\Models\Pledge;
1414
use App\Notifications\Header;
1515
use App\Traits\UserAware;
16+
use Illuminate\Support\Arr;
1617

1718
class NavigationService
1819
{
@@ -174,17 +175,19 @@ protected function notifications(): array
174175
foreach ($this->user->notifications()->unread()->take(5)->get() as $not) {
175176
$url = '';
176177
// @phpstan-ignore-next-line
177-
if (\Illuminate\Support\Arr::has($not->data['params'], 'link')) {
178-
// @phpstan-ignore-next-line
179-
$url = $not->data['params']['link'];
178+
$data = $not->data;
179+
if (Arr::has($data['params'], 'link')) {
180+
$url = $data['params']['link'];
180181
if (! \Illuminate\Support\Str::startsWith($url, 'http')) {
181182
$url = url(app()->getLocale() . '/' . $url);
182183
}
184+
} elseif (Arr::has($data['params'], 'route')) {
185+
$url = route($data['params']['route']);
183186
}
184187
$notifications[] = [
185188
'id' => $not->id,
186-
'icon' => $not->data['icon'], // @phpstan-ignore-line
187-
'text' => __('notifications.' . $not->data['key'], $not->data['params']), // @phpstan-ignore-line
189+
'icon' => $data['icon'],
190+
'text' => __('notifications.' . $data['key'], $data['params']),
188191
'url' => $url,
189192
'dismiss' => route('notifications.read', $not->id),
190193
'dismiss_text' => __('header.notifications.dismiss'),

‎lang/en/notifications.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
return [
4+
'apps' => [
5+
'discord' => [
6+
'invalid' => 'Your Discord token has expired. Please re-sync your Discord and Kanka account.',
7+
],
8+
],
49
'campaign' => [
510
'application' => [
611
'approved' => 'Your application to the :campaign campaign has been approved.',
@@ -72,7 +77,7 @@
7277
'comments' => [
7378
'new_reply' => ':user has replied to your comment in :plugin.',
7479
'new_comment' => ':user has left a new comment in your plugin :plugin.',
75-
],
80+
],
7681
],
7782
'unread' => 'New notification',
7883
];

‎resources/views/notifications/index.blade.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?php /** @var \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Pagination\LengthAwarePaginator $notifications */?>
1+
<?php /** @var \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Pagination\LengthAwarePaginator $notifications */
2+
use \Illuminate\Support\Arr;
3+
use \Illuminate\Support\Str;
4+
?>
25
@extends('layouts.app', [
36
'title' => __('notifications.index.title'),
47
'breadcrumbs' => false,
@@ -32,10 +35,10 @@
3235
<td>
3336
@if (!empty($notification->data['icon']))
3437
<i class="fa-solid fa-{{ $notification->data['icon'] }} text-{{ $notification->data['colour'] }}"></i>
35-
@if(\Illuminate\Support\Arr::has($notification->data['params'], 'link'))
38+
@if(Arr::has($notification->data['params'], 'link'))
3639
@php
3740
$url = $notification->data['params']['link'];
38-
if (!\Illuminate\Support\Str::startsWith($url, 'http')) {
41+
if (!Str::startsWith($url, 'http')) {
3942
$url = url(app()->getLocale() . '/' . $url);
4043
}
4144
// Fix to new links?
@@ -44,6 +47,10 @@
4447
<a href="{{ $url }}">
4548
{!! __('notifications.' . $notification->data['key'], $notification->data['params']) !!}
4649
</a>
50+
@elseif (Arr::has($notification->data['params'], 'route'))
51+
<a href="{{ route($notification->data['params']['route']) }}">
52+
{!! __('notifications.' . $notification->data['key'], $notification->data['params']) !!}
53+
</a>
4754
@else
4855
{!! __('notifications.' . $notification->data['key'], $notification->data['params']) !!}
4956
@endif

0 commit comments

Comments
 (0)