aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/DB
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/DB')
-rw-r--r--perllib/FixMyStreet/DB/Factories.pm3
-rw-r--r--perllib/FixMyStreet/DB/Result/Body.pm5
-rw-r--r--perllib/FixMyStreet/DB/Result/Comment.pm45
-rw-r--r--perllib/FixMyStreet/DB/Result/Problem.pm59
-rw-r--r--perllib/FixMyStreet/DB/Result/User.pm18
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/Body.pm75
-rw-r--r--perllib/FixMyStreet/DB/ResultSet/Problem.pm6
7 files changed, 145 insertions, 66 deletions
diff --git a/perllib/FixMyStreet/DB/Factories.pm b/perllib/FixMyStreet/DB/Factories.pm
index 7b8234aec..0e99608e1 100644
--- a/perllib/FixMyStreet/DB/Factories.pm
+++ b/perllib/FixMyStreet/DB/Factories.pm
@@ -22,7 +22,7 @@ use parent "DBIx::Class::Factory";
__PACKAGE__->resultset(FixMyStreet::DB->resultset("Problem"));
-__PACKAGE__->exclude(['body']);
+__PACKAGE__->exclude(['body', 'photo_id']);
__PACKAGE__->fields({
postcode => '',
@@ -30,6 +30,7 @@ __PACKAGE__->fields({
detail => __PACKAGE__->seq(sub { 'Detail #' . (shift()+1) }),
name => __PACKAGE__->callback(sub { shift->get('user')->name }),
bodies_str => __PACKAGE__->callback(sub { shift->get('body')->id }),
+ photo => __PACKAGE__->callback(sub { shift->get('photo_id') }),
confirmed => \'current_timestamp',
whensent => \'current_timestamp',
state => 'confirmed',
diff --git a/perllib/FixMyStreet/DB/Result/Body.pm b/perllib/FixMyStreet/DB/Result/Body.pm
index e5cd2b907..07bea276c 100644
--- a/perllib/FixMyStreet/DB/Result/Body.pm
+++ b/perllib/FixMyStreet/DB/Result/Body.pm
@@ -158,10 +158,11 @@ sub areas {
sub first_area_children {
my ( $self ) = @_;
- my $area_id = $self->body_areas->first->area_id;
+ my $body_area = $self->body_areas->first;
+ return unless $body_area;
my $cobrand = $self->result_source->schema->cobrand;
- my $children = mySociety::MaPit::call('area/children', $area_id,
+ my $children = mySociety::MaPit::call('area/children', $body_area->area_id,
type => $cobrand->area_types_children,
);
diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm
index 60fd31510..8a4dbe475 100644
--- a/perllib/FixMyStreet/DB/Result/Comment.pm
+++ b/perllib/FixMyStreet/DB/Result/Comment.pm
@@ -156,26 +156,6 @@ sub url {
return "/report/" . $self->problem_id . '#update_' . $self->id;
}
-sub photos {
- my $self = shift;
- my $photoset = $self->get_photoset;
- my $i = 0;
- my $id = $self->id;
- my @photos = map {
- my $cachebust = substr($_, 0, 8);
- my ($hash, $format) = split /\./, $_;
- {
- id => $hash,
- url_temp => "/photo/temp.$hash.$format",
- url_temp_full => "/photo/fulltemp.$hash.$format",
- url => "/photo/c/$id.$i.$format?$cachebust",
- url_full => "/photo/c/$id.$i.full.$format?$cachebust",
- idx => $i++,
- }
- } $photoset->all_ids;
- return \@photos;
-}
-
=head2 latest_moderation_log_entry
Return most recent ModerationLog object
@@ -293,4 +273,29 @@ sub problem_state_display {
return $update_state;
}
+sub is_latest {
+ my $self = shift;
+ my $latest_update = $self->result_source->resultset->search(
+ { problem_id => $self->problem_id, state => 'confirmed' },
+ { order_by => [ { -desc => 'confirmed' }, { -desc => 'id' } ] }
+ )->first;
+ return $latest_update->id == $self->id;
+}
+
+sub hide {
+ my $self = shift;
+
+ my $ret = {};
+
+ # If we're hiding an update, see if it marked as fixed and unfix if so
+ if ($self->mark_fixed && $self->is_latest && $self->problem->state =~ /^fixed/) {
+ $self->problem->state('confirmed');
+ $self->problem->update;
+ $ret->{reopened} = 1;
+ }
+ $self->get_photoset->delete_cached;
+ $self->update({ state => 'hidden' });
+ return $ret;
+}
+
1;
diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm
index 8625bf17a..c73f7efca 100644
--- a/perllib/FixMyStreet/DB/Result/Problem.pm
+++ b/perllib/FixMyStreet/DB/Result/Problem.pm
@@ -867,6 +867,33 @@ sub update_send_failed {
} );
}
+=head2 updates_sent_to_body
+
+Returns 1 if updates left on this report will be sent to any of the receiving
+bodies by some mechanism. Right now that mechanism is Open311.
+
+=cut
+
+sub updates_sent_to_body {
+ my $self = shift;
+ return unless $self->send_method_used && $self->send_method_used eq 'Open311';
+
+ # Some bodies only send updates *to* FMS, they don't receive updates.
+ # NB See also the list in bin/send-comments
+ my $excluded = qr{Lewisham|Oxfordshire};
+
+ my @bodies = values %{ $self->bodies };
+ my @updates_sent = grep {
+ $_->send_comments &&
+ (
+ $_->send_method eq 'Open311' ||
+ $_->send_method eq 'Noop' # Sending might be temporarily disabled
+ ) &&
+ !($_->name =~ /$excluded/)
+ } @bodies;
+ return scalar @updates_sent;
+}
+
sub add_send_method {
my $self = shift;
my $sender = shift;
@@ -919,38 +946,6 @@ sub latest_moderation_log_entry {
return $self->admin_log_entries->search({ action => 'moderation' }, { order_by => { -desc => 'id' } })->first;
}
-sub photos {
- my $self = shift;
- my $photoset = $self->get_photoset;
- my $i = 0;
- my $id = $self->id;
- my @photos = map {
- my $cachebust = substr($_, 0, 8);
- # Some Varnish configurations (e.g. on mySociety infra) strip cookies from
- # images, which means image requests will be redirected to the login page
- # if LOGIN_REQUIRED is set. To stop this happening, Varnish should be
- # configured to not strip cookies if the cookie_passthrough param is
- # present, which this line ensures will be if LOGIN_REQUIRED is set.
- my $extra = '';
- if (FixMyStreet->config('LOGIN_REQUIRED')) {
- $cachebust .= '&cookie_passthrough=1';
- $extra = '?cookie_passthrough=1';
- }
- my ($hash, $format) = split /\./, $_;
- {
- id => $hash,
- url_temp => "/photo/temp.$hash.$format$extra",
- url_temp_full => "/photo/fulltemp.$hash.$format$extra",
- url => "/photo/$id.$i.$format?$cachebust",
- url_full => "/photo/$id.$i.full.$format?$cachebust",
- url_tn => "/photo/$id.$i.tn.$format?$cachebust",
- url_fp => "/photo/$id.$i.fp.$format?$cachebust",
- idx => $i++,
- }
- } $photoset->all_ids;
- return \@photos;
-}
-
__PACKAGE__->has_many(
"admin_log_entries",
"FixMyStreet::DB::Result::AdminLog",
diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm
index d02039ac3..db68236bf 100644
--- a/perllib/FixMyStreet/DB/Result/User.pm
+++ b/perllib/FixMyStreet/DB/Result/User.pm
@@ -125,11 +125,15 @@ with 'FixMyStreet::Roles::Extra';
__PACKAGE__->many_to_many( planned_reports => 'user_planned_reports', 'report' );
+sub cost {
+ FixMyStreet->test_mode ? 1 : 12;
+}
+
__PACKAGE__->add_columns(
"password" => {
encode_column => 1,
encode_class => 'Crypt::Eksblowfish::Bcrypt',
- encode_args => { cost => 8 },
+ encode_args => { cost => cost() },
encode_check_method => 'check_password',
},
);
@@ -150,8 +154,9 @@ sub username {
sub phone_display {
my $self = shift;
return $self->phone unless $self->phone;
+ my $country = FixMyStreet->config('PHONE_COUNTRY');
my $parsed = FixMyStreet::SMS->parse_username($self->phone);
- return $parsed->{phone} ? $parsed->{phone}->format : $self->phone;
+ return $parsed->{phone} ? $parsed->{phone}->format_for_country($country) : $self->phone;
}
sub latest_anonymity {
@@ -195,9 +200,13 @@ sub check_for_errors {
} elsif ($self->phone_verified) {
my $parsed = FixMyStreet::SMS->parse_username($self->phone);
if (!$parsed->{phone}) {
+ # Errors with the phone number may apply to both the username or
+ # phone field depending on the form.
$errors{username} = _('Please check your phone number is correct');
+ $errors{phone} = _('Please check your phone number is correct');
} elsif (!$parsed->{may_be_mobile}) {
$errors{username} = _('Please enter a mobile number');
+ $errors{phone} = _('Please enter a mobile number');
}
}
@@ -395,6 +404,11 @@ sub admin_user_body_permissions {
});
}
+sub has_2fa {
+ my $self = shift;
+ return $self->is_superuser && $self->get_extra_metadata('2fa_secret');
+}
+
sub contributing_as {
my ($self, $other, $c, $bodies) = @_;
$bodies = [ keys %$bodies ] if ref $bodies eq 'HASH';
diff --git a/perllib/FixMyStreet/DB/ResultSet/Body.pm b/perllib/FixMyStreet/DB/ResultSet/Body.pm
index e79d038b1..0aa3e8240 100644
--- a/perllib/FixMyStreet/DB/ResultSet/Body.pm
+++ b/perllib/FixMyStreet/DB/ResultSet/Body.pm
@@ -3,6 +3,25 @@ use base 'DBIx::Class::ResultSet';
use strict;
use warnings;
+use POSIX qw(strcoll);
+
+=head1 Name
+
+FixMyStreet::DB::ResultSet::Body - a ResultSet class for the Body model.
+
+=head1 Synopsis
+
+ my @bodies = $rs->for_areas(1, 2)->active->with_area_count->translated->all_sorted;
+
+=head1 Functions
+
+=over
+
+=item for_areas
+
+This restricts the ResultSet to bodies covering the area IDs provided.
+
+=cut
sub for_areas {
my ( $rs, @areas ) = @_;
@@ -14,14 +33,64 @@ sub for_areas {
return $result;
}
-sub all_translated {
+=item active
+
+This restricts the ResultSet to bodies that are not marked as deleted.
+
+=cut
+
+sub active {
+ my $rs = shift;
+ $rs->search({ deleted => 0 });
+}
+
+=item translated
+
+This joins the ResultSet to the translation table, adding the `msgstr`
+column containing possible translations of the body name.
+
+=cut
+
+sub translated {
my $rs = shift;
my $schema = $rs->result_source->schema;
- my @bodies = $rs->search(undef, {
+ $rs->search(undef, {
'+columns' => { 'msgstr' => 'translations.msgstr' },
join => 'translations',
bind => [ 'name', $schema->lang, 'body' ],
- })->all;
+ });
+}
+
+=item with_area_count
+
+This adds the number of areas associated with each body to the ResultSet,
+in the area_count column.
+
+=cut
+
+sub with_area_count {
+ my $rs = shift;
+ $rs->search(undef, {
+ '+select' => [ { count => 'area_id' } ],
+ '+as' => [ 'area_count' ],
+ join => 'body_areas',
+ distinct => 1,
+ });
+}
+
+=item all_sorted
+
+This returns all results, as C<all()>, but sorted by their name column
+(which will be the translated names if present).
+
+=back
+
+=cut
+
+sub all_sorted {
+ my $rs = shift;
+ my @bodies = $rs->all;
+ @bodies = sort { strcoll($a->name, $b->name) } @bodies;
return @bodies;
}
diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm
index ae45351c4..3f083c073 100644
--- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm
+++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm
@@ -232,12 +232,6 @@ sub categories_summary {
return \%categories;
}
-sub send_reports {
- my ( $rs, $site_override ) = @_;
- require FixMyStreet::Script::Reports;
- return FixMyStreet::Script::Reports::send($site_override);
-}
-
sub include_comment_counts {
my $rs = shift;
my $order_by = $rs->{attrs}{order_by};