Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dupe suggestions can be disabled on a per-category basis in config #5162

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
- Add `send_state` column to reports. #4048
- Return random unprocessed row to daemon.
- A cobrand level text override for the details field label on new reports can now be configured.
- Cobrands can provide per-category custom distances for duplicate lookup. #4746
- Cobrands can provide per-category custom distances for duplicate lookup. #4746 #5162
- Add perl 5.38 support.
- Add plain text template previews to /_dev/email. #5105
- Performance improvements:
Expand Down
66 changes: 36 additions & 30 deletions perllib/FixMyStreet/App/Controller/Report.pm
Original file line number Diff line number Diff line change
Expand Up @@ -727,38 +727,42 @@ sub _nearby_json :Private {
# This is for the list template, this is a list on that page.
$c->stash->{page} = 'report';

my $dist = $self->_find_distance($c, $params);
$params->{distance} = $dist / 1000 unless $params->{distance}; # DB measures in km
if (my $dist = $self->_find_distance($c, $params)) { # distance of 0 means we can skip lookup entirely
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is fine, but could be done as an early return if not present, then don't need to indent everything further

$params->{distance} = $dist / 1000 unless $params->{distance}; # DB measures in km

my $pin_size = $c->get_param('pin_size') || '';
$pin_size = 'small' unless $pin_size =~ /^(mini|small|normal|big)$/;
my $pin_size = $c->get_param('pin_size') || '';
$pin_size = 'small' unless $pin_size =~ /^(mini|small|normal|big)$/;

$params->{extra} = $c->cobrand->call_hook('display_location_extra_params');
$params->{limit} = 5;
$params->{extra} = $c->cobrand->call_hook('display_location_extra_params');
$params->{limit} = 5;

my $nearby = $c->model('DB::Nearby')->nearby($c, %$params);
my $nearby = $c->model('DB::Nearby')->nearby($c, %$params);

# Want to treat these as if they were on map
$nearby = [ map { $_->problem } @$nearby ];
my @pins = map {
my $p = $_->pin_data('around');
[ $p->{latitude}, $p->{longitude}, $p->{colour},
$p->{id}, $p->{title}, $pin_size, JSON->false
]
} @$nearby;
# Want to treat these as if they were on map
$nearby = [ map { $_->problem } @$nearby ];
my @pins = map {
my $p = $_->pin_data('around');
[ $p->{latitude}, $p->{longitude}, $p->{colour},
$p->{id}, $p->{title}, $pin_size, JSON->false
]
} @$nearby;

my @extra_pins = $c->cobrand->call_hook('extra_nearby_pins', $params->{latitude}, $params->{longitude}, $dist);
@pins = (@pins, @extra_pins) if @extra_pins;
my @extra_pins = $c->cobrand->call_hook('extra_nearby_pins', $params->{latitude}, $params->{longitude}, $dist);
@pins = (@pins, @extra_pins) if @extra_pins;

my $list_html = $c->render_fragment(
'report/nearby.html',
{ reports => $nearby, inline_maps => $c->get_param("inline_maps") ? 1 : 0, extra_pins => \@extra_pins }
);
my $json = { pins => \@pins };
$json->{reports_list} = $list_html if $list_html;
my $body = encode_json($json);
$c->res->content_type('application/json; charset=utf-8');
$c->res->body($body);
my $list_html = $c->render_fragment(
'report/nearby.html',
{ reports => $nearby, inline_maps => $c->get_param("inline_maps") ? 1 : 0, extra_pins => \@extra_pins }
);
my $json = { pins => \@pins };
$json->{reports_list} = $list_html if $list_html;
my $body = encode_json($json);
$c->res->content_type('application/json; charset=utf-8');
$c->res->body($body);
} else {
$c->res->content_type('application/json; charset=utf-8');
$c->res->body(encode_json({ 'pins' => []}));
}
}
}

Expand All @@ -769,7 +773,9 @@ sub _nearby_json :Private {
# b) distances for a group/parent category
# c) distances by mode
#
# NOTE: Distances for category or group only apply for the 'suggestions' mode.
# NOTES:
# - Distances for category or group only apply for the 'suggestions' mode.
# - Returning a distance of 0 means we can skip the nearby lookup entirely.
sub _find_distance {
my ($self, $c, $params) = @_;

Expand All @@ -782,17 +788,17 @@ sub _find_distance {
my $category = $params->{categories}[0];
my $group = $params->{group};

$dist = $category && $cobrand_distances->{$mode}{$category}
$dist = $category && defined $cobrand_distances->{$mode}{$category}
? $cobrand_distances->{$mode}{$category}
: $group && $cobrand_distances->{$mode}{$group}
: $group && defined $cobrand_distances->{$mode}{$group}
? $cobrand_distances->{$mode}{$group}
: $cobrand_distances->{$mode}{_fallback};
} else {
$dist = $cobrand_distances->{$mode};
}
}

$dist ||= $c->get_param('distance') || '';
$dist //= $c->get_param('distance') || '';
$dist = 1000 unless $dist =~ /^\d+$/;
$dist = 1000 if $dist > 1000;

Expand Down
2 changes: 2 additions & 0 deletions perllib/FixMyStreet/Cobrand/Default.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,8 @@ inspector de-duplication and report duplicate suggestions features.

Defaults to 1000m for inspectors, 250m for duplicate suggestions.

Returning a distance of 0 means no nearby reports will be returned at all.

Should return a hashref of the form

{
Expand Down
22 changes: 22 additions & 0 deletions t/app/controller/around.t
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ subtest 'check nearby lookup, cobrand custom distances per category' => sub {
_fallback => 800,
'Subcat 1 in Group 1' => 100,
'Group 1' => 400,
'No suggestions category' => 0,
},
},
},
Expand All @@ -611,6 +612,12 @@ subtest 'check nearby lookup, cobrand custom distances per category' => sub {
longitude => -1.256179,
category => 'Subcat in Group 2',
});
$mech->create_problems_for_body( 1, $body->id, 'Around page', {
postcode => 'OX20 1SZ',
latitude => 51.754926,
longitude => -1.256179,
category => 'No suggestions category',
});

note 'filter_category = Subcat 1 in Group 1, filter_group = Group 1';
for my $test (
Expand Down Expand Up @@ -702,6 +709,21 @@ subtest 'check nearby lookup, cobrand custom distances per category' => sub {
);
}
}

note 'filter_category = No suggestions category';
for my $test (
{ lat => 51.754926, lon => -1.256179 }, # 0m away
{ lat => 51.7549, lon => -1.256 }, # 12m away
{ lat => 51.752, lon => -1.256 }, # 325m away
{ lat => 51.7485, lon => -1.256 }, # 714m away
{ lat => 51.74, lon => -1.256 }, # 1660m away
) {
my $json = $mech->get_ok_json( '/around/nearby?latitude=' . $test->{lat}
. '&longitude=' . $test->{lon}
. '&mode=suggestions'
. '&filter_category=No suggestions category' );
is_deeply $json, { pins => [] }, 'No suggestions category should not be shown';
}
};
};

Expand Down
Loading