aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB/ResultSet/Problem.pm
blob: 359d5224ac303c7c99b61ac6d6fcc0f75149797b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package FixMyStreet::DB::ResultSet::Problem;
use base 'DBIx::Class::ResultSet';

use strict;
use warnings;

use Memcached;
use mySociety::Locale;
use FixMyStreet::DB;

my $site_key;

sub set_restriction {
    my ( $rs, $key ) = @_;
    $site_key = $key;
}

sub body_query {
    my ($rs, $bodies) = @_;
    unless (ref $bodies eq 'ARRAY') {
        $bodies = [ map { ref $_ ? $_->id : $_ } $bodies ];
    }
    \[ "regexp_split_to_array(bodies_str, ',') && ?", [ {} => $bodies ] ]
}

# Edits PARAMS in place to either hide non_public reports, or show them
# if user is superuser (all) or inspector (correct body)
sub non_public_if_possible {
    my ($rs, $params, $c) = @_;
    if ($c->user_exists) {
        my $only_non_public = $c->stash->{only_non_public} ? 1 : 0;
        if ($c->user->is_superuser) {
            # See all reports, no restriction
            $params->{non_public} = 1 if $only_non_public;
        } elsif ($c->user->has_body_permission_to('report_inspect') ||
                 $c->user->has_body_permission_to('report_mark_private')) {
            if ($only_non_public) {
                $params->{'-and'} = [
                    non_public => 1,
                    $rs->body_query($c->user->from_body->id),
                ];
            } else {
                $params->{'-or'} = [
                    non_public => 0,
                    $rs->body_query($c->user->from_body->id),
                ];
            }
        } else {
            $params->{non_public} = 0;
        }
    } else {
        $params->{non_public} = 0;
    }
}

sub to_body {
    my ($rs, $bodies, $join) = @_;
    return $rs unless $bodies;
    $join = { join => 'problem' } if $join;
    $rs = $rs->search(
        # This isn't using $rs->body_query because $rs might be Problem, Comment, or Nearby
        FixMyStreet::DB::ResultSet::Problem->body_query($bodies),
        $join
    );
    return $rs;
}

# Front page statistics

sub _cache_timeout {
    FixMyStreet->config('CACHE_TIMEOUT') // 3600;
}

sub recent_fixed {
    my $rs = shift;
    my $key = "recent_fixed:$site_key";
    my $result = Memcached::get($key);
    unless ($result) {
        $result = $rs->search( {
            state => [ FixMyStreet::DB::Result::Problem->fixed_states() ],
            lastupdate => { '>', \"current_timestamp-'1 month'::interval" },
        } )->count;
        Memcached::set($key, $result, _cache_timeout());
    }
    return $result;
}

sub number_comments {
    my $rs = shift;
    my $key = "number_comments:$site_key";
    my $result = Memcached::get($key);
    unless ($result) {
        $result = $rs->search(
            { 'comments.state' => 'confirmed' },
            { join => 'comments' }
        )->count;
        Memcached::set($key, $result, _cache_timeout());
    }
    return $result;
}

sub recent_new {
    my ( $rs, $interval ) = @_;
    (my $key = $interval) =~ s/\s+//g;
    $key = "recent_new:$site_key:$key";
    my $result = Memcached::get($key);
    unless ($result) {
        $result = $rs->search( {
            state => [ FixMyStreet::DB::Result::Problem->visible_states() ],
            confirmed => { '>', \"current_timestamp-'$interval'::interval" },
        } )->count;
        Memcached::set($key, $result, _cache_timeout());
    }
    return $result;
}

# Front page recent lists

sub recent {
    my ( $rs ) = @_;
    return _recent( $rs, 5 );
}

sub recent_photos {
    my ( $rs, $num, $lat, $lon, $dist ) = @_;
    return _recent( $rs, $num, $lat, $lon, $dist, 1);
}

