Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report page rendering and form submission for appealing a resolution. #7714

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/lib/admin/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class ModerationCase extends db.ExpandoModel<String> {
required this.status,
required this.subject,
required this.url,
required this.appealedCaseId,
}) {
id = caseId;
opened = clock.now().toUtc();
Expand Down Expand Up @@ -130,6 +131,7 @@ abstract class ModerationDetectedBy {

abstract class ModerationKind {
static const notification = 'notification';
static const appeal = 'appeal';
}

abstract class ModerationStatus {
Expand Down
35 changes: 32 additions & 3 deletions app/lib/frontend/handlers/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';

import 'package:_pub_shared/data/account_api.dart';
import 'package:clock/clock.dart';
import 'package:pub_dev/admin/backend.dart';
import 'package:pub_dev/shared/configuration.dart';
import 'package:shelf/shelf.dart' as shelf;

Expand Down Expand Up @@ -44,11 +45,15 @@ Future<shelf.Response> reportPageHandler(shelf.Request request) async {
final url = request.requestedUri.queryParameters['url'];
_verifyUrl(url);

final caseId = request.requestedUri.queryParameters['appeal'];
await _verifyCaseId(caseId, subject);

return htmlResponse(
renderReportPage(
sessionData: requestContext.sessionData,
subject: subject,
url: url,
caseId: caseId,
),
headers: CacheControl.explicitlyPrivate.headers,
);
Expand Down Expand Up @@ -102,6 +107,24 @@ void _verifyUrl(String? urlParam) {
}
}

Future<void> _verifyCaseId(String? caseId, ModerationSubject subject) async {
if (caseId == null) {
return null;
}

final mc = await adminBackend.lookupModerationCase(caseId);
if (mc == null) {
throw NotFoundException.resource('case_id "$caseId"');
}
InvalidInputException.check(mc.status != ModerationStatus.pending,
'The reported case is not closed yet.');

final hasSubject = mc.subject == subject.fqn ||
mc.getActionLog().entries.any((e) => e.subject == subject.fqn);
InvalidInputException.check(hasSubject,
'The reported case has no resolution on subject "${subject.fqn}".');
}

/// Handles POST /api/report
Future<String> processReportPageHandler(
shelf.Request request, ReportForm form) async {
Expand Down Expand Up @@ -142,6 +165,7 @@ Future<String> processReportPageHandler(
await _verifySubject(subject!);

_verifyUrl(form.url);
await _verifyCaseId(form.caseId, subject);

InvalidInputException.checkStringLength(
form.message,
Expand All @@ -150,24 +174,29 @@ Future<String> processReportPageHandler(
maximum: 8192,
);

final isAppeal = form.caseId != null;

// If the email sending fails, we may have pending [ModerationCase] entities
// in the datastore. These would be reviewed and processed manually.
await withRetryTransaction(dbService, (tx) async {
final mc = ModerationCase.init(
caseId: caseId,
reporterEmail: userEmail!,
source: ModerationDetectedBy.externalNotification,
kind: ModerationKind.notification,
kind: isAppeal ? ModerationKind.appeal : ModerationKind.notification,
status: ModerationStatus.pending,
subject: subject.fqn,
url: form.url,
appealedCaseId: form.caseId,
);
tx.insert(mc);
});

final kind = isAppeal ? 'appeal' : 'report';
final bodyText = <String>[
'New report received on ${now.toIso8601String()}: $caseId',
'New $kind received on ${now.toIso8601String()}: $caseId',
if (form.url != null) 'URL: ${form.url}',
if (isAppeal) 'Appealed case ID: ${form.caseId}',
'Subject: ${subject.fqn}',
'Message:\n${form.message}',
].join('\n\n');
Expand All @@ -178,5 +207,5 @@ Future<String> processReportPageHandler(
bodyText: bodyText,
));

return 'Report submitted successfully.';
return 'The $kind was submitted successfully.';
}
Loading
Loading