diff options
Diffstat (limited to 'perllib/FixMyStreet/DB/Result/Comment.pm')
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 88 |
1 files changed, 77 insertions, 11 deletions
diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index b217bf96c..82476ba10 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -110,6 +110,61 @@ with 'FixMyStreet::Roles::Abuser', 'FixMyStreet::Roles::Moderation', 'FixMyStreet::Roles::PhotoSet'; +=head2 FOREIGNBUILDARGS + +Make sure that when creating a new Comment object, certain +other fields are set based upon the supplied data. + +=cut + +sub FOREIGNBUILDARGS { + my ($class, $opts) = @_; + + if (my $user = $opts->{user}) { + my $name; + if ($user->is_superuser) { + $opts->{extra}->{is_superuser} = 1; + $name = _('an administrator'); + } elsif (my $body = $user->from_body) { + $opts->{extra}->{is_body_user} = $body->id; + $name = $body->name; + $name = 'Island Roads' if $name eq 'Isle of Wight Council'; + } else { + $name = $user->name; + } + $opts->{name} //= $name; + } + + $opts->{anonymous} //= 0; + $opts->{mark_fixed} //= 0; + $opts->{state} //= 'confirmed'; # it's only public updates that need to be unconfirmed + if ($opts->{state} eq 'confirmed') { + $opts->{confirmed} //= \'current_timestamp'; + } + + return $opts; +}; + +=head2 around user + +Also make sure we catch the setting of a user on an object at a time other than +object creation, to set the extra field needed. + +=cut + +around user => sub { + my ( $orig, $self ) = ( shift, shift ); + my $res = $self->$orig(@_); + if (@_) { + if ($_[0]->is_superuser) { + $self->set_extra_metadata( is_superuser => 1 ); + } elsif (my $body = $_[0]->from_body) { + $self->set_extra_metadata( is_body_user => $body->id ); + } + } + return $res; +}; + =head2 get_cobrand_logged Get a cobrand object for the cobrand the update was made on. @@ -207,13 +262,19 @@ about an update. Can include HTML. =cut sub meta_line { - my ( $self, $c ) = @_; + my ( $self, $user ) = @_; + my $cobrand = $self->result_source->schema->cobrand; my $meta = ''; - if ($self->anonymous or !$self->name) { - $meta = sprintf( _( 'Posted anonymously at %s' ), Utils::prettify_dt( $self->confirmed ) ) - } elsif ($self->user->from_body || $self->get_extra_metadata('is_body_user') || $self->get_extra_metadata('is_superuser') ) { + my $contributed_as = $self->get_extra_metadata('contributed_as') || ''; + my $staff = $self->user->from_body || $self->get_extra_metadata('is_body_user') || $self->get_extra_metadata('is_superuser'); + my $anon = $self->anonymous || !$self->name; + + if ($anon && (!$staff || $contributed_as eq 'anonymous_user' || $contributed_as eq 'another_user')) { + $meta = $cobrand->call_hook(update_anonymous_message => $self); + $meta ||= sprintf( _( 'Posted anonymously at %s' ), Utils::prettify_dt( $self->confirmed ) ) + } elsif ($staff) { my $user_name = FixMyStreet::Template::html_filter($self->user->name); my $body; if ($self->get_extra_metadata('is_superuser')) { @@ -237,9 +298,9 @@ sub meta_line { $body = 'Island Roads'; } } - my $cobrand_always_view_body_user = $c->cobrand->call_hook("always_view_body_contribute_details"); + my $cobrand_always_view_body_user = $cobrand->call_hook("always_view_body_contribute_details"); my $can_view_contribute = $cobrand_always_view_body_user || - ($c->user_exists && $c->user->has_permission_to('view_body_contribute_details', $self->problem->bodies_str_ids)); + ($user && $user->has_permission_to('view_body_contribute_details', $self->problem->bodies_str_ids)); if ($self->text) { if ($can_view_contribute) { $meta = sprintf( _( 'Posted by <strong>%s</strong> (%s) at %s' ), $body, $user_name, Utils::prettify_dt( $self->confirmed ) ); @@ -268,16 +329,20 @@ sub problem_state_processed { my $self = shift; return 'fixed - user' if $self->mark_fixed; return 'confirmed' if $self->mark_open; - return $self->problem_state; + my $cobrand = $self->result_source->schema->cobrand; + my $cobrand_state = $cobrand->call_hook(problem_state_processed => $self); + + return $cobrand_state || $self->problem_state; } sub problem_state_display { - my ( $self, $c ) = @_; + my $self = shift; my $state = $self->problem_state_processed; return '' unless $state; - my $cobrand_name = $c->cobrand->moniker; + my $cobrand = $self->result_source->schema->cobrand; + my $cobrand_name = $cobrand->moniker; my $names = join(',,', @{$self->problem->body_names}); if ($names =~ /(Bromley|Isle of Wight|TfL)/) { ($cobrand_name = lc $1) =~ s/ //g; @@ -313,7 +378,8 @@ sub hide { } sub as_hashref { - my ($self, $c, $cols) = @_; + my ($self, $cols) = @_; + my $cobrand = $self->result_source->schema->cobrand; my $out = { id => $self->id, @@ -329,7 +395,7 @@ sub as_hashref { if ($self->confirmed) { $out->{confirmed} = $self->confirmed if !$cols || $cols->{confirmed}; - $out->{confirmed_pp} = $c->cobrand->prettify_dt( $self->confirmed ) if !$cols || $cols->{confirmed_pp}; + $out->{confirmed_pp} = $cobrand->prettify_dt( $self->confirmed ) if !$cols || $cols->{confirmed_pp}; } return $out; |