aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Moderate.pm
diff options
context:
space:
mode:
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Moderate.pm')
-rw-r--r--perllib/FixMyStreet/App/Controller/Moderate.pm120
1 files changed, 44 insertions, 76 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Moderate.pm b/perllib/FixMyStreet/App/Controller/Moderate.pm
index 90396fa07..154f09176 100644
--- a/perllib/FixMyStreet/App/Controller/Moderate.pm
+++ b/perllib/FixMyStreet/App/Controller/Moderate.pm
@@ -23,9 +23,9 @@ data to change.
- user to be from_body
- user to have a "moderate" record in user_body_permissions
-The original data of the report is stored in moderation_original_data, so
-that it can be reverted/consulted if required. All moderation events are
-stored in admin_log.
+The original and previous data of the report is stored in
+moderation_original_data, so that it can be reverted/consulted if required.
+All moderation events are stored in admin_log.
=head1 SEE ALSO
@@ -55,7 +55,7 @@ sub report : Chained('moderate') : PathPart('report') : CaptureArgs(1) {
$c->forward('/auth/check_csrf_token');
- my $original = $problem->find_or_new_related( moderation_original_data => {
+ $c->stash->{history} = $problem->new_related( moderation_original_data => {
title => $problem->title,
detail => $problem->detail,
photo => $problem->photo,
@@ -63,10 +63,10 @@ sub report : Chained('moderate') : PathPart('report') : CaptureArgs(1) {
longitude => $problem->longitude,
latitude => $problem->latitude,
category => $problem->category,
- extra => $problem->extra,
+ $problem->extra ? (extra => $problem->extra) : (),
});
+ $c->stash->{original} = $problem->moderation_original_data || $c->stash->{history};
$c->stash->{problem} = $problem;
- $c->stash->{problem_original} = $original;
$c->stash->{moderation_reason} = $c->get_param('moderation_reason') // '';
}
@@ -119,23 +119,32 @@ sub moderating_user_name {
return $user->from_body ? $user->from_body->name : _('an administrator');
}
-sub report_moderate_audit : Private {
- my ($self, $c, @types) = @_;
+sub moderate_log_entry : Private {
+ my ($self, $c, $object_type, @types) = @_;
my $user = $c->user->obj;
my $reason = $c->stash->{'moderation_reason'};
- my $problem = $c->stash->{problem} or die;
+ my $object = $object_type eq 'update' ? $c->stash->{comment} : $c->stash->{problem};
my $types_csv = join ', ' => @types;
+ # We attach the log to the moderation entry if present, or the object if not (hiding)
$c->model('DB::AdminLog')->create({
action => 'moderation',
user => $user,
admin_user => moderating_user_name($user),
- object_id => $problem->id,
- object_type => 'problem',
+ object_id => $c->stash->{history}->id || $object->id,
+ object_type => $c->stash->{history}->id ? 'moderation' : $object_type,
reason => (sprintf '%s (%s)', $reason, $types_csv),
});
+}
+
+sub report_moderate_audit : Private {
+ my ($self, $c, @types) = @_;
+
+ my $problem = $c->stash->{problem} or die;
+
+ $c->forward('moderate_log_entry', [ 'problem', @types ]);
if ($problem->user->email_verified && $c->cobrand->send_moderation_notifications) {
my $token = $c->model("DB::Token")->create({
@@ -143,6 +152,7 @@ sub report_moderate_audit : Private {
data => { id => $problem->id }
});
+ my $types_csv = join ', ' => @types;
$c->send_email( 'problem-moderated.txt', {
to => [ [ $problem->user->email, $problem->name ] ],
types => $types_csv,
@@ -150,6 +160,7 @@ sub report_moderate_audit : Private {
problem => $problem,
report_uri => $c->stash->{report_uri},
report_complain_uri => $c->stash->{cobrand_base} . '/contact?m=' . $token->token,
+ moderated_data => $c->stash->{history},
});
}
}
@@ -172,29 +183,22 @@ sub report_moderate_hide : Private {
sub moderate_text : Private {
my ($self, $c, $thing) = @_;
- my ($object, $original, $param);
+ my $object = $c->stash->{comment} || $c->stash->{problem};
+ my $param = $c->stash->{comment} ? 'update_' : 'problem_';
+
my $thing_for_original_table = $thing;
- if (my $comment = $c->stash->{comment}) {
- $object = $comment;
- $original = $c->stash->{comment_original};
- $param = 'update_';
- # Update 'text' field is stored in original table's 'detail' field
- $thing_for_original_table = 'detail' if $thing eq 'text';
- } else {
- $object = $c->stash->{problem};
- $original = $c->stash->{problem_original};
- $param = 'problem_';
- }
+ # Update 'text' field is stored in original table's 'detail' field
+ $thing_for_original_table = 'detail' if $c->stash->{comment} && $thing eq 'text';
my $old = $object->$thing;
- my $original_thing = $original->$thing_for_original_table;
+ my $original_thing = $c->stash->{original}->$thing_for_original_table;
my $new = $c->get_param($param . 'revert_' . $thing) ?
$original_thing
: $c->get_param($param . $thing);
if ($new ne $old) {
- $original->insert unless $original->in_storage;
+ $c->stash->{history}->insert;
$object->update({ $thing => $new });
return $thing_for_original_table;
}
@@ -205,18 +209,11 @@ sub moderate_text : Private {
sub moderate_boolean : Private {
my ( $self, $c, $thing, $reverse ) = @_;
- my ($object, $original, $param);
- if (my $comment = $c->stash->{comment}) {
- $object = $comment;
- $original = $c->stash->{comment_original};
- $param = 'update_';
- } else {
- $object = $c->stash->{problem};
- $original = $c->stash->{problem_original};
- $param = 'problem_';
- }
+ my $object = $c->stash->{comment} || $c->stash->{problem};
+ my $param = $c->stash->{comment} ? 'update_' : 'problem_';
+ my $original = $c->stash->{original}->photo;
- return if $thing eq 'photo' && !$original->photo;
+ return if $thing eq 'photo' && !$original;
my $new;
if ($reverse) {
@@ -227,9 +224,9 @@ sub moderate_boolean : Private {
my $old = $object->$thing ? 1 : 0;
if ($new != $old) {
- $original->insert unless $original->in_storage;
+ $c->stash->{history}->insert;
if ($thing eq 'photo') {
- $object->update({ $thing => $new ? $original->photo : undef });
+ $object->update({ $thing => $new ? $original : undef });
} else {
$object->update({ $thing => $new });
}
@@ -241,14 +238,7 @@ sub moderate_boolean : Private {
sub moderate_extra : Private {
my ($self, $c) = @_;
- my ($object, $original);
- if (my $comment = $c->stash->{comment}) {
- $object = $comment;
- $original = $c->stash->{comment_original};
- } else {
- $object = $c->stash->{problem};
- $original = $c->stash->{problem_original};
- }
+ my $object = $c->stash->{comment} || $c->stash->{problem};
my $changed;
my @extra = grep { /^extra\./ } keys %{$c->req->params};
@@ -257,12 +247,12 @@ sub moderate_extra : Private {
my $old = $object->get_extra_metadata($field_name) || '';
my $new = $c->get_param($_);
if ($new ne $old) {
- $original->insert unless $original->in_storage;
$object->set_extra_metadata($field_name, $new);
$changed = 1;
}
}
if ($changed) {
+ $c->stash->{history}->insert;
$object->update;
return 'extra';
}
@@ -272,7 +262,6 @@ sub moderate_location : Private {
my ($self, $c) = @_;
my $problem = $c->stash->{problem};
- my $original = $c->stash->{problem_original};
my $moved = $c->forward('/admin/report_edit_location', [ $problem ]);
if (!$moved) {
@@ -283,7 +272,7 @@ sub moderate_location : Private {
}
if ($moved == 2) {
- $original->insert unless $original->in_storage;
+ $c->stash->{history}->insert;
$problem->update;
return 'location';
}
@@ -299,12 +288,11 @@ sub moderate_category : Private {
$c->forward('/admin/categories_for_point');
my $problem = $c->stash->{problem};
- my $original = $c->stash->{problem_original};
my $changed = $c->forward( '/admin/report_edit_category', [ $problem, 1 ] );
# It might need to set_report_extras in future
if ($changed) {
- $original->insert unless $original->in_storage;
+ $c->stash->{history}->insert;
$problem->update;
return 'category';
}
@@ -317,14 +305,14 @@ sub update : Chained('report') : PathPart('update') : CaptureArgs(1) {
# Make sure user can moderate this update
$c->detach unless $comment && $c->user->can_moderate($comment);
- my $original = $comment->find_or_new_related( moderation_original_data => {
+ $c->stash->{history} = $comment->new_related( moderation_original_data => {
detail => $comment->text,
photo => $comment->photo,
anonymous => $comment->anonymous,
- extra => $comment->extra,
+ $comment->extra ? (extra => $comment->extra) : (),
});
$c->stash->{comment} = $comment;
- $c->stash->{comment_original} = $original;
+ $c->stash->{original} = $comment->moderation_original_data || $c->stash->{history};
}
sub moderate_update : Chained('update') : PathPart('') : Args(0) {
@@ -338,27 +326,7 @@ sub moderate_update : Chained('update') : PathPart('') : Args(0) {
$c->forward('moderate_extra'),
$c->forward('moderate_boolean', [ 'photo' ]);
- $c->detach( 'update_moderate_audit', \@types )
-}
-
-sub update_moderate_audit : Private {
- my ($self, $c, @types) = @_;
-
- my $user = $c->user->obj;
- my $reason = $c->stash->{'moderation_reason'};
- my $problem = $c->stash->{problem} or die;
- my $comment = $c->stash->{comment} or die;
-
- my $types_csv = join ', ' => @types;
-
- $c->model('DB::AdminLog')->create({
- action => 'moderation',
- user => $user,
- admin_user => moderating_user_name($user),
- object_id => $comment->id,
- object_type => 'update',
- reason => (sprintf '%s (%s)', $reason, $types_csv),
- });
+ $c->detach('moderate_log_entry', [ 'update', @types ]);
}
sub update_moderate_hide : Private {
@@ -369,7 +337,7 @@ sub update_moderate_hide : Private {
if ($c->get_param('update_hide')) {
$comment->hide;
- $c->detach( 'update_moderate_audit', ['hide'] ); # break chain here.
+ $c->detach('moderate_log_entry', [ 'update', 'hide' ]); # break chain here.
}
return;
}