Add push notifications and device token management
This commit is contained in:
parent
9d4c736d95
commit
bb161dfd25
14 changed files with 5320 additions and 6 deletions
|
@ -142,7 +142,9 @@ sub startup {
|
|||
$api_public->post('/register')->to('api-register#post_register');
|
||||
$api_public->post('/logout')->to('api-auth#post_logout');
|
||||
$api_public->post('/feedback')->to('api-feedback#post_feedback');
|
||||
$api_public->post('/check-device-token')->to('api-devices#check_token');
|
||||
$api_public->post('/add-device-token')->to('api-devices#add_token');
|
||||
$api_public->post('/get-topics')->to('api-sendmessage#get_topics');
|
||||
$api_public->post('/get-device-tokens')->to('api-devices#get_tokens');
|
||||
$api_public->post('/send-message')->to('api-sendmessage#post_message');
|
||||
|
||||
|
|
|
@ -9,9 +9,12 @@ use Carp;
|
|||
|
||||
has error_messages => sub {
|
||||
return {
|
||||
devicetoken => {
|
||||
required => { message => 'Device token is required', status => 400 },
|
||||
in_resultset => { message => 'Device token not found', status => 400 },
|
||||
#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 },
|
||||
},
|
||||
sender => {
|
||||
required => { message => 'Sender name is required', status => 400 },
|
||||
|
@ -66,13 +69,33 @@ sub create_jwt_from_path_and_scopes
|
|||
return $jwt->encode;
|
||||
}
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
sub post_message {
|
||||
my $c = shift;
|
||||
|
||||
my $validation = $c->validation;
|
||||
$validation->input( $c->stash->{api_json} );
|
||||
|
||||
$validation->required('devicetoken')->in_resultset('token', $c->schema->resultset('DeviceToken'));
|
||||
#$validation->required('devicetokens')->in_resultset('token', $c->schema->resultset('DeviceToken'));
|
||||
$validation->required('topic');
|
||||
$validation->required('sender')->in_resultset('name', $c->schema->resultset('Organisation'));
|
||||
$validation->required('messagetext');
|
||||
|
||||
|
@ -86,7 +109,7 @@ sub post_message {
|
|||
|
||||
$request->content(JSON::encode_json ({
|
||||
message => {
|
||||
token => $validation->param('devicetoken'),
|
||||
topic => $validation->param('topic'),
|
||||
notification => {
|
||||
title => $validation->param('sender'),
|
||||
body => $validation->param('messagetext')
|
||||
|
|
|
@ -6,7 +6,7 @@ use warnings;
|
|||
|
||||
use base 'DBIx::Class::Schema';
|
||||
|
||||
our $VERSION = 32;
|
||||
our $VERSION = 33;
|
||||
|
||||
__PACKAGE__->load_namespaces;
|
||||
|
||||
|
|
44
lib/Pear/LocalLoop/Schema/Result/DeviceSubscription.pm
Normal file
44
lib/Pear/LocalLoop/Schema/Result/DeviceSubscription.pm
Normal file
|
@ -0,0 +1,44 @@
|
|||
package Pear::LocalLoop::Schema::Result::DeviceSubscription;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use base 'DBIx::Class::Core';
|
||||
|
||||
__PACKAGE__->table("device_subscriptions");
|
||||
|
||||
__PACKAGE__->add_columns(
|
||||
"id" => {
|
||||
data_type => "integer",
|
||||
is_auto_increment => 1,
|
||||
is_nullable => 0,
|
||||
},
|
||||
"device_token_id" => {
|
||||
data_type => "integer",
|
||||
is_foreign_key => 1,
|
||||
is_nullable => 0,
|
||||
},
|
||||
"topic_id" => {
|
||||
data_type => "integer",
|
||||
is_foreign_key => 1,
|
||||
is_nullable => 0,
|
||||
},
|
||||
);
|
||||
|
||||
__PACKAGE__->set_primary_key("id");
|
||||
|
||||
__PACKAGE__->belongs_to(
|
||||
"device_token",
|
||||
"Pear::LocalLoop::Schema::Result::DeviceToken",
|
||||
"device_token_id",
|
||||
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
|
||||
);
|
||||
|
||||
__PACKAGE__->belongs_to(
|
||||
"topic",
|
||||
"Pear::LocalLoop::Schema::Result::Topic",
|
||||
"topic_id",
|
||||
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
|
||||
);
|
||||
|
||||
1;
|
|
@ -44,4 +44,13 @@ __PACKAGE__->belongs_to(
|
|||
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
|
||||
);
|
||||
|
||||
__PACKAGE__->has_many(
|
||||
"device_subscriptions",
|
||||
"Pear::LocalLoop::Schema::Result::DeviceSubscription",
|
||||
{ "foreign.device_token_id" => "self.id" },
|
||||
{ cascade_copy => 0, cascade_delete => 0 },
|
||||
);
|
||||
|
||||
__PACKAGE__->many_to_many('topics' => 'device_subscriptions', 'topic');
|
||||
|
||||
1;
|
||||
|
|
40
lib/Pear/LocalLoop/Schema/Result/Topic.pm
Normal file
40
lib/Pear/LocalLoop/Schema/Result/Topic.pm
Normal file
|
@ -0,0 +1,40 @@
|
|||
package Pear::LocalLoop::Schema::Result::Topic;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use base 'DBIx::Class::Core';
|
||||
|
||||
__PACKAGE__->load_components( qw/
|
||||
InflateColumn::DateTime
|
||||
TimeStamp
|
||||
FilterColumn
|
||||
/);
|
||||
|
||||
__PACKAGE__->table("topics");
|
||||
|
||||
__PACKAGE__->add_columns(
|
||||
"id" => {
|
||||
data_type => "integer",
|
||||
is_auto_increment => 1,
|
||||
is_nullable => 0,
|
||||
},
|
||||
"name" => {
|
||||
data_type => "varchar",
|
||||
size => 200,
|
||||
is_nullable => 0,
|
||||
},
|
||||
);
|
||||
|
||||
__PACKAGE__->set_primary_key("id");
|
||||
|
||||
__PACKAGE__->has_many(
|
||||
"device_subscriptions",
|
||||
"Pear::LocalLoop::Schema::Result::DeviceSubscription",
|
||||
{ "foreign.topic_id" => "self.id" },
|
||||
{ cascade_copy => 0, cascade_delete => 0 },
|
||||
);
|
||||
|
||||
__PACKAGE__->many_to_many('device_tokens' => 'device_subscriptions', 'device_token');
|
||||
|
||||
1;
|
Reference in a new issue