diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Offline.pm')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Offline.pm | 106 |
1 files changed, 71 insertions, 35 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Offline.pm b/perllib/FixMyStreet/App/Controller/Offline.pm index 57cbe201c..adb3de14d 100644 --- a/perllib/FixMyStreet/App/Controller/Offline.pm +++ b/perllib/FixMyStreet/App/Controller/Offline.pm @@ -33,42 +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', - }); - } - - 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, + 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", @@ -82,6 +52,72 @@ sub manifest: Path("/.well-known/manifest.webmanifest") { $c->res->body($json); } +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_theme:$cobrand"; + # ignore_cache_and_defaults is only used in the admin, so no harm bypassing cache + 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', + }); + } + + my @icons; + my $uri = '/theme/' . $cobrand; + 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 || $ignore_cache_and_defaults) { + push @icons, + { src => "/cobrands/fixmystreet/images/192.png", sizes => "192x192", type => "image/png" }, + { src => "/cobrands/fixmystreet/images/512.png", sizes => "512x512", type => "image/png" }; + } + + $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, $manifest_theme); + } + } + + return $manifest_theme; +} + +sub _clear_manifest_theme_cache : Private { + my ($self, $c, $cobrand ) = @_; + + Memcached::delete("manifest_theme:$cobrand"); +} + __PACKAGE__->meta->make_immutable; 1; |