diff options
author | Matthew Somerville <matthew@mysociety.org> | 2020-07-10 14:38:17 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2020-07-14 18:25:19 +0100 |
commit | 67823bc788ce744e1228a8602b9a5aa805771ced (patch) | |
tree | 0b1eddd4e071314573e52206d0061cab022e1cd0 /perllib/FixMyStreet/DB/Result/Comment.pm | |
parent | eed6158981240afce10824e82fa86f14b38f3167 (diff) |
Centralise update creation to include fields.
Given the user, we can infer the name if not provided, and the extra
data if a staff user. We can also provide defaults for various other
fields. Always have superuser take precedence over from_body.
Diffstat (limited to 'perllib/FixMyStreet/DB/Result/Comment.pm')
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Comment.pm | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/DB/Result/Comment.pm b/perllib/FixMyStreet/DB/Result/Comment.pm index 68e267d00..c876f3459 100644 --- a/perllib/FixMyStreet/DB/Result/Comment.pm +++ b/perllib/FixMyStreet/DB/Result/Comment.pm @@ -110,6 +110,41 @@ 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 get_cobrand_logged Get a cobrand object for the cobrand the update was made on. |