aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Script
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/Script')
-rw-r--r--perllib/FixMyStreet/Script/Alerts.pm7
-rw-r--r--perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm10
-rw-r--r--perllib/FixMyStreet/Script/Inactive.pm7
-rw-r--r--perllib/FixMyStreet/Script/TfL/AutoClose.pm129
4 files changed, 143 insertions, 10 deletions
diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm
index cb1f022fa..fa90ede48 100644
--- a/perllib/FixMyStreet/Script/Alerts.pm
+++ b/perllib/FixMyStreet/Script/Alerts.pm
@@ -41,6 +41,7 @@ sub send() {
$item_table.photo as item_photo,
$item_table.problem_state as item_problem_state,
$item_table.cobrand as item_cobrand,
+ $item_table.extra as item_extra,
$head_table.*
from alert, $item_table, $head_table
where alert.parameter::integer = $head_table.id
@@ -307,6 +308,10 @@ sub _send_aggregated_alert_email(%) {
# Ignore phone-only users
return unless $data{alert_user}->email_verified;
+ # Mark user as active as they're being sent an alert
+ $data{alert_user}->set_last_active;
+ $data{alert_user}->update;
+
my $email = $data{alert_user}->email;
my ($domain) = $email =~ m{ @ (.*) \z }x;
return if $data{schema}->resultset('Abuse')->search( {
@@ -323,7 +328,7 @@ sub _send_aggregated_alert_email(%) {
} );
$data{unsubscribe_url} = $cobrand->base_url( $data{cobrand_data} ) . '/A/' . $token->token;
- my $sender = FixMyStreet::Email::unique_verp_id('alert', $data{alert_id});
+ my $sender = FixMyStreet::Email::unique_verp_id([ 'alert', $data{alert_id} ], $cobrand->call_hook('verp_email_domain'));
my $result = FixMyStreet::Email::send_cron(
$data{schema},
"$data{template}.txt",
diff --git a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm
index 7ba763515..7c183ecbc 100644
--- a/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm
+++ b/perllib/FixMyStreet/Script/ArchiveOldEnquiries.pm
@@ -141,7 +141,6 @@ sub close_problems {
my $problems = shift;
my $extra = { auto_closed_by_script => 1 };
- $extra->{is_superuser} = 1 if !$opts->{user_name};
my $cobrand;
while (my $problem = $problems->next) {
@@ -152,16 +151,9 @@ sub close_problems {
$cobrand->set_lang_and_domain($problem->lang, 1);
}
- my $timestamp = \'current_timestamp';
my $comment = $problem->add_to_comments( {
text => $opts->{closure_text} || '',
- created => $timestamp,
- confirmed => $timestamp,
- user_id => $opts->{user},
- name => $opts->{user_name} || _('an administrator'),
- mark_fixed => 0,
- anonymous => 0,
- state => 'confirmed',
+ user => FixMyStreet::DB->resultset("User")->find($opts->{user}),
problem_state => $opts->{closed_state},
extra => $extra,
} );
diff --git a/perllib/FixMyStreet/Script/Inactive.pm b/perllib/FixMyStreet/Script/Inactive.pm
index 8dd524ce1..6b3372a2b 100644
--- a/perllib/FixMyStreet/Script/Inactive.pm
+++ b/perllib/FixMyStreet/Script/Inactive.pm
@@ -158,8 +158,14 @@ sub delete_reports {
sub anonymize_users {
my $self = shift;
+ my $body_users = FixMyStreet::DB->resultset("Body")->search({
+ comment_user_id => { '!=' => undef },
+ }, {
+ columns => 'comment_user_id',
+ });
my $users = FixMyStreet::DB->resultset("User")->search({
last_active => { '<', interval($self->anonymize) },
+ id => { -not_in => $body_users->as_query },
email => { -not_like => 'removed-%@' . FixMyStreet->config('EMAIL_DOMAIN') },
});
@@ -179,6 +185,7 @@ sub email_inactive_users {
});
while (my $user = $users->next) {
next if $user->get_extra_metadata('inactive_email_sent');
+ next unless $user->email && $user->email_verified;
say "Emailing user #" . $user->id if $self->verbose;
next if $self->dry_run;
diff --git a/perllib/FixMyStreet/Script/TfL/AutoClose.pm b/perllib/FixMyStreet/Script/TfL/AutoClose.pm
new file mode 100644
index 000000000..687a29e7f
--- /dev/null
+++ b/perllib/FixMyStreet/Script/TfL/AutoClose.pm
@@ -0,0 +1,129 @@
+package FixMyStreet::Script::TfL::AutoClose;
+
+use v5.14;
+
+use Moo;
+use CronFns;
+use FixMyStreet;
+use FixMyStreet::Cobrand;
+use FixMyStreet::DB;
+use Types::Standard qw(InstanceOf Maybe);
+
+has commit => ( is => 'ro', default => 0 );
+
+has verbose => ( is => 'ro', default => 0 );
+
+has body => (
+ is => 'lazy',
+ isa => Maybe[InstanceOf['FixMyStreet::DB::Result::Body']],
+ default => sub {
+ my $body = FixMyStreet::DB->resultset('Body')->find({ name => 'TfL' });
+ return $body;
+ }
+);
+
+has days => (
+ is => 'ro',
+ default => 28
+);
+
+sub close {
+ my $self = shift;
+
+ die "Can't find body\n" unless $self->body;
+ warn "DRY RUN: use --commit to close reports\n" unless $self->commit;
+ my $categories = $self->categories;
+ $self->close_reports($categories);
+}
+
+has newest => (
+ is => 'lazy',
+ isa => InstanceOf['DateTime'],
+ default => sub {
+ my $self = shift;
+ my $days = $self->days * -1;
+ my $date = DateTime->now->add( days => $days )->truncate( to => 'day' );
+ return $date;
+ }
+);
+
+# get list of cateories that have a response template for the fixed
+# state marked as auto-response.
+sub categories {
+ my $self = shift;
+
+ my $templates = FixMyStreet::DB->resultset('ResponseTemplate')->search({
+ state => 'fixed - council',
+ auto_response => 1,
+ body_id => $self->body->id,
+ });
+
+ my %categories;
+ for my $template ( $templates->all ) {
+ map { $categories{$_->category} = $template; } $template->contacts->all;
+ }
+
+ return \%categories;
+}
+
+# find reports in relevant categories that have been set to action
+# scheduled for 30 days.
+sub close_reports {
+ my ($self, $categories) = @_;
+
+ my $dtf = FixMyStreet::DB->schema->storage->datetime_parser;
+
+ my $reports = FixMyStreet::DB->resultset('Problem')->search({
+ category => { -in => [ keys %$categories ] },
+ 'me.state' => 'action scheduled',
+ bodies_str => $self->body->id,
+ 'comments.state' => 'confirmed',
+ 'comments.problem_state' => 'action scheduled',
+ },
+ {
+ group_by => 'me.id',
+ join => [ 'comments' ],
+ having => \[ 'MIN(comments.confirmed) < ?', $dtf->format_datetime($self->newest) ]
+ });
+
+ my $count = 0;
+ for my $r ( $reports->all ) {
+ my $comments = FixMyStreet::DB->resultset('Comment')->search(
+ { problem_id => $r->id },
+ { order_by => 'confirmed' }
+ );
+ my $earliest;
+ while ( my $c = $comments->next ) {
+ if ( $c->problem_state ne 'action scheduled' ) {
+ $earliest = undef;
+ next;
+ }
+ $earliest = $c->confirmed unless defined $earliest;
+ }
+ next unless defined $earliest && $earliest < $self->newest;
+ if ($self->commit) {
+ $r->update({
+ state => 'fixed - council',
+ lastupdate => \'current_timestamp',
+ });
+ my $c = FixMyStreet::DB->resultset('Comment')->new(
+ {
+ problem => $r,
+ text => $categories->{$r->category}->text,
+ state => 'confirmed',
+ problem_state => 'fixed - council',
+ user => $self->body->comment_user,
+ confirmed => \'current_timestamp'
+ }
+ );
+ $c->insert;
+ }
+ $count++;
+ }
+
+ say "$count reports closed" if $self->verbose;
+
+ return 1;
+}
+
+1;