Add push notifications and device token management

This commit is contained in:
Ben Goldsworthy 2020-11-30 17:34:13 +00:00
parent 9d4c736d95
commit bb161dfd25
14 changed files with 5320 additions and 6 deletions

View file

@ -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')

View file

@ -6,7 +6,7 @@ use warnings;
use base 'DBIx::Class::Schema';
our $VERSION = 32;
our $VERSION = 33;
__PACKAGE__->load_namespaces;

View 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;

View file

@ -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;

View 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;