diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-11-22 22:06:53 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-11-26 12:49:23 +0000 |
commit | c69891dffcb3810ebb951af4a563c556b2deeb0d (patch) | |
tree | 3ba5a477e0f5d92062c94c032c2f9c9a4feb3e86 /perllib | |
parent | eb2aba46eabc8d90656b760cf4900f56119de9ca (diff) |
Show moderation history in report/update admin.
Diffstat (limited to 'perllib')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Moderate.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm | 116 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 7 |
4 files changed, 132 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Moderate.pm b/perllib/FixMyStreet/App/Controller/Moderate.pm index a6eb59ca1..3ed2022ee 100644 --- a/perllib/FixMyStreet/App/Controller/Moderate.pm +++ b/perllib/FixMyStreet/App/Controller/Moderate.pm @@ -63,7 +63,7 @@ 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; @@ -299,7 +299,7 @@ sub update : Chained('report') : PathPart('update') : CaptureArgs(1) { detail => $comment->text, photo => $comment->photo, anonymous => $comment->anonymous, - extra => $comment->extra, + $comment->extra ? (extra => $comment->extra) : (), }); $c->stash->{comment} = $comment; $c->stash->{original} = $comment->moderation_original_data || $c->stash->{history}; diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index d3551956f..3a2b7d84b 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -197,6 +197,13 @@ __PACKAGE__->has_many( } ); +sub moderation_history { + my $self = shift; + return $self->moderation_original_datas->search({ + problem_id => $self->problem_id, + }, { order_by => { -desc => 'id' } })->all; +} + # This will return the oldest moderation_original_data, if any. # The plural can be used to return all entries. __PACKAGE__->might_have( diff --git a/perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm b/perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm index 7478eca87..42ff75295 100644 --- a/perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm +++ b/perllib/FixMyStreet/DB/Result/ModerationOriginalData.pm @@ -70,10 +70,126 @@ __PACKAGE__->belongs_to( # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:zFOhQnS4WfVzD7qHxaAr6w use Moo; +use Text::Diff; +use Data::Dumper; with 'FixMyStreet::Roles::Extra'; __PACKAGE__->load_components("+FixMyStreet::DB::RABXColumn"); __PACKAGE__->rabx_column('extra'); +sub compare_with { + my ($self, $other) = @_; + if ($self->comment_id) { + my $new_detail = $other->can('text') ? $other->text : $other->detail; + return { + detail => string_diff($self->detail, $new_detail), + photo => $self->compare_photo($other), + anonymous => $self->compare_anonymous($other), + extra => $self->compare_extra($other), + }; + } + return { + title => string_diff($self->title, $other->title), + detail => string_diff($self->detail, $other->detail), + photo => $self->compare_photo($other), + anonymous => $self->compare_anonymous($other), + coords => $self->compare_coords($other), + category => string_diff($self->category, $other->category, single => 1), + extra => $self->compare_extra($other), + } +} + +sub compare_anonymous { + my ($self, $other) = @_; + string_diff( + $self->anonymous ? _('Yes') : _('No'), + $other->anonymous ? _('Yes') : _('No'), + ); +} + +sub compare_coords { + my ($self, $other) = @_; + my $old = join ',', $self->latitude, $self->longitude; + my $new = join ',', $other->latitude, $other->longitude; + string_diff($old, $new, single => 1); +} + +sub compare_photo { + my ($self, $other) = @_; + + my $old = $self->photo || ''; + my $new = $other->photo || ''; + return '' if $old eq $new; + + $old = [ split /,/, $old ]; + $new = [ split /,/, $new ]; + + my $diff = Algorithm::Diff->new( $old, $new ); + my (@added, @deleted); + while ( $diff->Next ) { + next if $diff->Same; + push @deleted, $diff->Items(1); + push @added, $diff->Items(2); + } + return (join ', ', map { + "<del style='background-color:#fcc'>$_</del>"; + } @deleted) . (join ', ', map { + "<ins style='background-color:#cfc'>$_</ins>"; + } @added); +} + +sub compare_extra { + my ($self, $other) = @_; + + my $old = $self->get_extra_metadata; + my $new = $other->get_extra_metadata; + + my $both = { %$old, %$new }; + my @all_keys = sort keys %$both; + my @s; + foreach (@all_keys) { + if ($old->{$_} && $new->{$_}) { + push @s, string_diff("$_ = $old->{$_}", "$_ = $new->{$_}"); + } elsif ($new->{$_}) { + push @s, string_diff("", "$_ = $new->{$_}"); + } else { + push @s, string_diff("$_ = $old->{$_}", ""); + } + } + return join ', ', @s; +} + +sub string_diff { + my ($old, $new, %options) = @_; + + return '' if $old eq $new; + + $old = FixMyStreet::Template::html_filter($old); + $new = FixMyStreet::Template::html_filter($new); + + if ($options{single}) { + $old = [ $old ]; + $new = [ $new ]; + } + $old = [ split //, $old ] unless ref $old; + $new = [ split //, $new ] unless ref $new; + my $diff = Algorithm::Diff->new( $old, $new ); + my $string; + while ($diff->Next) { + my $d = $diff->Diff; + if ($d & 1) { + my $deleted = join '', $diff->Items(1); + $string .= "<del style='background-color:#fcc'>$deleted</del>"; + } + my $inserted = join '', $diff->Items(2); + if ($d & 2) { + $string .= "<ins style='background-color:#cfc'>$inserted</ins>"; + } else { + $string .= $inserted; + } + } + return $string; +} + 1; diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index ffc6aba93..a7884f7d2 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -973,6 +973,13 @@ sub latest_moderation_log_entry { return $self->admin_log_entries->search({ action => 'moderation' }, { order_by => { -desc => 'id' } })->first; } +sub moderation_history { + my $self = shift; + return $self->moderation_original_datas->search({ + comment_id => undef, + }, { order_by => { -desc => 'id' } })->all; +} + __PACKAGE__->has_many( "admin_log_entries", "FixMyStreet::DB::Result::AdminLog", |