Skip to content

Commit

Permalink
Store pending ModerationCase before sending email on the report page.
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Apr 18, 2024
1 parent 4277020 commit 6366a62
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
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

0 comments on commit 6366a62

Please sign in to comment.