2
2
3
3
namespace App \Http \Controllers \Backend ;
4
4
5
- use App \Enum \WebhookEvent ;
5
+ use App \Enum \WebhookEvent as WebhookEventEnum ;
6
6
use App \Exceptions \PermissionException ;
7
7
use App \Http \Controllers \Controller ;
8
8
use App \Http \Resources \StatusResource ;
12
12
use App \Models \User ;
13
13
use App \Models \Webhook ;
14
14
use App \Models \WebhookCreationRequest ;
15
+ use App \Models \WebhookEvent ;
15
16
use Carbon \Carbon ;
17
+ use Illuminate \Database \Eloquent \Builder ;
16
18
use Illuminate \Notifications \DatabaseNotification ;
17
19
use Illuminate \Support \Facades \DB ;
18
20
use Illuminate \Support \Facades \Gate ;
19
21
use Illuminate \Support \Facades \Log ;
20
- use Laravel \Passport \Client ;
21
22
use Spatie \WebhookServer \WebhookCall ;
22
23
23
24
abstract class WebhookController extends Controller {
@@ -33,14 +34,21 @@ public static function createWebhook(
33
34
$ secret = bin2hex (random_bytes (32 ));
34
35
$ client = $ request ->client ()->first ();
35
36
$ user = $ request ->user ()->first ();
36
- $ events = $ request -> events ;
37
+
37
38
$ webhook = Webhook::create ([
38
39
'oauth_client_id ' => $ client ->id ,
39
40
'url ' => $ request ->url ,
40
41
'secret ' => $ secret ,
41
- 'events ' => $ events ,
42
42
'user_id ' => $ user ->id
43
43
]);
44
+
45
+ foreach (explode (", " , $ request ->events ) as $ event ) {
46
+ WebhookEvent::create ([
47
+ 'webhook_id ' => $ webhook ->id ,
48
+ 'event ' => $ event ,
49
+ ]);
50
+ }
51
+
44
52
DB ::commit ();
45
53
46
54
Log::debug ("Created a new webhook. " , ['webhook ' => $ webhook ]);
@@ -67,27 +75,30 @@ public static function deleteWebhook(
67
75
return true ;
68
76
}
69
77
70
- public static function sendStatusWebhook (Status $ status , WebhookEvent $ event ): void {
78
+ public static function sendStatusWebhook (Status $ status , WebhookEventEnum $ event ): void {
71
79
self ::dispatchWebhook ($ status ->user , $ event , [
72
80
'status ' => new StatusResource ($ status )
73
81
]);
74
82
}
75
83
76
84
public static function sendNotificationWebhook (User $ user , DatabaseNotification $ notification ): void {
77
- self ::dispatchWebhook ($ user , WebhookEvent ::NOTIFICATION , [
85
+ self ::dispatchWebhook ($ user , WebhookEventEnum ::NOTIFICATION , [
78
86
'notification ' => new UserNotificationResource ($ notification )
79
87
]);
80
88
}
81
89
82
- public static function dispatchWebhook (User $ user , WebhookEvent $ event , array $ data ): void {
90
+ public static function dispatchWebhook (User $ user , WebhookEventEnum $ event , array $ data ): void {
83
91
if (!config ("trwl.webhooks_active " )) {
84
92
return ;
85
93
}
86
94
87
95
$ webhooks = $ user ->webhooks ()
88
- ->whereBitflag ('events ' , $ event ->value )
96
+ ->withWhereHas ('events ' , function ($ builder ) use ($ event ) {
97
+ $ builder ->where ('event ' , '= ' , $ event );
98
+ })
89
99
->where ('user_id ' , $ user ->id )
90
100
->get ();
101
+
91
102
foreach ($ webhooks as $ webhook ) {
92
103
Log::debug ("Sending webhook " , [
93
104
'webhook_id ' => $ webhook ->id ,
@@ -100,7 +111,7 @@ public static function dispatchWebhook(User $user, WebhookEvent $event, array $d
100
111
'X-Trwl-Webhook-Id ' => $ webhook ->id ,
101
112
])
102
113
->payload ([
103
- 'event ' => $ event ->name () ,
114
+ 'event ' => $ event ->value ,
104
115
...$ data
105
116
])
106
117
->useSecret ($ webhook ->secret )
@@ -116,14 +127,14 @@ public static function createWebhookRequest(
116
127
OAuthClient $ client ,
117
128
string $ oauthCode ,
118
129
string $ url ,
119
- int $ events ,
130
+ array $ events ,
120
131
): WebhookCreationRequest {
121
132
return WebhookCreationRequest::create ([
122
133
'id ' => hash ('sha256 ' , $ oauthCode ),
123
134
'user_id ' => $ user ->id ,
124
135
'oauth_client_id ' => $ client ->id ,
125
136
'expires_at ' => Carbon::now ()->addHour (),
126
- 'events ' => $ events ,
137
+ 'events ' => implode ( " , " , $ events) ,
127
138
'url ' => $ url ,
128
139
]);
129
140
}
0 commit comments