Skip to content

Commit f322c9f

Browse files
notif [nfc]: Move NotificationOpenPayload to a separate file
1 parent 580892a commit f322c9f

File tree

4 files changed

+299
-287
lines changed

4 files changed

+299
-287
lines changed

lib/notifications/display.dart

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import '../widgets/message_list.dart';
2121
import '../widgets/page.dart';
2222
import '../widgets/store.dart';
2323
import '../widgets/theme.dart';
24+
import 'open.dart';
2425

2526
AndroidNotificationHostApi get _androidHost => ZulipBinding.instance.androidNotificationHost;
2627

@@ -550,86 +551,3 @@ class NotificationDisplayManager {
550551
return null;
551552
}
552553
}
553-
554-
/// The information contained in 'zulip://notification/…' internal
555-
/// Android intent data URL, used for notification-open flow.
556-
class NotificationOpenPayload {
557-
final Uri realmUrl;
558-
final int userId;
559-
final Narrow narrow;
560-
561-
NotificationOpenPayload({
562-
required this.realmUrl,
563-
required this.userId,
564-
required this.narrow,
565-
});
566-
567-
factory NotificationOpenPayload.parseUrl(Uri url) {
568-
if (url case Uri(
569-
scheme: 'zulip',
570-
host: 'notification',
571-
queryParameters: {
572-
'realm_url': var realmUrlStr,
573-
'user_id': var userIdStr,
574-
'narrow_type': var narrowType,
575-
// In case of narrowType == 'topic':
576-
// 'channel_id' and 'topic' handled below.
577-
578-
// In case of narrowType == 'dm':
579-
// 'all_recipient_ids' handled below.
580-
},
581-
)) {
582-
final realmUrl = Uri.parse(realmUrlStr);
583-
final userId = int.parse(userIdStr, radix: 10);
584-
585-
final Narrow narrow;
586-
switch (narrowType) {
587-
case 'topic':
588-
final channelIdStr = url.queryParameters['channel_id']!;
589-
final channelId = int.parse(channelIdStr, radix: 10);
590-
final topicStr = url.queryParameters['topic']!;
591-
narrow = TopicNarrow(channelId, TopicName(topicStr));
592-
case 'dm':
593-
final allRecipientIdsStr = url.queryParameters['all_recipient_ids']!;
594-
final allRecipientIds = allRecipientIdsStr.split(',')
595-
.map((idStr) => int.parse(idStr, radix: 10))
596-
.toList(growable: false);
597-
narrow = DmNarrow(allRecipientIds: allRecipientIds, selfUserId: userId);
598-
default:
599-
throw const FormatException();
600-
}
601-
602-
return NotificationOpenPayload(
603-
realmUrl: realmUrl,
604-
userId: userId,
605-
narrow: narrow,
606-
);
607-
} else {
608-
// TODO(dart): simplify after https://github.com/dart-lang/language/issues/2537
609-
throw const FormatException();
610-
}
611-
}
612-
613-
Uri buildUrl() {
614-
return Uri(
615-
scheme: 'zulip',
616-
host: 'notification',
617-
queryParameters: <String, String>{
618-
'realm_url': realmUrl.toString(),
619-
'user_id': userId.toString(),
620-
...(switch (narrow) {
621-
TopicNarrow(streamId: var channelId, :var topic) => {
622-
'narrow_type': 'topic',
623-
'channel_id': channelId.toString(),
624-
'topic': topic.apiName,
625-
},
626-
DmNarrow(:var allRecipientIds) => {
627-
'narrow_type': 'dm',
628-
'all_recipient_ids': allRecipientIds.join(','),
629-
},
630-
_ => throw UnsupportedError('Found an unexpected Narrow of type ${narrow.runtimeType}.'),
631-
})
632-
},
633-
);
634-
}
635-
}

lib/notifications/open.dart

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import '../api/model/model.dart';
2+
import '../model/narrow.dart';
3+
4+
/// The information contained in 'zulip://notification/…' internal
5+
/// Android intent data URL, used for notification-open flow.
6+
class NotificationOpenPayload {
7+
final Uri realmUrl;
8+
final int userId;
9+
final Narrow narrow;
10+
11+
NotificationOpenPayload({
12+
required this.realmUrl,
13+
required this.userId,
14+
required this.narrow,
15+
});
16+
17+
factory NotificationOpenPayload.parseUrl(Uri url) {
18+
if (url case Uri(
19+
scheme: 'zulip',
20+
host: 'notification',
21+
queryParameters: {
22+
'realm_url': var realmUrlStr,
23+
'user_id': var userIdStr,
24+
'narrow_type': var narrowType,
25+
// In case of narrowType == 'topic':
26+
// 'channel_id' and 'topic' handled below.
27+
28+
// In case of narrowType == 'dm':
29+
// 'all_recipient_ids' handled below.
30+
},
31+
)) {
32+
final realmUrl = Uri.parse(realmUrlStr);
33+
final userId = int.parse(userIdStr, radix: 10);
34+
35+
final Narrow narrow;
36+
switch (narrowType) {
37+
case 'topic':
38+
final channelIdStr = url.queryParameters['channel_id']!;
39+
final channelId = int.parse(channelIdStr, radix: 10);
40+
final topicStr = url.queryParameters['topic']!;
41+
narrow = TopicNarrow(channelId, TopicName(topicStr));
42+
case 'dm':
43+
final allRecipientIdsStr = url.queryParameters['all_recipient_ids']!;
44+
final allRecipientIds = allRecipientIdsStr.split(',')
45+
.map((idStr) => int.parse(idStr, radix: 10))
46+
.toList(growable: false);
47+
narrow = DmNarrow(allRecipientIds: allRecipientIds, selfUserId: userId);
48+
default:
49+
throw const FormatException();
50+
}
51+
52+
return NotificationOpenPayload(
53+
realmUrl: realmUrl,
54+
userId: userId,
55+
narrow: narrow,
56+
);
57+
} else {
58+
// TODO(dart): simplify after https://github.com/dart-lang/language/issues/2537
59+
throw const FormatException();
60+
}
61+
}
62+
63+
Uri buildUrl() {
64+
return Uri(
65+
scheme: 'zulip',
66+
host: 'notification',
67+
queryParameters: <String, String>{
68+
'realm_url': realmUrl.toString(),
69+
'user_id': userId.toString(),
70+
...(switch (narrow) {
71+
TopicNarrow(streamId: var channelId, :var topic) => {
72+
'narrow_type': 'topic',
73+
'channel_id': channelId.toString(),
74+
'topic': topic.apiName,
75+
},
76+
DmNarrow(:var allRecipientIds) => {
77+
'narrow_type': 'dm',
78+
'all_recipient_ids': allRecipientIds.join(','),
79+
},
80+
_ => throw UnsupportedError('Found an unexpected Narrow of type ${narrow.runtimeType}.'),
81+
})
82+
},
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)