diff options
Diffstat (limited to 'perllib/FixMyStreet')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 63 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 15 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Auth.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Auth/Phone.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Auth/Profile.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 11 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/State.pm | 6 | ||||
-rw-r--r-- | perllib/FixMyStreet/SMS.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/Alerts.pm | 21 |
10 files changed, 82 insertions, 54 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index b854f16f5..719b04cf6 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -797,6 +797,19 @@ sub reports : Path('reports') { } +sub update_user : Private { + my ($self, $c, $object) = @_; + my $parsed = FixMyStreet::SMS->parse_username($c->get_param('username')); + if ($parsed->{email} || ($parsed->{phone} && $parsed->{may_be_mobile})) { + my $user = $c->model('DB::User')->find_or_create({ $parsed->{type} => $parsed->{username} }); + if ($user->id && $user->id != $object->user->id) { + $object->user( $user ); + return 1; + } + } + return 0; +} + sub report_edit : Path('report_edit') : Args(1) { my ( $self, $c, $id ) = @_; @@ -901,14 +914,7 @@ sub report_edit : Path('report_edit') : Args(1) { $problem->set_inflated_columns(\%columns); $c->forward( '/admin/report_edit_category', [ $problem ] ); - - my $parsed = FixMyStreet::SMS->parse_username($c->get_param('username')); - if ($parsed->{email} || ($parsed->{phone} && $parsed->{phone}->is_mobile)) { - my $user = $c->model('DB::User')->find_or_create({ $parsed->{type} => $parsed->{username} }); - if ($user->id && $user->id != $problem->user->id) { - $problem->user( $user ); - } - } + $c->forward('update_user', [ $problem ]); # Deal with photos my $remove_photo_param = $self->_get_remove_photo_param($c); @@ -1256,14 +1262,7 @@ sub update_edit : Path('update_edit') : Args(1) { $update->anonymous( $c->get_param('anonymous') ); $update->state( $new_state ); - my $parsed = FixMyStreet::SMS->parse_username($c->get_param('username')); - if ($parsed->{email} || ($parsed->{phone} && $parsed->{phone}->is_mobile)) { - my $user = $c->model('DB::User')->find_or_create({ $parsed->{type} => $parsed->{username} }); - if ($user->id && $user->id != $update->user->id) { - $edited = 1; - $update->user( $user ); - } - } + $edited = 1 if $c->forward('update_user', [ $update ]); if ( $new_state eq 'confirmed' and $old_state eq 'unconfirmed' ) { $update->confirmed( \'current_timestamp' ); @@ -1303,6 +1302,18 @@ sub update_edit : Path('update_edit') : Args(1) { return 1; } +sub phone_check : Private { + my ($self, $c, $phone) = @_; + my $parsed = FixMyStreet::SMS->parse_username($phone); + if ($parsed->{phone} && $parsed->{may_be_mobile}) { + return $parsed->{username}; + } elsif ($parsed->{phone}) { + $c->stash->{field_errors}->{phone} = _('Please enter a mobile number'); + } else { + $c->stash->{field_errors}->{phone} = _('Please check your phone number is correct'); + } +} + sub user_add : Path('user_edit') : Args(0) { my ( $self, $c ) = @_; @@ -1334,14 +1345,8 @@ sub user_add : Path('user_edit') : Args(0) { } if ($phone_v) { - my $parsed = FixMyStreet::SMS->parse_username($phone); - if ($parsed->{phone} && $parsed->{phone}->is_mobile) { - $phone = $parsed->{username}; - } elsif ($parsed->{phone}) { - $c->stash->{field_errors}->{phone} = _('Please enter a mobile number'); - } else { - $c->stash->{field_errors}->{phone} = _('Please check your phone number is correct'); - } + my $parsed_phone = $c->forward('phone_check', [ $phone ]); + $phone = $parsed_phone if $parsed_phone; } my $existing_email = $email_v && $c->model('DB::User')->find( { email => $email } ); @@ -1422,14 +1427,8 @@ sub user_edit : Path('user_edit') : Args(1) { } if ($phone_v) { - my $parsed = FixMyStreet::SMS->parse_username($phone); - if ($parsed->{phone} && $parsed->{phone}->is_mobile) { - $phone = $parsed->{username}; - } elsif ($parsed->{phone}) { - $c->stash->{field_errors}->{phone} = _('Please enter a mobile number'); - } else { - $c->stash->{field_errors}->{phone} = _('Please check your phone number is correct'); - } + my $parsed_phone = $c->forward('phone_check', [ $phone ]); + $phone = $parsed_phone if $parsed_phone; } unless ($user->name) { diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index b872084ff..30c023317 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -8,6 +8,7 @@ use FixMyStreet::Map; use Encode; use JSON::MaybeXS; use Utils; +use Try::Tiny; =head1 NAME @@ -366,13 +367,17 @@ sub lookup_by_ref : Private { external_id => $ref ]); - if ( $problems->count == 0) { - $c->detach( '/page_error_404_not_found', [] ); - } elsif ( $problems->count == 1 ) { - $c->res->redirect( $c->uri_for( '/report', $problems->first->id ) ); - } else { + my $count = try { + $problems->count; + } catch { + 0; + }; + + if ($count > 1) { $c->stash->{ref} = $ref; $c->stash->{matching_reports} = [ $problems->all ]; + } elsif ($count == 1) { + $c->res->redirect( $c->uri_for( '/report', $problems->first->id ) ); } } diff --git a/perllib/FixMyStreet/App/Controller/Auth.pm b/perllib/FixMyStreet/App/Controller/Auth.pm index b453f593b..80e407147 100644 --- a/perllib/FixMyStreet/App/Controller/Auth.pm +++ b/perllib/FixMyStreet/App/Controller/Auth.pm @@ -119,7 +119,7 @@ sub code_sign_in : Private { my $parsed = FixMyStreet::SMS->parse_username($username); if ($parsed->{type} eq 'phone' && FixMyStreet->config('SMS_AUTHENTICATION')) { - $c->forward('phone/sign_in', [ $parsed->{phone} ]); + $c->forward('phone/sign_in', [ $parsed ]); } else { $c->forward('email_sign_in', [ $parsed->{username} ]); } diff --git a/perllib/FixMyStreet/App/Controller/Auth/Phone.pm b/perllib/FixMyStreet/App/Controller/Auth/Phone.pm index 4e9f92596..e4ffc2205 100644 --- a/perllib/FixMyStreet/App/Controller/Auth/Phone.pm +++ b/perllib/FixMyStreet/App/Controller/Auth/Phone.pm @@ -45,19 +45,19 @@ and sets up the token/etc to deal with the response. =cut sub sign_in : Private { - my ( $self, $c, $phone ) = @_; + my ( $self, $c, $parsed ) = @_; - unless ($phone) { + unless ($parsed->{phone}) { $c->stash->{username_error} = 'other_phone'; return; } - unless ($phone->is_mobile) { + unless ($parsed->{may_be_mobile}) { $c->stash->{username_error} = 'nonmobile'; return; } - (my $number = $phone->format) =~ s/\s+//g; + (my $number = $parsed->{phone}->format) =~ s/\s+//g; if ( FixMyStreet->config('SIGNUPS_DISABLED') && !$c->model('DB::User')->find({ phone => $number }) diff --git a/perllib/FixMyStreet/App/Controller/Auth/Profile.pm b/perllib/FixMyStreet/App/Controller/Auth/Profile.pm index ecf009150..acffd3019 100644 --- a/perllib/FixMyStreet/App/Controller/Auth/Profile.pm +++ b/perllib/FixMyStreet/App/Controller/Auth/Profile.pm @@ -118,14 +118,14 @@ sub change_phone : Path('/auth/change_phone') { # If we've not used a mobile and we're not specifically verifying, # and phone isn't our only verified way of logging in, # then allow change of number (for e.g. landline). - if (!FixMyStreet->config('SMS_AUTHENTICATION') || (!$parsed->{phone}->is_mobile && !$c->stash->{verifying} && $c->user->email_verified)) { + if (!FixMyStreet->config('SMS_AUTHENTICATION') || (!$parsed->{may_be_mobile} && !$c->stash->{verifying} && $c->user->email_verified)) { $c->user->update({ phone => $phone, phone_verified => 0 }); $c->flash->{flash_message} = _('You have successfully added your phone number.'); $c->res->redirect('/my'); $c->detach; } - $c->forward('/auth/phone/sign_in', [ $parsed->{phone} ]); + $c->forward('/auth/phone/sign_in', [ $parsed ]); } sub verify_item : Path('/auth/verify') : Args(1) { diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 562f29693..409a6e1ab 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -274,14 +274,9 @@ sub problem_state_display { return FixMyStreet::DB->resultset("State")->display('confirmed', 1); } elsif ($self->problem_state) { my $state = $self->problem_state; - if ($state eq 'not responsible') { - $update_state = _( "not the council's responsibility" ); - if ($cobrand eq 'bromley' || $self->problem->to_body_named('Bromley')) { - $update_state = 'third party responsibility'; - } - } else { - $update_state = FixMyStreet::DB->resultset("State")->display($state, 1); - } + my $cobrand_name = $cobrand; + $cobrand_name = 'bromley' if $self->problem->to_body_named('Bromley'); + $update_state = FixMyStreet::DB->resultset("State")->display($state, 1, $cobrand_name); } return $update_state; diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index 296cf997d..d02039ac3 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -196,7 +196,7 @@ sub check_for_errors { my $parsed = FixMyStreet::SMS->parse_username($self->phone); if (!$parsed->{phone}) { $errors{username} = _('Please check your phone number is correct'); - } elsif (!$parsed->{phone}->is_mobile) { + } elsif (!$parsed->{may_be_mobile}) { $errors{username} = _('Please enter a mobile number'); } } diff --git a/perllib/FixMyStreet/DB/ResultSet/State.pm b/perllib/FixMyStreet/DB/ResultSet/State.pm index de56c738c..3e6169aeb 100644 --- a/perllib/FixMyStreet/DB/ResultSet/State.pm +++ b/perllib/FixMyStreet/DB/ResultSet/State.pm @@ -58,7 +58,7 @@ sub fixed { [ $_[0]->_filter(sub { $_->type eq 'fixed' }) ] } # This function can be used to return that label's display name. sub display { - my ($rs, $label, $single_fixed) = @_; + my ($rs, $label, $single_fixed, $cobrand) = @_; my $unchanging = { unconfirmed => _("Unconfirmed"), hidden => _("Hidden"), @@ -72,6 +72,10 @@ sub display { }; $label = 'fixed' if $single_fixed && $label =~ /^fixed - (council|user)$/; return $unchanging->{$label} if $unchanging->{$label}; + if ($cobrand && $label eq 'not responsible') { + return 'third party responsibility' if $cobrand eq 'bromley'; + return _("not the council's responsibility"); + } my ($state) = $rs->_filter(sub { $_->label eq $label }); return $label unless $state; $state->name($translate_now->{$label}) if $translate_now->{$label}; diff --git a/perllib/FixMyStreet/SMS.pm b/perllib/FixMyStreet/SMS.pm index ec9251a1a..c71cceadc 100644 --- a/perllib/FixMyStreet/SMS.pm +++ b/perllib/FixMyStreet/SMS.pm @@ -94,15 +94,19 @@ sub parse_username { } }; + my $may_be_mobile = 0; if ($phone) { $type = 'phone'; # Store phone without spaces ($username = $phone->format) =~ s/\s+//g; + # Is this mobile definitely or possibly a mobile? (+1 numbers) + $may_be_mobile = 1 if $phone->is_mobile || (!defined $phone->is_mobile && $phone->is_geographic); } return { type => $type, phone => $phone, + may_be_mobile => $may_be_mobile, username => $username, }; } diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm index 86f11c7b5..4b5641f9e 100644 --- a/perllib/FixMyStreet/Script/Alerts.pm +++ b/perllib/FixMyStreet/Script/Alerts.pm @@ -39,6 +39,7 @@ sub send() { $item_table.name as item_name, $item_table.anonymous as item_anonymous, $item_table.confirmed as item_confirmed, $item_table.photo as item_photo, + $item_table.problem_state as item_problem_state, $head_table.* from alert, $item_table, $head_table where alert.parameter::integer = $head_table.id @@ -65,6 +66,7 @@ sub send() { $query = FixMyStreet::DB->schema->storage->dbh->prepare($query); $query->execute(); my $last_alert_id; + my $last_problem_state = ''; my %data = ( template => $alert_type->template, data => [], schema => $schema ); while (my $row = $query->fetchrow_hashref) { @@ -86,7 +88,26 @@ sub send() { alert_id => $row->{alert_id}, parameter => $row->{item_id}, } ); + + # this is currently only for new_updates + if (defined($row->{item_text})) { + # this might throw up the odd false positive but only in cases where the + # state has changed and there was already update text + if ($row->{item_problem_state} && + !( $last_problem_state eq '' && $row->{item_problem_state} eq 'confirmed' ) && + $last_problem_state ne $row->{item_problem_state} + ) { + my $state = FixMyStreet::DB->resultset("State")->display($row->{item_problem_state}, 1, $cobrand); + + my $update = _('State changed to:') . ' ' . $state; + $row->{item_text} = $row->{item_text} ? $row->{item_text} . "\n\n" . $update : + $update; + } + next unless $row->{item_text}; + } + if ($last_alert_id && $last_alert_id != $row->{alert_id}) { + $last_problem_state = ''; _send_aggregated_alert_email(%data); %data = ( template => $alert_type->template, data => [], schema => $schema ); } |