diff options
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Offline.pm | 73 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Root.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/FixMyStreet.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/ManifestTheme.pm | 47 | ||||
-rw-r--r-- | perllib/FixMyStreet/TestMech.pm | 2 |
5 files changed, 114 insertions, 22 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Offline.pm b/perllib/FixMyStreet/App/Controller/Offline.pm index f5d895318..57cbe201c 100644 --- a/perllib/FixMyStreet/App/Controller/Offline.pm +++ b/perllib/FixMyStreet/App/Controller/Offline.pm @@ -1,5 +1,9 @@ package FixMyStreet::App::Controller::Offline; + +use Image::Size; +use JSON::MaybeXS; use Moose; +use Path::Tiny; use namespace::autoclean; BEGIN { extends 'Catalyst::Controller'; } @@ -10,35 +14,72 @@ FixMyStreet::App::Controller::Offline - Catalyst Controller =head1 DESCRIPTION -Offline pages Catalyst Controller. +Offline pages Catalyst Controller - service worker handling =head1 METHODS =cut -sub have_appcache : Private { +sub service_worker : Path("/service-worker.js") { my ($self, $c) = @_; - return $c->user_exists && $c->user->has_body_permission_to('planned_reports') - && !($c->user->is_superuser && FixMyStreet->staging_flag('enable_appcache', 0)); + $c->res->content_type('application/javascript'); } -sub manifest : Path("/offline/appcache.manifest") { +sub fallback : Local { my ($self, $c) = @_; - unless ($c->forward('have_appcache')) { - $c->response->status(404); - $c->response->body('NOT FOUND'); - } - $c->res->content_type('text/cache-manifest; charset=utf-8'); - $c->res->header(Cache_Control => 'no-cache, no-store'); } -sub appcache : Path("/offline/appcache") { +sub manifest: Path("/.well-known/manifest.webmanifest") { my ($self, $c) = @_; - $c->detach('/page_error_404_not_found', []) if keys %{$c->req->params} && !$c->req->query_keywords; - unless ($c->forward('have_appcache')) { - $c->response->status(404); - $c->response->body('NOT FOUND'); + $c->res->content_type('application/manifest+json'); + + my $theme = $c->model('DB::ManifestTheme')->find({ cobrand => $c->cobrand->moniker }); + unless ( $theme ) { + $theme = $c->model('DB::ManifestTheme')->new({ + name => $c->stash->{site_name}, + short_name => $c->stash->{site_name}, + background_colour => '#ffffff', + theme_colour => '#ffd000', + }); } + + my @icons; + my $uri = '/theme/' . $c->cobrand->moniker; + my $theme_path = path(FixMyStreet->path_to('web' . $uri)); + $theme_path->visit( + sub { + my ($x, $y, $typ) = Image::Size::imgsize($_->stringify); + push @icons, { + src => join('/', $uri, $_->basename), + sizes => join('x', $x, $y), + type => $typ eq 'PNG' ? 'image/png' : $typ eq 'GIF' ? 'image/gif' : $typ eq 'JPG' ? 'image/jpeg' : '', + }; + } + ); + + unless (@icons) { + push @icons, + { src => "/cobrands/fixmystreet/images/192.png", sizes => "192x192", type => "image/png" }, + { src => "/cobrands/fixmystreet/images/512.png", sizes => "512x512", type => "image/png" }; + } + + my $data = { + name => $theme->name, + short_name => $theme->short_name, + background_color => $theme->background_colour, + theme_color => $theme->theme_colour, + icons => \@icons, + lang => $c->stash->{lang_code}, + display => "minimal-ui", + start_url => "/?pwa", + scope => "/", + }; + if ($c->cobrand->can('manifest')) { + $data = { %$data, %{$c->cobrand->manifest} }; + } + + my $json = encode_json($data); + $c->res->body($json); } __PACKAGE__->meta->make_immutable; diff --git a/perllib/FixMyStreet/App/Controller/Root.pm b/perllib/FixMyStreet/App/Controller/Root.pm index fb6d063be..caaa260ff 100644 --- a/perllib/FixMyStreet/App/Controller/Root.pm +++ b/perllib/FixMyStreet/App/Controller/Root.pm @@ -159,11 +159,6 @@ sub check_login_required : Private { }x; return if $c->request->path =~ $whitelist; - # Blacklisted URLs immediately 404 - # This is primarily to work around a Safari bug where the appcache - # URL is requested in an infinite loop if it returns a 302 redirect. - $c->detach('/page_error_404_not_found', []) if $c->request->path =~ /^offline/; - $c->detach( '/auth/redirect' ); } diff --git a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm index 8339ceede..ba82949c7 100644 --- a/perllib/FixMyStreet/Cobrand/FixMyStreet.pm +++ b/perllib/FixMyStreet/Cobrand/FixMyStreet.pm @@ -351,4 +351,13 @@ sub update_email_shortlisted_user { FixMyStreet::Cobrand::TfL::update_email_shortlisted_user($self, $update); } +sub manifest { + return { + related_applications => [ + { platform => 'play', url => 'https://play.google.com/store/apps/details?id=org.mysociety.FixMyStreet', id => 'org.mysociety.FixMyStreet' }, + { platform => 'itunes', url => 'https://apps.apple.com/gb/app/fixmystreet/id297456545', id => 'id297456545' }, + ], + }; +} + 1; diff --git a/perllib/FixMyStreet/DB/Result/ManifestTheme.pm b/perllib/FixMyStreet/DB/Result/ManifestTheme.pm new file mode 100644 index 000000000..a2f49eacb --- /dev/null +++ b/perllib/FixMyStreet/DB/Result/ManifestTheme.pm @@ -0,0 +1,47 @@ +use utf8; +package FixMyStreet::DB::Result::ManifestTheme; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; +__PACKAGE__->load_components( + "FilterColumn", + "FixMyStreet::InflateColumn::DateTime", + "FixMyStreet::EncodedColumn", +); +__PACKAGE__->table("manifest_theme"); +__PACKAGE__->add_columns( + "id", + { + data_type => "integer", + is_auto_increment => 1, + is_nullable => 0, + sequence => "manifest_theme_id_seq", + }, + "cobrand", + { data_type => "text", is_nullable => 0 }, + "name", + { data_type => "text", is_nullable => 0 }, + "short_name", + { data_type => "text", is_nullable => 0 }, + "background_colour", + { data_type => "text", is_nullable => 1 }, + "theme_colour", + { data_type => "text", is_nullable => 1 }, + "images", + { data_type => "text[]", is_nullable => 1 }, +); +__PACKAGE__->set_primary_key("id"); +__PACKAGE__->add_unique_constraint("manifest_theme_cobrand_key", ["cobrand"]); + + +# Created by DBIx::Class::Schema::Loader v0.07035 @ 2020-01-30 14:30:42 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Sgbva7nEVkjqG/+lQL/ryw + + +# You can replace this text with custom code or comments, and it will be preserved on regeneration +1; diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index 5bb975104..1b7fba1bd 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -585,7 +585,7 @@ sub get_ok_json { # check that the content-type of response is correct croak "Response was not JSON" - unless $res->header('Content-Type') =~ m{^application/json\b}; + unless $res->header('Content-Type') =~ m{^application/(?:[a-z]+\+)?json\b}; return decode_json( $res->content ); } |