diff options
author | Dave Arter <davea@mysociety.org> | 2020-01-31 18:29:59 +0000 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2020-02-26 15:25:31 +0000 |
commit | 3231c65a0a653695b726fb5a36b9ab7d9cf3a137 (patch) | |
tree | 7b7e03b1f6149253789a1c9de4a0b3af95a8fc59 | |
parent | 379410d69f5d2cad7bd0dca45a09e6096b3672d6 (diff) |
Use ManifestTheme theme_colour for front page theme-color meta tag
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm | 10 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Offline.pm | 62 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Root.pm | 4 | ||||
-rw-r--r-- | perllib/Memcached.pm | 4 | ||||
-rw-r--r-- | t/app/controller/admin/manifesttheme.t | 8 | ||||
-rw-r--r-- | t/app/controller/offline.t | 3 | ||||
-rw-r--r-- | templates/web/base/admin/manifesttheme/form.html | 2 | ||||
-rw-r--r-- | templates/web/base/common_header_tags.html | 7 |
8 files changed, 60 insertions, 40 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm b/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm index 19017fc6b..9e3bdc33e 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm @@ -44,7 +44,7 @@ sub edit :PathPart('') :Chained('item') :Args(0) { # We need to do this after form processing, in case a form POST has deleted # an icon. - $c->forward('/offline/_stash_manifest_icons', [ $c->stash->{obj}->cobrand, 1 ]); + $c->stash->{editing_manifest_theme} = $c->forward('/offline/_find_manifest_theme', [ $c->stash->{obj}->cobrand, 1 ]); return $form; } @@ -67,7 +67,7 @@ sub form { if ($c->get_param('delete_theme')) { $c->forward('_delete_all_manifest_icons'); - $c->forward('/offline/_clear_manifest_icons_cache', [ $theme->cobrand ]); + $c->forward('/offline/_clear_manifest_theme_cache', [ $theme->cobrand ]); $theme->delete; $c->forward('/admin/log_edit', [ $theme->id, 'manifesttheme', 'delete' ]); $c->response->redirect($c->uri_for($self->action_for('index'))); @@ -83,15 +83,15 @@ sub form { return unless $form->validated; $c->forward('/admin/log_edit', [ $theme->id, 'manifesttheme', $action ]); - $c->forward('/offline/_clear_manifest_icons_cache', [ $theme->cobrand ]); + $c->forward('/offline/_clear_manifest_theme_cache', [ $theme->cobrand ]); $c->response->redirect($c->uri_for($self->action_for('index'))); } sub _delete_all_manifest_icons :Private { my ($self, $c) = @_; - $c->forward('/offline/_stash_manifest_icons', [ $c->stash->{obj}->cobrand, 1 ]); - foreach my $icon ( @{ $c->stash->{manifest_icons} } ) { + my $theme = $c->forward('/offline/_find_manifest_theme', [ $c->stash->{obj}->cobrand, 1 ]); + foreach my $icon ( @{ $theme->{icons} } ) { unlink FixMyStreet->path_to('web', $icon->{src}); } } diff --git a/perllib/FixMyStreet/App/Controller/Offline.pm b/perllib/FixMyStreet/App/Controller/Offline.pm index 75bb7f7ff..adb3de14d 100644 --- a/perllib/FixMyStreet/App/Controller/Offline.pm +++ b/perllib/FixMyStreet/App/Controller/Offline.pm @@ -33,24 +33,12 @@ sub manifest: Path("/.well-known/manifest.webmanifest") { my ($self, $c) = @_; $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', - }); - } - - $c->forward("_stash_manifest_icons", [ $c->cobrand->moniker ]); - my $data = { - name => $theme->name, - short_name => $theme->short_name, - background_color => $theme->background_colour, - theme_color => $theme->theme_colour, - icons => $c->stash->{manifest_icons}, + name => $c->stash->{manifest_theme}->{name}, + short_name => $c->stash->{manifest_theme}->{short_name}, + background_color => $c->stash->{manifest_theme}->{background_colour}, + theme_color => $c->stash->{manifest_theme}->{theme_colour}, + icons => $c->stash->{manifest_theme}->{icons}, lang => $c->stash->{lang_code}, display => "minimal-ui", start_url => "/?pwa", @@ -64,14 +52,30 @@ sub manifest: Path("/.well-known/manifest.webmanifest") { $c->res->body($json); } -sub _stash_manifest_icons : Private { +sub _stash_manifest_theme : Private { + my ($self, $c, $cobrand) = @_; + + $c->stash->{manifest_theme} = $c->forward('_find_manifest_theme', [ $cobrand ]); +} + +sub _find_manifest_theme : Private { my ($self, $c, $cobrand, $ignore_cache_and_defaults) = @_; - my $key = "manifest_icons:$cobrand"; + my $key = "manifest_theme:$cobrand"; # ignore_cache_and_defaults is only used in the admin, so no harm bypassing cache - my $icons = $ignore_cache_and_defaults ? undef : Memcached::get($key); + my $manifest_theme = $ignore_cache_and_defaults ? undef : Memcached::get($key); + + unless ( $manifest_theme ) { + my $theme = $c->model('DB::ManifestTheme')->find({ cobrand => $cobrand }); + 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', + }); + } - unless ( $icons ) { my @icons; my $uri = '/theme/' . $cobrand; my $theme_path = path(FixMyStreet->path_to('web' . $uri)); @@ -92,20 +96,26 @@ sub _stash_manifest_icons : Private { { src => "/cobrands/fixmystreet/images/512.png", sizes => "512x512", type => "image/png" }; } - $icons = \@icons; + $manifest_theme = { + icons => \@icons, + background_colour => $theme->background_colour, + theme_colour => $theme->theme_colour, + name => $theme->name, + short_name => $theme->short_name, + }; unless ($ignore_cache_and_defaults) { - Memcached::set($key, $icons); + Memcached::set($key, $manifest_theme); } } - $c->stash->{manifest_icons} = $icons; + return $manifest_theme; } -sub _clear_manifest_icons_cache : Private { +sub _clear_manifest_theme_cache : Private { my ($self, $c, $cobrand ) = @_; - Memcached::set("manifest_icons:$cobrand", ""); + Memcached::delete("manifest_theme:$cobrand"); } __PACKAGE__->meta->make_immutable; diff --git a/perllib/FixMyStreet/App/Controller/Root.pm b/perllib/FixMyStreet/App/Controller/Root.pm index f9fefc2ae..71dcf8e27 100644 --- a/perllib/FixMyStreet/App/Controller/Root.pm +++ b/perllib/FixMyStreet/App/Controller/Root.pm @@ -42,6 +42,8 @@ sub auto : Private { $c->forward('check_password_expiry'); $c->detach('/auth/redirect') if $c->cobrand->call_hook('check_login_disallowed'); + $c->forward('/offline/_stash_manifest_theme', [ $c->cobrand->moniker ]); + return 1; } @@ -77,8 +79,6 @@ sub index : Path : Args(0) { $c->detach; } - $c->forward('/offline/_stash_manifest_icons', [ $c->cobrand->moniker ]); - $c->forward('/auth/get_csrf_token'); } diff --git a/perllib/Memcached.pm b/perllib/Memcached.pm index 05ceeb615..d03897e5a 100644 --- a/perllib/Memcached.pm +++ b/perllib/Memcached.pm @@ -29,4 +29,8 @@ sub set { instance->set(@_); } +sub delete { + instance->delete(@_); +} + 1; diff --git a/t/app/controller/admin/manifesttheme.t b/t/app/controller/admin/manifesttheme.t index 9a86bdeb7..c1b2d4542 100644 --- a/t/app/controller/admin/manifesttheme.t +++ b/t/app/controller/admin/manifesttheme.t @@ -114,8 +114,8 @@ subtest "cobrand admin lets you add an icon to an existing theme" => sub { ok $mech->success, 'Posted request successfully'; is $mech->uri->path, '/admin/manifesttheme/lincolnshire', "redirected back to edit page"; - $mech->content_contains($icon_filename); - $mech->content_contains("133x100"); + $mech->content_contains("<img src=\"/theme/lincolnshire/" . $icon_filename); + $mech->content_contains("<td class=\"icon-size\">133x100</td>"); my $icon_dest = path(FixMyStreet->path_to('web/theme/lincolnshire/', $icon_filename)); ok $icon_dest->exists, "Icon stored on disk"; }; @@ -132,8 +132,8 @@ subtest "cobrand admin lets you delete an icon from an existing theme" => sub { $mech->submit_form_ok( { with_fields => $fields } ); is $mech->uri->path, '/admin/manifesttheme/lincolnshire', "redirected back to edit page"; - $mech->content_lacks($icon_filename); - $mech->content_lacks("133x100"); + $mech->content_lacks("<img src=\"/theme/lincolnshire/" . $icon_filename); + $mech->content_lacks("<td class=\"icon-size\">133x100</td>"); ok !$icon_dest->exists, "Icon removed from disk"; }; diff --git a/t/app/controller/offline.t b/t/app/controller/offline.t index a3feae514..876475264 100644 --- a/t/app/controller/offline.t +++ b/t/app/controller/offline.t @@ -1,6 +1,7 @@ use FixMyStreet::TestMech; use FixMyStreet::DB; use Path::Tiny; +use Memcached; my $mech = FixMyStreet::TestMech->new; @@ -12,6 +13,7 @@ FixMyStreet::override_config { my $image_path = path('t/app/controller/sample.jpg'); $image_path->copy($theme_dir->child('sample.jpg')); subtest 'manifest' => sub { + Memcached::delete("manifest_theme:test"); my $j = $mech->get_ok_json('/.well-known/manifest.webmanifest'); is $j->{name}, 'FixMyStreet', 'correct name'; is $j->{theme_color}, '#ffd000', 'correct theme colour'; @@ -22,6 +24,7 @@ FixMyStreet::override_config { }, 'correct icon'; }; subtest 'themed manifest' => sub { + Memcached::delete("manifest_theme:test"); FixMyStreet::DB->resultset('ManifestTheme')->create({ cobrand => "test", name => "My Test Cobrand FMS", diff --git a/templates/web/base/admin/manifesttheme/form.html b/templates/web/base/admin/manifesttheme/form.html index 4c97f292e..6d02487a6 100644 --- a/templates/web/base/admin/manifesttheme/form.html +++ b/templates/web/base/admin/manifesttheme/form.html @@ -36,7 +36,7 @@ </tr> </thead> <tbody> - [% FOREACH icon IN manifest_icons %] + [% FOREACH icon IN editing_manifest_theme.icons %] <tr> <td><img src="[% icon.src %]" /></td> <td class="icon-size">[% icon.sizes %]</td> diff --git a/templates/web/base/common_header_tags.html b/templates/web/base/common_header_tags.html index cf485c7b9..6edcc63a4 100644 --- a/templates/web/base/common_header_tags.html +++ b/templates/web/base/common_header_tags.html @@ -2,8 +2,11 @@ <meta http-equiv="content-type" content="text/html; charset=utf-8"> <link rel="manifest" href="/.well-known/manifest.webmanifest"> -[% FOREACH icon IN manifest_icons %] - <link rel="apple-touch-icon" sizes="[% icon.sizes %]" href="[% icon.src %]"> +[% IF manifest_theme %] + <meta name='theme-color' content='[% manifest_theme.theme_colour %]'> + [% FOREACH icon IN manifest_theme.icons %] + <link rel="apple-touch-icon" sizes="[% icon.sizes %]" href="[% icon.src %]"> + [% END %] [% END %] [% IF csrf_token %] |