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

Store pending ModerationCase before sending email on the report page. #7660

Merged
merged 1 commit into from
Apr 22, 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
16 changes: 14 additions & 2 deletions app/lib/admin/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:clock/clock.dart';
import 'package:pub_dev/shared/utils.dart';

import '../shared/datastore.dart' as db;

Expand Down Expand Up @@ -77,13 +76,26 @@ class ModerationCase extends db.ExpandoModel<String> {
ModerationCase();

ModerationCase.init({
required String caseId,
required this.reporterUserId,
required this.reporterEmail,
required this.detectedBy,
required this.kind,
required this.status,
}) {
id = createUuid();
id = caseId;
opened = clock.now().toUtc();
}
}

abstract class ModerationDetectedBy {
static const externalNotification = 'external-notification';
}

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

abstract class ModerationStatus {
static const pending = 'pending';
}
22 changes: 19 additions & 3 deletions app/lib/frontend/handlers/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import 'package:clock/clock.dart';
import 'package:shelf/shelf.dart' as shelf;

import '../../account/backend.dart';
import '../../admin/models.dart';
import '../../frontend/email_sender.dart';
import '../../frontend/handlers/cache_control.dart';
import '../../shared/datastore.dart';
import '../../shared/email.dart';
import '../../shared/exceptions.dart';
import '../../shared/handlers.dart';
Expand Down Expand Up @@ -39,7 +41,7 @@ Future<String> processReportPageHandler(
throw NotFoundException('Experimental flag is not enabled.');
}
final now = clock.now().toUtc();
final id = '${now.toIso8601String().split('T').first}/${createUuid()}';
final caseId = '${now.toIso8601String().split('T').first}/${createUuid()}';

final isAuthenticated = requestContext.sessionData?.isAuthenticated ?? false;
final user = isAuthenticated ? await requireAuthenticatedWebUser() : null;
Expand All @@ -61,13 +63,27 @@ Future<String> processReportPageHandler(
maximum: 8192,
);

// 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,
reporterUserId: user?.userId,
reporterEmail: userEmail!,
detectedBy: ModerationDetectedBy.externalNotification,
kind: ModerationKind.notification,
status: ModerationStatus.pending,
);
tx.insert(mc);
});

final bodyText = <String>[
'New report recieved on ${now.toIso8601String()}: $id',
'New report recieved on ${now.toIso8601String()}: $caseId',
'Message:\n${form.message}',
].join('\n\n');

await emailSender.sendMessage(createReportPageAdminEmail(
id: id,
id: caseId,
userEmail: userEmail!,
bodyText: bodyText,
));
Expand Down
Loading