aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Moderate.pm
blob: 45a303309fbee720d5e57bce6f649b8bb96c843c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package FixMyStreet::App::Controller::Moderate;

use Moose;
use namespace::autoclean;
use Algorithm::Diff;
BEGIN { extends 'Catalyst::Controller'; }

=head1 NAME

FixMyStreet::App::Controller::Moderate - process a moderation event

=head1 DESCRIPTION

The intent of this is that council users will be able to moderate reports
by themselves, but without requiring access to the full admin panel.

From a given report page, an authenticated user will be able to press
the "moderate" button on report and any updates to bring up a form with
data to change.

(Authentication requires:

  - 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.

=head1 SEE ALSO

DB tables:

    AdminLog
    ModerationOriginalData
    UserBodyPermissions

=cut

sub moderate : Chained('/') : PathPart('moderate') : CaptureArgs(0) { }

sub report : Chained('moderate') : PathPart('report') : CaptureArgs(1) {
    my ($self, $c, $id) = @_;
    my $problem = $c->model('DB::Problem')->find($id);
    $c->detach unless $problem;

    my $cobrand_base = $c->cobrand->base_url_for_report( $problem );
    my $report_uri = $cobrand_base . $problem->url;
    $c->stash->{cobrand_base} = $cobrand_base;
    $c->stash->{report_uri} = $report_uri;
    $c->res->redirect( $report_uri ); # this will be the final endpoint after all processing...

    # ... and immediately, if the user isn't logged in
    $c->detach unless $c->user_exists;

    $c->forward('/auth/check_csrf_token');

    my $original = $problem->find_or_new_related( moderation_original_data => {
        title => $problem->title,
        detail => $problem->detail,
        photo => $problem->photo,
        anonymous => $problem->anonymous,
    });
    $c->stash->{problem} = $problem;
    $c->stash->{problem_original} = $original;
    $c->stash->{moderation_reason} = $c->get_param('moderation_reason') // '';
}

sub moderate_report : Chained('report') : PathPart('') : Args(0) {
    my ($self, $c) = @_;

    # Make sure user can moderate this report
    $c->detach unless $c->user->can_moderate($c->stash->{problem});

    $c->forward('report_moderate_hide');

    my @types = grep $_,
        $c->forward('moderate_text', [ 'title' ]),
        $c->forward('moderate_text', [ 'detail' ]),
        $c->forward('moderate_boolean', [ 'anonymous', 'show_name' ]),
        $c->forward('moderate_boolean', [ 'photo' ]);

    $c->detach( 'report_moderate_audit', \@types )
}

sub moderating_user_name {
    my $user = shift;
    return $user->from_body ? $user->from_body->name : _('an administrator');
}

sub report_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 $types_csv = join ', ' => @types;

    $c->model('DB::AdminLog')->create({
        action => 'moderation',
        user => $user,
        admin_user => moderating_user_name($user),
        object_id => $problem->id,
        object_type => 'problem',
        reason => (sprintf '%s (%s)', $reason, $types_csv),
    });

    if ($problem->user->email_verified && $c->cobrand->send_moderation_notifications) {
        my $token = $c->model("DB::Token")->create({
            scope => 'moderation',
            data => { id => $problem->id }
        });

        $c->send_email( 'problem-moderated.txt', {
            to => [ [ $problem->user->email, $problem->name ] ],
            types => $types_csv,
            user => $problem->user,
            problem => $problem,
            report_uri => $c->stash->{report_uri},
            report_complain_uri => $c->stash->{cobrand_base} . '/contact?m=' . $token->token,
        });
    }
}

sub report_moderate_hide : Private {
    my ( $self, $c ) = @_;

    my $problem = $c->stash->{problem} or die;

    if ($c->get_param('problem_hide')) {

        $problem->update({ state => 'hidden' });
        $problem->get_photoset->delete_cached;

        $c->res->redirect( '/' ); # Go directly to front-page
        $c->detach( 'report_moderate_audit', ['hide'] ); # break chain here.
    }
}

sub moderate_text : Private {
    my ($self, $c, $thing) = @_;

    my ($object, $original, $param);
    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_';
    }

    my $old = $object->$thing;
    my $original_thing = $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;
        $object->update({ $thing => $new });
        return $thing_for_original_table;
    }

    return;
}

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_';
    }

    return if $thing eq 'photo' && !$original->photo;

    my $new;
    if ($reverse) {
        $new = $c->get_param($param . $reverse) ? 0 : 1;
    } else {
        $new = $c->get_param($param . $thing) ? 1 : 0;
    }
    my $old = $object->$thing ? 1 : 0;

    if ($new != $old) {
        $original->insert unless $original->in_storage;
        if ($thing eq 'photo') {
            $object->update({ $thing => $new ? $original->photo : undef });
        } else {
            $object->update({ $thing => $new });
        }
        return $thing;
    }
    return;
}

sub update : Chained('report') : PathPart('update') : CaptureArgs(1) {
    my ($self, $c, $id) = @_;
    my $comment = $c->stash->{problem}->comments->find($id);

    # 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 => {
        detail => $comment->text,
        photo => $comment->photo,
        anonymous => $comment->anonymous,
    });
    $c->stash->{comment} = $comment;
    $c->stash->{comment_original} = $original;
}

sub moderate_update : Chained('update') : PathPart('') : Args(0) {
    my ($self, $c) = @_;

    $c->forward('update_moderate_hide');

    my @types = grep $_,
        $c->forward('moderate_text', [ 'text' ]),
        $c->forward('moderate_boolean', [ 'anonymous', 'show_name' ]),
        $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),
    });
}

sub update_moderate_hide : Private {
    my ( $self, $c ) = @_;

    my $problem = $c->stash->{problem} or die;
    my $comment = $c->stash->{comment} or die;

    if ($c->get_param('update_hide')) {
        $comment->hide;
        $c->detach( 'update_moderate_audit', ['hide'] ); # break chain here.
    }
    return;
}

__PACKAGE__->meta->make_immutable;

1;