diff options
Diffstat (limited to 'perllib/FixMyStreet/DB/ResultSet')
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/DefectType.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Nearby.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 79 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/ResponsePriority.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/ResponseTemplate.pm | 5 |
5 files changed, 61 insertions, 39 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/DefectType.pm b/perllib/FixMyStreet/DB/ResultSet/DefectType.pm index b2ef77f7c..5b1247129 100644 --- a/perllib/FixMyStreet/DB/ResultSet/DefectType.pm +++ b/perllib/FixMyStreet/DB/ResultSet/DefectType.pm @@ -4,7 +4,6 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; use Moo; -use HTML::Entities; with('FixMyStreet::Roles::ContactExtra'); @@ -16,10 +15,10 @@ sub map_extras { my ($rs, @ts) = @_; return map { my $meta = $_->get_extra_metadata(); - my %extra = map { $_ => encode_entities($meta->{$_}) } keys %$meta; + my %extra = map { $_ => $meta->{$_} } keys %$meta; { id => $_->id, - name => encode_entities($_->name), + name => $_->name, extra => \%extra } } @ts; diff --git a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm index 6e5e0220f..b075e3664 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm @@ -10,22 +10,26 @@ sub to_body { } sub nearby { - my ( $rs, $c, $dist, $ids, $limit, $mid_lat, $mid_lon, $categories, $states ) = @_; + my ( $rs, $c, $dist, $ids, $limit, $mid_lat, $mid_lon, $categories, $states, $extra_params ) = @_; unless ( $states ) { $states = FixMyStreet::DB::Result::Problem->visible_states(); } my $params = { - non_public => 0, state => [ keys %$states ], }; $params->{id} = { -not_in => $ids } if $ids; $params->{category} = $categories if $categories && @$categories; + FixMyStreet::DB::ResultSet::Problem->non_public_if_possible($params, $c); + $rs = $c->cobrand->problems_restriction($rs); + # Add in any optional extra query parameters + $params = { %$params, %$extra_params } if $extra_params; + my $attrs = { prefetch => 'problem', bind => [ $mid_lat, $mid_lon, $dist ], diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 3f083c073..ef078ed08 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -15,15 +15,41 @@ sub set_restriction { $site_key = $key; } -sub to_body { - my ($rs, $bodies, $join) = @_; - return $rs unless $bodies; +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) { + if ($c->user->is_superuser) { + # See all reports, no restriction + } elsif ($c->user->has_body_permission_to('report_inspect')) { + $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( - \[ "regexp_split_to_array(bodies_str, ',') && ?", [ {} => $bodies ] ], + # 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; @@ -92,8 +118,8 @@ sub _recent { my $key = $photos ? 'recent_photos' : 'recent'; $key .= ":$site_key:$num"; - # unconfirmed might be returned for e.g. Zurich, but would mean in moderation, so no photo - my @states = grep { $_ ne 'unconfirmed' } FixMyStreet::DB::Result::Problem->visible_states(); + # 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, @@ -106,34 +132,25 @@ sub _recent { }; my $probs; - my $new = 0; - if (defined $lat) { - my $dist2 = $dist; # Create a copy of the variable to stop it being stringified into a locale in the next line! - $key .= ":$lat:$lon:$dist2"; - $probs = Memcached::get($key); - unless ($probs) { - $attrs->{bind} = [ $lat, $lon, $dist ]; - $attrs->{join} = 'nearby'; - $probs = [ mySociety::Locale::in_gb_locale { - $rs->search( $query, $attrs )->all; - } ]; - $new = 1; - } + 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); - unless ($probs) { + if ($probs) { + # Need to reattach schema so that confirmed column gets reinflated. + $probs->[0]->result_source->schema( $rs->result_source->schema ) if $probs->[0]; + # Catch any cached ones since hidden + $probs = [ grep { ! $_->is_hidden } @$probs ]; + } else { $probs = [ $rs->search( $query, $attrs )->all ]; - $new = 1; + Memcached::set($key, $probs, 3600); } } - if ( $new ) { - Memcached::set($key, $probs, 3600); - } else { - # Need to reattach schema so that confirmed column gets reinflated. - $probs->[0]->result_source->schema( $rs->result_source->schema ) if $probs->[0]; - } - return $probs; } @@ -151,13 +168,17 @@ sub around_map { } my $q = { - non_public => 0, state => [ keys %{$p{states}} ], latitude => { '>=', $p{min_lat}, '<', $p{max_lat} }, longitude => { '>=', $p{min_lon}, '<', $p{max_lon} }, }; $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}); }; diff --git a/perllib/FixMyStreet/DB/ResultSet/ResponsePriority.pm b/perllib/FixMyStreet/DB/ResultSet/ResponsePriority.pm index 89bb4dfd7..96f7cf7a0 100644 --- a/perllib/FixMyStreet/DB/ResultSet/ResponsePriority.pm +++ b/perllib/FixMyStreet/DB/ResultSet/ResponsePriority.pm @@ -4,7 +4,6 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; use Moo; -use HTML::Entities; with('FixMyStreet::Roles::ContactExtra'); @@ -14,7 +13,7 @@ sub join_table { sub map_extras { my ($rs, @ts) = @_; - return map { { id => $_->id, name => encode_entities($_->name) } } @ts; + return map { { id => $_->id, name => $_->name } } @ts; } 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/ResponseTemplate.pm b/perllib/FixMyStreet/DB/ResultSet/ResponseTemplate.pm index aa070daa3..46fcba153 100644 --- a/perllib/FixMyStreet/DB/ResultSet/ResponseTemplate.pm +++ b/perllib/FixMyStreet/DB/ResultSet/ResponseTemplate.pm @@ -2,7 +2,6 @@ package FixMyStreet::DB::ResultSet::ResponseTemplate; use base 'DBIx::Class::ResultSet'; use Moo; -use HTML::Entities; with('FixMyStreet::Roles::ContactExtra'); @@ -17,8 +16,8 @@ sub name_column { sub map_extras { my ($rs, @ts) = @_; return map { - my $out = { id => encode_entities($_->text), name => encode_entities($_->title) }; - $out->{state} = encode_entities($_->state) if $_->state; + my $out = { id => $_->text, name => $_->title }; + $out->{state} = $_->state if $_->state; $out; } @ts; } |