2020-11-01 18:06:11 +00:00
package Pear::LocalLoop::Controller::Api::Sendmessage ;
use Mojo::Base 'Mojolicious::Controller' ;
2020-11-08 15:05:53 +00:00
use LWP::UserAgent ;
use JSON ;
2020-11-08 17:35:29 +00:00
use JSON::Parse 'parse_json' ;
2020-11-08 15:05:53 +00:00
use Mojo::JWT ;
use Mojo::File ;
use Carp ;
2020-11-01 18:06:11 +00:00
has error_messages = > sub {
return {
2020-11-30 17:34:13 +00:00
#devicetokens => {
# required => { message => 'Device token is required', status => 400 },
# in_resultset => { message => 'Device token not found', status => 400 },
#},
topic = > {
required = > { message = > 'Topic is required' , status = > 400 } ,
2020-11-08 17:35:29 +00:00
} ,
sender = > {
required = > { message = > 'Sender name is required' , status = > 400 } ,
2020-11-28 19:53:19 +00:00
in_resultset = > { message = > 'Sender org not found' , status = > 400 } ,
2020-11-01 18:06:11 +00:00
} ,
messagetext = > {
required = > { message = > 'Message is required' , status = > 400 } ,
} ,
} ;
} ;
2020-11-08 15:05:53 +00:00
= begin comment
Credit: Peter Scott / StackOverflow
https: //s tackoverflow . com /a/ 53357961 / 4580273
Credit: jeffez / StackOverflow
https: //s tackoverflow . com /q/ 56556438 / 4580273
= cut
2020-11-08 17:35:29 +00:00
my $ jwt = create_jwt_from_path_and_scopes ( './localspend-47012.json' , 'email https://www.googleapis.com/auth/cloud-platform' ) ;
2020-11-08 15:05:53 +00:00
my $ ua = LWP::UserAgent - > new ( ) ;
2020-11-08 17:35:29 +00:00
my $ response = $ ua - > post ( 'https://www.googleapis.com/oauth2/v4/token' ,
2020-11-08 15:05:53 +00:00
{
'grant_type' = > 'urn:ietf:params:oauth:grant-type:jwt-bearer' ,
'assertion' = > $ jwt
}
) ;
2020-11-08 17:35:29 +00:00
my $ bearer_token = parse_json ( $ response - > content ) ;
2020-11-08 15:05:53 +00:00
sub create_jwt_from_path_and_scopes
{
my ( $ path , $ scope ) = @ _ ;
croak ( "No path provided" ) if not defined $ path ;
croak ( "$path not available" ) if not - f $ path ;
my $ json = decode_json ( Mojo::File - > new ( $ path ) - > slurp ) ;
croak ( "No Private key in $path" ) if not defined $ json - > { private_key } ;
croak ( "Not a service account" ) if $ json - > { type } ne 'service_account' ;
my $ jwt = Mojo::JWT - > new ( ) ;
$ jwt - > algorithm ( 'RS256' ) ;
$ jwt - > secret ( $ json - > { private_key } ) ;
$ jwt - > claims ( {
iss = > $ json - > { client_email } ,
scope = > $ scope ,
aud = > 'https://www.googleapis.com/oauth2/v4/token' ,
iat = > time ( ) ,
exp = > time ( ) + 3600
} ) ;
$ jwt - > set_iat ( 1 ) ;
return $ jwt - > encode ;
}
2020-11-30 17:34:13 +00:00
sub get_topics {
my $ c = shift ;
my $ topic_rs = $ c - > schema - > resultset ( 'Topic' ) ;
my @ topics = (
map { {
id = > $ _ - > id ,
name = > $ _ - > name ,
numberOfSubscribers = > $ _ - > search_related ( 'device_subscriptions' , { 'topic_id' = > $ _ - > id } ) - > count ,
} } $ topic_rs - > all
) ;
return $ c - > render ( json = > {
success = > Mojo::JSON - > true ,
topics = > \ @ topics ,
} ) ;
}
2020-11-01 18:06:11 +00:00
sub post_message {
my $ c = shift ;
my $ validation = $ c - > validation ;
$ validation - > input ( $ c - > stash - > { api_json } ) ;
2020-11-30 17:34:13 +00:00
#$validation->required('devicetokens')->in_resultset('token', $c->schema->resultset('DeviceToken'));
$ validation - > required ( 'topic' ) ;
2020-11-28 19:53:19 +00:00
$ validation - > required ( 'sender' ) - > in_resultset ( 'name' , $ c - > schema - > resultset ( 'Organisation' ) ) ;
2020-11-01 18:06:11 +00:00
$ validation - > required ( 'messagetext' ) ;
return $ c - > api_validation_error if $ validation - > has_error ;
2020-11-08 15:05:53 +00:00
my $ end_point = "https://fcm.googleapis.com/v1/projects/localspend-47012/messages:send" ;
my $ request = HTTP::Request - > new ( 'POST' , $ end_point ) ;
2020-11-08 17:35:29 +00:00
$ request - > header ( 'Authorization' = > "Bearer $bearer_token->{access_token}" ) ;
2020-11-08 15:05:53 +00:00
$ request - > header ( 'Content-Type' = > 'application/json' ) ;
$ request - > content ( JSON:: encode_json ( {
message = > {
2020-11-30 17:34:13 +00:00
topic = > $ validation - > param ( 'topic' ) ,
2020-11-08 15:05:53 +00:00
notification = > {
2020-11-08 17:35:29 +00:00
title = > $ validation - > param ( 'sender' ) ,
body = > $ validation - > param ( 'messagetext' )
2020-11-08 15:05:53 +00:00
} ,
webpush = > {
headers = > {
2020-11-08 17:35:29 +00:00
urgency = > 'very-low'
2020-11-08 15:05:53 +00:00
} ,
notification = > {
2020-11-08 17:35:29 +00:00
title = > $ validation - > param ( 'sender' ) ,
body = > $ validation - > param ( 'messagetext' ) ,
2020-11-08 15:05:53 +00:00
}
}
}
} ) ) ;
2020-11-08 17:35:29 +00:00
my $ response = $ ua - > request ( $ request ) ;
if ( $ response - > is_success ) {
return $ c - > render ( json = > {
success = > Mojo::JSON - > true ,
message = > 'Your message has been sent successfully!' ,
} ) ;
} elsif ( $ response - > is_error ) {
return $ c - > render (
json = > {
success = > Mojo::JSON - > false ,
message = > [
$ response - > decoded_content ,
$ jwt ,
$ bearer_token
] ,
error = > 'message_error' ,
} ,
status = > $ response - > code ,
) ;
}
2020-11-01 18:06:11 +00:00
}
1 ;