Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,15 @@ class NotificationDataExtractor {
break;

case Action.canceled:
// Canceled orders don't generate persistent notifications
return null;
final order = event.getPayload<Order>();
final currentSession = session;

// Maker-side expirations arrive as canceled events for a pending order with no peer.
// Preserve that context so the notification copy can explain the timeout clearly.
if (order?.status == Status.pending && currentSession?.peer == null) {
values['cancellation_reason'] = 'peer_timeout';
}
break;
Comment on lines +151 to +159

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify whether any call sites omit the `session:` argument.
# Expected: if any call omits `session`, current logic may misclassify cancellations.
rg -nP --type=dart 'NotificationDataExtractor\.extractFromMostroMessage\s*\(' -C2
rg -nP --type=dart 'extractFromMostroMessage\s*\([^)]*session\s*:' -C2

Repository: MostroP2P/mobile

Length of output: 2705


🏁 Script executed:

# Examine the function signature for extractFromMostroMessage
fd notification_data_extractor.dart -x cat -n {} | head -80

# Check how 'peer_timeout' value is used downstream
rg -n "peer_timeout" --type=dart

Repository: MostroP2P/mobile

Length of output: 4612


Guard timeout tagging on explicit session presence.

At Line 156, currentSession?.peer == null evaluates true when session is missing, causing cancellations without session context to be incorrectly tagged as peer_timeout. This prevents proper maker vs. taker differentiation as required by timeout handling guidelines.

♻️ Proposed adjustment
-        if (order?.status == Status.pending && currentSession?.peer == null) {
+        if (order?.status == Status.pending &&
+            currentSession != null &&
+            currentSession.peer == null) {
           values['cancellation_reason'] = 'peer_timeout';
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/features/notifications/utils/notification_data_extractor.dart` around
lines 151 - 159, The cancellation is being tagged as 'peer_timeout' even when
there is no session because the check uses currentSession?.peer == null which is
true for a null session; in the notification_data_extractor logic around
event.getPayload<Order>() and session/currentSession, change the condition to
require an explicit session object before checking peer (e.g. ensure
currentSession != null && currentSession.peer == null) so
values['cancellation_reason'] is only set to 'peer_timeout' when a session
exists and its peer is null.


case Action.cooperativeCancelInitiatedByYou:
// No additional values needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ class NotificationMessageMapper {
return s.notification_order_canceled_title;
case 'notification_order_canceled_message':
return s.notification_order_canceled_message;
case 'notification_order_canceled_peer_timeout_message':
return s.notification_order_canceled_peer_timeout_message;
case 'notification_cooperative_cancel_initiated_by_you_title':
return s.notification_cooperative_cancel_initiated_by_you_title;
case 'notification_cooperative_cancel_initiated_by_you_message':
Expand Down Expand Up @@ -360,4 +362,4 @@ class NotificationMessageMapper {
return _resolveLocalizationKey(localizations, messageKey);
}
}
}
}
1 change: 1 addition & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@
"notification_dispute_started_message": "A dispute has been initiated",
"notification_order_canceled_title": "Order canceled",
"notification_order_canceled_message": "The order has been canceled",
"notification_order_canceled_peer_timeout_message": "The counterparty did not respond in time. The order has been canceled.",
"notification_cooperative_cancel_initiated_by_you_title": "Cancellation requested",
"notification_cooperative_cancel_initiated_by_you_message": "You requested to cancel the order, waiting for peer confirmation",
"notification_cooperative_cancel_initiated_by_peer_title": "Cancellation request",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/intl_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@
"notification_dispute_started_message": "Se ha iniciado una disputa",
"notification_order_canceled_title": "Orden cancelada",
"notification_order_canceled_message": "La orden ha sido cancelada",
"notification_order_canceled_peer_timeout_message": "La contraparte no respondió a tiempo. La orden ha sido cancelada.",
"notification_cooperative_cancel_initiated_by_you_title": "Cancelación solicitada",
"notification_cooperative_cancel_initiated_by_you_message": "Solicitaste cancelar la orden, esperando confirmación del par",
"notification_cooperative_cancel_initiated_by_peer_title": "Solicitud de cancelación",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/intl_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@
"notification_dispute_started_message": "Un différend a été initié",
"notification_order_canceled_title": "Commande annulée",
"notification_order_canceled_message": "La commande a été annulée",
"notification_order_canceled_peer_timeout_message": "La contrepartie n'a pas répondu à temps. La commande a été annulée.",
"notification_cooperative_cancel_initiated_by_you_title": "Annulation demandée",
"notification_cooperative_cancel_initiated_by_you_message": "Vous avez demandé d'annuler la commande, en attente de confirmation du pair",
"notification_cooperative_cancel_initiated_by_peer_title": "Demande d'annulation",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/intl_it.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,7 @@
"notification_dispute_started_message": "È stata avviata una disputa",
"notification_order_canceled_title": "Ordine annullato",
"notification_order_canceled_message": "L'ordine è stato annullato",
"notification_order_canceled_peer_timeout_message": "La controparte non ha risposto in tempo. L'ordine è stato annullato.",
"notification_cooperative_cancel_initiated_by_you_title": "Cancellazione richiesta",
"notification_cooperative_cancel_initiated_by_you_message": "Hai richiesto di annullare l'ordine, in attesa della conferma del peer",
"notification_cooperative_cancel_initiated_by_peer_title": "Richiesta di cancellazione",
Expand Down
104 changes: 104 additions & 0 deletions test/features/notifications/notification_message_mapper_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import 'package:dart_nostr/dart_nostr.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mostro_mobile/data/models.dart';
import 'package:mostro_mobile/data/models/enums/action.dart';
import 'package:mostro_mobile/data/models/enums/order_type.dart';
import 'package:mostro_mobile/data/models/enums/role.dart';
import 'package:mostro_mobile/data/models/enums/status.dart';
import 'package:mostro_mobile/features/notifications/utils/notification_data_extractor.dart';
import 'package:mostro_mobile/features/notifications/utils/notification_message_mapper.dart';

void main() {
group('NotificationMessageMapper', () {
test('returns timeout-specific message key for expired counterparty cancellations', () {
final key = NotificationMessageMapper.getMessageKeyWithContext(
Action.canceled,
const {'cancellation_reason': 'peer_timeout'},
);

expect(key, 'notification_order_canceled_peer_timeout_message');
});

test('keeps generic cancellation copy for other canceled orders', () {
final key = NotificationMessageMapper.getMessageKeyWithContext(
Action.canceled,
const {},
);

expect(key, 'notification_order_canceled_message');
});
});

group('NotificationDataExtractor', () {
test('tags maker-side timeout cancellations with peer_timeout reason', () async {
final message = MostroMessage(
id: 'order-123',
action: Action.canceled,
payload: const Order(
id: 'order-123',
kind: OrderType.sell,
status: Status.pending,
fiatCode: 'USD',
fiatAmount: 100,
paymentMethod: 'Cash',
),
);

final session = Session(
masterKey: NostrKeyPairs(private: '1' * 64, public: '2' * 64),
tradeKey: NostrKeyPairs(private: '3' * 64, public: '4' * 64),
keyIndex: 0,
fullPrivacy: false,
startTime: DateTime.utc(2026, 1, 1),
orderId: 'order-123',
role: Role.seller,
);

final notification = await NotificationDataExtractor.extractFromMostroMessage(
message,
null,
session: session,
);

expect(notification, isNotNull);
expect(notification!.action, Action.canceled);
expect(notification.values['cancellation_reason'], 'peer_timeout');
});

test('does not tag cancellations after a peer was established', () async {
final message = MostroMessage(
id: 'order-123',
action: Action.canceled,
payload: const Order(
id: 'order-123',
kind: OrderType.sell,
status: Status.pending,
fiatCode: 'USD',
fiatAmount: 100,
paymentMethod: 'Cash',
buyerTradePubkey: '5' * 64,
),
);

final session = Session(
masterKey: NostrKeyPairs(private: '1' * 64, public: '2' * 64),
tradeKey: NostrKeyPairs(private: '3' * 64, public: '4' * 64),
keyIndex: 0,
fullPrivacy: false,
startTime: DateTime.utc(2026, 1, 1),
orderId: 'order-123',
role: Role.seller,
peer: Peer(publicKey: '5' * 64),
);

final notification = await NotificationDataExtractor.extractFromMostroMessage(
message,
null,
session: session,
);

expect(notification, isNotNull);
expect(notification!.values.containsKey('cancellation_reason'), isFalse);
});
});
}