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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
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 (there is
currently no admin interface for this. Should be added, but
while we're trialing this, it's a simple case of adding a DB record
manually)
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);
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 authorized
$c->detach unless $c->user_exists;
$c->detach unless $c->user->has_permission_to(moderate => $problem->bodies_str_ids);
$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) = @_;
$c->forward('report_moderate_hide');
my @types = grep $_,
$c->forward('report_moderate_title'),
$c->forward('report_moderate_detail'),
$c->forward('report_moderate_anon'),
$c->forward('report_moderate_photo');
$c->detach( 'report_moderate_audit', \@types )
}
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 => $user->name,
object_id => $problem->id,
object_type => 'problem',
reason => (sprintf '%s (%s)', $reason, $types_csv),
});
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' });
$c->res->redirect( '/' ); # Go directly to front-page
$c->detach( 'report_moderate_audit', ['hide'] ); # break chain here.
}
}
sub report_moderate_title : Private {
my ( $self, $c ) = @_;
my $problem = $c->stash->{problem} or die;
my $original = $c->stash->{problem_original};
my $old_title = $problem->title;
my $original_title = $original->title;
my $title = $c->get_param('problem_revert_title') ?
$original_title
: $self->diff($original_title, $c->get_param('problem_title'));
if ($title ne $old_title) {
$original->insert unless $original->in_storage;
$problem->update({ title => $title });
return 'title';
}
return;
}
sub report_moderate_detail : Private {
my ( $self, $c ) = @_;
my $problem = $c->stash->{problem} or die;
my $original = $c->stash->{problem_original};
my $old_detail = $problem->detail;
my $original_detail = $original->detail;
my $detail = $c->get_param('problem_revert_detail') ?
$original_detail
: $self->diff($original_detail, $c->get_param('problem_detail'));
if ($detail ne $old_detail) {
$original->insert unless $original->in_storage;
$problem->update({ detail => $detail });
return 'detail';
}
return;
}
sub report_moderate_anon : Private {
my ( $self, $c ) = @_;
my $problem = $c->stash->{problem} or die;
my $original = $c->stash->{problem_original};
my $show_user = $c->get_param('problem_show_name') ? 1 : 0;
my $anonymous = $show_user ? 0 : 1;
my $old_anonymous = $problem->anonymous ? 1 : 0;
if ($anonymous != $old_anonymous) {
$original->insert unless $original->in_storage;
$problem->update({ anonymous => $anonymous });
return 'anonymous';
}
return;
}
sub report_moderate_photo : Private {
my ( $self, $c ) = @_;
my $problem = $c->stash->{problem} or die;
my $original = $c->stash->{problem_original};
return unless $original->photo;
my $show_photo = $c->get_param('problem_show_photo') ? 1 : 0;
my $old_show_photo = $problem->photo ? 1 : 0;
if ($show_photo != $old_show_photo) {
$original->insert unless $original->in_storage;
$problem->update({ photo => $show_photo ? $original->photo : undef });
return 'photo';
}
return;
}
sub update : Chained('report') : PathPart('update') : CaptureArgs(1) {
my ($self, $c, $id) = @_;
my $comment = $c->stash->{problem}->comments->find($id);
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('update_moderate_detail'),
$c->forward('update_moderate_anon'),
$c->forward('update_moderate_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 => $user->name,
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->update({ state => 'hidden' });
$c->detach( 'update_moderate_audit', ['hide'] ); # break chain here.
}
return;
}
sub update_moderate_detail : Private {
my ( $self, $c ) = @_;
my $problem = $c->stash->{problem} or die;
my $comment = $c->stash->{comment} or die;
my $original = $c->stash->{comment_original};
my $old_detail = $comment->text;
my $original_detail = $original->detail;
my $detail = $c->get_param('update_revert_detail') ?
$original_detail
: $self->diff($original_detail, $c->get_param('update_detail'));
if ($detail ne $old_detail) {
$original->insert unless $original->in_storage;
$comment->update({ text => $detail });
return 'detail';
}
return;
}
sub update_moderate_anon : Private {
my ( $self, $c ) = @_;
my $problem = $c->stash->{problem} or die;
my $comment = $c->stash->{comment} or die;
my $original = $c->stash->{comment_original};
my $show_user = $c->get_param('update_show_name') ? 1 : 0;
my $anonymous = $show_user ? 0 : 1;
my $old_anonymous = $comment->anonymous ? 1 : 0;
if ($anonymous != $old_anonymous) {
$original->insert unless $original->in_storage;
$comment->update({ anonymous => $anonymous });
return 'anonymous';
}
return;
}
sub update_moderate_photo : Private {
my ( $self, $c ) = @_;
my $problem = $c->stash->{problem} or die;
my $comment = $c->stash->{comment} or die;
my $original = $c->stash->{comment_original};
return unless $original->photo;
my $show_photo = $c->get_param('update_show_photo') ? 1 : 0;
my $old_show_photo = $comment->photo ? 1 : 0;
if ($show_photo != $old_show_photo) {
$original->insert unless $original->in_storage;
$comment->update({ photo => $show_photo ? $original->photo : undef });
return 'photo';
}
}
sub return_text : Private {
my ($self, $c, $text) = @_;
$c->res->content_type('text/plain; charset=utf-8');
$c->res->body( $text // '' );
}
sub diff {
my ($self, $old, $new) = @_;
$new =~s/\[\.{3}\]//g;
my $diff = Algorithm::Diff->new( [ split //, $old ], [ split //, $new ] );
my $string;
while ($diff->Next) {
my $d = $diff->Diff;
if ($d & 1) {
my $deleted = join '', $diff->Items(1);
unless ($deleted =~/^\s*$/) {
$string .= ' ' if $deleted =~/^ /;
$string .= '[...]';
$string .= ' ' if $deleted =~/ $/;
}
}
$string .= join '', $diff->Items(2);
}
return $string;
}
__PACKAGE__->meta->make_immutable;
1;
|