sub _recent {
    my ( $rs, $num, $lat, $lon, $dist, $photos ) = @_;

    my $key = $photos ? 'recent_photos' : 'recent';
    $key .= ":$site_key:$num";

    # submitted might be returned for e.g. Zurich, but would mean in moderation, so no photo
    my @states = grep { $_ ne 'submitted' } FixMyStreet::DB::Result::Problem->visible_states();
    my $query = {
        non_public => 0,
        state      => \@states,
    };
    $query->{photo} = { '!=', undef } if $photos;

    my $attrs = {
        order_by => { -desc => \'coalesce(confirmed, created)' },
        rows => $num,
    };

    my $probs;
    if (defined $lat) { # No caching
        $attrs->{bind} = [ $lat, $lon, $dist ];
        $attrs->{join} = 'nearby';
        $probs = [ mySociety::Locale::in_gb_locale {
            $rs->search( $query, $attrs )->all;
        } ];
    } else {
        $probs = Memcached::get($key);
        if ($probs) {
            # Need to refetch to check if hidden since cached
            $probs = [ $rs->search({
                id => [ map { $_->id } @$probs ],
                %$query,
            }, $attrs)->all ];
        } else {
            $probs = [ $rs->search( $query, $attrs )->all ];
            Memcached::set($key, $probs, _cache_timeout());
        }
    }

    return $probs;
}

# Problems around a location

sub around_map {
    my ( $rs, $c, %p) = @_;
    my $attr = {
        order_by => $p{order},
    };
    $attr->{rows} = $c->cobrand->reports_per_page;

    unless ( $p{states} ) {
        $p{states} = FixMyStreet::DB::Result::Problem->visible_states();
    }

    my $q = {
            state => [ keys %{$p{states}} ],
            latitude => { '>=', $p{min_lat}, '<', $p{max_lat} },
            longitude => { '>=', $p{min_lon}, '<', $p{max_lon} },
    };

    $q->{$c->stash->{report_age_field}} = { '>=', \"current_timestamp-'$p{report_age}'::interval" } if
        $p{report_age};
    $q->{category} = $p{categories} if $p{categories} && @{$p{categories}};

    $rs->non_public_if_possible($q, $c);

    # Add in any optional extra query parameters
    $q = { %$q, %{$p{extra}} } if $p{extra};

    my $problems = mySociety::Locale::in_gb_locale {
        $rs->search( $q, $attr )->include_comment_counts->page($p{page});
    };
    return $problems;
}

# Admin functions

sub timeline {
    my ( $rs ) = @_;

    return $rs->search(
        {
            -or => {
                'me.created' => { '>=', \"current_timestamp-'7 days'::interval" },
                'me.confirmed' => { '>=', \"current_timestamp-'7 days'::interval" },
                'me.whensent' => { '>=', \"current_timestamp-'7 days'::interval" },
            }
        },
        {
            prefetch => 'user',
        }
    );
}

sub summary_count {
    my ( $rs ) = @_;

    return $rs->search(
        undef,
        {
            group_by => ['state'],
            select   => [ 'state', { count => 'id' } ],
            as       => [qw/state state_count/]
        }
    );
}

sub unique_users {
    my ( $rs ) = @_;

    return $rs->search( {
        state => [ FixMyStreet::DB::Result::Problem->visible_states() ],
    }, {
        columns => [ 'user_id' ],
        distinct => 1,
    } );
}

sub categories_summary {
    my ( $rs ) = @_;

    my $fixed_case = "case when state IN ( '" . join( "', '", FixMyStreet::DB::Result::Problem->fixed_states() ) . "' ) then 1 else null end";
    my $categories = $rs->search( {
        state => [ FixMyStreet::DB::Result::Problem->visible_states() ],
        whensent => { '<' => \"NOW() - INTERVAL '4 weeks'" },
    }, {
        select   => [ 'category', { count => 'id' }, { count => \$fixed_case } ],
        as       => [ 'category', 'c', 'fixed' ],
        group_by => [ 'category' ],
        result_class => 'DBIx::Class::ResultClass::HashRefInflator'
    } );
    my %categories = map { $_->{category} => { total => $_->{c}, fixed => $_->{fixed} } } $categories->all;
    return \%categories;
}

sub include_comment_counts {
    my $rs = shift;
    my $order_by = $rs->{attrs}{order_by};
    return $rs unless ref $order_by eq 'ARRAY' && ref $order_by->[0] eq 'HASH' && $order_by->[0]->{-desc} eq 'comment_count';
    $rs->search({}, {
        '+select' => [ {
            "" => \'(select count(*) from comment where problem_id=me.id and state=\'confirmed\')',
            -as => 'comment_count'
        } ]
    });
}

1;