aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Offline.pm
blob: 75bb7f7ff3410eb4eaef14dc48d4dad68f47d4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package FixMyStreet::App::Controller::Offline;

use Image::Size;
use JSON::MaybeXS;
use Moose;
use Path::Tiny;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Offline - Catalyst Controller

=head1 DESCRIPTION

Offline pages Catalyst Controller - service worker handling

=head1 METHODS

=cut

sub service_worker : Path("/service-worker.js") {
    my ($self, $c) = @_;
    $c->res->content_type('application/javascript');
}

sub fallback : Local {
    my ($self, $c) = @_;
}

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},
        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);
}

sub _stash_manifest_icons : Private {
    my ($self, $c, $cobrand, $ignore_cache_and_defaults) = @_;

    my $key = "manifest_icons:$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);

    unless ( $icons ) {
        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" };
        }

        $icons = \@icons;

        unless ($ignore_cache_and_defaults) {
            Memcached::set($key, $icons);
        }
    }

    $c->stash->{manifest_icons} = $icons;
}

sub _clear_manifest_icons_cache : Private {
    my ($self, $c, $cobrand ) = @_;

    Memcached::set("manifest_icons:$cobrand", "");
}

__PACKAGE__->meta->make_immutable;

1;