mohawk2 / Mojolicious-Plugin-WebPush

Mojolicious plugin to aid real-time web push
https://metacpan.org/pod/Mojolicious::Plugin::WebPush
1 stars 0 forks source link

Suggest passing $c to all callback functions #1

Closed uniejo closed 3 years ago

uniejo commented 3 years ago

I would like for the controller ($c) to be available in callback functions. In my case, I need the controller for calling a user authentication helper, and for logging (controller log has context on top of app log handle), and for database access. I am not using Mojo::Lite, so app() does not seem to be readily available. It is instead available as $c->app.

uniejo commented 3 years ago

Could not figure how to push my suggested change to a new branch, so I have included it here:

commit 3d2d459afccae9603af71906c8ee8f233a6f1003
Author: Erik Johansen <perl6-git@uniejo.dk>
Date:   Sat Sep 19 12:05:31 2020 +0200

    Add c to callbacks

diff --git a/lib/Mojolicious/Plugin/WebPush.pm b/lib/Mojolicious/Plugin/WebPush.pm
index 6c3a3d9..48d07f3 100644
--- a/lib/Mojolicious/Plugin/WebPush.pm
+++ b/lib/Mojolicious/Plugin/WebPush.pm
@@ -46,8 +46,8 @@ sub _make_route_handler {
     return _error($c, $body) if !$decode_ok;
     eval { validate_subs_info($body) };
     return _error($c, $@) if $@;
-    return $subs_session2user_p->($c->session)->then(
-      sub { $subs_create_p->($_[0], $body) },
+    return $subs_session2user_p->($c, $c->session)->then(
+      sub { $subs_create_p->($c, $_[0], $body) },
     )->then(
       sub { $c->render(json => { data => { success => \1 } }) },
       sub { _error($c, @_) },
@@ -135,10 +135,10 @@ sub register {
   $app->helper('webpush.create_p' => sub {
     eval { validate_subs_info($_[2]) };
     return Mojo::Promise->reject($@) if $@;
-    $conf->{subs_create_p}->(@_[1,2]);
+    $conf->{subs_create_p}->(@_);
   });
-  $app->helper('webpush.read_p' => sub { $conf->{subs_read_p}->($_[1]) });
-  $app->helper('webpush.delete_p' => sub { $conf->{subs_delete_p}->($_[1]) });
+  $app->helper('webpush.read_p' => sub { $conf->{subs_read_p}->(@_) });
+  $app->helper('webpush.delete_p' => sub { $conf->{subs_delete_p}->(@_) });
   $app->helper('webpush.aud' => \&_aud_helper);
   $app->helper('webpush.authorization' => (grep !$conf->{$_}, @AUTH_CONF)
     ? sub { die "Must provide @AUTH_CONF\n" }
@@ -190,23 +190,23 @@ Mojolicious::Plugin::WebPush - plugin to aid real-time web push
   };

   sub subs_session2user_p {
-    my ($session) = @_;
+    my ($c, $session) = @_;
     return Mojo::Promise->reject("Session not logged in") if !$session->{user_id};
     Mojo::Promise->resolve($session->{user_id});
   }

   sub subs_create_p {
-    my ($session, $subs_info) = @_;
+    my ($c, $session, $subs_info) = @_;
     app->db->save_subs_p($session->{user_id}, $subs_info);
   }

   sub subs_read_p {
-    my ($user_id) = @_;
+    my ($c, $user_id) = @_;
     app->db->lookup_subs_p($user_id);
   }

   sub subs_delete_p {
-    my ($user_id) = @_;
+    my ($c, $user_id) = @_;
     app->db->delete_subs_p($user_id);
   }

diff --git a/t/lib/TestUtils.pm b/t/lib/TestUtils.pm
index d3350c6..940971b 100644
--- a/t/lib/TestUtils.pm
+++ b/t/lib/TestUtils.pm
@@ -34,24 +34,26 @@ sub webpush_config {
 }

 sub subs_session2user_p {
-  return Mojo::Promise->reject("Session not logged in") if !$_[0]{user_id};
-  Mojo::Promise->resolve($_[0]{user_id});
+  my($c, $session) = @_;
+  my $user_id = $c->session->{user_id}
+    or return Mojo::Promise->reject("Session not logged in");
+  Mojo::Promise->resolve($user_id);
 }

 sub subs_create_p {
-  my ($user_id, $subs_info) = @_;
+  my ($c, $user_id, $subs_info) = @_;
   $userdb{$user_id} = $subs_info;
   Mojo::Promise->resolve(1);
 }

 sub subs_read_p {
-  my ($user_id) = @_;
+  my ($c, $user_id) = @_;
   return Mojo::Promise->reject("Not found: '$user_id'") if !$userdb{$user_id};
   Mojo::Promise->resolve($userdb{$user_id});
 }

 sub subs_delete_p {
-  my ($user_id) = @_;
+  my ($c, $user_id) = @_;
   return Mojo::Promise->reject("Not found: '$user_id'") if !$userdb{$user_id};
   Mojo::Promise->resolve(delete $userdb{$user_id});
 }
mohawk2 commented 3 years ago

Thanks for the patch! I slightly adjusted the TestUtils bit so the $session was used, not $c->session. I also changed the commit author so it was your anonymised uniejo@users.noreply.github.com address, so GitHub now recognises it as by you. Released as 0.04.