-
Notifications
You must be signed in to change notification settings - Fork 20
feat: detect admin/dispute DMs in background notification pipeline #498
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
Draft
AndreaDiazCorreia
wants to merge
3
commits into
main
Choose a base branch
from
feat/admin-chat-background-notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−5
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ac4d251
feat: detect admin/dispute DM format in background notification service
AndreaDiazCorreia fda96a6
refactor: extract DM payload detection into shared NostrUtils.isDmPay…
AndreaDiazCorreia a072019
feat: add sendDm and cooperativeCancelAccepted to notification data e…
AndreaDiazCorreia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
test/features/notifications/services/background_notification_dm_detection_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mostro_mobile/data/models/enums/action.dart'; | ||
| import 'package:mostro_mobile/data/models/mostro_message.dart'; | ||
| import 'package:mostro_mobile/features/notifications/utils/notification_data_extractor.dart'; | ||
| import 'package:mostro_mobile/shared/utils/nostr_utils.dart'; | ||
|
|
||
| /// Tests for the admin/dispute DM background notification pipeline (Phase 1). | ||
| /// | ||
| /// Validates three layers: | ||
| /// 1. [NostrUtils.isDmPayload] — pure detection of the `{"dm": ...}` envelope | ||
| /// 2. [MostroMessage] construction — synthetic message with [Action.sendDm] | ||
| /// 3. [NotificationDataExtractor] — ensures sendDm is NOT marked temporary | ||
| void main() { | ||
| group('NostrUtils.isDmPayload', () { | ||
| test('detects dm wrapper key in decoded JSON', () { | ||
| final dmPayload = jsonDecode( | ||
| '[{"dm": {"action": "send-dm", "payload": {"text_message": "Hello"}}}]', | ||
| ); | ||
|
|
||
| expect(dmPayload, isList); | ||
| expect(dmPayload, isNotEmpty); | ||
| expect(NostrUtils.isDmPayload(dmPayload[0]), isTrue); | ||
| }); | ||
|
|
||
| test('does not detect dm key in standard Mostro message', () { | ||
| final orderPayload = jsonDecode( | ||
| '[{"order": {"action": "new-order", "id": "abc123", "payload": null}}]', | ||
| ); | ||
|
|
||
| expect(NostrUtils.isDmPayload(orderPayload[0]), isFalse); | ||
| }); | ||
|
|
||
| test('does not detect dm key in restore message', () { | ||
| final restorePayload = jsonDecode( | ||
| '[{"restore": {"action": "restore-session", "id": "abc123"}}]', | ||
| ); | ||
|
|
||
| expect(NostrUtils.isDmPayload(restorePayload[0]), isFalse); | ||
| }); | ||
|
|
||
| test('does not detect dm key in cant-do message', () { | ||
| final cantDoPayload = jsonDecode( | ||
| '[{"cant-do": {"action": "cant-do", "payload": null}}]', | ||
| ); | ||
|
|
||
| expect(NostrUtils.isDmPayload(cantDoPayload[0]), isFalse); | ||
| }); | ||
|
|
||
| test('handles dm payload with minimal content', () { | ||
| final dmPayload = jsonDecode('[{"dm": {}}]'); | ||
|
|
||
| expect(NostrUtils.isDmPayload(dmPayload[0]), isTrue); | ||
| }); | ||
|
|
||
| test('returns false for non-Map types', () { | ||
| expect(NostrUtils.isDmPayload('string'), isFalse); | ||
| expect(NostrUtils.isDmPayload(42), isFalse); | ||
| expect(NostrUtils.isDmPayload(null), isFalse); | ||
| expect(NostrUtils.isDmPayload([]), isFalse); | ||
| }); | ||
| }); | ||
|
|
||
| group('MostroMessage construction for DM', () { | ||
| test('preserves orderId and timestamp with sendDm action', () { | ||
| const testOrderId = 'test-order-123'; | ||
| const testTimestamp = 1700000000000; | ||
|
|
||
| final message = MostroMessage( | ||
| action: Action.sendDm, | ||
| id: testOrderId, | ||
| timestamp: testTimestamp, | ||
| ); | ||
|
|
||
| expect(message.action, Action.sendDm); | ||
| expect(message.id, testOrderId); | ||
| expect(message.timestamp, testTimestamp); | ||
| }); | ||
| }); | ||
|
|
||
| group('NotificationDataExtractor for sendDm', () { | ||
| test('produces non-temporary notification data', () async { | ||
| final message = MostroMessage( | ||
| action: Action.sendDm, | ||
| id: 'order-abc', | ||
| timestamp: 1700000000000, | ||
| ); | ||
|
|
||
| final data = await NotificationDataExtractor.extractFromMostroMessage( | ||
| message, | ||
| null, | ||
| ); | ||
|
|
||
| expect(data, isNotNull); | ||
| expect(data!.isTemporary, isFalse); | ||
| expect(data.action, Action.sendDm); | ||
| expect(data.orderId, 'order-abc'); | ||
| }); | ||
|
|
||
| test('returns empty values map (no payload extraction)', () async { | ||
| final message = MostroMessage( | ||
| action: Action.sendDm, | ||
| id: 'order-xyz', | ||
| ); | ||
|
|
||
| final data = await NotificationDataExtractor.extractFromMostroMessage( | ||
| message, | ||
| null, | ||
| ); | ||
|
|
||
| expect(data, isNotNull); | ||
| expect(data!.values, isEmpty); | ||
| }); | ||
| }); | ||
|
|
||
| group('NotificationDataExtractor for cooperativeCancelAccepted', () { | ||
| test('produces non-temporary notification data', () async { | ||
| final message = MostroMessage( | ||
| action: Action.cooperativeCancelAccepted, | ||
| id: 'order-cancel', | ||
| ); | ||
|
|
||
| final data = await NotificationDataExtractor.extractFromMostroMessage( | ||
| message, | ||
| null, | ||
| ); | ||
|
|
||
| expect(data, isNotNull); | ||
| expect(data!.isTemporary, isFalse); | ||
| expect(data.action, Action.cooperativeCancelAccepted); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
matchingSession.orderIdnull produces a null notification payload.matchingSession.orderIdisString?. If it is null (unlinked session),MostroMessage.idis null, sopayload: mostroMessage.idpassed toflutterLocalNotificationsPlugin.showis also null. Tapping the notification navigates to/notificationsrather than/trade_detail/$orderId.In practice dispute sessions are always linked to an order, so this is low-risk, but an explicit guard here makes the intent clear.
🛡️ Proposed defensive guard
if (NostrUtils.isDmPayload(firstItem)) { + if (matchingSession.orderId == null) { + logger.w('DM payload detected but session has no orderId — notification will lack deep-link'); + } return MostroMessage( action: mostro_action.Action.sendDm, id: matchingSession.orderId, timestamp: event.createdAt?.millisecondsSinceEpoch, ); }🤖 Prompt for AI Agents