diff options
94 files changed, 1387 insertions, 1329 deletions
diff --git a/bin/fetch-comments b/bin/fetch-comments index 4eb9def14..55ba70d85 100755 --- a/bin/fetch-comments +++ b/bin/fetch-comments @@ -15,14 +15,11 @@ BEGIN { require "$d/../setenv.pl"; } -use FixMyStreet::App; use CronFns; my ($verbose, $nomail) = CronFns::options(); -use Open311; use Open311::GetServiceRequestUpdates; - my $updates = Open311::GetServiceRequestUpdates->new( verbose => $verbose ); $updates->fetch; diff --git a/bin/fetch-comments-24hs b/bin/fetch-comments-24hs index 27853bcc1..df208439e 100755 --- a/bin/fetch-comments-24hs +++ b/bin/fetch-comments-24hs @@ -19,11 +19,9 @@ BEGIN { use DateTime; use DateTime::Format::W3CDTF; -use FixMyStreet::App; use CronFns; my ($verbose, $nomail) = CronFns::options(); -use Open311; use Open311::GetServiceRequestUpdates; my $dt = DateTime->now(); diff --git a/bin/fiksgatami/showroadinfo b/bin/fiksgatami/showroadinfo index 658fca01c..fda6fbb6b 100755 --- a/bin/fiksgatami/showroadinfo +++ b/bin/fiksgatami/showroadinfo @@ -3,11 +3,12 @@ use strict; use warnings; -use FixMyStreet::App; +use FixMyStreet::Cobrand; +use FixMyStreet::DB; use FixMyStreet::Geocode::OSM; use Data::Dumper; -FixMyStreet::App->model('DB::Problem')->send_reports(); +FixMyStreet::DB->resultset('Problem')->send_reports(); my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('fiksgatami')->new(); diff --git a/bin/fiksgatami/showroadstats b/bin/fiksgatami/showroadstats index 83c3c4310..9bc8de70b 100755 --- a/bin/fiksgatami/showroadstats +++ b/bin/fiksgatami/showroadstats @@ -3,10 +3,11 @@ use strict; use warnings; -use FixMyStreet::App; +use FixMyStreet::Cobrand; +use FixMyStreet::DB; use FixMyStreet::Geocode::OSM; -my @reports = FixMyStreet::App->model('DB::Problem')->search({ +my @reports = FixMyStreet::DB->resultset('Problem')->search({ state => [ FixMyStreet::DB::Result::Problem->visible_states() ], }, { rows => 40, diff --git a/bin/fixmystreet.com/populate_bing_cache b/bin/fixmystreet.com/populate_bing_cache index 17c8911d0..c644b34fa 100755 --- a/bin/fixmystreet.com/populate_bing_cache +++ b/bin/fixmystreet.com/populate_bing_cache @@ -6,12 +6,12 @@ require 5.8.0; use Data::Dumper; -use FixMyStreet::App; +use FixMyStreet::DB; use FixMyStreet::Geocode::Bing; my $bing_culture = 'en-GB'; -my $reports = FixMyStreet::App->model('DB::Problem')->search( +my $reports = FixMyStreet::DB->resultset('Problem')->search( { geocode => undef, confirmed => { '!=', undef }, diff --git a/bin/fixmystreet.com/update-send-questionnaire b/bin/fixmystreet.com/update-send-questionnaire index 7a231b919..40549c6e4 100755 --- a/bin/fixmystreet.com/update-send-questionnaire +++ b/bin/fixmystreet.com/update-send-questionnaire @@ -10,7 +10,8 @@ reports in cobrands that don't send questionnaires at all. use strict; use warnings; -use FixMyStreet::App; +use FixMyStreet::Cobrand; +use FixMyStreet::DB; my @cobrands; foreach my $cobrand ( FixMyStreet::Cobrand->available_cobrand_classes() ) { @@ -19,7 +20,7 @@ foreach my $cobrand ( FixMyStreet::Cobrand->available_cobrand_classes() ) { push @cobrands, $cls->moniker if !$cls->send_questionnaires(); } -my $problems = FixMyStreet::App->model('DB::Problem')->search({ +my $problems = FixMyStreet::DB->resultset('Problem')->search({ cobrand => \@cobrands, send_questionnaire => 1, }); diff --git a/bin/handlemail b/bin/handlemail index 5d4ac753c..597d08a5d 100755 --- a/bin/handlemail +++ b/bin/handlemail @@ -17,15 +17,14 @@ use strict; use warnings; require 5.8.0; -# Horrible boilerplate to set up appropriate library paths. -use FindBin; -use lib "$FindBin::Bin/../perllib"; -use lib "$FindBin::Bin/../commonlib/perllib"; - -use mySociety::Config; BEGIN { - mySociety::Config::set_file("$FindBin::Bin/../conf/general"); + use File::Basename qw(dirname); + use File::Spec; + my $d = dirname(File::Spec->rel2abs($0)); + require "$d/../setenv.pl"; } + +use FixMyStreet; use mySociety::Email; use mySociety::EmailUtil; use mySociety::HandleMail; @@ -40,7 +39,7 @@ my %data = mySociety::HandleMail::get_message(); if ($data{is_bounce_message}) { #my $a = mySociety::HandleMail::get_bounce_recipient($data{message}); #my $token = mySociety::HandleMail::get_token($a, - # 'fms-', mySociety::Config::get('EMAILDOMAIN') + # 'fms-', FixMyStreet->config('EMAILDOMAIN') #); #exit(0) if $token eq 'DO-NOT-REPLY'; # A bounce we don't care about exit(0); # drop all other bounces currently @@ -48,13 +47,13 @@ if ($data{is_bounce_message}) { # Not a bounce, send an automatic response my $template = 'reply-autoresponse'; -open FP, "$FindBin::Bin/../templates/email/default/$template" or exit 75; -$template = join('', <FP>); -close FP; +my $fp = FixMyStreet->path_to("templates", "email", "default", $template)->open or exit 75; +$template = join('', <$fp>); +$fp->close; # We generate this as a bounce. my $mail = mySociety::Email::construct_email({ - From => [ mySociety::Config::get('CONTACT_EMAIL'), 'FixMyStreet' ], + From => [ FixMyStreet->config('CONTACT_EMAIL'), 'FixMyStreet' ], To => $data{return_path}, _template_ => $template, _parameters_ => { }, diff --git a/bin/handlemail-support b/bin/handlemail-support index 9808e447a..0ccde8ca7 100755 --- a/bin/handlemail-support +++ b/bin/handlemail-support @@ -1,4 +1,4 @@ -#!/usr/bin/perl -w +#!/usr/bin/perl # # handlemail-support: # Handle an individual incoming mail message. @@ -11,17 +11,17 @@ # Email: matthew@mysociety.org; WWW: http://www.mysociety.org/ use strict; +use warnings; require 5.8.0; -# Horrible boilerplate to set up appropriate library paths. -use FindBin; -use lib "$FindBin::Bin/../perllib"; -use lib "$FindBin::Bin/../commonlib/perllib"; - -use mySociety::Config; BEGIN { - mySociety::Config::set_file("$FindBin::Bin/../conf/general"); + use File::Basename qw(dirname); + use File::Spec; + my $d = dirname(File::Spec->rel2abs($0)); + require "$d/../setenv.pl"; } + +use FixMyStreet; use mySociety::EmailUtil; use mySociety::HandleMail; @@ -32,7 +32,7 @@ forward_on(); # --- sub forward_on { - my ($l, $d) = split /\@/, mySociety::Config::get('CONTACT_EMAIL'); + my ($l, $d) = split /\@/, FixMyStreet->config('CONTACT_EMAIL'); if (mySociety::EmailUtil::EMAIL_SUCCESS != mySociety::EmailUtil::send_email( join("\n", @{$data{lines}}) . "\n", @@ -50,7 +50,7 @@ sub is_ignorable { my ($from, $subject, $body) = ($head->get('From'), $head->get('Subject'), $m->body); $body = join("\n", @$body); - open my $fp, "$FindBin::Bin/../../data/ignored-emails.csv" or exit 75; + my $fp = FixMyStreet->path_to("..", "data", "ignored-emails.csv")->open or exit 75; while (<$fp>) { chomp; my ($f, $s, $b) = split /,/; diff --git a/bin/open311-populate-service-list b/bin/open311-populate-service-list index 11f9005a5..211061258 100755 --- a/bin/open311-populate-service-list +++ b/bin/open311-populate-service-list @@ -10,7 +10,7 @@ BEGIN { require "$d/../setenv.pl"; } -use FixMyStreet::App; +use FixMyStreet::DB; use Open311::PopulateServiceList; use Getopt::Long::Descriptive; @@ -22,7 +22,7 @@ my ($opt, $usage) = describe_options( ); print($usage->text), exit if $opt->help; -my $bodies = FixMyStreet::App->model('DB::Body')->search( { +my $bodies = FixMyStreet::DB->resultset('Body')->search( { id => { '!=', 2237 }, # XXX Until Oxfordshire does do so send_method => 'Open311' } ); diff --git a/bin/open311-update-reports b/bin/open311-update-reports index 6a1c894d7..378f8e8bc 100755 --- a/bin/open311-update-reports +++ b/bin/open311-update-reports @@ -17,17 +17,18 @@ BEGIN { } use Open311::GetUpdates; -use FixMyStreet::App; +use FixMyStreet; +use FixMyStreet::DB; # FIXME - make this configurable and/or better -my $system_user = FixMyStreet::App->model('DB::User')->find_or_create( +my $system_user = FixMyStreet::DB->resultset('User')->find_or_create( { - email => FixMyStreet::App->config->{'CONTACT_EMAIL'}, + email => FixMyStreet->config('CONTACT_EMAIL'), name => 'System User', } ); -my $body_list = FixMyStreet::App->model('DB::Body'); +my $body_list = FixMyStreet::DB->resultset('Body'); my $update = Open311::GetUpdates->new( body_list => $body_list, diff --git a/bin/problem-creation-graph b/bin/problem-creation-graph index 3e46777f4..a8554058a 100755 --- a/bin/problem-creation-graph +++ b/bin/problem-creation-graph @@ -20,7 +20,7 @@ BEGIN { use File::Temp qw(tempfile); -use FixMyStreet::App; +use FixMyStreet::DB; my %config = ( gplot_output => "set terminal png font 'Vera.ttf' 9 size 1200,400", @@ -43,7 +43,7 @@ sub grab_data { my ($fh, $filename) = tempfile("fms-creation-rate-graph-data-XXXXXXXXXX", UNLINK => 1); $sources{$type} = $filename; - my @entries = FixMyStreet::App->model('DB::Problem')->search( + my @entries = FixMyStreet::DB->resultset('Problem')->search( $selection, { columns => [ { 'date' => { date => 'created' } }, diff --git a/bin/problems-filed-graph b/bin/problems-filed-graph index a930e4346..950797ebe 100755 --- a/bin/problems-filed-graph +++ b/bin/problems-filed-graph @@ -20,7 +20,7 @@ BEGIN { use File::Temp qw(tempfile); -use FixMyStreet::App; +use FixMyStreet::DB; my %config = ( gplot_output => "set terminal png font 'Vera.ttf' 9 size 1200,600", @@ -29,7 +29,7 @@ my %config = ( my ($fh, $source) = tempfile("fms-report-rate-graph-data-nonwmc-XXXXXXXXXX", UNLINK => 1); -my @entries = FixMyStreet::App->model('DB::Problem')->search({ +my @entries = FixMyStreet::DB->resultset('Problem')->search({ state => { -not_in => [ 'unconfirmed', 'hidden', 'partial' ] }, }, { columns => [ diff --git a/bin/send-alerts b/bin/send-alerts index 94f7d5509..5c8a5412c 100755 --- a/bin/send-alerts +++ b/bin/send-alerts @@ -17,14 +17,13 @@ BEGIN { require "$d/../setenv.pl"; } -use CGI; # XXX use CronFns; -use mySociety::Config; -use FixMyStreet::App; +use FixMyStreet; +use FixMyStreet::Script::Alerts; -my $site = CronFns::site(mySociety::Config::get('BASE_URL')); +my $site = CronFns::site(FixMyStreet->config('BASE_URL')); CronFns::language($site); -FixMyStreet::App->model('DB::AlertType')->email_alerts(); +FixMyStreet::Script::Alerts::send(); diff --git a/bin/send-comments b/bin/send-comments index fbbd57891..bf5f27ce0 100755 --- a/bin/send-comments +++ b/bin/send-comments @@ -20,15 +20,12 @@ BEGIN { require "$d/../setenv.pl"; } -use Digest::MD5; -use Encode; use CronFns; -use FixMyStreet::App; - -use Utils; -use mySociety::Config; - +use DateTime; +use FixMyStreet; +use FixMyStreet::Cobrand; +use FixMyStreet::DB; use Open311; # send_method config values found in by-area config data, for selecting to appropriate method @@ -39,10 +36,10 @@ use constant COUNCIL_ID_OXFORDSHIRE => 2237; # Set up site, language etc. my ($verbose, $nomail) = CronFns::options(); -my $base_url = mySociety::Config::get('BASE_URL'); +my $base_url = FixMyStreet->config('BASE_URL'); my $site = CronFns::site($base_url); -my $bodies = FixMyStreet::App->model('DB::Body')->search( { +my $bodies = FixMyStreet::DB->resultset('Body')->search( { send_method => SEND_METHOD_OPEN311, send_comments => 1, } ); @@ -54,7 +51,7 @@ while ( my $body = $bodies->next ) { next if $body->areas->{+COUNCIL_ID_OXFORDSHIRE}; my $use_extended = 0; - my $comments = FixMyStreet::App->model('DB::Comment')->search( { + my $comments = FixMyStreet::DB->resultset('Comment')->search( { 'me.whensent' => undef, 'me.external_id' => undef, 'me.state' => 'confirmed', diff --git a/bin/send-questionnaires b/bin/send-questionnaires index 93d58ed77..85eef810c 100755 --- a/bin/send-questionnaires +++ b/bin/send-questionnaires @@ -17,17 +17,14 @@ BEGIN { require "$d/../setenv.pl"; } -use CGI; # XXX Awkward kludge -use Encode; use CronFns; - -use FixMyStreet::App; -use mySociety::Config; +use FixMyStreet; +use FixMyStreet::Script::Questionnaires; my %params; ( $params{verbose}, $params{nomail} ) = CronFns::options(); -$params{site} = CronFns::site(mySociety::Config::get('BASE_URL')); +$params{site} = CronFns::site(FixMyStreet->config('BASE_URL')); CronFns::language($params{site}); -FixMyStreet::App->model('DB::Questionnaire')->send_questionnaires( \%params ); +FixMyStreet::Script::Questionnaires::send( \%params ); diff --git a/bin/send-reports b/bin/send-reports index 3c6f26cdb..81be691e7 100755 --- a/bin/send-reports +++ b/bin/send-reports @@ -17,13 +17,12 @@ BEGIN { require "$d/../setenv.pl"; } -use CGI; # XXX use CronFns; -use mySociety::Config; -use FixMyStreet::App; +use FixMyStreet; +use FixMyStreet::Script::Reports; -my $site = CronFns::site(mySociety::Config::get('BASE_URL')); +my $site = CronFns::site(FixMyStreet->config('BASE_URL')); CronFns::language($site); -FixMyStreet::App->model('DB::Problem')->send_reports(); +FixMyStreet::Script::Reports::send(); diff --git a/bin/update-all-reports b/bin/update-all-reports index 0627cedc2..25405e8ad 100755 --- a/bin/update-all-reports +++ b/bin/update-all-reports @@ -17,7 +17,9 @@ BEGIN { require "$d/../setenv.pl"; } -use FixMyStreet::App; +use FixMyStreet; +use FixMyStreet::DB; + use File::Path (); use File::Slurp; use JSON; @@ -32,7 +34,7 @@ if ( FixMyStreet->config('BASE_URL') =~ /zurich|zueri/ ) { $age_column = 'created'; } -my $problems = FixMyStreet::App->model("DB::Problem")->search( +my $problems = FixMyStreet::DB->resultset('Problem')->search( { state => [ FixMyStreet::DB::Result::Problem->visible_states() ], }, diff --git a/bin/update-schema b/bin/update-schema index 1af08b002..c6b197deb 100755 --- a/bin/update-schema +++ b/bin/update-schema @@ -39,20 +39,19 @@ BEGIN { require "$bin_dir/../setenv.pl"; } -use mySociety::Config; +use FixMyStreet; use mySociety::DBHandle qw(dbh); use mySociety::MaPit; use Getopt::Long; use Pod::Usage; -mySociety::Config::set_file("$bin_dir/../conf/general"); my %args = ( - Name => mySociety::Config::get('FMS_DB_NAME'), - User => mySociety::Config::get('FMS_DB_USER'), - Password => mySociety::Config::get('FMS_DB_PASS'), + Name => FixMyStreet->config('FMS_DB_NAME'), + User => FixMyStreet->config('FMS_DB_USER'), + Password => FixMyStreet->config('FMS_DB_PASS'), ); -$args{Host} = mySociety::Config::get('FMS_DB_HOST', undef) if mySociety::Config::get('FMS_DB_HOST'); -$args{Port} = mySociety::Config::get('FMS_DB_PORT', undef) if mySociety::Config::get('FMS_DB_PORT'); +$args{Host} = FixMyStreet->config('FMS_DB_HOST', undef) if FixMyStreet->config('FMS_DB_HOST'); +$args{Port} = FixMyStreet->config('FMS_DB_PORT', undef) if FixMyStreet->config('FMS_DB_PORT'); mySociety::DBHandle::configure( %args ); my ($commit, $version, $downgrade, $help); diff --git a/bin/zerotb/import_clinic_list.pl b/bin/zerotb/import_clinic_list.pl index 359a63925..f7bdbc092 100755 --- a/bin/zerotb/import_clinic_list.pl +++ b/bin/zerotb/import_clinic_list.pl @@ -1,7 +1,7 @@ #!/usr/bin/perl use strict; -use FixMyStreet::App; +use FixMyStreet::DB; use Text::CSV; use Getopt::Long::Descriptive; @@ -23,7 +23,7 @@ my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<:encoding(utf8)", $opt->file or die "Failed to open " . $opt->file . ": $!"; -my $clinic_user = FixMyStreet::App->model('DB::User')->find_or_create({ +my $clinic_user = FixMyStreet::DB->resultset('User')->find_or_create({ email => $opt->email }); if ( not $clinic_user->in_storage ) { @@ -34,7 +34,7 @@ if ( not $clinic_user->in_storage ) { my $title_row = $csv->getline( $fh ); while ( my $row = $csv->getline( $fh ) ) { - my $clinics = FixMyStreet::App->model('DB::Problem')->search({ + my $clinics = FixMyStreet::DB->resultset('Problem')->search({ title => $row->[TITLE] }); @@ -42,7 +42,7 @@ while ( my $row = $csv->getline( $fh ) ) { my $p; my $count = $clinics->count; if ( $count == 0 ) { - $p = FixMyStreet::App->model('DB::Problem')->create({ + $p = FixMyStreet::DB->resultset('Problem')->create({ title => $row->[TITLE], latitude => $lat, longitude => $long, @@ -76,14 +76,14 @@ while ( my $row = $csv->getline( $fh ) ) { $p->discard_changes; # disabling existing alerts in case email addresses have changed - my $existing = FixMyStreet::App->model('DB::Alert')->search({ + my $existing = FixMyStreet::DB->resultset('Alert')->search({ alert_type => 'new_updates', parameter => $p->id }); $existing->update( { confirmed => 0 } ); if ( $row->[EMAIL] ) { - my $u = FixMyStreet::App->model('DB::User')->find_or_new({ + my $u = FixMyStreet::DB->resultset('User')->find_or_new({ email => $row->[EMAIL] }); $u->insert unless $u->in_storage; @@ -95,7 +95,7 @@ while ( my $row = $csv->getline( $fh ) ) { sub create_update_alert { my ( $user, $p, $verbose ) = @_; - my $a = FixMyStreet::App->model('DB::Alert')->find_or_new({ + my $a = FixMyStreet::DB->resultset('Alert')->find_or_new({ alert_type => 'new_updates', user => $user, parameter => $p->id, diff --git a/bin/zurich/convert_internal_notes_to_comments b/bin/zurich/convert_internal_notes_to_comments index ddf74851f..76db0a45b 100755 --- a/bin/zurich/convert_internal_notes_to_comments +++ b/bin/zurich/convert_internal_notes_to_comments @@ -17,15 +17,13 @@ the superuser, or one created just for this purpose? use strict; use warnings; -use FixMyStreet::App; +use FixMyStreet::DB; # Because it is not possible to determine the user that last edited the # internal_notes we need require a user to assign all the comments to. my $comment_user_id = $ARGV[0] || die "Usage: $0 id_of_user_for_comments"; -my $comment_user = FixMyStreet::App # - ->model('DB::User') # - ->find($comment_user_id) +my $comment_user = FixMyStreet::DB->resultset('User')->find($comment_user_id) || die "Could not find user with id '$comment_user_id'"; # We use now as the time for the internal note. This is not the time it was @@ -39,7 +37,7 @@ my $comment_state = 'hidden'; # Load all the comments, more reliable than trying to search on the contents of # the extra field. -my $problems = FixMyStreet::App->model('DB::Problem')->search(); +my $problems = FixMyStreet::DB->resultset('Problem')->search(); while ( my $problem = $problems->next() ) { diff --git a/bin/zurich/overdue-alert b/bin/zurich/overdue-alert index 700a642f4..0473208c5 100755 --- a/bin/zurich/overdue-alert +++ b/bin/zurich/overdue-alert @@ -19,7 +19,9 @@ BEGIN { use DateTime; use CronFns; -use FixMyStreet::App; +use FixMyStreet::Cobrand; +use FixMyStreet::DB; +use FixMyStreet::Email; my ($verbose, $nomail) = CronFns::options(); @@ -28,7 +30,7 @@ my $now = DateTime->now(); exit if FixMyStreet::Cobrand::Zurich::is_public_holiday($now) or FixMyStreet::Cobrand::Zurich::is_weekend($now); my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('zurich')->new(); -my %bodies = map { $_->id => $_ } FixMyStreet::App->model("DB::Body")->all; +my %bodies = map { $_->id => $_ } FixMyStreet::DB->resultset("Body")->all; loop_through( 'alert-moderation-overdue.txt', 0, 1, [ 'unconfirmed' ] ); loop_through( 'alert-overdue.txt', 1, 6, 'in progress' ); @@ -36,10 +38,10 @@ loop_through( 'alert-overdue.txt', 0, 6, ['confirmed', 'planned'] ); sub loop_through { my ( $template, $include_parent, $days, $states ) = @_; - my $dtf = FixMyStreet::App->model("DB")->storage->datetime_parser; + my $dtf = FixMyStreet::DB->storage->datetime_parser; my $date_threshold = $dtf->format_datetime(FixMyStreet::Cobrand::Zurich::sub_days( $now, $days )); - my $reports = FixMyStreet::App->model("DB::Problem")->search( { + my $reports = FixMyStreet::DB->resultset("Problem")->search( { state => $states, created => { '<', $date_threshold }, bodies_str => { '!=', undef }, @@ -76,7 +78,8 @@ sub send_alert { push @$to, [ $parent_email, $parent->name ]; } - FixMyStreet::App->send_email_cron( + FixMyStreet::Email::send_cron( + FixMyStreet::DB->storage->schema, { _template_ => $template, _parameters_ => $h, @@ -67,6 +67,7 @@ requires 'LWP::UserAgent'; requires 'Math::Trig'; requires 'Module::Pluggable'; requires 'Moose'; +requires 'MooX::Types::MooseLike'; requires 'namespace::autoclean'; requires 'Net::DNS::Resolver'; requires 'Net::Domain::TLD'; diff --git a/cpanfile.snapshot b/cpanfile.snapshot index 5bf0dc4ba..48ca6f324 100644 --- a/cpanfile.snapshot +++ b/cpanfile.snapshot @@ -3870,10 +3870,10 @@ DISTRIBUTIONS Module::Build 0.38 Test::More 0.62 if 0 - Module-Runtime-0.013 - pathname: Z/ZE/ZEFRAM/Module-Runtime-0.013.tar.gz + Module-Runtime-0.014 + pathname: Z/ZE/ZEFRAM/Module-Runtime-0.014.tar.gz provides: - Module::Runtime 0.013 + Module::Runtime 0.014 requirements: Module::Build 0 Test::More 0 @@ -3938,6 +3938,14 @@ DISTRIBUTIONS Module::Runtime 0 Moo 1.003000 Role::Tiny 0 + MooX-Types-MooseLike-0.29 + pathname: M/MA/MATEU/MooX-Types-MooseLike-0.29.tar.gz + provides: + MooX::Types::MooseLike 0.29 + MooX::Types::MooseLike::Base 0.29 + requirements: + ExtUtils::MakeMaker 0 + Module::Runtime 0.014 Moose-2.0604 pathname: D/DO/DOY/Moose-2.0604.tar.gz provides: diff --git a/db/rerun_dbic_loader.pl b/db/rerun_dbic_loader.pl index d7dfd1c10..958b28241 100755 --- a/db/rerun_dbic_loader.pl +++ b/db/rerun_dbic_loader.pl @@ -32,6 +32,6 @@ make_schema_at( components => [ 'FilterColumn', 'InflateColumn::DateTime', 'EncodedColumn' ], }, - FixMyStreet->dbic_connect_info(), + [ FixMyStreet->dbic_connect_info ], ); diff --git a/perllib/FixMyStreet.pm b/perllib/FixMyStreet.pm index 76befb96a..1a9c8ff60 100644 --- a/perllib/FixMyStreet.pm +++ b/perllib/FixMyStreet.pm @@ -95,15 +95,12 @@ sub override_config($&) { mySociety::MaPit::configure($config->{MAPIT_URL}) if $config->{MAPIT_URL}; - # For historical reasons, we have two ways of asking for config variables. - # Override them both, I'm sure we'll find time to get rid of one eventually. - # - # NB: though we have these two functions, templates tend to use [% c.config %] + # NB: though we have this, templates tend to use [% c.config %]. # This overriding happens after $c->config is set, so note that # FixMyStreet::App->setup_request rewrites $c->config if we are in # test_mode, so tests should Just Work there too. - my $override_guard1 = Sub::Override->new( + my $override_guard = Sub::Override->new( "FixMyStreet::config", sub { my ($class, $key) = @_; @@ -113,30 +110,19 @@ sub override_config($&) { return $orig_config->{$key} if exists $orig_config->{$key}; } ); - my $override_guard2 = Sub::Override->new( - "mySociety::Config::get", - sub ($;$) { - my ($key, $default) = @_; - return $config->{$key} if exists $config->{$key}; - my $orig_config = mySociety::Config::load_default(); - return $orig_config->{$key} if exists $orig_config->{$key}; - return $default if @_ == 2; - } - ); FixMyStreet::Map::reload_allowed_maps() if $config->{MAP_TYPE}; $code->(); - $override_guard1->restore(); - $override_guard2->restore(); + $override_guard->restore(); mySociety::MaPit::configure() if $config->{MAPIT_URL}; FixMyStreet::Map::reload_allowed_maps() if $config->{MAP_TYPE}; } =head2 dbic_connect_info - $connect_info = FixMyStreet->dbic_connect_info(); + $connect_info = FixMyStreet->dbic_connect_info; Returns the array that DBIx::Class::Schema needs to connect to the database. Most of the values are read from the config file and others are hordcoded here. @@ -172,7 +158,7 @@ sub dbic_connect_info { }; my $dbic_args = {}; - return [ $dsn, $user, $password, $dbi_args, $dbic_args ]; + return ( $dsn, $user, $password, $dbi_args, $dbic_args ); } =head2 configure_mysociety_dbhandle @@ -221,21 +207,24 @@ sub get_email_template { return $template; } -my $tz = DateTime::TimeZone->new( name => "local" ); +my $tz; my $tz_f; -$tz_f = DateTime::TimeZone->new( name => FixMyStreet->config('TIME_ZONE') ) - if FixMyStreet->config('TIME_ZONE'); sub local_time_zone { + $tz //= DateTime::TimeZone->new( name => "local" ); return $tz; } sub time_zone { + $tz_f //= DateTime::TimeZone->new( name => FixMyStreet->config('TIME_ZONE') ) + if FixMyStreet->config('TIME_ZONE'); return $tz_f; } sub set_time_zone { my ($class, $dt) = @_; + my $tz = local_time_zone(); + my $tz_f = time_zone(); $dt->set_time_zone($tz); $dt->set_time_zone($tz_f) if $tz_f; } diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index c9286b177..b8ce2e051 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -2,9 +2,6 @@ package FixMyStreet::App; use Moose; use namespace::autoclean; -# Should move away from Email::Send, but until then: -$Return::Value::NO_CLUCK = 1; - use Catalyst::Runtime 5.80; use FixMyStreet; use FixMyStreet::Cobrand; @@ -12,6 +9,7 @@ use Memcached; use mySociety::Email; use mySociety::Random qw(random_bytes); use FixMyStreet::Map; +use FixMyStreet::Email; use Utils; use Path::Class; @@ -319,7 +317,7 @@ sub send_email { ] }; - return if $c->is_abuser($vars->{to}); + return if FixMyStreet::Email::is_abuser($c->model('DB')->schema, $vars->{to}); # render the template my $content = $c->view('Email')->render( $c, $template, $vars ); @@ -347,7 +345,7 @@ sub send_email { ) }; if (my $attachments = $extra_stash_values->{attachments}) { - $email_text = munge_attachments($email_text, $attachments); + $email_text = FixMyStreet::Email::munge_attachments($email_text, $attachments); } # send the email @@ -356,107 +354,6 @@ sub send_email { return $email; } -sub send_email_cron { - my ( $c, $params, $env_from, $nomail, $cobrand, $lang_code ) = @_; - - my $sender = $c->config->{DO_NOT_REPLY_EMAIL}; - $env_from ||= $sender; - if (!$params->{From}) { - my $sender_name = $cobrand->contact_name; - $params->{From} = [ $sender, _($sender_name) ]; - } - - return 1 if $c->is_abuser($params->{To}); - - $params->{'Message-ID'} = sprintf('<fms-cron-%s-%s@%s>', time(), - unpack('h*', random_bytes(5, 1)), FixMyStreet->config('EMAIL_DOMAIN') - ); - - # This is all to set the path for the templates processor so we can override - # signature and site names in emails using templates in the old style emails. - # It's a bit involved as not everywhere we use it knows about the cobrand so - # we can't assume there will be one. - my $include_path = FixMyStreet->path_to( 'templates', 'email', 'default' )->stringify; - if ( $cobrand ) { - $include_path = - FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker )->stringify . ':' - . $include_path; - if ( $lang_code ) { - $include_path = - FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker, $lang_code )->stringify . ':' - . $include_path; - } - } - my $tt = Template->new({ - INCLUDE_PATH => $include_path - }); - my ($sig, $site_name); - $tt->process( 'signature.txt', $params, \$sig ); - $sig = Encode::decode('utf8', $sig); - $params->{_parameters_}->{signature} = $sig; - - $tt->process( 'site-name.txt', $params, \$site_name ); - $site_name = Utils::trim_text(Encode::decode('utf8', $site_name)); - $params->{_parameters_}->{site_name} = $site_name; - - $params->{_line_indent} = ''; - my $attachments = delete $params->{attachments}; - - my $email = mySociety::Locale::in_gb_locale { mySociety::Email::construct_email($params) }; - - $email = munge_attachments($email, $attachments) if $attachments; - - if ($nomail) { - print $email; - return 1; # Failure - } else { - my %model_args; - if (!FixMyStreet->test_mode && $env_from eq FixMyStreet->config('CONTACT_EMAIL')) { - $model_args{mailer} = 'FixMyStreet::EmailSend::ContactEmail'; - } - my $result = $c->model('EmailSend', %model_args)->send($email); - return $result ? 0 : 1; - } -} - -sub munge_attachments { - my ($message, $attachments) = @_; - # $attachments should be an array_ref of things that can be parsed to Email::MIME, - # for example - # [ - # body => $binary_data, - # attributes => { - # content_type => 'image/jpeg', - # encoding => 'base64', - # filename => '1234.1.jpeg', - # name => '1234.1.jpeg', - # }, - # ... - # ] - # - # XXX: mySociety::Email::construct_email isn't using a MIME library and - # requires more analysis to refactor, so for now, we'll simply parse the - # generated MIME and add attachments. - # - # (Yes, this means that the email is constructed by Email::Simple, munged - # manually by custom code, turned back into Email::Simple, and then munged - # with Email::MIME. What's your point?) - - require Email::MIME; - my $mime = Email::MIME->new($message); - $mime->parts_add([ map { Email::MIME->create(%$_)} @$attachments ]); - my $data = $mime->as_string; - - # unsure why Email::MIME adds \r\n. Possibly mail client should handle - # gracefully, BUT perhaps as the segment constructed by - # mySociety::Email::construct_email strips to \n, they seem not to. - # So we re-run the same regexp here to the added part. - $data =~ s/\r\n/\n/gs; - - return $data; -} - - =head2 uri_with $uri = $c->uri_with( ... ); @@ -535,58 +432,6 @@ sub render_fragment { $c->view('Web')->render($c, $template, $vars); } -=head2 get_photo_params - -Returns a hashref of details of any attached photo for use in templates. -Hashref contains height, width and url keys. - -=cut - -sub get_photo_params { - my ($self, $key) = @_; - - return {} unless $self->photo; - - $key = ($key eq 'id') ? '' : "/$key"; - - my $pre = "/photo$key/" . $self->id; - my $post = '.jpeg'; - my $photo = {}; - - if (length($self->photo) == 40) { - $post .= '?' . $self->photo; - $photo->{url_full} = "$pre.full$post"; - # XXX Can't use size here because {url} (currently 250px height) may be - # being used, but at this point it doesn't yet exist to find the width - # $str = FixMyStreet->config('UPLOAD_DIR') . $self->photo . '.jpeg'; - } else { - my $str = \$self->photo; - ( $photo->{width}, $photo->{height} ) = Image::Size::imgsize( $str ); - } - - $photo->{url} = "$pre$post"; - $photo->{url_tn} = "$pre.tn$post"; - $photo->{url_fp} = "$pre.fp$post"; - - return $photo; -} - -sub is_abuser { - my ($c, $to) = @_; - my $email; - if (ref($to) eq 'ARRAY') { - if (ref($to->[0]) eq 'ARRAY') { - $email = $to->[0][0]; - } else { - $email = $to->[0]; - } - } else { - $email = $to; - } - my ($domain) = $email =~ m{ @ (.*) \z }x; - return $c->model('DB::Abuse')->search( { email => [ $email, $domain ] } )->first; -} - =head2 get_param $param = $c->get_param('name'); diff --git a/perllib/FixMyStreet/App/Controller/JSON.pm b/perllib/FixMyStreet/App/Controller/JSON.pm index 959ead245..be738bb8b 100644 --- a/perllib/FixMyStreet/App/Controller/JSON.pm +++ b/perllib/FixMyStreet/App/Controller/JSON.pm @@ -8,7 +8,6 @@ use JSON; use DateTime; use DateTime::Format::ISO8601; use List::MoreUtils 'uniq'; -use FixMyStreet::App; =head1 NAME @@ -81,7 +80,7 @@ sub problems : Local { $date_col = 'lastupdate'; } - my $dt_parser = FixMyStreet::App->model('DB')->schema->storage->datetime_parser; + my $dt_parser = $c->model('DB')->schema->storage->datetime_parser; my $one_day = DateTime::Duration->new( days => 1 ); my $query = { diff --git a/perllib/FixMyStreet/App/Controller/Rss.pm b/perllib/FixMyStreet/App/Controller/Rss.pm index b817fe326..586d5e7ae 100755 --- a/perllib/FixMyStreet/App/Controller/Rss.pm +++ b/perllib/FixMyStreet/App/Controller/Rss.pm @@ -218,7 +218,7 @@ sub query_main : Private { . ($alert_type->head_table ? $alert_type->head_table . '_id=? and ' : '') . $alert_type->item_where . ' order by ' . $alert_type->item_order; - my $rss_limit = mySociety::Config::get('RSS_LIMIT'); + my $rss_limit = FixMyStreet->config('RSS_LIMIT'); $query .= " limit $rss_limit" unless $c->stash->{type} =~ /^all/; my $q = $c->model('DB::Alert')->result_source->storage->dbh->prepare($query); diff --git a/perllib/FixMyStreet/App/Model/DB.pm b/perllib/FixMyStreet/App/Model/DB.pm index f9e43172f..ac1f98dc9 100644 --- a/perllib/FixMyStreet/App/Model/DB.pm +++ b/perllib/FixMyStreet/App/Model/DB.pm @@ -8,7 +8,7 @@ use FixMyStreet; __PACKAGE__->config( schema_class => 'FixMyStreet::DB', - connect_info => FixMyStreet->dbic_connect_info, + connect_info => sub { FixMyStreet::DB->storage->dbh }, ); =head1 NAME diff --git a/perllib/FixMyStreet/App/Model/EmailSend.pm b/perllib/FixMyStreet/App/Model/EmailSend.pm index 475026267..93751d4a6 100644 --- a/perllib/FixMyStreet/App/Model/EmailSend.pm +++ b/perllib/FixMyStreet/App/Model/EmailSend.pm @@ -4,67 +4,16 @@ use base 'Catalyst::Model::Factory'; use strict; use warnings; -use FixMyStreet; -use Email::Send; - =head1 NAME FixMyStreet::App::Model::EmailSend =head1 DESCRIPTION -Thin wrapper around Email::Send - configuring it correctly acording to our config. - -If the config value 'SMTP_SMARTHOST' is set then email is routed via SMTP to -that. Otherwise it is sent using a 'sendmail' like binary on the local system. - -And finally if if FixMyStreet->test_mode returns true then emails are not sent -at all but are stored in memory for the test suite to inspect (using -Email::Send::Test). +Catalyst Model wrapper around FixMyStreet::EmailSend =cut -my $args = undef; - -if ( FixMyStreet->test_mode ) { - - # Email::Send::Test - $args = { mailer => 'Test', }; -} -elsif ( my $smtp_host = FixMyStreet->config('SMTP_SMARTHOST') ) { - - # Email::Send::SMTP - my $type = FixMyStreet->config('SMTP_TYPE') || ''; - my $port = FixMyStreet->config('SMTP_PORT') || ''; - my $username = FixMyStreet->config('SMTP_USERNAME') || ''; - my $password = FixMyStreet->config('SMTP_PASSWORD') || ''; - - unless ($port) { - $port = 25; - $port = 465 if $type eq 'ssl'; - $port = 587 if $type eq 'tls'; - } - - my $mailer_args = [ - Host => $smtp_host, - Port => $port, - ]; - push @$mailer_args, ssl => 1 if $type eq 'ssl'; - push @$mailer_args, tls => 1 if $type eq 'tls'; - push @$mailer_args, username => $username, password => $password - if $username && $password; - $args = { - mailer => 'FixMyStreet::EmailSend::DoNotReply', - mailer_args => $mailer_args, - }; -} -else { - - # Email::Send::Sendmail - $args = { mailer => 'Sendmail' }; -} - __PACKAGE__->config( - class => 'Email::Send', - args => $args, + class => 'FixMyStreet::EmailSend', ); diff --git a/perllib/FixMyStreet/App/Model/PhotoSet.pm b/perllib/FixMyStreet/App/Model/PhotoSet.pm index b18460821..f1334ff38 100644 --- a/perllib/FixMyStreet/App/Model/PhotoSet.pm +++ b/perllib/FixMyStreet/App/Model/PhotoSet.pm @@ -56,7 +56,7 @@ has upload_dir => ( lazy => 1, default => sub { my $self = shift; - my $cache_dir = path( $self->c->config->{UPLOAD_DIR} ); + my $cache_dir = path( FixMyStreet->config('UPLOAD_DIR') ); $cache_dir->mkpath; unless ( -d $cache_dir && -w $cache_dir ) { warn "Can't find/write to photo cache directory '$cache_dir'"; diff --git a/perllib/FixMyStreet/Cobrand.pm b/perllib/FixMyStreet/Cobrand.pm index ff7d7f943..9f61635d8 100644 --- a/perllib/FixMyStreet/Cobrand.pm +++ b/perllib/FixMyStreet/Cobrand.pm @@ -8,7 +8,7 @@ use warnings; use FixMyStreet; use Carp; -use Moose; +use Package::Stash; use Module::Pluggable sub_name => '_cobrands', @@ -77,11 +77,12 @@ sub available_cobrand_classes { sub class { my $avail = shift; return $avail->{class} if $avail->{class}; - my $moniker = $avail->{moniker}; - Class::MOP::Class->create("FixMyStreet::Cobrand::$moniker" => ( - superclasses => [ 'FixMyStreet::Cobrand::Default' ], - )); - return "FixMyStreet::Cobrand::$moniker"; + my $moniker = "FixMyStreet::Cobrand::$avail->{moniker}"; + my $class = bless {}, $moniker; + my $stash = Package::Stash->new($moniker); + my $isa = $stash->get_or_add_symbol('@ISA'); + @{$isa} = ('FixMyStreet::Cobrand::Default'); + return $moniker; } =head2 get_class_for_host diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 9541f2601..48c997047 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -682,7 +682,7 @@ sub get_body_sender { if ( $body->can_be_devolved ) { # look up via category - my $config = FixMyStreet::App->model("DB::Contact")->search( { body_id => $body->id, category => $category } )->first; + my $config = $body->result_source->schema->resultset("Contact")->search( { body_id => $body->id, category => $category } )->first; if ( $config->send_method ) { return { method => $config->send_method, config => $config }; } else { diff --git a/perllib/FixMyStreet/Cobrand/UKCouncils.pm b/perllib/FixMyStreet/Cobrand/UKCouncils.pm index 074da0915..0fb8c23d0 100644 --- a/perllib/FixMyStreet/Cobrand/UKCouncils.pm +++ b/perllib/FixMyStreet/Cobrand/UKCouncils.pm @@ -43,7 +43,7 @@ sub problems { sub base_url { my $self = shift; - my $base_url = mySociety::Config::get('BASE_URL'); + my $base_url = FixMyStreet->config('BASE_URL'); my $u = $self->council_url; if ( $base_url !~ /$u/ ) { # council cobrands are not https so transform to http as well @@ -113,7 +113,7 @@ sub owns_problem { if (ref $report eq 'HASH') { return unless $report->{bodies_str}; @bodies = split /,/, $report->{bodies_str}; - @bodies = FixMyStreet::App->model('DB::Body')->search({ id => \@bodies })->all; + @bodies = FixMyStreet::DB->resultset('Body')->search({ id => \@bodies })->all; } else { # Object @bodies = values %{$report->bodies}; } diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index 1376f1e45..b7c9e9f45 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -850,7 +850,7 @@ sub admin_report_edit { # Add new update from status_update if (my $update = $c->get_param('status_update')) { - FixMyStreet::App->model('DB::Comment')->create( { + $c->model('DB::Comment')->create( { text => $update, user => $c->user->obj, state => 'unconfirmed', @@ -1010,10 +1010,10 @@ sub _admin_send_email { } sub munge_sendreport_params { - my ($self, $c, $row, $h, $params) = @_; + my ($self, $row, $h, $params) = @_; if ($row->state =~ /^(closed|investigating)$/ && $row->get_extra_metadata('publish_photo')) { # we attach images to reports sent to external bodies - my $photoset = $row->get_photoset($c); + my $photoset = $row->get_photoset(); my @images = $photoset->all_images or return; my $index = 0; diff --git a/perllib/FixMyStreet/DB.pm b/perllib/FixMyStreet/DB.pm index a1767abe9..d920c809f 100644 --- a/perllib/FixMyStreet/DB.pm +++ b/perllib/FixMyStreet/DB.pm @@ -15,5 +15,8 @@ __PACKAGE__->load_namespaces; # Created by DBIx::Class::Schema::Loader v0.07017 @ 2012-03-08 17:19:55 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:CjFpUvon7KggFM7OF7VK/w -# You can replace this text with custom code or comments, and it will be preserved on regeneration +use FixMyStreet; + +__PACKAGE__->connection(FixMyStreet->dbic_connect_info); + 1; diff --git a/perllib/FixMyStreet/DB/Result/Alert.pm b/perllib/FixMyStreet/DB/Result/Alert.pm index 35cce8368..2a52a7bca 100644 --- a/perllib/FixMyStreet/DB/Result/Alert.pm +++ b/perllib/FixMyStreet/DB/Result/Alert.pm @@ -70,7 +70,7 @@ __PACKAGE__->belongs_to( # You can replace this text with custom code or comments, and it will be preserved on regeneration -use Moose; +use Moo; use namespace::clean -except => [ 'meta' ]; with 'FixMyStreet::Roles::Abuser'; @@ -113,7 +113,4 @@ sub disable { return 1; } -# need the inline_constuctor bit as we don't inherit from Moose -__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); - 1; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 0caaa8968..41e8cf315 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -96,8 +96,8 @@ __PACKAGE__->belongs_to( __PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); __PACKAGE__->rabx_column('extra'); -use Image::Size; -use Moose; +use Moo; +use Utils::Photo; use namespace::clean -except => [ 'meta' ]; with 'FixMyStreet::Roles::Abuser'; @@ -156,7 +156,7 @@ Returns a hashref of details of any attached photo for use in templates. sub get_photo_params { my $self = shift; - return FixMyStreet::App::get_photo_params($self, 'c'); + return Utils::Photo::get_photo_params($self, 'c'); } =head2 meta_problem_state @@ -214,7 +214,4 @@ __PACKAGE__->might_have( { cascade_copy => 0, cascade_delete => 1 }, ); -# we need the inline_constructor bit as we don't inherit from Moose -__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); - 1; diff --git a/perllib/FixMyStreet/DB/Result/Contact.pm b/perllib/FixMyStreet/DB/Result/Contact.pm index 2fbb0716d..dab5432c6 100644 --- a/perllib/FixMyStreet/DB/Result/Contact.pm +++ b/perllib/FixMyStreet/DB/Result/Contact.pm @@ -63,12 +63,9 @@ __PACKAGE__->belongs_to( __PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); __PACKAGE__->rabx_column('extra'); -use Moose; +use Moo; use namespace::clean -except => [ 'meta' ]; with 'FixMyStreet::Roles::Extra'; -# we need the inline_constructor bit as we don't inherit from Moose -__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); - 1; diff --git a/perllib/FixMyStreet/DB/Result/Nearby.pm b/perllib/FixMyStreet/DB/Result/Nearby.pm index d3d228788..adeba703a 100644 --- a/perllib/FixMyStreet/DB/Result/Nearby.pm +++ b/perllib/FixMyStreet/DB/Result/Nearby.pm @@ -6,7 +6,7 @@ use strict; use warnings; use base 'DBIx::Class::Core'; -use Moose; +use Moo; use namespace::clean -except => [ 'meta' ]; __PACKAGE__->table( 'NONE' ); @@ -27,7 +27,4 @@ __PACKAGE__->belongs_to( __PACKAGE__->result_source_instance ->name( \'problem_find_nearby(?,?,?)' ); -# we need the inline_constructor bit as we don't inherit from Moose -__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); - 1; diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index 3b7f8bcfd..f2df41f09 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -157,10 +157,10 @@ __PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); __PACKAGE__->rabx_column('extra'); __PACKAGE__->rabx_column('geocode'); -use Image::Size; -use Moose; +use Moo; use namespace::clean -except => [ 'meta' ]; use Utils; +use Utils::Photo; with 'FixMyStreet::Roles::Abuser', 'FixMyStreet::Roles::Extra'; @@ -462,7 +462,7 @@ sub bodies($) { my $self = shift; return {} unless $self->bodies_str; my $bodies = $self->bodies_str_ids; - my @bodies = FixMyStreet::App->model('DB::Body')->search({ id => $bodies })->all; + my @bodies = $self->result_source->schema->resultset('Body')->search({ id => $bodies })->all; return { map { $_->id => $_ } @bodies }; } @@ -477,6 +477,11 @@ sub url { return "/report/" . $self->id; } +sub admin_url { + my ($self, $cobrand) = @_; + return $cobrand->admin_base_url . '/report_edit/' . $self->id; +} + =head2 get_photo_params Returns a hashref of details of the attached photo, if any, for use in templates. @@ -491,7 +496,7 @@ sub get_photo_params { # use Carp 'cluck'; # cluck "get_photo_params called"; # TEMPORARY die to make sure I've done right thing with Zurich templates my $self = shift; - return FixMyStreet::App::get_photo_params($self, 'id'); + return Utils::Photo::get_photo_params($self, 'id'); } =head2 is_open @@ -628,7 +633,7 @@ sub body { $body = join( _(' and '), map { my $name = $_->name; - if ($c and mySociety::Config::get('AREA_LINKS_FROM_PROBLEMS')) { + if ($c and FixMyStreet->config('AREA_LINKS_FROM_PROBLEMS')) { '<a href="' . $_->url($c) . '">' . $name . '</a>'; } else { $name; @@ -648,7 +653,7 @@ order of title. sub response_templates { my $problem = shift; - return FixMyStreet::App->model('DB::ResponseTemplate')->search( + return $problem->result_source->schema->resultset('ResponseTemplate')->search( { body_id => $problem->bodies_str_ids }, @@ -754,20 +759,17 @@ sub update_from_open311_service_request { $status_notes = $request->{status_notes}; } - my $update = FixMyStreet::App->model('DB::Comment')->new( - { - problem_id => $self->id, - state => 'confirmed', - created => $updated || \'current_timestamp', - confirmed => \'current_timestamp', - text => $status_notes, - mark_open => 0, - mark_fixed => 0, - user => $system_user, - anonymous => 0, - name => $body->name, - } - ); + my $update = $self->new_related(comments => { + state => 'confirmed', + created => $updated || \'current_timestamp', + confirmed => \'current_timestamp', + text => $status_notes, + mark_open => 0, + mark_fixed => 0, + user => $system_user, + anonymous => 0, + name => $body->name, + }); my $w3c = DateTime::Format::W3CDTF->new; my $req_time = $w3c->parse_datetime( $request->{updated_datetime} ); @@ -894,7 +896,4 @@ sub get_time_spent { return $admin_logs ? $admin_logs->get_column('sum_time_spent') : 0; } -# we need the inline_constructor bit as we don't inherit from Moose -__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); - 1; diff --git a/perllib/FixMyStreet/DB/Result/Questionnaire.pm b/perllib/FixMyStreet/DB/Result/Questionnaire.pm index 6f2941546..30f2ab7ce 100644 --- a/perllib/FixMyStreet/DB/Result/Questionnaire.pm +++ b/perllib/FixMyStreet/DB/Result/Questionnaire.pm @@ -43,7 +43,7 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-09-10 17:11:54 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:oL1Hk4/bNG14CY74GA75SA -use Moose; +use Moo; use namespace::clean -except => [ 'meta' ]; my $stz = sub { diff --git a/perllib/FixMyStreet/DB/ResultSet/Alert.pm b/perllib/FixMyStreet/DB/ResultSet/Alert.pm index bb1c61141..1866e9b06 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Alert.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Alert.pm @@ -8,7 +8,7 @@ sub timeline_created { my ( $rs, $restriction ) = @_; my $prefetch = - FixMyStreet::App->model('DB')->schema->storage->sql_maker->quote_char ? + $rs->result_source->storage->sql_maker->quote_char ? [ qw/alert_type user/ ] : [ qw/alert_type/ ]; diff --git a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm index 25c727e25..a801600ea 100644 --- a/perllib/FixMyStreet/DB/ResultSet/AlertType.pm +++ b/perllib/FixMyStreet/DB/ResultSet/AlertType.pm @@ -4,272 +4,10 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; -use mySociety::DBHandle qw(dbh); -use mySociety::Gaze; -use mySociety::Locale; -use mySociety::MaPit; -use IO::String; -use RABX; - -# Child must have confirmed, id, email, state(!) columns -# If parent/child, child table must also have name and text -# and foreign key to parent must be PARENT_id sub email_alerts ($) { my ( $rs ) = @_; - - my $q = $rs->search( { ref => { -not_like => '%local_problems%' } } ); - while (my $alert_type = $q->next) { - my $ref = $alert_type->ref; - my $head_table = $alert_type->head_table; - my $item_table = $alert_type->item_table; - my $query = 'select alert.id as alert_id, alert.user_id as alert_user_id, alert.lang as alert_lang, alert.cobrand as alert_cobrand, - alert.cobrand_data as alert_cobrand_data, alert.parameter as alert_parameter, alert.parameter2 as alert_parameter2, '; - if ($head_table) { - $query .= " - $item_table.id as item_id, $item_table.text as item_text, - $item_table.name as item_name, $item_table.anonymous as item_anonymous, - $item_table.confirmed as item_confirmed, - $head_table.* - from alert, $item_table, $head_table - where alert.parameter::integer = $head_table.id - and $item_table.${head_table}_id = $head_table.id - "; - } else { - $query .= " $item_table.*, - $item_table.id as item_id - from alert, $item_table - where 1 = 1"; - } - $query .= " - and alert_type='$ref' and whendisabled is null and $item_table.confirmed >= whensubscribed - and $item_table.confirmed >= current_timestamp - '7 days'::interval - and (select whenqueued from alert_sent where alert_sent.alert_id = alert.id and alert_sent.parameter::integer = $item_table.id) is null - and $item_table.user_id <> alert.user_id - and " . $alert_type->item_where . " - and alert.confirmed = 1 - order by alert.id, $item_table.confirmed"; - # XXX Ugh - needs work - $query =~ s/\?/alert.parameter/ if ($query =~ /\?/); - $query =~ s/\?/alert.parameter2/ if ($query =~ /\?/); - - $query = dbh()->prepare($query); - $query->execute(); - my $last_alert_id; - my %data = ( template => $alert_type->template, data => '' ); - while (my $row = $query->fetchrow_hashref) { - - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->{alert_cobrand})->new(); - $cobrand->set_lang_and_domain( $row->{alert_lang}, 1, FixMyStreet->path_to('locale')->stringify ); - - # Cobranded and non-cobranded messages can share a database. In this case, the conf file - # should specify a vhost to send the reports for each cobrand, so that they don't get sent - # more than once if there are multiple vhosts running off the same database. The email_host - # call checks if this is the host that sends mail for this cobrand. - next unless $cobrand->email_host; - - # this is for the new_updates alerts - next if $row->{non_public} and $row->{user_id} != $row->{alert_user_id}; - - FixMyStreet::App->model('DB::AlertSent')->create( { - alert_id => $row->{alert_id}, - parameter => $row->{item_id}, - } ); - if ($last_alert_id && $last_alert_id != $row->{alert_id}) { - _send_aggregated_alert_email(%data); - %data = ( template => $alert_type->template, data => '' ); - } - - # create problem status message for the templates - if ( FixMyStreet::DB::Result::Problem::fixed_states()->{$row->{state}} ) { - $data{state_message} = _("This report is currently marked as fixed."); - } elsif ( FixMyStreet::DB::Result::Problem::closed_states()->{$row->{state}} ) { - $data{state_message} = _("This report is currently marked as closed.") - } else { - $data{state_message} = _("This report is currently marked as open."); - } - - my $url = $cobrand->base_url_for_report($row); - # this is currently only for new_updates - if ($row->{item_text}) { - if ( $cobrand->moniker ne 'zurich' && $row->{alert_user_id} == $row->{user_id} ) { - # This is an alert to the same user who made the report - make this a login link - # Don't bother with Zurich which has no accounts - my $user = FixMyStreet::App->model('DB::User')->find( { - id => $row->{alert_user_id} - } ); - $data{alert_email} = $user->email; - my $token_obj = FixMyStreet::App->model('DB::Token')->create( { - scope => 'alert_to_reporter', - data => { - id => $row->{id}, - } - } ); - $data{problem_url} = $url . "/R/" . $token_obj->token; - } else { - $data{problem_url} = $url . "/report/" . $row->{id}; - } - $data{data} .= $row->{item_name} . ' : ' if $row->{item_name} && !$row->{item_anonymous}; - if ( $cobrand->include_time_in_update_alerts ) { - my $parser = DateTime::Format::Pg->new(); - my $dt = $parser->parse_timestamp( $row->{item_confirmed} ); - # We need to always set this otherwise we end up with the DateTime - # object being in the floating timezone in which case applying a - # subsequent timezone set will have no effect. - # this is basically recreating the code from the inflate wrapper - # in the database model. - FixMyStreet->set_time_zone($dt); - $data{data} .= $cobrand->prettify_dt( $dt, 'alert' ) . "\n\n"; - } - $data{data} .= $row->{item_text} . "\n\n------\n\n"; - #Â this is ward and council problems - } else { - $data{data} .= $url . "/report/" . $row->{id} . " - $row->{title}\n\n"; - if ( exists $row->{geocode} && $row->{geocode} && $ref =~ /ward|council/ ) { - my $nearest_st = _get_address_from_gecode( $row->{geocode} ); - $data{data} .= $nearest_st if $nearest_st; - } - $data{data} .= "\n\n------\n\n"; - } - if (!$data{alert_user_id}) { - %data = (%data, %$row); - if ($ref eq 'area_problems' || $ref eq 'council_problems' || $ref eq 'ward_problems') { - my $va_info = mySociety::MaPit::call('area', $row->{alert_parameter}); - $data{area_name} = $va_info->{name}; - } - if ($ref eq 'ward_problems') { - my $va_info = mySociety::MaPit::call('area', $row->{alert_parameter2}); - $data{ward_name} = $va_info->{name}; - } - } - $data{cobrand} = $row->{alert_cobrand}; - $data{cobrand_data} = $row->{alert_cobrand_data}; - $data{lang} = $row->{alert_lang}; - $last_alert_id = $row->{alert_id}; - } - if ($last_alert_id) { - _send_aggregated_alert_email(%data); - } - } - - # Nearby done separately as the table contains the parameters - my $template = $rs->find( { ref => 'local_problems' } )->template; - my $query = FixMyStreet::App->model('DB::Alert')->search( { - alert_type => 'local_problems', - whendisabled => undef, - confirmed => 1 - }, { - order_by => 'id' - } ); - while (my $alert = $query->next) { - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($alert->cobrand)->new(); - next unless $cobrand->email_host; - next if $alert->is_from_abuser; - - my $longitude = $alert->parameter; - my $latitude = $alert->parameter2; - my $d = mySociety::Gaze::get_radius_containing_population($latitude, $longitude, 200000); - # Convert integer to GB locale string (with a ".") - $d = mySociety::Locale::in_gb_locale { - sprintf("%f", int($d*10+0.5)/10); - }; - my $states = "'" . join( "', '", FixMyStreet::DB::Result::Problem::visible_states() ) . "'"; - my %data = ( template => $template, data => '', alert_id => $alert->id, alert_email => $alert->user->email, lang => $alert->lang, cobrand => $alert->cobrand, cobrand_data => $alert->cobrand_data ); - my $q = "select problem.id, problem.bodies_str, problem.postcode, problem.geocode, problem.title from problem_find_nearby(?, ?, ?) as nearby, problem, users - where nearby.problem_id = problem.id - and problem.user_id = users.id - and problem.state in ($states) - and problem.non_public = 'f' - and problem.confirmed >= ? and problem.confirmed >= current_timestamp - '7 days'::interval - and (select whenqueued from alert_sent where alert_sent.alert_id = ? and alert_sent.parameter::integer = problem.id) is null - and users.email <> ? - order by confirmed desc"; - $q = dbh()->prepare($q); - $q->execute($latitude, $longitude, $d, $alert->whensubscribed, $alert->id, $alert->user->email); - while (my $row = $q->fetchrow_hashref) { - FixMyStreet::App->model('DB::AlertSent')->create( { - alert_id => $alert->id, - parameter => $row->{id}, - } ); - my $url = $cobrand->base_url_for_report($row); - $data{data} .= $url . "/report/" . $row->{id} . " - $row->{title}\n\n"; - if ( exists $row->{geocode} && $row->{geocode} ) { - my $nearest_st = _get_address_from_gecode( $row->{geocode} ); - $data{data} .= $nearest_st if $nearest_st; - } - $data{data} .= "\n\n------\n\n"; - } - _send_aggregated_alert_email(%data) if $data{data}; - } -} - -sub _send_aggregated_alert_email(%) { - my %data = @_; - - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($data{cobrand})->new(); - - $cobrand->set_lang_and_domain( $data{lang}, 1, FixMyStreet->path_to('locale')->stringify ); - - if (!$data{alert_email}) { - my $user = FixMyStreet::App->model('DB::User')->find( { - id => $data{alert_user_id} - } ); - $data{alert_email} = $user->email; - } - - my ($domain) = $data{alert_email} =~ m{ @ (.*) \z }x; - return if FixMyStreet::App->model('DB::Abuse')->search( { - email => [ $data{alert_email}, $domain ] - } )->first; - - my $token = FixMyStreet::App->model("DB::Token")->new_result( { - scope => 'alert', - data => { - id => $data{alert_id}, - type => 'unsubscribe', - email => $data{alert_email}, - } - } ); - $data{unsubscribe_url} = $cobrand->base_url( $data{cobrand_data} ) . '/A/' . $token->token; - - my $template = FixMyStreet->get_email_template($cobrand->moniker, $data{lang}, "$data{template}.txt"); - - my $result = FixMyStreet::App->send_email_cron( - { - _template_ => $template, - _parameters_ => \%data, - To => $data{alert_email}, - }, - undef, - 0, - $cobrand, - $data{lang} - ); - - unless ($result) { - $token->insert(); - } else { - print "Failed to send alert $data{alert_id}!"; - } -} - -sub _get_address_from_gecode { - my $geocode = shift; - - return '' unless defined $geocode; - utf8::encode($geocode) if utf8::is_utf8($geocode); - my $h = new IO::String($geocode); - my $data = RABX::wire_rd($h); - - my $str = ''; - - my $address = $data->{resourceSets}[0]{resources}[0]{address}; - my @address; - push @address, $address->{addressLine} if $address->{addressLine} && $address->{addressLine} ne 'Street'; - push @address, $address->{locality} if $address->{locality}; - $str .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s\n\n"), - join( ', ', @address ) ) if @address; - - return $str; + require FixMyStreet::Script::Alerts; + FixMyStreet::Script::Alerts::send(@_); } 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Comment.pm b/perllib/FixMyStreet/DB/ResultSet/Comment.pm index 1b6afb819..3059baab1 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Comment.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Comment.pm @@ -14,7 +14,7 @@ sub timeline { my ( $rs, $body_restriction ) = @_; my $prefetch = - FixMyStreet::App->model('DB')->schema->storage->sql_maker->quote_char ? + $rs->result_source->storage->sql_maker->quote_char ? [ qw/user/ ] : []; diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index e9f5d0f8e..f82f0135c 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -4,14 +4,9 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; -use CronFns; - -use Utils; -use mySociety::Config; -use mySociety::MaPit; - -use FixMyStreet::App; -use FixMyStreet::SendReport; +use Memcached; +use mySociety::Locale; +use FixMyStreet::DB; my $site_key; @@ -173,7 +168,7 @@ sub timeline { my ( $rs ) = @_; my $prefetch = - FixMyStreet::App->model('DB')->schema->storage->sql_maker->quote_char ? + $rs->result_source->storage->sql_maker->quote_char ? [ qw/user/ ] : []; @@ -235,302 +230,10 @@ sub categories_summary { return \%categories; } -sub get_admin_url { - my ($rs, $cobrand, $row) = @_; - return $cobrand->admin_base_url . '/report_edit/' . $row->id; -} - sub send_reports { my ( $rs, $site_override ) = @_; - - # Set up site, language etc. - my ($verbose, $nomail, $debug_mode) = CronFns::options(); - - my $base_url = mySociety::Config::get('BASE_URL'); - my $site = $site_override || CronFns::site($base_url); - - my $states = [ 'confirmed', 'fixed' ]; - $states = [ 'unconfirmed', 'confirmed', 'in progress', 'planned', 'closed', 'investigating' ] if $site eq 'zurich'; - my $unsent = $rs->search( { - state => $states, - whensent => undef, - bodies_str => { '!=', undef }, - } ); - my (%notgot, %note); - - my $send_report = FixMyStreet::SendReport->new(); - my $senders = $send_report->get_senders; - - my $debug_unsent_count = 0; - debug_print("starting to loop through unsent problem reports...") if $debug_mode; - while (my $row = $unsent->next) { - - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); - - if ($debug_mode) { - $debug_unsent_count++; - print "\n"; - debug_print("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : ""), $row->id); - } - - # Cobranded and non-cobranded messages can share a database. In this case, the conf file - # should specify a vhost to send the reports for each cobrand, so that they don't get sent - # more than once if there are multiple vhosts running off the same database. The email_host - # call checks if this is the host that sends mail for this cobrand. - if (! $cobrand->email_host()) { - debug_print("skipping because this host does not send reports for cobrand " . $cobrand->moniker, $row->id) if $debug_mode; - next; - } - - $cobrand->set_lang_and_domain($row->lang, 1); - if ( $row->is_from_abuser) { - $row->update( { state => 'hidden' } ); - debug_print("hiding because its sender is flagged as an abuser", $row->id) if $debug_mode; - next; - } elsif ( $row->title =~ /app store test/i ) { - $row->update( { state => 'hidden' } ); - debug_print("hiding because it is an app store test message", $row->id) if $debug_mode; - next; - } - - # Template variables for the email - my $email_base_url = $cobrand->base_url_for_report($row); - my %h = map { $_ => $row->$_ } qw/id title detail name category latitude longitude used_map/; - map { $h{$_} = $row->user->$_ || '' } qw/email phone/; - $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) ) - if $row->confirmed; - - $h{query} = $row->postcode; - $h{url} = $email_base_url . $row->url; - $h{admin_url} = $rs->get_admin_url($cobrand, $row); - $h{phone_line} = $h{phone} ? _('Phone:') . " $h{phone}\n\n" : ''; - if ($row->photo) { - $h{has_photo} = _("This web page also contains a photo of the problem, provided by the user.") . "\n\n"; - $h{image_url} = $email_base_url . '/photo/' . $row->id . '.full.jpeg'; - } else { - $h{has_photo} = ''; - $h{image_url} = ''; - } - $h{fuzzy} = $row->used_map ? _('To view a map of the precise location of this issue') - : _('The user could not locate the problem on a map, but to see the area around the location they entered'); - $h{closest_address} = ''; - - if ( $row->used_map ) { - $h{closest_address} = $cobrand->find_closest( $h{latitude}, $h{longitude}, $row ); - } - - if ( $cobrand->allow_anonymous_reports && - $row->user->email eq $cobrand->anonymous_account->{'email'} - ) { - $h{anonymous_report} = 1; - $h{user_details} = _('This report was submitted anonymously'); - } else { - $h{user_details} = sprintf(_('Name: %s'), $row->name) . "\n\n"; - $h{user_details} .= sprintf(_('Email: %s'), $row->user->email) . "\n\n"; - } - - $h{easting_northing} = ''; - - if ($cobrand->can('process_additional_metadata_for_email')) { - $cobrand->process_additional_metadata_for_email($row, \%h); - } - - my $bodies = FixMyStreet::App->model("DB::Body")->search( - { id => $row->bodies_str_ids }, - { order_by => 'name' }, - ); - - my $missing; - if ($row->bodies_missing) { - my @missing = FixMyStreet::App->model("DB::Body")->search( - { id => [ split /,/, $row->bodies_missing ] }, - { order_by => 'name' } - )->get_column('name')->all; - $missing = join(' / ', @missing) if @missing; - } - - my @dear; - my %reporters = (); - while (my $body = $bodies->next) { - my $sender_info = $cobrand->get_body_sender( $body, $row->category ); - my $sender = "FixMyStreet::SendReport::" . $sender_info->{method}; - - if ( ! exists $senders->{ $sender } ) { - warn sprintf "No such sender [ $sender ] for body %s ( %d )", $body->name, $body->id; - next; - } - $reporters{ $sender } ||= $sender->new(); - - if ( $reporters{ $sender }->should_skip( $row ) ) { - debug_print("skipped by sender " . $sender_info->{method} . " (might be due to previous failed attempts?)", $row->id) if $debug_mode; - } else { - debug_print("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $body->send_method, $row->id) if $debug_mode; - push @dear, $body->name; - $reporters{ $sender }->add_body( $body, $sender_info->{config} ); - } - - # If we are in the UK include eastings and northings, and nearest stuff - if ( $cobrand->country eq 'GB' && !$h{easting} ) { - my $coordsyst = 'G'; - my $first_area = $body->body_areas->first->area_id; - my $area_info = mySociety::MaPit::call('area', $first_area); - $coordsyst = 'I' if $area_info->{type} eq 'LGD'; - - ( $h{easting}, $h{northing} ) = Utils::convert_latlon_to_en( $h{latitude}, $h{longitude}, $coordsyst ); - - # email templates don't have conditionals so we need to format this here - $h{easting_northing} = "Easting/Northing"; - $h{easting_northing} .= " (IE)" if $coordsyst eq 'I'; - $h{easting_northing} .= ": $h{easting}/$h{northing}\n\n"; - } - } - - unless ( keys %reporters ) { - die 'Report not going anywhere for ID ' . $row->id . '!'; - } - - unless (@dear) { - debug_print("can't send because sender count is zero", $row->id) if $debug_mode; - next; - } - - if ($h{category} eq _('Other')) { - $h{category_footer} = _('this type of local problem'); - $h{category_line} = ''; - } else { - $h{category_footer} = "'" . $h{category} . "'"; - $h{category_line} = sprintf(_("Category: %s"), $h{category}) . "\n\n"; - } - - if ( $row->subcategory ) { - $h{subcategory_line} = sprintf(_("Subcategory: %s"), $row->subcategory) . "\n\n"; - } else { - $h{subcategory_line} = "\n\n"; - } - - $h{bodies_name} = join(_(' and '), @dear); - if ($h{category} eq _('Other')) { - $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system.") . " ]\n\n" - : ''; - } else { - $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to several councils covering the location of the problem, as the category selected is provided for all of them; please ignore it if you're not the correct council to deal with the issue.") . " ]\n\n" - : ''; - } - $h{missing} = ''; - if ($missing) { - $h{missing} = '[ ' - . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $missing) - . " ]\n\n"; - } - - if (mySociety::Config::get('STAGING_SITE') && !mySociety::Config::get('SEND_REPORTS_ON_STAGING')) { - # on a staging server send emails to ourselves rather than the bodies - %reporters = map { $_ => $reporters{$_} } grep { /FixMyStreet::SendReport::(Email|EmptyHomes)/ } keys %reporters; - unless (%reporters) { - %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() ); - } - } - - # Multiply results together, so one success counts as a success. - my $result = -1; - - for my $sender ( keys %reporters ) { - debug_print("sending using " . $sender, $row->id) if $debug_mode; - $result *= $reporters{ $sender }->send( $row, \%h ); - if ( $reporters{ $sender }->unconfirmed_counts) { - foreach my $e (keys %{ $reporters{ $sender }->unconfirmed_counts } ) { - foreach my $c (keys %{ $reporters{ $sender }->unconfirmed_counts->{$e} }) { - $notgot{$e}{$c} += $reporters{ $sender }->unconfirmed_counts->{$e}{$c}; - } - } - %note = ( - %note, - %{ $reporters{ $sender }->unconfirmed_notes } - ); - } - } - - unless ($result) { - $row->update( { - whensent => \'current_timestamp', - lastupdate => \'current_timestamp', - } ); - if ( $cobrand->report_sent_confirmation_email && !$h{anonymous_report}) { - _send_report_sent_email( $row, \%h, $nomail, $cobrand ); - } - debug_print("send successful: OK", $row->id) if $debug_mode; - } else { - my @errors; - for my $sender ( keys %reporters ) { - unless ( $reporters{ $sender }->success ) { - push @errors, $reporters{ $sender }->error; - } - } - $row->update_send_failed( join( '|', @errors ) ); - debug_print("send FAILED: " . join( '|', @errors ), $row->id) if $debug_mode; - } - } - if ($debug_mode) { - print "\n"; - if ($debug_unsent_count) { - debug_print("processed all unsent reports (total: $debug_unsent_count)"); - } else { - debug_print("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); - } - } - - if ($verbose || $debug_mode) { - print "Council email addresses that need checking:\n" if keys %notgot; - foreach my $e (keys %notgot) { - foreach my $c (keys %{$notgot{$e}}) { - print " " . $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n"; - } - } - my $sending_errors = ''; - my $unsent = $rs->search( { - state => [ 'confirmed', 'fixed' ], - whensent => undef, - bodies_str => { '!=', undef }, - send_fail_count => { '>', 0 } - } ); - while (my $row = $unsent->next) { - my $base_url = mySociety::Config::get('BASE_URL'); - $sending_errors .= "* " . $base_url . "/report/" . $row->id . ", failed " - . $row->send_fail_count . " times, last at " . $row->send_fail_timestamp - . ", reason " . $row->send_fail_reason . "\n"; - } - if ($sending_errors) { - print "The following reports had problems sending:\n$sending_errors"; - } - } -} - -sub _send_report_sent_email { - my $row = shift; - my $h = shift; - my $nomail = shift; - my $cobrand = shift; - - my $template = FixMyStreet->get_email_template($row->cobrand, $row->lang, 'confirm_report_sent.txt'); - - FixMyStreet::App->send_email_cron( - { - _template_ => $template, - _parameters_ => $h, - To => $row->user->email, - From => [ mySociety::Config::get('CONTACT_EMAIL'), $cobrand->contact_name ], - }, - mySociety::Config::get('CONTACT_EMAIL'), - $nomail, - $cobrand - ); -} - -sub debug_print { - my $msg = shift; - my $id = shift || ''; - $id = "report $id: " if $id; - print "[] $id$msg\n"; + require FixMyStreet::Script::Reports; + FixMyStreet::Script::Reports::send($site_override); } 1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm b/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm index bf1c68c49..6a1ce0d78 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Questionnaire.pm @@ -3,111 +3,11 @@ use base 'DBIx::Class::ResultSet'; use strict; use warnings; -use Encode; -use Utils; sub send_questionnaires { my ( $rs, $params ) = @_; - $rs->send_questionnaires_period( '4 weeks', $params ); - $rs->send_questionnaires_period( '26 weeks', $params ) - if $params->{site} eq 'emptyhomes'; -} - -sub send_questionnaires_period { - my ( $rs, $period, $params ) = @_; - - # Select all problems that need a questionnaire email sending - my $q_params = { - state => [ FixMyStreet::DB::Result::Problem::visible_states() ], - whensent => [ - '-and', - { '!=', undef }, - { '<', \"current_timestamp - '$period'::interval" }, - ], - send_questionnaire => 1, - }; - # FIXME Do these a bit better... - if ($params->{site} eq 'emptyhomes' && $period eq '4 weeks') { - $q_params->{'(select max(whensent) from questionnaire where me.id=problem_id)'} = undef; - } elsif ($params->{site} eq 'emptyhomes' && $period eq '26 weeks') { - $q_params->{'(select max(whensent) from questionnaire where me.id=problem_id)'} = { '!=', undef }; - } else { - $q_params->{'-or'} = [ - '(select max(whensent) from questionnaire where me.id=problem_id)' => undef, - '(select max(whenanswered) from questionnaire where me.id=problem_id)' => { '<', \"current_timestamp - '$period'::interval" } - ]; - } - - my $unsent = FixMyStreet::App->model('DB::Problem')->search( $q_params, { - order_by => { -desc => 'confirmed' } - } ); - - while (my $row = $unsent->next) { - - my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); - $cobrand->set_lang_and_domain($row->lang, 1); - - # Not all cobrands send questionnaires - next unless $cobrand->send_questionnaires; - next if $row->is_from_abuser; - - # Cobranded and non-cobranded messages can share a database. In this case, the conf file - # should specify a vhost to send the reports for each cobrand, so that they don't get sent - # more than once if there are multiple vhosts running off the same database. The email_host - # call checks if this is the host that sends mail for this cobrand. - next unless $cobrand->email_host; - - my $template; - if ($params->{site} eq 'emptyhomes') { - ($template = $period) =~ s/ //; - $template = Utils::read_file( FixMyStreet->path_to( "templates/email/emptyhomes/" . $row->lang . "/questionnaire-$template.txt" )->stringify ); - } else { - $template = FixMyStreet->get_email_template($cobrand->moniker, $row->lang, 'questionnaire.txt'); - } - - my %h = map { $_ => $row->$_ } qw/name title detail category/; - $h{created} = Utils::prettify_duration( time() - $row->confirmed->epoch, 'week' ); - - my $questionnaire = FixMyStreet::App->model('DB::Questionnaire')->create( { - problem_id => $row->id, - whensent => \'current_timestamp', - } ); - - # We won't send another questionnaire unless they ask for it (or it was - # the first EHA questionnaire. - $row->send_questionnaire( 0 ) - if $params->{site} ne 'emptyhomes' || $period eq '26 weeks'; - - my $token = FixMyStreet::App->model("DB::Token")->new_result( { - scope => 'questionnaire', - data => $questionnaire->id, - } ); - $h{url} = $cobrand->base_url($row->cobrand_data) . '/Q/' . $token->token; - - print "Sending questionnaire " . $questionnaire->id . ", problem " - . $row->id . ", token " . $token->token . " to " - . $row->user->email . "\n" - if $params->{verbose}; - - my $result = FixMyStreet::App->send_email_cron( - { - _template_ => $template, - _parameters_ => \%h, - To => [ [ $row->user->email, $row->name ] ], - }, - undef, - $params->{nomail}, - $cobrand - ); - unless ($result) { - print " ...success\n" if $params->{verbose}; - $row->update(); - $token->insert(); - } else { - print " ...failed\n" if $params->{verbose}; - $questionnaire->delete; - } - } + require FixMyStreet::Script::Questionnaires; + FixMyStreet::Script::Questionnaires::send($params); } sub timeline { diff --git a/perllib/FixMyStreet/Email.pm b/perllib/FixMyStreet/Email.pm index 4a2784787..a65a5e340 100644 --- a/perllib/FixMyStreet/Email.pm +++ b/perllib/FixMyStreet/Email.pm @@ -1,7 +1,13 @@ package FixMyStreet::Email; +use Encode; +use Template; +use mySociety::Email; +use mySociety::Locale; +use mySociety::Random qw(random_bytes); use Utils::Email; use FixMyStreet; +use FixMyStreet::EmailSend; sub test_dmarc { my $email = shift; @@ -9,4 +15,122 @@ sub test_dmarc { return Utils::Email::test_dmarc($email); } +sub is_abuser { + my ($schema, $to) = @_; + + my $email; + if (ref($to) eq 'ARRAY') { + if (ref($to->[0]) eq 'ARRAY') { + $email = $to->[0][0]; + } else { + $email = $to->[0]; + } + } else { + $email = $to; + } + + my ($domain) = $email =~ m{ @ (.*) \z }x; + return $schema->resultset('Abuse')->search( { email => [ $email, $domain ] } )->first; +} + +sub send_cron { + my ( $schema, $params, $env_from, $nomail, $cobrand, $lang_code ) = @_; + + my $sender = FixMyStreet->config('DO_NOT_REPLY_EMAIL'); + $env_from ||= $sender; + if (!$params->{From}) { + my $sender_name = $cobrand->contact_name; + $params->{From} = [ $sender, _($sender_name) ]; + } + + return 1 if is_abuser($schema, $params->{To}); + + $params->{'Message-ID'} = sprintf('<fms-cron-%s-%s@%s>', time(), + unpack('h*', random_bytes(5, 1)), FixMyStreet->config('EMAIL_DOMAIN') + ); + + # This is all to set the path for the templates processor so we can override + # signature and site names in emails using templates in the old style emails. + # It's a bit involved as not everywhere we use it knows about the cobrand so + # we can't assume there will be one. + my $include_path = FixMyStreet->path_to( 'templates', 'email', 'default' )->stringify; + if ( $cobrand ) { + $include_path = + FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker )->stringify . ':' + . $include_path; + if ( $lang_code ) { + $include_path = + FixMyStreet->path_to( 'templates', 'email', $cobrand->moniker, $lang_code )->stringify . ':' + . $include_path; + } + } + my $tt = Template->new({ + INCLUDE_PATH => $include_path + }); + my ($sig, $site_name); + $tt->process( 'signature.txt', $params, \$sig ); + $sig = Encode::decode('utf8', $sig); + $params->{_parameters_}->{signature} = $sig; + + $tt->process( 'site-name.txt', $params, \$site_name ); + $site_name = Utils::trim_text(Encode::decode('utf8', $site_name)); + $params->{_parameters_}->{site_name} = $site_name; + + $params->{_line_indent} = ''; + my $attachments = delete $params->{attachments}; + + my $email = mySociety::Locale::in_gb_locale { mySociety::Email::construct_email($params) }; + + $email = munge_attachments($email, $attachments) if $attachments; + + if ($nomail) { + print $email; + return 1; # Failure + } else { + my %model_args; + if (!FixMyStreet->test_mode && $env_from eq FixMyStreet->config('CONTACT_EMAIL')) { + $model_args{mailer} = 'FixMyStreet::EmailSend::ContactEmail'; + } + my $result = FixMyStreet::EmailSend->new(%model_args)->send($email); + return $result ? 0 : 1; + } +} + +sub munge_attachments { + my ($message, $attachments) = @_; + # $attachments should be an array_ref of things that can be parsed to Email::MIME, + # for example + # [ + # body => $binary_data, + # attributes => { + # content_type => 'image/jpeg', + # encoding => 'base64', + # filename => '1234.1.jpeg', + # name => '1234.1.jpeg', + # }, + # ... + # ] + # + # XXX: mySociety::Email::construct_email isn't using a MIME library and + # requires more analysis to refactor, so for now, we'll simply parse the + # generated MIME and add attachments. + # + # (Yes, this means that the email is constructed by Email::Simple, munged + # manually by custom code, turned back into Email::Simple, and then munged + # with Email::MIME. What's your point?) + + require Email::MIME; + my $mime = Email::MIME->new($message); + $mime->parts_add([ map { Email::MIME->create(%$_)} @$attachments ]); + my $data = $mime->as_string; + + # unsure why Email::MIME adds \r\n. Possibly mail client should handle + # gracefully, BUT perhaps as the segment constructed by + # mySociety::Email::construct_email strips to \n, they seem not to. + # So we re-run the same regexp here to the added part. + $data =~ s/\r\n/\n/gs; + + return $data; +} + 1; diff --git a/perllib/FixMyStreet/EmailSend.pm b/perllib/FixMyStreet/EmailSend.pm new file mode 100644 index 000000000..29c93b2d6 --- /dev/null +++ b/perllib/FixMyStreet/EmailSend.pm @@ -0,0 +1,70 @@ +package FixMyStreet::EmailSend; + +use strict; +use warnings; + +BEGIN { + # Should move away from Email::Send, but until then: + $Return::Value::NO_CLUCK = 1; +} + +use FixMyStreet; +use Email::Send; + +=head1 NAME + +FixMyStreet::EmailSend + +=head1 DESCRIPTION + +Thin wrapper around Email::Send - configuring it correctly according to our config. + +If the config value 'SMTP_SMARTHOST' is set then email is routed via SMTP to +that. Otherwise it is sent using a 'sendmail' like binary on the local system. + +And finally if if FixMyStreet->test_mode returns true then emails are not sent +at all but are stored in memory for the test suite to inspect (using +Email::Send::Test). + +=cut + +my $args = undef; + +if ( FixMyStreet->test_mode ) { + # Email::Send::Test + $args = { mailer => 'Test', }; +} elsif ( my $smtp_host = FixMyStreet->config('SMTP_SMARTHOST') ) { + # Email::Send::SMTP + my $type = FixMyStreet->config('SMTP_TYPE') || ''; + my $port = FixMyStreet->config('SMTP_PORT') || ''; + my $username = FixMyStreet->config('SMTP_USERNAME') || ''; + my $password = FixMyStreet->config('SMTP_PASSWORD') || ''; + + unless ($port) { + $port = 25; + $port = 465 if $type eq 'ssl'; + $port = 587 if $type eq 'tls'; + } + + my $mailer_args = [ + Host => $smtp_host, + Port => $port, + ]; + push @$mailer_args, ssl => 1 if $type eq 'ssl'; + push @$mailer_args, tls => 1 if $type eq 'tls'; + push @$mailer_args, username => $username, password => $password + if $username && $password; + $args = { + mailer => 'FixMyStreet::EmailSend::DoNotReply', + mailer_args => $mailer_args, + }; +} else { + # Email::Send::Sendmail + $args = { mailer => 'Sendmail' }; +} + +sub new { + my ($cls, %hash) = @_; + my %args = ( %$args, %hash ); + return Email::Send->new(\%args); +} diff --git a/perllib/FixMyStreet/Geocode/Bing.pm b/perllib/FixMyStreet/Geocode/Bing.pm index d7db10ae6..a846f3348 100644 --- a/perllib/FixMyStreet/Geocode/Bing.pm +++ b/perllib/FixMyStreet/Geocode/Bing.pm @@ -8,6 +8,7 @@ package FixMyStreet::Geocode::Bing; use strict; +use FixMyStreet::Geocode; use Utils; # string STRING CONTEXT @@ -71,7 +72,7 @@ sub reverse { my ( $latitude, $longitude, $bing_culture ) = @_; # Get nearest road-type thing from Bing - my $key = mySociety::Config::get('BING_MAPS_API_KEY', ''); + my $key = FixMyStreet->config('BING_MAPS_API_KEY', ''); if ($key) { my $url = "http://dev.virtualearth.net/REST/v1/Locations/$latitude,$longitude?key=$key"; $url .= '&c=' . $bing_culture if $bing_culture; diff --git a/perllib/FixMyStreet/Roles/Abuser.pm b/perllib/FixMyStreet/Roles/Abuser.pm index b9e951305..fc76565ca 100644 --- a/perllib/FixMyStreet/Roles/Abuser.pm +++ b/perllib/FixMyStreet/Roles/Abuser.pm @@ -1,6 +1,6 @@ package FixMyStreet::Roles::Abuser; -use Moose::Role; +use Moo::Role; =head2 is_from_abuser diff --git a/perllib/FixMyStreet/Roles/Extra.pm b/perllib/FixMyStreet/Roles/Extra.pm index f815a3e9a..19fc91873 100644 --- a/perllib/FixMyStreet/Roles/Extra.pm +++ b/perllib/FixMyStreet/Roles/Extra.pm @@ -1,5 +1,5 @@ package FixMyStreet::Roles::Extra; -use Moose::Role; +use Moo::Role; =head1 NAME @@ -9,12 +9,9 @@ FixMyStreet::Roles::Extra - role for accessing {extra} field This is to applied to a DB class like Problem or Contacts that has a rich {extra} field: - use Moose; + use Moo; with 'FixMyStreet::Roles::Extra'; -(NB: there is actually a little more boilerplate, because DBIC doesn't actually -inherit from Moose, see ::Problem for an example.) - Then: $contact->set_extra_fields( diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm new file mode 100644 index 000000000..fea897a24 --- /dev/null +++ b/perllib/FixMyStreet/Script/Alerts.pm @@ -0,0 +1,293 @@ +package FixMyStreet::Script::Alerts; + +use strict; +use warnings; + +use DateTime::Format::Pg; +use IO::String; + +use mySociety::DBHandle qw(dbh); +use mySociety::Gaze; +use mySociety::Locale; +use mySociety::MaPit; +use RABX; + +use FixMyStreet::Cobrand; +use FixMyStreet::DB; +use FixMyStreet::Email; + +FixMyStreet->configure_mysociety_dbhandle; + +# Child must have confirmed, id, email, state(!) columns +# If parent/child, child table must also have name and text +# and foreign key to parent must be PARENT_id +sub send() { + my $rs = FixMyStreet::DB->resultset('AlertType'); + my $schema = $rs->result_source->schema; + + my $q = $rs->search( { ref => { -not_like => '%local_problems%' } } ); + while (my $alert_type = $q->next) { + my $ref = $alert_type->ref; + my $head_table = $alert_type->head_table; + my $item_table = $alert_type->item_table; + my $query = 'select alert.id as alert_id, alert.user_id as alert_user_id, alert.lang as alert_lang, alert.cobrand as alert_cobrand, + alert.cobrand_data as alert_cobrand_data, alert.parameter as alert_parameter, alert.parameter2 as alert_parameter2, '; + if ($head_table) { + $query .= " + $item_table.id as item_id, $item_table.text as item_text, + $item_table.name as item_name, $item_table.anonymous as item_anonymous, + $item_table.confirmed as item_confirmed, + $head_table.* + from alert, $item_table, $head_table + where alert.parameter::integer = $head_table.id + and $item_table.${head_table}_id = $head_table.id + "; + } else { + $query .= " $item_table.*, + $item_table.id as item_id + from alert, $item_table + where 1 = 1"; + } + $query .= " + and alert_type='$ref' and whendisabled is null and $item_table.confirmed >= whensubscribed + and $item_table.confirmed >= current_timestamp - '7 days'::interval + and (select whenqueued from alert_sent where alert_sent.alert_id = alert.id and alert_sent.parameter::integer = $item_table.id) is null + and $item_table.user_id <> alert.user_id + and " . $alert_type->item_where . " + and alert.confirmed = 1 + order by alert.id, $item_table.confirmed"; + # XXX Ugh - needs work + $query =~ s/\?/alert.parameter/ if ($query =~ /\?/); + $query =~ s/\?/alert.parameter2/ if ($query =~ /\?/); + + $query = dbh()->prepare($query); + $query->execute(); + my $last_alert_id; + my %data = ( template => $alert_type->template, data => '', schema => $schema ); + while (my $row = $query->fetchrow_hashref) { + + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->{alert_cobrand})->new(); + $cobrand->set_lang_and_domain( $row->{alert_lang}, 1, FixMyStreet->path_to('locale')->stringify ); + + # Cobranded and non-cobranded messages can share a database. In this case, the conf file + # should specify a vhost to send the reports for each cobrand, so that they don't get sent + # more than once if there are multiple vhosts running off the same database. The email_host + # call checks if this is the host that sends mail for this cobrand. + next unless $cobrand->email_host; + + # this is for the new_updates alerts + next if $row->{non_public} and $row->{user_id} != $row->{alert_user_id}; + + $schema->resultset('AlertSent')->create( { + alert_id => $row->{alert_id}, + parameter => $row->{item_id}, + } ); + if ($last_alert_id && $last_alert_id != $row->{alert_id}) { + _send_aggregated_alert_email(%data); + %data = ( template => $alert_type->template, data => '', schema => $schema ); + } + + # create problem status message for the templates + if ( FixMyStreet::DB::Result::Problem::fixed_states()->{$row->{state}} ) { + $data{state_message} = _("This report is currently marked as fixed."); + } elsif ( FixMyStreet::DB::Result::Problem::closed_states()->{$row->{state}} ) { + $data{state_message} = _("This report is currently marked as closed.") + } else { + $data{state_message} = _("This report is currently marked as open."); + } + + my $url = $cobrand->base_url_for_report($row); + # this is currently only for new_updates + if ($row->{item_text}) { + if ( $cobrand->moniker ne 'zurich' && $row->{alert_user_id} == $row->{user_id} ) { + # This is an alert to the same user who made the report - make this a login link + # Don't bother with Zurich which has no accounts + my $user = $schema->resultset('User')->find( { + id => $row->{alert_user_id} + } ); + $data{alert_email} = $user->email; + my $token_obj = $schema->resultset('Token')->create( { + scope => 'alert_to_reporter', + data => { + id => $row->{id}, + } + } ); + $data{problem_url} = $url . "/R/" . $token_obj->token; + } else { + $data{problem_url} = $url . "/report/" . $row->{id}; + } + $data{data} .= $row->{item_name} . ' : ' if $row->{item_name} && !$row->{item_anonymous}; + if ( $cobrand->include_time_in_update_alerts ) { + my $parser = DateTime::Format::Pg->new(); + my $dt = $parser->parse_timestamp( $row->{item_confirmed} ); + # We need to always set this otherwise we end up with the DateTime + # object being in the floating timezone in which case applying a + # subsequent timezone set will have no effect. + # this is basically recreating the code from the inflate wrapper + # in the database model. + FixMyStreet->set_time_zone($dt); + $data{data} .= $cobrand->prettify_dt( $dt, 'alert' ) . "\n\n"; + } + $data{data} .= $row->{item_text} . "\n\n------\n\n"; + #Â this is ward and council problems + } else { + $data{data} .= $url . "/report/" . $row->{id} . " - $row->{title}\n\n"; + if ( exists $row->{geocode} && $row->{geocode} && $ref =~ /ward|council/ ) { + my $nearest_st = _get_address_from_gecode( $row->{geocode} ); + $data{data} .= $nearest_st if $nearest_st; + } + $data{data} .= "\n\n------\n\n"; + } + if (!$data{alert_user_id}) { + %data = (%data, %$row); + if ($ref eq 'area_problems' || $ref eq 'council_problems' || $ref eq 'ward_problems') { + my $va_info = mySociety::MaPit::call('area', $row->{alert_parameter}); + $data{area_name} = $va_info->{name}; + } + if ($ref eq 'ward_problems') { + my $va_info = mySociety::MaPit::call('area', $row->{alert_parameter2}); + $data{ward_name} = $va_info->{name}; + } + } + $data{cobrand} = $row->{alert_cobrand}; + $data{cobrand_data} = $row->{alert_cobrand_data}; + $data{lang} = $row->{alert_lang}; + $last_alert_id = $row->{alert_id}; + } + if ($last_alert_id) { + _send_aggregated_alert_email(%data); + } + } + + # Nearby done separately as the table contains the parameters + my $template = $rs->find( { ref => 'local_problems' } )->template; + my $query = $schema->resultset('Alert')->search( { + alert_type => 'local_problems', + whendisabled => undef, + confirmed => 1 + }, { + order_by => 'id' + } ); + while (my $alert = $query->next) { + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($alert->cobrand)->new(); + next unless $cobrand->email_host; + next if $alert->is_from_abuser; + + my $longitude = $alert->parameter; + my $latitude = $alert->parameter2; + my $d = mySociety::Gaze::get_radius_containing_population($latitude, $longitude, 200000); + # Convert integer to GB locale string (with a ".") + $d = mySociety::Locale::in_gb_locale { + sprintf("%f", int($d*10+0.5)/10); + }; + my $states = "'" . join( "', '", FixMyStreet::DB::Result::Problem::visible_states() ) . "'"; + my %data = ( + template => $template, + data => '', + alert_id => $alert->id, + alert_email => $alert->user->email, + lang => $alert->lang, + cobrand => $alert->cobrand, + cobrand_data => $alert->cobrand_data, + schema => $schema, + ); + my $q = "select problem.id, problem.bodies_str, problem.postcode, problem.geocode, problem.title from problem_find_nearby(?, ?, ?) as nearby, problem, users + where nearby.problem_id = problem.id + and problem.user_id = users.id + and problem.state in ($states) + and problem.non_public = 'f' + and problem.confirmed >= ? and problem.confirmed >= current_timestamp - '7 days'::interval + and (select whenqueued from alert_sent where alert_sent.alert_id = ? and alert_sent.parameter::integer = problem.id) is null + and users.email <> ? + order by confirmed desc"; + $q = dbh()->prepare($q); + $q->execute($latitude, $longitude, $d, $alert->whensubscribed, $alert->id, $alert->user->email); + while (my $row = $q->fetchrow_hashref) { + $schema->resultset('AlertSent')->create( { + alert_id => $alert->id, + parameter => $row->{id}, + } ); + my $url = $cobrand->base_url_for_report($row); + $data{data} .= $url . "/report/" . $row->{id} . " - $row->{title}\n\n"; + if ( exists $row->{geocode} && $row->{geocode} ) { + my $nearest_st = _get_address_from_gecode( $row->{geocode} ); + $data{data} .= $nearest_st if $nearest_st; + } + $data{data} .= "\n\n------\n\n"; + } + _send_aggregated_alert_email(%data) if $data{data}; + } +} + +sub _send_aggregated_alert_email(%) { + my %data = @_; + + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($data{cobrand})->new(); + + $cobrand->set_lang_and_domain( $data{lang}, 1, FixMyStreet->path_to('locale')->stringify ); + + if (!$data{alert_email}) { + my $user = $data{schema}->resultset('User')->find( { + id => $data{alert_user_id} + } ); + $data{alert_email} = $user->email; + } + + my ($domain) = $data{alert_email} =~ m{ @ (.*) \z }x; + return if $data{schema}->resultset('Abuse')->search( { + email => [ $data{alert_email}, $domain ] + } )->first; + + my $token = $data{schema}->resultset("Token")->new_result( { + scope => 'alert', + data => { + id => $data{alert_id}, + type => 'unsubscribe', + email => $data{alert_email}, + } + } ); + $data{unsubscribe_url} = $cobrand->base_url( $data{cobrand_data} ) . '/A/' . $token->token; + + my $template = FixMyStreet->get_email_template($cobrand->moniker, $data{lang}, "$data{template}.txt"); + + my $result = FixMyStreet::Email::send_cron( + $data{schema}, + { + _template_ => $template, + _parameters_ => \%data, + To => $data{alert_email}, + }, + undef, + 0, + $cobrand, + $data{lang} + ); + + unless ($result) { + $token->insert(); + } else { + print "Failed to send alert $data{alert_id}!"; + } +} + +sub _get_address_from_gecode { + my $geocode = shift; + + return '' unless defined $geocode; + utf8::encode($geocode) if utf8::is_utf8($geocode); + my $h = new IO::String($geocode); + my $data = RABX::wire_rd($h); + + my $str = ''; + + my $address = $data->{resourceSets}[0]{resources}[0]{address}; + my @address; + push @address, $address->{addressLine} if $address->{addressLine} && $address->{addressLine} ne 'Street'; + push @address, $address->{locality} if $address->{locality}; + $str .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s\n\n"), + join( ', ', @address ) ) if @address; + + return $str; +} + +1; diff --git a/perllib/FixMyStreet/Script/Questionnaires.pm b/perllib/FixMyStreet/Script/Questionnaires.pm new file mode 100644 index 000000000..2d676f15d --- /dev/null +++ b/perllib/FixMyStreet/Script/Questionnaires.pm @@ -0,0 +1,117 @@ +package FixMyStreet::Script::Questionnaires; + +use strict; +use warnings; +use Utils; +use FixMyStreet::DB; +use FixMyStreet::Email; +use FixMyStreet::Cobrand; + +sub send { + my ( $params ) = @_; + send_questionnaires_period( '4 weeks', $params ); + send_questionnaires_period( '26 weeks', $params ) + if $params->{site} eq 'emptyhomes'; +} + +sub send_questionnaires_period { + my ( $period, $params ) = @_; + + my $rs = FixMyStreet::DB->resultset('Questionnaire'); + + # Select all problems that need a questionnaire email sending + my $q_params = { + state => [ FixMyStreet::DB::Result::Problem::visible_states() ], + whensent => [ + '-and', + { '!=', undef }, + { '<', \"current_timestamp - '$period'::interval" }, + ], + send_questionnaire => 1, + }; + # FIXME Do these a bit better... + if ($params->{site} eq 'emptyhomes' && $period eq '4 weeks') { + $q_params->{'(select max(whensent) from questionnaire where me.id=problem_id)'} = undef; + } elsif ($params->{site} eq 'emptyhomes' && $period eq '26 weeks') { + $q_params->{'(select max(whensent) from questionnaire where me.id=problem_id)'} = { '!=', undef }; + } else { + $q_params->{'-or'} = [ + '(select max(whensent) from questionnaire where me.id=problem_id)' => undef, + '(select max(whenanswered) from questionnaire where me.id=problem_id)' => { '<', \"current_timestamp - '$period'::interval" } + ]; + } + + my $unsent = FixMyStreet::DB->resultset('Problem')->search( $q_params, { + order_by => { -desc => 'confirmed' } + } ); + + while (my $row = $unsent->next) { + + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); + $cobrand->set_lang_and_domain($row->lang, 1); + + # Not all cobrands send questionnaires + next unless $cobrand->send_questionnaires; + next if $row->is_from_abuser; + + # Cobranded and non-cobranded messages can share a database. In this case, the conf file + # should specify a vhost to send the reports for each cobrand, so that they don't get sent + # more than once if there are multiple vhosts running off the same database. The email_host + # call checks if this is the host that sends mail for this cobrand. + next unless $cobrand->email_host; + + my $template; + if ($params->{site} eq 'emptyhomes') { + ($template = $period) =~ s/ //; + $template = Utils::read_file( FixMyStreet->path_to( "templates/email/emptyhomes/" . $row->lang . "/questionnaire-$template.txt" )->stringify ); + } else { + $template = FixMyStreet->get_email_template($cobrand->moniker, $row->lang, 'questionnaire.txt'); + } + + my %h = map { $_ => $row->$_ } qw/name title detail category/; + $h{created} = Utils::prettify_duration( time() - $row->confirmed->epoch, 'week' ); + + my $questionnaire = $rs->create( { + problem_id => $row->id, + whensent => \'current_timestamp', + } ); + + # We won't send another questionnaire unless they ask for it (or it was + # the first EHA questionnaire. + $row->send_questionnaire( 0 ) + if $params->{site} ne 'emptyhomes' || $period eq '26 weeks'; + + my $token = FixMyStreet::DB->resultset("Token")->new_result( { + scope => 'questionnaire', + data => $questionnaire->id, + } ); + $h{url} = $cobrand->base_url($row->cobrand_data) . '/Q/' . $token->token; + + print "Sending questionnaire " . $questionnaire->id . ", problem " + . $row->id . ", token " . $token->token . " to " + . $row->user->email . "\n" + if $params->{verbose}; + + my $result = FixMyStreet::Email::send_cron( + $rs->result_source->schema, + { + _template_ => $template, + _parameters_ => \%h, + To => [ [ $row->user->email, $row->name ] ], + }, + undef, + $params->{nomail}, + $cobrand + ); + unless ($result) { + print " ...success\n" if $params->{verbose}; + $row->update(); + $token->insert(); + } else { + print " ...failed\n" if $params->{verbose}; + $questionnaire->delete; + } + } +} + +1; diff --git a/perllib/FixMyStreet/Script/Reports.pm b/perllib/FixMyStreet/Script/Reports.pm new file mode 100644 index 000000000..55b1bb21c --- /dev/null +++ b/perllib/FixMyStreet/Script/Reports.pm @@ -0,0 +1,313 @@ +package FixMyStreet::Script::Reports; + +use strict; +use warnings; + +use CronFns; +use DateTime::Format::Pg; + +use Utils; +use mySociety::MaPit; + +use FixMyStreet; +use FixMyStreet::Cobrand; +use FixMyStreet::DB; +use FixMyStreet::Email; +use FixMyStreet::SendReport; + +sub send(;$) { + my ($site_override) = @_; + my $rs = FixMyStreet::DB->resultset('Problem'); + + # Set up site, language etc. + my ($verbose, $nomail, $debug_mode) = CronFns::options(); + + my $base_url = FixMyStreet->config('BASE_URL'); + my $site = $site_override || CronFns::site($base_url); + + my $states = [ 'confirmed', 'fixed' ]; + $states = [ 'unconfirmed', 'confirmed', 'in progress', 'planned', 'closed', 'investigating' ] if $site eq 'zurich'; + my $unsent = $rs->search( { + state => $states, + whensent => undef, + bodies_str => { '!=', undef }, + } ); + my (%notgot, %note); + + my $send_report = FixMyStreet::SendReport->new(); + my $senders = $send_report->get_senders; + + my $debug_unsent_count = 0; + debug_print("starting to loop through unsent problem reports...") if $debug_mode; + while (my $row = $unsent->next) { + + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($row->cobrand)->new(); + + if ($debug_mode) { + $debug_unsent_count++; + print "\n"; + debug_print("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : ""), $row->id); + } + + # Cobranded and non-cobranded messages can share a database. In this case, the conf file + # should specify a vhost to send the reports for each cobrand, so that they don't get sent + # more than once if there are multiple vhosts running off the same database. The email_host + # call checks if this is the host that sends mail for this cobrand. + if (! $cobrand->email_host()) { + debug_print("skipping because this host does not send reports for cobrand " . $cobrand->moniker, $row->id) if $debug_mode; + next; + } + + $cobrand->set_lang_and_domain($row->lang, 1); + if ( $row->is_from_abuser) { + $row->update( { state => 'hidden' } ); + debug_print("hiding because its sender is flagged as an abuser", $row->id) if $debug_mode; + next; + } elsif ( $row->title =~ /app store test/i ) { + $row->update( { state => 'hidden' } ); + debug_print("hiding because it is an app store test message", $row->id) if $debug_mode; + next; + } + + # Template variables for the email + my $email_base_url = $cobrand->base_url_for_report($row); + my %h = map { $_ => $row->$_ } qw/id title detail name category latitude longitude used_map/; + map { $h{$_} = $row->user->$_ || '' } qw/email phone/; + $h{confirmed} = DateTime::Format::Pg->format_datetime( $row->confirmed->truncate (to => 'second' ) ) + if $row->confirmed; + + $h{query} = $row->postcode; + $h{url} = $email_base_url . $row->url; + $h{admin_url} = $row->admin_url($cobrand); + $h{phone_line} = $h{phone} ? _('Phone:') . " $h{phone}\n\n" : ''; + if ($row->photo) { + $h{has_photo} = _("This web page also contains a photo of the problem, provided by the user.") . "\n\n"; + $h{image_url} = $email_base_url . '/photo/' . $row->id . '.full.jpeg'; + } else { + $h{has_photo} = ''; + $h{image_url} = ''; + } + $h{fuzzy} = $row->used_map ? _('To view a map of the precise location of this issue') + : _('The user could not locate the problem on a map, but to see the area around the location they entered'); + $h{closest_address} = ''; + + if ( $row->used_map ) { + $h{closest_address} = $cobrand->find_closest( $h{latitude}, $h{longitude}, $row ); + } + + if ( $cobrand->allow_anonymous_reports && + $row->user->email eq $cobrand->anonymous_account->{'email'} + ) { + $h{anonymous_report} = 1; + $h{user_details} = _('This report was submitted anonymously'); + } else { + $h{user_details} = sprintf(_('Name: %s'), $row->name) . "\n\n"; + $h{user_details} .= sprintf(_('Email: %s'), $row->user->email) . "\n\n"; + } + + $h{easting_northing} = ''; + + if ($cobrand->can('process_additional_metadata_for_email')) { + $cobrand->process_additional_metadata_for_email($row, \%h); + } + + my $bodies = FixMyStreet::DB->resultset('Body')->search( + { id => $row->bodies_str_ids }, + { order_by => 'name' }, + ); + + my $missing; + if ($row->bodies_missing) { + my @missing = FixMyStreet::DB->resultset("Body")->search( + { id => [ split /,/, $row->bodies_missing ] }, + { order_by => 'name' } + )->get_column('name')->all; + $missing = join(' / ', @missing) if @missing; + } + + my @dear; + my %reporters = (); + while (my $body = $bodies->next) { + my $sender_info = $cobrand->get_body_sender( $body, $row->category ); + my $sender = "FixMyStreet::SendReport::" . $sender_info->{method}; + + if ( ! exists $senders->{ $sender } ) { + warn sprintf "No such sender [ $sender ] for body %s ( %d )", $body->name, $body->id; + next; + } + $reporters{ $sender } ||= $sender->new(); + + if ( $reporters{ $sender }->should_skip( $row ) ) { + debug_print("skipped by sender " . $sender_info->{method} . " (might be due to previous failed attempts?)", $row->id) if $debug_mode; + } else { + debug_print("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $body->send_method, $row->id) if $debug_mode; + push @dear, $body->name; + $reporters{ $sender }->add_body( $body, $sender_info->{config} ); + } + + # If we are in the UK include eastings and northings, and nearest stuff + if ( $cobrand->country eq 'GB' && !$h{easting} ) { + my $coordsyst = 'G'; + my $first_area = $body->body_areas->first->area_id; + my $area_info = mySociety::MaPit::call('area', $first_area); + $coordsyst = 'I' if $area_info->{type} eq 'LGD'; + + ( $h{easting}, $h{northing} ) = Utils::convert_latlon_to_en( $h{latitude}, $h{longitude}, $coordsyst ); + + # email templates don't have conditionals so we need to format this here + $h{easting_northing} = "Easting/Northing"; + $h{easting_northing} .= " (IE)" if $coordsyst eq 'I'; + $h{easting_northing} .= ": $h{easting}/$h{northing}\n\n"; + } + } + + unless ( keys %reporters ) { + die 'Report not going anywhere for ID ' . $row->id . '!'; + } + + unless (@dear) { + debug_print("can't send because sender count is zero", $row->id) if $debug_mode; + next; + } + + if ($h{category} eq _('Other')) { + $h{category_footer} = _('this type of local problem'); + $h{category_line} = ''; + } else { + $h{category_footer} = "'" . $h{category} . "'"; + $h{category_line} = sprintf(_("Category: %s"), $h{category}) . "\n\n"; + } + + if ( $row->subcategory ) { + $h{subcategory_line} = sprintf(_("Subcategory: %s"), $row->subcategory) . "\n\n"; + } else { + $h{subcategory_line} = "\n\n"; + } + + $h{bodies_name} = join(_(' and '), @dear); + if ($h{category} eq _('Other')) { + $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to both councils covering the location of the problem, as the user did not categorise it; please ignore it if you're not the correct council to deal with the issue, or let us know what category of problem this is so we can add it to our system.") . " ]\n\n" + : ''; + } else { + $h{multiple} = @dear>1 ? "[ " . _("This email has been sent to several councils covering the location of the problem, as the category selected is provided for all of them; please ignore it if you're not the correct council to deal with the issue.") . " ]\n\n" + : ''; + } + $h{missing} = ''; + if ($missing) { + $h{missing} = '[ ' + . sprintf(_('We realise this problem might be the responsibility of %s; however, we don\'t currently have any contact details for them. If you know of an appropriate contact address, please do get in touch.'), $missing) + . " ]\n\n"; + } + + if (FixMyStreet->config('STAGING_SITE') && !FixMyStreet->config('SEND_REPORTS_ON_STAGING')) { + # on a staging server send emails to ourselves rather than the bodies + %reporters = map { $_ => $reporters{$_} } grep { /FixMyStreet::SendReport::(Email|EmptyHomes)/ } keys %reporters; + unless (%reporters) { + %reporters = ( 'FixMyStreet::SendReport::Email' => FixMyStreet::SendReport::Email->new() ); + } + } + + # Multiply results together, so one success counts as a success. + my $result = -1; + + for my $sender ( keys %reporters ) { + debug_print("sending using " . $sender, $row->id) if $debug_mode; + $result *= $reporters{ $sender }->send( $row, \%h ); + if ( $reporters{ $sender }->unconfirmed_counts) { + foreach my $e (keys %{ $reporters{ $sender }->unconfirmed_counts } ) { + foreach my $c (keys %{ $reporters{ $sender }->unconfirmed_counts->{$e} }) { + $notgot{$e}{$c} += $reporters{ $sender }->unconfirmed_counts->{$e}{$c}; + } + } + %note = ( + %note, + %{ $reporters{ $sender }->unconfirmed_notes } + ); + } + } + + unless ($result) { + $row->update( { + whensent => \'current_timestamp', + lastupdate => \'current_timestamp', + } ); + if ( $cobrand->report_sent_confirmation_email && !$h{anonymous_report}) { + _send_report_sent_email( $row, \%h, $nomail, $cobrand ); + } + debug_print("send successful: OK", $row->id) if $debug_mode; + } else { + my @errors; + for my $sender ( keys %reporters ) { + unless ( $reporters{ $sender }->success ) { + push @errors, $reporters{ $sender }->error; + } + } + $row->update_send_failed( join( '|', @errors ) ); + debug_print("send FAILED: " . join( '|', @errors ), $row->id) if $debug_mode; + } + } + if ($debug_mode) { + print "\n"; + if ($debug_unsent_count) { + debug_print("processed all unsent reports (total: $debug_unsent_count)"); + } else { + debug_print("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); + } + } + + if ($verbose || $debug_mode) { + print "Council email addresses that need checking:\n" if keys %notgot; + foreach my $e (keys %notgot) { + foreach my $c (keys %{$notgot{$e}}) { + print " " . $notgot{$e}{$c} . " problem, to $e category $c (" . $note{$e}{$c}. ")\n"; + } + } + my $sending_errors = ''; + my $unsent = $rs->search( { + state => [ 'confirmed', 'fixed' ], + whensent => undef, + bodies_str => { '!=', undef }, + send_fail_count => { '>', 0 } + } ); + while (my $row = $unsent->next) { + my $base_url = FixMyStreet->config('BASE_URL'); + $sending_errors .= "* " . $base_url . "/report/" . $row->id . ", failed " + . $row->send_fail_count . " times, last at " . $row->send_fail_timestamp + . ", reason " . $row->send_fail_reason . "\n"; + } + if ($sending_errors) { + print "The following reports had problems sending:\n$sending_errors"; + } + } +} + +sub _send_report_sent_email { + my $row = shift; + my $h = shift; + my $nomail = shift; + my $cobrand = shift; + + my $template = FixMyStreet->get_email_template($row->cobrand, $row->lang, 'confirm_report_sent.txt'); + + FixMyStreet::Email::send_cron( + $row->result_source->schema, + { + _template_ => $template, + _parameters_ => $h, + To => $row->user->email, + From => [ FixMyStreet->config('CONTACT_EMAIL'), $cobrand->contact_name ], + }, + FixMyStreet->config('CONTACT_EMAIL'), + $nomail, + $cobrand + ); +} + +sub debug_print { + my $msg = shift; + my $id = shift || ''; + $id = "report $id: " if $id; + print "[] $id$msg\n"; +} + +1; diff --git a/perllib/FixMyStreet/SendReport.pm b/perllib/FixMyStreet/SendReport.pm index 9967b0663..40ec4caf2 100644 --- a/perllib/FixMyStreet/SendReport.pm +++ b/perllib/FixMyStreet/SendReport.pm @@ -1,20 +1,21 @@ package FixMyStreet::SendReport; -use Moose; +use Moo; +use MooX::Types::MooseLike::Base qw(:all); use Module::Pluggable sub_name => 'senders', search_path => __PACKAGE__, require => 1; -has 'body_config' => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); -has 'bodies' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); -has 'to' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); -has 'bcc' => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } ); -has 'success' => ( is => 'rw', isa => 'Bool', default => 0 ); -has 'error' => ( is => 'rw', isa => 'Str', default => '' ); -has 'unconfirmed_counts' => ( 'is' => 'rw', isa => 'HashRef', default => sub { {} } ); -has 'unconfirmed_notes' => ( 'is' => 'rw', isa => 'HashRef', default => sub { {} } ); +has 'body_config' => ( is => 'rw', isa => HashRef, default => sub { {} } ); +has 'bodies' => ( is => 'rw', isa => ArrayRef, default => sub { [] } ); +has 'to' => ( is => 'rw', isa => ArrayRef, default => sub { [] } ); +has 'bcc' => ( is => 'rw', isa => ArrayRef, default => sub { [] } ); +has 'success' => ( is => 'rw', isa => Bool, default => 0 ); +has 'error' => ( is => 'rw', isa => Str, default => '' ); +has 'unconfirmed_counts' => ( 'is' => 'rw', isa => HashRef, default => sub { {} } ); +has 'unconfirmed_notes' => ( 'is' => 'rw', isa => HashRef, default => sub { {} } ); sub should_skip { diff --git a/perllib/FixMyStreet/SendReport/EastHants.pm b/perllib/FixMyStreet/SendReport/EastHants.pm index 44bc084b1..3eb8ffcfa 100644 --- a/perllib/FixMyStreet/SendReport/EastHants.pm +++ b/perllib/FixMyStreet/SendReport/EastHants.pm @@ -1,6 +1,6 @@ package FixMyStreet::SendReport::EastHants; -use Moose; +use Moo; BEGIN { extends 'FixMyStreet::SendReport'; } @@ -28,7 +28,7 @@ EOF } sub send { - return if mySociety::Config::get('STAGING_SITE'); + return if FixMyStreet->config('STAGING_SITE'); my ( $self, $row, $h ) = @_; diff --git a/perllib/FixMyStreet/SendReport/Email.pm b/perllib/FixMyStreet/SendReport/Email.pm index bac408510..4d2c8bb17 100644 --- a/perllib/FixMyStreet/SendReport/Email.pm +++ b/perllib/FixMyStreet/SendReport/Email.pm @@ -1,6 +1,6 @@ package FixMyStreet::SendReport::Email; -use Moose; +use Moo; use FixMyStreet::Email; BEGIN { extends 'FixMyStreet::SendReport'; } @@ -11,7 +11,7 @@ sub build_recipient_list { my $all_confirmed = 1; foreach my $body ( @{ $self->bodies } ) { - my $contact = FixMyStreet::App->model("DB::Contact")->find( { + my $contact = $row->result_source->schema->resultset("Contact")->find( { deleted => 0, body_id => $body->id, category => $row->category @@ -75,7 +75,7 @@ sub send { my $recips = $self->build_recipient_list( $row, $h ); # on a staging server send emails to ourselves rather than the bodies - if (mySociety::Config::get('STAGING_SITE') && !mySociety::Config::get('SEND_REPORTS_ON_STAGING') && !FixMyStreet->test_mode) { + if (FixMyStreet->config('STAGING_SITE') && !FixMyStreet->config('SEND_REPORTS_ON_STAGING') && !FixMyStreet->test_mode) { $recips = 1; @{$self->to} = [ $row->user->email, $self->to->[0][1] || $row->name ]; } @@ -94,20 +94,19 @@ sub send { From => $self->send_from( $row ), }; - my $app = FixMyStreet::App->new( cobrand => $cobrand ); - - $cobrand->munge_sendreport_params($app, $row, $h, $params) if $cobrand->can('munge_sendreport_params'); + $cobrand->munge_sendreport_params($row, $h, $params) if $cobrand->can('munge_sendreport_params'); $params->{Bcc} = $self->bcc if @{$self->bcc}; if (FixMyStreet::Email::test_dmarc($params->{From}[0])) { $params->{'Reply-To'} = [ $params->{From} ]; - $params->{From} = [ mySociety::Config::get('CONTACT_EMAIL'), $params->{From}[1] ]; + $params->{From} = [ FixMyStreet->config('CONTACT_EMAIL'), $params->{From}[1] ]; } - my $result = $app->send_email_cron( + my $result = FixMyStreet::Email::send_cron( + $row->result_source->schema, $params, - mySociety::Config::get('CONTACT_EMAIL'), + FixMyStreet->config('CONTACT_EMAIL'), $nomail, $cobrand ); diff --git a/perllib/FixMyStreet/SendReport/EmptyHomes.pm b/perllib/FixMyStreet/SendReport/EmptyHomes.pm index ce69aaac3..b5faf8ddc 100644 --- a/perllib/FixMyStreet/SendReport/EmptyHomes.pm +++ b/perllib/FixMyStreet/SendReport/EmptyHomes.pm @@ -1,6 +1,6 @@ package FixMyStreet::SendReport::EmptyHomes; -use Moose; +use Moo; use namespace::autoclean; use mySociety::MaPit; @@ -12,7 +12,7 @@ sub build_recipient_list { my $all_confirmed = 1; foreach my $body ( @{ $self->bodies } ) { - my $contact = FixMyStreet::App->model("DB::Contact")->find( { + my $contact = $row->result_source->schema->resultset("Contact")->find( { deleted => 0, body_id => $body->id, category => 'Empty property', @@ -34,11 +34,11 @@ sub build_recipient_list { my $area_info = mySociety::MaPit::call('area', $body->body_areas->first->area_id); my $country = $area_info->{country}; if ($country eq 'W') { - push @{$self->bcc}, 'wales@' . mySociety::Config::get('EMAIL_DOMAIN'); + push @{$self->bcc}, 'wales@' . FixMyStreet->config('EMAIL_DOMAIN'); } elsif ($country eq 'S') { - push @{$self->bcc}, 'scotland@' . mySociety::Config::get('EMAIL_DOMAIN'); + push @{$self->bcc}, 'scotland@' . FixMyStreet->config('EMAIL_DOMAIN'); } else { - push @{$self->bcc}, 'eha@' . mySociety::Config::get('EMAIL_DOMAIN'); + push @{$self->bcc}, 'eha@' . FixMyStreet->config('EMAIL_DOMAIN'); } } diff --git a/perllib/FixMyStreet/SendReport/Noop.pm b/perllib/FixMyStreet/SendReport/Noop.pm index f2e0a3bdb..60edda373 100644 --- a/perllib/FixMyStreet/SendReport/Noop.pm +++ b/perllib/FixMyStreet/SendReport/Noop.pm @@ -1,6 +1,6 @@ package FixMyStreet::SendReport::Noop; -use Moose; +use Moo; BEGIN { extends 'FixMyStreet::SendReport'; } diff --git a/perllib/FixMyStreet/SendReport/Open311.pm b/perllib/FixMyStreet/SendReport/Open311.pm index fa216466e..4844aa2e9 100644 --- a/perllib/FixMyStreet/SendReport/Open311.pm +++ b/perllib/FixMyStreet/SendReport/Open311.pm @@ -1,12 +1,10 @@ package FixMyStreet::SendReport::Open311; -use Moose; +use Moo; use namespace::autoclean; BEGIN { extends 'FixMyStreet::SendReport'; } -use FixMyStreet::App; -use mySociety::Config; use DateTime::Format::W3CDTF; use Open311; use Readonly; @@ -85,7 +83,7 @@ sub send { } # FIXME: we've already looked this up before - my $contact = FixMyStreet::App->model("DB::Contact")->find( { + my $contact = $row->result_source->schema->resultset("Contact")->find( { deleted => 0, body_id => $body->id, category => $row->category diff --git a/perllib/FixMyStreet/SendReport/Refused.pm b/perllib/FixMyStreet/SendReport/Refused.pm index d71fc5c2c..c6c1a660d 100644 --- a/perllib/FixMyStreet/SendReport/Refused.pm +++ b/perllib/FixMyStreet/SendReport/Refused.pm @@ -1,6 +1,6 @@ package FixMyStreet::SendReport::Refused; -use Moose; +use Moo; BEGIN { extends 'FixMyStreet::SendReport::Noop'; } diff --git a/perllib/FixMyStreet/SendReport/Zurich.pm b/perllib/FixMyStreet/SendReport/Zurich.pm index 2838440cb..a8730bbe4 100644 --- a/perllib/FixMyStreet/SendReport/Zurich.pm +++ b/perllib/FixMyStreet/SendReport/Zurich.pm @@ -1,6 +1,6 @@ package FixMyStreet::SendReport::Zurich; -use Moose; +use Moo; BEGIN { extends 'FixMyStreet::SendReport::Email'; } @@ -14,7 +14,7 @@ sub build_recipient_list { # Wunsch set, but external_message hasn't yet been filled in. TODO should # we instead be holding off sending?) if ( $row->external_body ) { - $body = FixMyStreet::App->model("DB::Body")->find( { id => $row->external_body } ); + $body = $row->result_source->schema->resultset("Body")->find( { id => $row->external_body } ); $h->{bodies_name} = $body->name; $h->{external_message} = $row->get_extra_metadata('external_message') || ''; } @@ -29,7 +29,7 @@ sub build_recipient_list { my $parent = $body->parent; if ($parent && !$parent->parent) { # Division, might have an individual contact email address - my $contact = FixMyStreet::App->model("DB::Contact")->find( { + my $contact = $row->result_source->schema->resultset("Contact")->find( { body_id => $body->id, category => $row->category } ); @@ -72,7 +72,7 @@ sub send_from { if ( $row->external_body ) { my $body = @{ $self->bodies }[0]; my $body_email = $body->endpoint; - my $contact = FixMyStreet::App->model("DB::Contact")->find( { + my $contact = $body->result_source->schema->resultset("Contact")->find( { body_id => $body->id, category => $row->category } ); diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index cc5f9dd71..d3adce08b 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -66,7 +66,7 @@ sub create_user_ok { my ($email) = @_; my $user = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => $email } ); ok $user, "found/created user for $email"; @@ -147,7 +147,7 @@ sub delete_user { my $user = ref $email_or_user ? $email_or_user - : FixMyStreet::App->model('DB::User') + : FixMyStreet::DB->resultset('User') ->find( { email => $email_or_user } ); # If no user found we can't delete them @@ -567,7 +567,7 @@ sub delete_problems_for_body { my $mech = shift; my $body = shift; - my $reports = FixMyStreet::App->model('DB::Problem')->search( { bodies_str => $body } ); + my $reports = FixMyStreet::DB->resultset('Problem')->search( { bodies_str => $body } ); if ( $reports ) { for my $r ( $reports->all ) { $r->comments->delete; @@ -587,7 +587,7 @@ sub create_contact_ok { note => 'Created for test', @_ ); - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( \%contact_params ); + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( \%contact_params ); ok $contact, 'found/created contact ' . $contact->category;; return $contact; } @@ -596,7 +596,7 @@ sub create_body_ok { my $self = shift; my ( $area_id, $name, %extra ) = @_; - my $body = FixMyStreet::App->model('DB::Body'); + my $body = FixMyStreet::DB->resultset('Body'); my $params = { name => $name }; if ($extra{id}) { $body = $body->update_or_create({ %$params, id => $extra{id} }, { key => 'primary' }); @@ -606,7 +606,7 @@ sub create_body_ok { ok $body, "found/created body $name"; $body->body_areas->delete; - FixMyStreet::App->model('DB::BodyArea')->find_or_create({ + FixMyStreet::DB->resultset('BodyArea')->find_or_create({ area_id => $area_id, body_id => $body->id, }); @@ -621,7 +621,7 @@ sub create_problems_for_body { my $dt = $params->{dt} || DateTime->now(); my $user = $params->{user} || - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); delete $params->{user}; @@ -656,7 +656,7 @@ sub create_problems_for_body { my %report_params = ( %$default_params, %$params ); my $problem = - FixMyStreet::App->model('DB::Problem')->create( \%report_params ); + FixMyStreet::DB->resultset('Problem')->create( \%report_params ); push @problems, $problem; $count--; diff --git a/perllib/Open311.pm b/perllib/Open311.pm index 58ae96bc2..e500219bc 100644 --- a/perllib/Open311.pm +++ b/perllib/Open311.pm @@ -2,31 +2,34 @@ package Open311; use utf8; use URI; -use Moose; +use Moo; +use MooX::Types::MooseLike::Base qw(:all); use XML::Simple; use LWP::Simple; use LWP::UserAgent; use DateTime::Format::W3CDTF; use HTTP::Request::Common qw(POST); - -has jurisdiction => ( is => 'ro', isa => 'Str' );; -has api_key => ( is => 'ro', isa => 'Str' ); -has endpoint => ( is => 'ro', isa => 'Str' ); -has test_mode => ( is => 'ro', isa => 'Bool' ); -has test_uri_used => ( is => 'rw', 'isa' => 'Str' ); +use FixMyStreet::Cobrand; +use FixMyStreet::DB; + +has jurisdiction => ( is => 'ro', isa => Str );; +has api_key => ( is => 'ro', isa => Str ); +has endpoint => ( is => 'ro', isa => Str ); +has test_mode => ( is => 'ro', isa => Bool ); +has test_uri_used => ( is => 'rw', 'isa' => Str ); has test_req_used => ( is => 'rw' ); has test_get_returns => ( is => 'rw' ); has endpoints => ( is => 'rw', default => sub { { services => 'services.xml', requests => 'requests.xml', service_request_updates => 'servicerequestupdates.xml', update => 'servicerequestupdates.xml' } } ); -has debug => ( is => 'ro', isa => 'Bool', default => 0 ); -has debug_details => ( is => 'rw', 'isa' => 'Str', default => '' ); -has success => ( is => 'rw', 'isa' => 'Bool', default => 0 ); -has error => ( is => 'rw', 'isa' => 'Str', default => '' ); -has always_send_latlong => ( is => 'ro', isa => 'Bool', default => 1 ); -has send_notpinpointed => ( is => 'ro', isa => 'Bool', default => 0 ); -has extended_description => ( is => 'ro', isa => 'Str', default => 1 ); -has use_service_as_deviceid => ( is => 'ro', isa => 'Bool', default => 0 ); -has use_extended_updates => ( is => 'ro', isa => 'Bool', default => 0 ); -has extended_statuses => ( is => 'ro', isa => 'Bool', default => 0 ); +has debug => ( is => 'ro', isa => Bool, default => 0 ); +has debug_details => ( is => 'rw', 'isa' => Str, default => '' ); +has success => ( is => 'rw', 'isa' => Bool, default => 0 ); +has error => ( is => 'rw', 'isa' => Str, default => '' ); +has always_send_latlong => ( is => 'ro', isa => Bool, default => 1 ); +has send_notpinpointed => ( is => 'ro', isa => Bool, default => 0 ); +has extended_description => ( is => 'ro', isa => Str, default => 1 ); +has use_service_as_deviceid => ( is => 'ro', isa => Bool, default => 0 ); +has use_extended_updates => ( is => 'ro', isa => Bool, default => 0 ); +has extended_statuses => ( is => 'ro', isa => Bool, default => 0 ); before [ qw/get_service_list get_service_meta_info get_service_requests get_service_request_updates diff --git a/perllib/Open311/GetServiceRequestUpdates.pm b/perllib/Open311/GetServiceRequestUpdates.pm index 1e5f4dc6b..6d846de42 100644 --- a/perllib/Open311/GetServiceRequestUpdates.pm +++ b/perllib/Open311/GetServiceRequestUpdates.pm @@ -1,15 +1,16 @@ package Open311::GetServiceRequestUpdates; -use Moose; +use Moo; use Open311; -use FixMyStreet::App; +use FixMyStreet::DB; use DateTime::Format::W3CDTF; has system_user => ( is => 'rw' ); -has start_date => ( is => 'ro', default => undef ); -has end_date => ( is => 'ro', default => undef ); +has start_date => ( is => 'ro', default => sub { undef } ); +has end_date => ( is => 'ro', default => sub { undef } ); has suppress_alerts => ( is => 'rw', default => 0 ); has verbose => ( is => 'ro', default => 0 ); +has schema => ( is =>'ro', lazy => 1, default => sub { FixMyStreet::DB->connect } ); Readonly::Scalar my $AREA_ID_BROMLEY => 2482; Readonly::Scalar my $AREA_ID_OXFORDSHIRE => 2237; @@ -17,7 +18,7 @@ Readonly::Scalar my $AREA_ID_OXFORDSHIRE => 2237; sub fetch { my $self = shift; - my $bodies = FixMyStreet::App->model('DB::Body')->search( + my $bodies = $self->schema->resultset('Body')->search( { send_method => 'Open311', send_comments => 1, @@ -91,7 +92,7 @@ sub update_comments { my $criteria = { external_id => $request_id, }; - $problem = FixMyStreet::App->model('DB::Problem')->to_body($body)->search( $criteria ); + $problem = $self->schema->resultset('Problem')->to_body($body)->search( $criteria ); if (my $p = $problem->first) { my $c = $p->comments->search( { external_id => $request->{update_id} } ); @@ -99,7 +100,7 @@ sub update_comments { if ( !$c->first ) { my $comment_time = DateTime::Format::W3CDTF->parse_datetime( $request->{updated_datetime} ); - my $comment = FixMyStreet::App->model('DB::Comment')->new( + my $comment = $self->schema->resultset('Comment')->new( { problem => $p, user => $self->system_user, @@ -137,7 +138,7 @@ sub update_comments { $comment->insert(); if ( $self->suppress_alerts ) { - my @alerts = FixMyStreet::App->model('DB::Alert')->search( { + my @alerts = $self->schema->resultset('Alert')->search( { alert_type => 'new_updates', parameter => $p->id, confirmed => 1, @@ -145,7 +146,7 @@ sub update_comments { } ); for my $alert (@alerts) { - my $alerts_sent = FixMyStreet::App->model('DB::AlertSent')->find_or_create( { + my $alerts_sent = $self->schema->resultset('AlertSent')->find_or_create( { alert_id => $alert->id, parameter => $comment->id, } ); diff --git a/perllib/Open311/GetUpdates.pm b/perllib/Open311/GetUpdates.pm index bc55086f0..901e78809 100644 --- a/perllib/Open311/GetUpdates.pm +++ b/perllib/Open311/GetUpdates.pm @@ -1,8 +1,8 @@ package Open311::GetUpdates; -use Moose; +use Moo; use Open311; -use FixMyStreet::App; +use FixMyStreet::Cobrand; has body_list => ( is => 'ro' ); has system_user => ( is => 'ro' ); @@ -17,7 +17,7 @@ sub get_updates { api_key => $body->api_key ); - my $reports = FixMyStreet::App->model('DB::Problem')->to_body($body)->search( + my $reports = $body->result_source->schema->resultset('Problem')->to_body($body)->search( { state => { 'IN', [qw/confirmed fixed/] }, -and => [ @@ -62,8 +62,7 @@ sub update_reports { my $request_id = $request->{service_request_id}; - my $problem = - FixMyStreet::App->model('DB::Problem') + my $problem = $body->result_source->schema->resultset('Problem') ->search( { external_id => $request_id, } ); if (my $p = $problem->first) { diff --git a/perllib/Open311/PopulateServiceList.pm b/perllib/Open311/PopulateServiceList.pm index 5f45382e2..15a827217 100644 --- a/perllib/Open311/PopulateServiceList.pm +++ b/perllib/Open311/PopulateServiceList.pm @@ -1,21 +1,17 @@ package Open311::PopulateServiceList; -use Moose; -use LWP::Simple; -use XML::Simple; -use FixMyStreet::App; +use Moo; use Open311; has bodies => ( is => 'ro' ); has found_contacts => ( is => 'rw', default => sub { [] } ); has verbose => ( is => 'ro', default => 0 ); +has schema => ( is => 'ro', lazy => 1, default => sub { FixMyStreet::DB->connect } ); has _current_body => ( is => 'rw' ); has _current_open311 => ( is => 'rw' ); has _current_service => ( is => 'rw' ); -my $bodies = FixMyStreet::App->model('DB::Body'); - sub process_bodies { my $self = shift; @@ -43,7 +39,7 @@ sub process_body { unless ( $list && $list->{service} ) { if ($self->verbose >= 1) { my $id = $self->_current_body->id; - my $mapit_url = mySociety::Config::get('MAPIT_URL'); + my $mapit_url = FixMyStreet->config('MAPIT_URL'); my $areas = join( ",", keys %{$self->_current_body->areas} ); warn "Body $id for areas $areas - $mapit_url/areas/$areas.html - did not return a service list\n"; warn $open311->error; @@ -93,7 +89,7 @@ sub process_service { $self->_current_service->{service_name}; print $self->_current_service->{service_code} . ': ' . $category . "\n" if $self->verbose >= 2; - my $contacts = FixMyStreet::App->model( 'DB::Contact')->search( + my $contacts = $self->schema->resultset('Contact')->search( { body_id => $self->_current_body->id, -OR => [ @@ -173,7 +169,7 @@ sub _create_contact { my $contact; eval { - $contact = FixMyStreet::App->model( 'DB::Contact')->create( + $contact = $self->schema->resultset('Contact')->create( { email => $self->_current_service->{service_code}, body_id => $self->_current_body->id, @@ -287,7 +283,7 @@ sub _normalize_service_name { sub _delete_contacts_not_in_service_list { my $self = shift; - my $found_contacts = FixMyStreet::App->model( 'DB::Contact')->search( + my $found_contacts = $self->schema->resultset('Contact')->search( { email => { -not_in => $self->found_contacts }, body_id => $self->_current_body->id, diff --git a/perllib/Utils/Photo.pm b/perllib/Utils/Photo.pm new file mode 100644 index 000000000..a1af90fe1 --- /dev/null +++ b/perllib/Utils/Photo.pm @@ -0,0 +1,41 @@ +package Utils::Photo; + +use Image::Size; + +=head2 get_photo_params + +Returns a hashref of details of any attached photo for use in templates. +Hashref contains height, width and url keys. + +=cut + +sub get_photo_params { + my ($self, $key) = @_; + + return {} unless $self->photo; + + $key = ($key eq 'id') ? '' : "/$key"; + + my $pre = "/photo$key/" . $self->id; + my $post = '.jpeg'; + my $photo = {}; + + if (length($self->photo) == 40) { + $post .= '?' . $self->photo; + $photo->{url_full} = "$pre.full$post"; + # XXX Can't use size here because {url} (currently 250px height) may be + # being used, but at this point it doesn't yet exist to find the width + # $str = FixMyStreet->config('UPLOAD_DIR') . $self->photo . '.jpeg'; + } else { + my $str = \$self->photo; + ( $photo->{width}, $photo->{height} ) = Image::Size::imgsize( $str ); + } + + $photo->{url} = "$pre$post"; + $photo->{url_tn} = "$pre.tn$post"; + $photo->{url_fp} = "$pre.fp$post"; + + return $photo; +} + +1; diff --git a/t/00-check-we-are-staging.t b/t/00-check-we-are-staging.t index 4c9a255fe..072b5df76 100644 --- a/t/00-check-we-are-staging.t +++ b/t/00-check-we-are-staging.t @@ -5,21 +5,12 @@ use Test::More; use FixMyStreet; -# check that all the fields listed in general-example are also present in -# general - helps prevent later test failures due to un-noticed additions to the -# config file. - -# This code will bail_out to prevent the test suite proceeding to save time if -# issues are found. - -# load the config file and store the contents in a readonly hash - -my $CONF_FILE = $ENV{FMS_OVERRIDE_CONFIG} || 'general'; -mySociety::Config::set_file( FixMyStreet->path_to("conf/${CONF_FILE}") ); +# check that we are running on staging BAIL_OUT( "Test suite modifies databases so should not be run on live servers" ) - unless mySociety::Config::get('STAGING_SITE', undef); + unless FixMyStreet->config('STAGING_SITE'); -ok mySociety::Config::get('STAGING_SITE', undef), 'staging server'; +my $staging = FixMyStreet->config('STAGING_SITE'); +ok $staging, 'staging server'; done_testing(); diff --git a/t/app/controller/admin.t b/t/app/controller/admin.t index e2dd5df19..96565e82b 100644 --- a/t/app/controller/admin.t +++ b/t/app/controller/admin.t @@ -1240,9 +1240,7 @@ subtest "Check admin_base_url" => sub { my $rs = FixMyStreet::App->model('DB::Problem'); my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($report->cobrand)->new(); - is (FixMyStreet::App->model('DB::Problem')->get_admin_url( - $cobrand, - $report), + is ($report->admin_url($cobrand), (sprintf 'https://secure.mysociety.org/admin/bci/report_edit/%d', $report_id), 'get_admin_url OK'); }; diff --git a/t/app/controller/rss.t b/t/app/controller/rss.t index db653c094..3e820cff3 100644 --- a/t/app/controller/rss.t +++ b/t/app/controller/rss.t @@ -181,7 +181,7 @@ my $report_to_county_council = FixMyStreet::App->model('DB::Problem')->find_or_c subtest "check RSS feeds on cobrand have correct URLs for non-cobrand reports" => sub { $mech->host('hart.fixmystreet.com'); - my $expected1 = mySociety::Config::get('BASE_URL') . '/report/' . $report_to_county_council->id; + my $expected1 = FixMyStreet->config('BASE_URL') . '/report/' . $report_to_county_council->id; my $expected2; FixMyStreet::override_config { diff --git a/t/app/model/alert_type.t b/t/app/model/alert_type.t index 2620dd68c..0130f404e 100644 --- a/t/app/model/alert_type.t +++ b/t/app/model/alert_type.t @@ -9,21 +9,21 @@ my $mech = FixMyStreet::TestMech->new(); # this is the easiest way to make sure we're not going # to get any emails sent by data kicking about in the database -FixMyStreet::App->model('DB::AlertType')->email_alerts(); +FixMyStreet::DB->resultset('AlertType')->email_alerts(); $mech->clear_emails_ok; my $user = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); ok $user, "created test user"; my $user2 = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'commenter@example.com', name => 'Commenter' } ); ok $user2, "created comment user"; my $user3 = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'bystander@example.com', name => 'Bystander' } ); ok $user3, "created bystander"; @@ -36,7 +36,7 @@ my $dt = DateTime->new( second => 23 ); -my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'SW1A 1AA', bodies_str => '2504', @@ -62,7 +62,7 @@ my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( my $report_id = $report->id; ok $report, "created test report - $report_id"; -my $comment = FixMyStreet::App->model('DB::Comment')->find_or_create( +my $comment = FixMyStreet::DB->resultset('Comment')->find_or_create( { problem_id => $report_id, user_id => $user2->id, @@ -74,7 +74,7 @@ my $comment = FixMyStreet::App->model('DB::Comment')->find_or_create( anonymous => 'f', } ); -my $comment2 = FixMyStreet::App->model('DB::Comment')->find_or_create( +my $comment2 = FixMyStreet::DB->resultset('Comment')->find_or_create( { problem_id => $report_id, user_id => $user2->id, @@ -90,7 +90,7 @@ my $comment2 = FixMyStreet::App->model('DB::Comment')->find_or_create( $comment->confirmed( \"current_timestamp - '3 days'::interval" ); $comment->update; -my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $alert = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user, parameter => $report_id, @@ -101,7 +101,7 @@ my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create( } ); -my $alert3 = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $alert3 = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user3, parameter => $report_id, @@ -129,7 +129,7 @@ for my $test ( subtest "correct summary for state of $test->{state}" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => [ $alert->id, $alert3->id ], parameter => $comment->id, @@ -139,7 +139,7 @@ for my $test ( $report->state( $test->{state} ); $report->update; - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); $mech->email_count_is( 2 ); my @emails = $mech->get_email; @@ -167,7 +167,7 @@ my $now = DateTime->now(); $report->confirmed( $now->ymd . ' ' . $now->hms ); $report->update(); -my $council_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $council_alert = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user2, parameter => 2504, @@ -181,7 +181,7 @@ my $council_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( subtest "correct text for title after URL" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $council_alert->id, parameter => $report->id, @@ -190,7 +190,7 @@ subtest "correct text for title after URL" => sub { FixMyStreet::override_config { MAPIT_URL => 'http://mapit.mysociety.org/', }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; my $email = $mech->get_email; @@ -304,7 +304,7 @@ foreach my $test ( subtest "correct Nearest Road text with $test->{desc}" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $council_alert->id, parameter => $report->id, @@ -327,7 +327,7 @@ foreach my $test ( FixMyStreet::override_config { MAPIT_URL => 'http://mapit.mysociety.org/', }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; my $email = $mech->get_email; @@ -341,7 +341,7 @@ foreach my $test ( }; } -my $ward_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $ward_alert = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user, parameter => 7117, @@ -352,7 +352,7 @@ my $ward_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( } ); -my $report_to_council = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report_to_council = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'WS13 6YY', bodies_str => '2434', @@ -376,7 +376,7 @@ my $report_to_council = FixMyStreet::App->model('DB::Problem')->find_or_create( } ); -my $report_to_county_council = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report_to_county_council = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'WS13 6YY', bodies_str => '2240', @@ -400,7 +400,7 @@ my $report_to_county_council = FixMyStreet::App->model('DB::Problem')->find_or_c } ); -my $report_outside_district = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report_outside_district = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'WS13 6YY', bodies_str => '2221', @@ -427,7 +427,7 @@ my $report_outside_district = FixMyStreet::App->model('DB::Problem')->find_or_cr subtest "check alerts from cobrand send main site url for alerts for different council" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $ward_alert->id, } @@ -436,14 +436,14 @@ subtest "check alerts from cobrand send main site url for alerts for different c FixMyStreet::override_config { MAPIT_URL => 'http://mapit.mysociety.org/', }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; my $email = $mech->get_email; my $body = $email->body; - my $expected1 = mySociety::Config::get('BASE_URL') . '/report/' . $report_to_county_council->id; - my $expected3 = mySociety::Config::get('BASE_URL') . '/report/' . $report_outside_district->id; + my $expected1 = FixMyStreet->config('BASE_URL') . '/report/' . $report_to_county_council->id; + my $expected3 = FixMyStreet->config('BASE_URL') . '/report/' . $report_outside_district->id; my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('hart')->new(); my $expected2 = $cobrand->base_url . '/report/' . $report_to_council->id; @@ -453,7 +453,7 @@ subtest "check alerts from cobrand send main site url for alerts for different c }; -my $local_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( +my $local_alert = FixMyStreet::DB->resultset('Alert')->find_or_create( { user => $user, parameter => -1.731322, @@ -468,18 +468,18 @@ my $local_alert = FixMyStreet::App->model('DB::Alert')->find_or_create( subtest "check local alerts from cobrand send main site url for alerts for different council" => sub { $mech->clear_emails_ok; - my $sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $local_alert->id, } )->delete; - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); my $email = $mech->get_email; my $body = $email->body; - my $expected1 = mySociety::Config::get('BASE_URL') . '/report/' . $report_to_county_council->id; + my $expected1 = FixMyStreet->config('BASE_URL') . '/report/' . $report_to_county_council->id; my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('hart')->new(); my $expected2 = $cobrand->base_url . '/report/' . $report_to_council->id; @@ -494,7 +494,7 @@ subtest "correct i18n-ed summary for state of closed" => sub { $report->update( { state => 'closed' } ); $alert->update( { lang => 'nb', cobrand => 'fiksgatami' } ); - FixMyStreet::App->model('DB::AlertSent')->search( { + FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => $alert->id, parameter => $comment->id, } )->delete; @@ -502,7 +502,7 @@ subtest "correct i18n-ed summary for state of closed" => sub { FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fiksgatami' ], }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; $mech->email_count_is( 1 ); diff --git a/t/app/model/comment.t b/t/app/model/comment.t index 3141af828..ae93824a7 100644 --- a/t/app/model/comment.t +++ b/t/app/model/comment.t @@ -6,9 +6,9 @@ use warnings; use Test::More tests => 2; use FixMyStreet; -use FixMyStreet::App; +use FixMyStreet::DB; -my $comment_rs = FixMyStreet::App->model('DB::Comment'); +my $comment_rs = FixMyStreet::DB->resultset('Comment'); my $comment = $comment_rs->new( { diff --git a/t/app/model/extra.t b/t/app/model/extra.t index 21c37336e..52e2d839c 100644 --- a/t/app/model/extra.t +++ b/t/app/model/extra.t @@ -3,13 +3,11 @@ use warnings; use Test::More; use utf8; -use FixMyStreet::App; +use FixMyStreet::DB; use Data::Dumper; use DateTime; -my $c = FixMyStreet::App->new; - -my $db = FixMyStreet::App->model('DB')->schema; +my $db = FixMyStreet::DB->connect; $db->txn_begin; my $body = $db->resultset('Body')->create({ name => 'ExtraTestingBody' }); diff --git a/t/app/model/moderation.t b/t/app/model/moderation.t index cdc9a91b0..8fa333db4 100644 --- a/t/app/model/moderation.t +++ b/t/app/model/moderation.t @@ -4,17 +4,17 @@ use Test::More; use Test::Exception; use utf8; -use FixMyStreet::App; +use FixMyStreet::DB; use Data::Dumper; use DateTime; my $dt = DateTime->now; -my $user = FixMyStreet::App->model('DB::User')->find_or_create({ +my $user = FixMyStreet::DB->resultset('User')->find_or_create({ name => 'Bob', email => 'bob@example.com', }); sub get_report_and_original_data { - my $report = FixMyStreet::App->model('DB::Problem')->create( + my $report = FixMyStreet::DB->resultset('Problem')->create( { postcode => 'BR1 3SB', bodies_str => '', diff --git a/t/app/model/photoset.t b/t/app/model/photoset.t index 9e566f873..938c77b2a 100644 --- a/t/app/model/photoset.t +++ b/t/app/model/photoset.t @@ -4,7 +4,7 @@ use Test::More; use Test::Exception; use utf8; -use FixMyStreet::App; +use FixMyStreet::DB; use Data::Dumper; use DateTime; use Path::Tiny 'path'; @@ -12,19 +12,22 @@ use File::Temp 'tempdir'; my $dt = DateTime->now; -my $c = FixMyStreet::App->new; my $UPLOAD_DIR = tempdir( CLEANUP => 1 ); -local $c->config->{UPLOAD_DIR} = $UPLOAD_DIR; -my $user = $c->model('DB::User')->find_or_create({ +my $db = FixMyStreet::DB->storage->schema; + +my $user = $db->resultset('User')->find_or_create({ name => 'Bob', email => 'bob@example.com', }); -my $image_path = path('t/app/controller/sample.jpg'); +FixMyStreet::override_config { + UPLOAD_DIR => $UPLOAD_DIR, +}, sub { -my $db = FixMyStreet::App->model('DB')->schema; $db->txn_begin; +my $image_path = path('t/app/controller/sample.jpg'); + sub make_report { my $photo_data = shift; return $db->resultset('Problem')->create({ @@ -54,23 +57,25 @@ sub make_report { subtest 'Photoset with photo inline in DB' => sub { my $report = make_report( $image_path->slurp ); - my $photoset = $report->get_photoset($c); + my $photoset = $report->get_photoset(); is $photoset->num_images, 1, 'Found just 1 image'; }; $image_path->copy( path( $UPLOAD_DIR, '0123456789012345678901234567890123456789.jpeg' ) ); subtest 'Photoset with 1 referenced photo' => sub { my $report = make_report( '0123456789012345678901234567890123456789' ); - my $photoset = $report->get_photoset($c); + my $photoset = $report->get_photoset(); is $photoset->num_images, 1, 'Found just 1 image'; }; subtest 'Photoset with 1 referenced photo' => sub { my $report = make_report( '0123456789012345678901234567890123456789,0123456789012345678901234567890123456789,0123456789012345678901234567890123456789' ); - my $photoset = $report->get_photoset($c); + my $photoset = $report->get_photoset(); is $photoset->num_images, 3, 'Found 3 images'; }; $db->txn_rollback; +}; + done_testing(); diff --git a/t/app/model/problem.t b/t/app/model/problem.t index 82569d72a..6f706a22a 100644 --- a/t/app/model/problem.t +++ b/t/app/model/problem.t @@ -7,13 +7,13 @@ use Test::More; use FixMyStreet::TestMech; use FixMyStreet; -use FixMyStreet::App; +use FixMyStreet::DB; use mySociety::Locale; use Sub::Override; mySociety::Locale::gettext_domain('FixMyStreet'); -my $problem_rs = FixMyStreet::App->model('DB::Problem'); +my $problem_rs = FixMyStreet::DB->resultset('Problem'); my $problem = $problem_rs->new( { @@ -147,7 +147,7 @@ for my $test ( }; } -my $user = FixMyStreet::App->model('DB::User')->find_or_create( +my $user = FixMyStreet::DB->resultset('User')->find_or_create( { email => 'system_user@example.com' } @@ -161,7 +161,7 @@ $problem->insert; my $tz_local = DateTime::TimeZone->new( name => 'local' ); -my $body = FixMyStreet::App->model('DB::Body')->new({ +my $body = FixMyStreet::DB->resultset('Body')->new({ name => 'Edinburgh City Council' }); @@ -521,7 +521,7 @@ foreach my $test ( { $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -541,7 +541,7 @@ foreach my $test ( { } ); FixMyStreet::override_config $override, sub { - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); }; $mech->email_count_is( $test->{ email_count } ); @@ -596,7 +596,7 @@ subtest 'check can set mutiple emails as a single contact' => sub { $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -615,7 +615,7 @@ subtest 'check can set mutiple emails as a single contact' => sub { } ); FixMyStreet::override_config $override, sub { - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); }; $mech->email_count_is(1); @@ -630,7 +630,7 @@ subtest 'check can turn on report sent email alerts' => sub { ); $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -648,7 +648,7 @@ subtest 'check can turn on report sent email alerts' => sub { send_fail_count => 0, } ); - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); $mech->email_count_is( 2 ); my @emails = $mech->get_email; @@ -675,7 +675,7 @@ subtest 'check can turn on report sent email alerts' => sub { subtest 'check iOS app store test reports not sent' => sub { $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -692,7 +692,7 @@ subtest 'check iOS app store test reports not sent' => sub { send_fail_count => 0, } ); - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); $mech->email_count_is( 0 ); @@ -704,7 +704,7 @@ subtest 'check iOS app store test reports not sent' => sub { subtest 'check reports from abuser not sent' => sub { $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->search( + $problem_rs->search( { whensent => undef } @@ -721,7 +721,7 @@ subtest 'check reports from abuser not sent' => sub { send_fail_count => 0, } ); - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); $mech->email_count_is( 1 ); @@ -734,10 +734,10 @@ subtest 'check reports from abuser not sent' => sub { whensent => undef, } ); - my $abuse = FixMyStreet::App->model('DB::Abuse')->create( { email => $problem->user->email } ); + my $abuse = FixMyStreet::DB->resultset('Abuse')->create( { email => $problem->user->email } ); $mech->clear_emails_ok; - FixMyStreet::App->model('DB::Problem')->send_reports(); + $problem_rs->send_reports(); $mech->email_count_is( 0 ); diff --git a/t/app/model/questionnaire.t b/t/app/model/questionnaire.t index 240d6d050..f82b071bd 100644 --- a/t/app/model/questionnaire.t +++ b/t/app/model/questionnaire.t @@ -8,9 +8,9 @@ use Test::More; use FixMyStreet; use FixMyStreet::TestMech; -my $user = FixMyStreet::App->model('DB::User')->find_or_create( { email => 'test@example.com' } ); +my $user = FixMyStreet::DB->resultset('User')->find_or_create( { email => 'test@example.com' } ); -my $problem = FixMyStreet::App->model('DB::Problem')->create( +my $problem = FixMyStreet::DB->resultset('Problem')->create( { postcode => 'EH99 1SP', latitude => 1, @@ -106,7 +106,7 @@ for my $test ( $mech->email_count_is(0); - FixMyStreet::App->model('DB::Questionnaire') + FixMyStreet::DB->resultset('Questionnaire') ->send_questionnaires( { site => 'fixmystreet' } ); $mech->email_count_is( $test->{send_email} ); diff --git a/t/app/model/token.t b/t/app/model/token.t index d72574bb1..c98cea395 100644 --- a/t/app/model/token.t +++ b/t/app/model/token.t @@ -6,7 +6,7 @@ use warnings; use Test::More; use FixMyStreet; -use FixMyStreet::App; +use FixMyStreet::DB; my %tests = ( nested_hash => { foo => 'bar', and => [ 'baz', 'bundy' ] }, @@ -14,7 +14,7 @@ my %tests = ( scalar => 123, ); -my $token_rs = FixMyStreet::App->model('DB::Token'); +my $token_rs = FixMyStreet::DB->resultset('Token'); foreach my $test_data_name ( sort keys %tests ) { my $test_data = $tests{$test_data_name}; diff --git a/t/app/sendreport/email.t b/t/app/sendreport/email.t index 65cd7bfa8..eacc6dcf6 100644 --- a/t/app/sendreport/email.t +++ b/t/app/sendreport/email.t @@ -6,7 +6,7 @@ use warnings; use Test::More; use FixMyStreet; -use FixMyStreet::App; +use FixMyStreet::DB; use FixMyStreet::SendReport::Email; use FixMyStreet::TestMech; use mySociety::Locale; @@ -17,7 +17,7 @@ my $e = FixMyStreet::SendReport::Email->new(); # area id 1000 my $params = { id => 1000, name => 'Council of the Thousand' }; -my $body = FixMyStreet::App->model('DB::Body')->find_or_create($params); +my $body = FixMyStreet::DB->resultset('Body')->find_or_create($params); ok $body, "found/created body"; my $contact = $mech->create_contact_ok( @@ -27,7 +27,7 @@ my $contact = $mech->create_contact_ok( note => '', ); -my $row = FixMyStreet::App->model('DB::Problem')->new( { +my $row = FixMyStreet::DB->resultset('Problem')->new( { bodies_str => '1000', category => 'category', cobrand => '', diff --git a/t/cobrand/bromley.t b/t/cobrand/bromley.t index fdded5606..91b34a289 100644 --- a/t/cobrand/bromley.t +++ b/t/cobrand/bromley.t @@ -21,7 +21,7 @@ my @reports = $mech->create_problems_for_body( 1, $body->id, 'Test', { my $report = $reports[0]; for my $update ('in progress', 'unable to fix') { - FixMyStreet::App->model('DB::Comment')->find_or_create( { + FixMyStreet::DB->resultset('Comment')->find_or_create( { problem_state => $update, problem_id => $report->id, user_id => $user->id, @@ -48,7 +48,7 @@ subtest 'testing special Open311 behaviour', sub { FixMyStreet::override_config { SEND_REPORTS_ON_STAGING => 1, }, sub { - FixMyStreet::App->model('DB::Problem')->send_reports(); + FixMyStreet::DB->resultset('Problem')->send_reports(); }; $report->discard_changes; ok $report->whensent, 'Report marked as sent'; diff --git a/t/cobrand/closest.t b/t/cobrand/closest.t index d06f7e9a0..43b36f608 100644 --- a/t/cobrand/closest.t +++ b/t/cobrand/closest.t @@ -4,7 +4,7 @@ use warnings; use Test::More; use mySociety::Locale; -use FixMyStreet::App; +use FixMyStreet::DB; use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; @@ -16,7 +16,7 @@ mySociety::Locale::gettext_domain( 'FixMyStreet' ); my $c = FixMyStreet::Cobrand::UK->new(); my $user = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); ok $user, "created test user"; @@ -29,7 +29,7 @@ my $dt = DateTime->new( second => 23 ); -my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'SW1A 1AA', bodies_str => '2504', diff --git a/t/cobrand/fixamingata.t b/t/cobrand/fixamingata.t index d181d3890..65236e6e1 100644 --- a/t/cobrand/fixamingata.t +++ b/t/cobrand/fixamingata.t @@ -43,7 +43,7 @@ $mech->email_count_is(0); FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fixamingata' ], }, sub { - FixMyStreet::App->model('DB::Problem')->send_reports(); + FixMyStreet::DB->resultset('Problem')->send_reports(); }; my $email = $mech->get_email; like $email->header('Content-Type'), qr/iso-8859-1/, 'encoding looks okay'; @@ -53,16 +53,16 @@ like $email->body, qr/V=E4nligen,/, 'signature looks correct'; $mech->clear_emails_ok; my $user = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); ok $user, "created test user"; my $user2 = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'commenter@example.com', name => 'Commenter' } ); ok $user2, "created comment user"; -my $comment = FixMyStreet::App->model('DB::Comment')->find_or_create({ +my $comment = FixMyStreet::DB->resultset('Comment')->find_or_create({ problem_id => $report->id, user_id => $user2->id, name => 'Other User', @@ -74,7 +74,7 @@ my $comment = FixMyStreet::App->model('DB::Comment')->find_or_create({ $comment->confirmed( \"current_timestamp - '3 days'::interval" ); $comment->update; -my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create({ +my $alert = FixMyStreet::DB->resultset('Alert')->find_or_create({ user => $user, parameter => $report->id, alert_type => 'new_updates', @@ -86,7 +86,7 @@ my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create({ FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fixamingata' ], }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; $mech->email_count_is(1); diff --git a/t/cobrand/fixmybarangay.t b/t/cobrand/fixmybarangay.t index 316739dfa..00e792341 100644 --- a/t/cobrand/fixmybarangay.t +++ b/t/cobrand/fixmybarangay.t @@ -30,8 +30,8 @@ $bsn->update( { send_method => 'Email' } ); my $dps = $mech->create_body_ok( 3, 'DPS', id => 3 ); $dps->update( { send_method => 'Open311', endpoint => 'http://dps.endpoint.example.com', jurisdiction => 'FMB', api_key => 'test' } ); -FixMyStreet::App->model('DB::BodyArea')->find_or_create({ area_id => 1, body_id => $dps->id }); -FixMyStreet::App->model('DB::BodyArea')->find_or_create({ area_id => 2, body_id => $dps->id }); +FixMyStreet::DB->resultset('BodyArea')->find_or_create({ area_id => 1, body_id => $dps->id }); +FixMyStreet::DB->resultset('BodyArea')->find_or_create({ area_id => 2, body_id => $dps->id }); # Create contacts for these bodies # TODO: log in as a Bgy user, and create a report using the front end, @@ -72,7 +72,7 @@ $mech->email_count_is(0); FixMyStreet::override_config { SEND_REPORTS_ON_STAGING => 1, }, sub { - FixMyStreet::App->model('DB::Problem')->send_reports('fixmybarangay'); + FixMyStreet::DB->resultset('Problem')->send_reports('fixmybarangay'); }; # Check BGY one sent by email @@ -89,10 +89,10 @@ is $dps_report->send_method_used, 'Open311', 'DPS report sent via Open311'; is $dps_report->external_id, 248, 'DPS report has right external ID'; my $fmb_test_email = 'luz_test_user@example.com'; -my $user = FixMyStreet::App->model('DB::User')->find_or_create( { email => $fmb_test_email, from_body => $luz->id, password => 'fmbsecret' } ); +my $user = FixMyStreet::DB->resultset('User')->find_or_create( { email => $fmb_test_email, from_body => $luz->id, password => 'fmbsecret' } ); ok $user, "test user does exist"; -my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create({ +my $alert = FixMyStreet::DB->resultset('Alert')->find_or_create({ user => $user, parameter => '-0.142497580865087', parameter2 => '51.5016605453401', @@ -105,7 +105,7 @@ my $alert = FixMyStreet::App->model('DB::Alert')->find_or_create({ FixMyStreet::override_config { ALLOWED_COBRANDS => [ 'fixmybarangay' ], }, sub { - FixMyStreet::App->model('DB::AlertType')->email_alerts(); + FixMyStreet::DB->resultset('AlertType')->email_alerts(); }; $mech->email_count_is(1); diff --git a/t/cobrand/get_body_sender.t b/t/cobrand/get_body_sender.t index a9ba49479..66cfc02b7 100644 --- a/t/cobrand/get_body_sender.t +++ b/t/cobrand/get_body_sender.t @@ -4,7 +4,7 @@ use warnings; use Test::More; use mySociety::Locale; -use FixMyStreet::App; +use FixMyStreet::DB; use_ok 'FixMyStreet::Cobrand'; @@ -12,10 +12,10 @@ mySociety::Locale::gettext_domain( 'FixMyStreet' ); my $c = FixMyStreet::Cobrand::FixMyStreet->new(); -FixMyStreet::App->model('DB::BodyArea')->search( { body_id => 1000 } )->delete; -FixMyStreet::App->model('DB::Body')->search( { name => 'Body of a Thousand' } )->delete; +FixMyStreet::DB->resultset('BodyArea')->search( { body_id => 1000 } )->delete; +FixMyStreet::DB->resultset('Body')->search( { name => 'Body of a Thousand' } )->delete; -my $body = FixMyStreet::App->model('DB::Body')->find_or_create({ +my $body = FixMyStreet::DB->resultset('Body')->find_or_create({ id => 1000, name => 'Body of a Thousand', }); diff --git a/t/cobrand/seesomething.t b/t/cobrand/seesomething.t index 57a8a11ed..09114f6f9 100644 --- a/t/cobrand/seesomething.t +++ b/t/cobrand/seesomething.t @@ -10,7 +10,7 @@ use FixMyStreet::TestMech; my $EMAIL = 'seesomething@example.com'; my $mech = FixMyStreet::TestMech->new; -my $db = FixMyStreet::App->model('DB')->schema; +my $db = FixMyStreet::DB->storage->schema; my $dt_parser = $db->storage->datetime_parser; $db->txn_begin; @@ -32,7 +32,7 @@ $user->update({ from_body => $body }); my $date = $dt_parser->format_datetime(DateTime->now); -my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( { +my $report = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'EH1 1BB', bodies_str => '2520', areas => ',2520,', diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t index 721ee547c..b686f78ae 100644 --- a/t/cobrand/zurich.t +++ b/t/cobrand/zurich.t @@ -19,9 +19,7 @@ use Path::Tiny; # commonlib/bin/gettext-makemo FixMyStreet use FixMyStreet; -my $c = FixMyStreet::App->new(); -my $cobrand = FixMyStreet::Cobrand::Zurich->new({ c => $c }); -$c->stash->{cobrand} = $cobrand; +my $cobrand = FixMyStreet::Cobrand::Zurich->new(); my $sample_file = path(__FILE__)->parent->parent->child("app/controller/sample.jpg"); ok $sample_file->exists, "sample file $sample_file exists"; @@ -37,7 +35,7 @@ sub send_reports_for_zurich { ALLOWED_COBRANDS => ['zurich'] }, sub { # Actually send the report - $c->model('DB::Problem')->send_reports('zurich'); + FixMyStreet::DB->resultset('Problem')->send_reports('zurich'); }; } sub reset_report_state { @@ -195,7 +193,7 @@ subtest "changing of categories" => sub { sub get_moderated_count { # my %date_params = ( ); - # my $moderated = FixMyStreet::App->model('DB::Problem')->search({ + # my $moderated = FixMyStreet::DB->resultset('Problem')->search({ # extra => { like => '%moderated_overdue,I1:0%' }, %date_params } )->count; # return $moderated; @@ -848,7 +846,7 @@ subtest "test stats" => sub { subtest "test admin_log" => sub { diag $report->id; - my @entries = FixMyStreet::App->model('DB::AdminLog')->search({ + my @entries = FixMyStreet::DB->resultset('AdminLog')->search({ object_type => 'problem', object_id => $report->id, }); @@ -868,7 +866,6 @@ subtest 'email images to external partners' => sub { my $photo = path(__FILE__)->parent->child('zurich-logo_portal.x.jpg')->slurp_raw; my $photoset = FixMyStreet::App::Model::PhotoSet->new({ - c => $c, data_items => [ $photo ], }); my $fileid = $photoset->data; diff --git a/t/map/tilma/original.t b/t/map/tilma/original.t index 9e296686d..4ddeed8e0 100644 --- a/t/map/tilma/original.t +++ b/t/map/tilma/original.t @@ -3,7 +3,7 @@ use strict; use warnings; use Test::More; -use FixMyStreet::App; +use FixMyStreet::DB; use FixMyStreet::Map; use FixMyStreet::TestMech; use DateTime; @@ -20,14 +20,14 @@ my $c = ctx_request('http://fixmystreet.com/test?bbox=-7.6,49.7,-7.5,49.8'); $mech->delete_user('test@example.com'); my $user = - FixMyStreet::App->model('DB::User') + FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); ok $user, "created test user"; my $dt = DateTime->now(); -my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( +my $report = FixMyStreet::DB->resultset('Problem')->find_or_create( { postcode => 'SW1A 1AA', bodies_str => '2504', diff --git a/t/open311.t b/t/open311.t index 15bebe2fc..6333355e8 100644 --- a/t/open311.t +++ b/t/open311.t @@ -5,7 +5,7 @@ use strict; use warnings; use Test::More; use Test::Warn; -use FixMyStreet::App; +use FixMyStreet::DB; use CGI::Simple; use HTTP::Response; use DateTime; @@ -26,9 +26,9 @@ is $o->_process_error( '503 - service unavailable' ), 'unknown error', 'error te my $o2 = Open311->new( endpoint => 'http://192.168.50.1/open311/', jurisdiction => 'example.org' ); -my $u = FixMyStreet::App->model('DB::User')->new( { email => 'test@example.org', name => 'A User' } ); +my $u = FixMyStreet::DB->resultset('User')->new( { email => 'test@example.org', name => 'A User' } ); -my $p = FixMyStreet::App->model('DB::Problem')->new( { +my $p = FixMyStreet::DB->resultset('Problem')->new( { latitude => 1, longitude => 1, title => 'title', @@ -45,12 +45,12 @@ warning_like {$o2->send_service_request( $p, { url => 'http://example.com/' }, 1 my $dt = DateTime->now(); -my $user = FixMyStreet::App->model('DB::User')->new( { +my $user = FixMyStreet::DB->resultset('User')->new( { name => 'Test User', email => 'test@example.com', } ); -my $problem = FixMyStreet::App->model('DB::Problem')->new( { +my $problem = FixMyStreet::DB->resultset('Problem')->new( { id => 80, external_id => 81, state => 'confirmed', @@ -207,7 +207,7 @@ for my $test ( } -my $comment = FixMyStreet::App->model('DB::Comment')->new( { +my $comment = FixMyStreet::DB->resultset('Comment')->new( { id => 38362, user => $user, problem => $problem, @@ -394,7 +394,7 @@ for my $test ( my $dt2 = $dt->clone; $dt2->add( 'minutes' => 1 ); -my $comment2 = FixMyStreet::App->model('DB::Comment')->new( { +my $comment2 = FixMyStreet::DB->resultset('Comment')->new( { id => 38363, user => $user, problem => $problem, diff --git a/t/open311/endpoint/warwick.t b/t/open311/endpoint/warwick.t index b51c601f3..5c47cd529 100644 --- a/t/open311/endpoint/warwick.t +++ b/t/open311/endpoint/warwick.t @@ -7,7 +7,7 @@ use Test::MockTime ':all'; use Data::Dumper; use JSON; -use FixMyStreet::App; +use FixMyStreet::DB; use Module::Loaded; BEGIN { mark_as_loaded('DBD::Oracle') } @@ -141,16 +141,16 @@ subtest "End to end" => sub { my $WARWICKSHIRE_MAPIT_ID = 2243; - my $db = FixMyStreet::App->model('DB')->schema; + my $db = FixMyStreet::DB->connect; $db->txn_begin; - my $body = FixMyStreet::App->model('DB::Body')->find_or_create( { + my $body = FixMyStreet::DB->resultset('Body')->find_or_create( { id => $WARWICKSHIRE_MAPIT_ID, name => 'Warwickshire County Council', }); - my $user = FixMyStreet::App->model('DB::User') + my $user = FixMyStreet::DB->resultset('User') ->find_or_create( { email => 'test@example.com', name => 'Test User' } ); $body->update({ @@ -175,7 +175,7 @@ subtest "End to end" => sub { my $bodies = self_rs($body); - my $p = Open311::PopulateServiceList->new( bodies => $bodies, verbose => 0 ); + my $p = Open311::PopulateServiceList->new( bodies => $bodies, verbose => 0, schema => $db ); $p->process_bodies; is $body->contacts->count, 1, 'Categories imported from Open311'; @@ -183,7 +183,7 @@ subtest "End to end" => sub { set_fixed_time('2014-07-20T15:05:00Z'); - my $problem = FixMyStreet::App->model('DB::Problem')->create({ + my $problem = FixMyStreet::DB->resultset('Problem')->create({ postcode => 'WC1 1AA', bodies_str => $WARWICKSHIRE_MAPIT_ID, areas => ",$WARWICKSHIRE_MAPIT_ID,", @@ -219,7 +219,7 @@ subtest "End to end" => sub { # self_rs($problem)->send_reports; ## instead, as we are in a transaction, we'll just delete everything else. - my $rs = FixMyStreet::App->model('DB::Problem'); + my $rs = FixMyStreet::DB->resultset('Problem'); $rs->search({ id => { '!=', $problem->id } })->delete; $rs->send_reports; }; @@ -242,7 +242,7 @@ subtest "End to end" => sub { is $problem->state, 'confirmed', 'sanity check status'; - my $updates = Open311::GetServiceRequestUpdates->new( verbose => 1 ); + my $updates = Open311::GetServiceRequestUpdates->new( verbose => 1, schema => $db ); $updates->fetch; $problem->discard_changes; diff --git a/t/open311/getservicerequestupdates.t b/t/open311/getservicerequestupdates.t index 0ab5b232d..46c3feda4 100644 --- a/t/open311/getservicerequestupdates.t +++ b/t/open311/getservicerequestupdates.t @@ -10,17 +10,17 @@ use_ok( 'Open311' ); use_ok( 'Open311::GetServiceRequestUpdates' ); use DateTime; use DateTime::Format::W3CDTF; -use FixMyStreet::App; +use FixMyStreet::DB; -my $user = FixMyStreet::App->model('DB::User')->find_or_create( +my $user = FixMyStreet::DB->resultset('User')->find_or_create( { email => 'system_user@example.com' } ); my %bodies = ( - 2482 => FixMyStreet::App->model("DB::Body")->new({ id => 2482 }), - 2651 => FixMyStreet::App->model("DB::Body")->new({ id => 2651 }), + 2482 => FixMyStreet::DB->resultset("Body")->new({ id => 2482 }), + 2651 => FixMyStreet::DB->resultset("Body")->new({ id => 2651 }), ); my $requests_xml = qq{<?xml version="1.0" encoding="utf-8"?> @@ -104,7 +104,7 @@ subtest 'check extended request parsed correctly' => sub { }; -my $problem_rs = FixMyStreet::App->model('DB::Problem'); +my $problem_rs = FixMyStreet::DB->resultset('Problem'); my $problem = $problem_rs->new( { postcode => 'EH99 1SP', @@ -353,7 +353,7 @@ for my $test ( is $problem->comments->count, 1, 'comment count'; $problem->discard_changes; - my $c = FixMyStreet::App->model('DB::Comment')->search( { external_id => $test->{external_id} } )->first; + my $c = FixMyStreet::DB->resultset('Comment')->search( { external_id => $test->{external_id} } )->first; ok $c, 'comment exists'; is $c->text, $test->{description}, 'text correct'; is $c->mark_fixed, $test->{mark_fixed}, 'mark_closed correct'; @@ -527,7 +527,7 @@ subtest 'check that existing comments are not duplicated' => sub { $problem->comments->delete; - my $comment = FixMyStreet::App->model('DB::Comment')->new( + my $comment = FixMyStreet::DB->resultset('Comment')->new( { problem => $problem, external_id => 638344, @@ -660,7 +660,7 @@ foreach my $test ( { $problem->update; my @alerts = map { - my $alert = FixMyStreet::App->model('DB::Alert')->create( { + my $alert = FixMyStreet::DB->resultset('Alert')->create( { alert_type => 'new_updates', parameter => $problem->id, confirmed => 1, @@ -680,7 +680,7 @@ foreach my $test ( { $update->update_comments( $o, $bodies{2482} ); $problem->discard_changes; - my $alerts_sent = FixMyStreet::App->model('DB::AlertSent')->search( + my $alerts_sent = FixMyStreet::DB->resultset('AlertSent')->search( { alert_id => [ map $_->id, @alerts ], parameter => $problem->comments->first->id, diff --git a/t/open311/getupdates.t b/t/open311/getupdates.t index 7dc7ff164..fef51e0e1 100644 --- a/t/open311/getupdates.t +++ b/t/open311/getupdates.t @@ -5,17 +5,18 @@ use warnings; use Test::More; use FixMyStreet; +use FixMyStreet::DB; use_ok( 'Open311::GetUpdates' ); use_ok( 'Open311' ); -my $user = FixMyStreet::App->model('DB::User')->find_or_create( +my $user = FixMyStreet::DB->resultset('User')->find_or_create( { email => 'system_user@example.com' } ); -my $body = FixMyStreet::App->model('DB::Body')->new( { +my $body = FixMyStreet::DB->resultset('Body')->new( { name => 'Test Body', } ); @@ -42,7 +43,7 @@ UPDATED_DATETIME </service_requests> }; -my $problem_rs = FixMyStreet::App->model('DB::Problem'); +my $problem_rs = FixMyStreet::DB->resultset('Problem'); my $problem = $problem_rs->new( { postcode => 'EH99 1SP', diff --git a/t/open311/populate-service-list.t b/t/open311/populate-service-list.t index 1574732fb..f001926d2 100644 --- a/t/open311/populate-service-list.t +++ b/t/open311/populate-service-list.t @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use FixMyStreet::App; +use FixMyStreet::DB; use_ok( 'Open311::PopulateServiceList' ); use_ok( 'Open311' ); @@ -13,7 +13,7 @@ use_ok( 'Open311' ); my $processor = Open311::PopulateServiceList->new(); ok $processor, 'created object'; -my $body = FixMyStreet::App->model('DB::Body')->find_or_create( { +my $body = FixMyStreet::DB->resultset('Body')->find_or_create( { id => 1, name => 'Body Numero Uno', } ); @@ -22,7 +22,7 @@ $body->body_areas->find_or_create({ } ); my $BROMLEY = 'Bromley Council'; -my $bromley = FixMyStreet::App->model('DB::Body')->find_or_create( { +my $bromley = FixMyStreet::DB->resultset('Body')->find_or_create( { id => 2482, name => $BROMLEY, } ); @@ -32,7 +32,7 @@ $bromley->body_areas->find_or_create({ } ); subtest 'check basic functionality' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); my $service_list = get_xml_simple_object( get_standard_xml() ); @@ -40,14 +40,14 @@ subtest 'check basic functionality' => sub { $processor->_current_body( $body ); $processor->process_services( $service_list ); - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 3, 'correct number of contacts'; }; subtest 'check non open311 contacts marked as deleted' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); - my $contact = FixMyStreet::App->model('DB::Contact')->create( + my $contact = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => 'contact@example.com', @@ -66,17 +66,17 @@ subtest 'check non open311 contacts marked as deleted' => sub { $processor->_current_body( $body ); $processor->process_services( $service_list ); - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 4, 'correct number of contacts'; - $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1, deleted => 1 } )->count(); + $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1, deleted => 1 } )->count(); is $contact_count, 1, 'correct number of deleted contacts'; }; subtest 'check email changed if matching category' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); - my $contact = FixMyStreet::App->model('DB::Contact')->create( + my $contact = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => '009', @@ -102,14 +102,14 @@ subtest 'check email changed if matching category' => sub { is $contact->confirmed, 1, 'contact still confirmed'; is $contact->deleted, 0, 'contact still not deleted'; - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 3, 'correct number of contacts'; }; subtest 'check category name changed if updated' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); - my $contact = FixMyStreet::App->model('DB::Contact')->create( + my $contact = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => '001', @@ -136,14 +136,14 @@ subtest 'check category name changed if updated' => sub { is $contact->confirmed, 1, 'contact still confirmed'; is $contact->deleted, 0, 'contact still not deleted'; - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 3, 'correct number of contacts'; }; subtest 'check conflicting contacts not changed' => sub { - FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->delete(); + FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->delete(); - my $contact = FixMyStreet::App->model('DB::Contact')->create( + my $contact = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => 'existing@example.com', @@ -158,7 +158,7 @@ subtest 'check conflicting contacts not changed' => sub { ok $contact, 'contact created'; - my $contact2 = FixMyStreet::App->model('DB::Contact')->create( + my $contact2 = FixMyStreet::DB->resultset('Contact')->create( { body_id => 1, email => '001', @@ -191,7 +191,7 @@ subtest 'check conflicting contacts not changed' => sub { is $contact2->confirmed, 1, 'second contact contact still confirmed'; is $contact2->deleted, 0, 'second contact contact still not deleted'; - my $contact_count = FixMyStreet::App->model('DB::Contact')->search( { body_id => 1 } )->count(); + my $contact_count = FixMyStreet::DB->resultset('Contact')->search( { body_id => 1 } )->count(); is $contact_count, 4, 'correct number of contacts'; }; @@ -215,7 +215,7 @@ subtest 'check meta data population' => sub { </service_definition> '; - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { body_id => 1, email => '001', @@ -395,7 +395,7 @@ for my $test ( $services_xml =~ s/metadata>false/metadata>true/ms; } - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { body_id => 1, email => '100', @@ -470,7 +470,7 @@ subtest 'check attribute ordering' => sub { </service_definition> '; - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { body_id => 1, email => '001', @@ -572,7 +572,7 @@ subtest 'check bromely skip code' => sub { </service_definition> '; - my $contact = FixMyStreet::App->model('DB::Contact')->find_or_create( + my $contact = FixMyStreet::DB->resultset('Contact')->find_or_create( { body_id => 1, email => '001', |