From 63c30f172ffcab5d5f3778a4f7abc4a7bab6ab96 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Tue, 17 Oct 2023 17:00:17 -0700 Subject: [PATCH] wip use recipient_type --- lib/api/notifications.dart | 20 +++++++++++++++----- test/api/notifications_test.dart | 9 ++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/api/notifications.dart b/lib/api/notifications.dart index b3daecf4d0..da75814d59 100644 --- a/lib/api/notifications.dart +++ b/lib/api/notifications.dart @@ -87,10 +87,12 @@ class MessageFcmMessage extends FcmMessageWithIdentity { final recipient = this.recipient; switch (recipient) { case FcmMessageDmRecipient(allRecipientIds: [_] || [_, _]): - break; + result['recipient_type'] = 'private'; case FcmMessageDmRecipient(:var allRecipientIds): + result['recipient_type'] = 'private'; result['pm_users'] = const _IntListConverter().toJson(allRecipientIds); case FcmMessageStreamRecipient(): + result['recipient_type'] = 'stream'; result['stream_id'] = const _IntConverter().toJson(recipient.streamId); if (recipient.streamName != null) result['stream'] = recipient.streamName; result['topic'] = recipient.topic; @@ -103,16 +105,21 @@ sealed class FcmMessageRecipient { FcmMessageRecipient(); factory FcmMessageRecipient.fromJson(Map json) { - // TODO look at recipient_type? - return json.containsKey('stream_id') - ? FcmMessageStreamRecipient.fromJson(json) - : FcmMessageDmRecipient.fromJson(json); + return switch (json['recipient_type']) { + 'stream' => FcmMessageStreamRecipient.fromJson(json), + 'private' => FcmMessageDmRecipient.fromJson(json), // TODO accept 'direct' + var recipientType => throw Exception( + 'FcmMessageRecipient.fromJson: unexpected recipient_type: $recipientType'), + }; } } @JsonSerializable(fieldRename: FieldRename.snake, createToJson: false) @_IntConverter() class FcmMessageStreamRecipient extends FcmMessageRecipient { + @JsonKey(includeToJson: true) + String get recipientType => 'stream'; + // Sending the stream ID in notifications is new in Zulip Server 5. // But handling the lack of it would add complication, and we don't strictly // need to -- we intend (#268) to cut pre-server-5 support before beta release. @@ -134,6 +141,9 @@ class FcmMessageStreamRecipient extends FcmMessageRecipient { } class FcmMessageDmRecipient extends FcmMessageRecipient { + @JsonKey(includeToJson: true) + String get recipientType => 'private'; + final List allRecipientIds; FcmMessageDmRecipient({required this.allRecipientIds}); diff --git a/test/api/notifications_test.dart b/test/api/notifications_test.dart index f9def8493d..73d658a9bb 100644 --- a/test/api/notifications_test.dart +++ b/test/api/notifications_test.dart @@ -111,7 +111,6 @@ void main() { check(parse(json).toJson()) .deepEquals({ ...json } ..remove('content_truncated') - ..remove('recipient_type') ..remove('alert')); } @@ -148,16 +147,16 @@ void main() { // test("${n++}", () => checkParseFails({ ...dmJson, 'realm_uri': 'zulip.example.com' })); // FAILS // test("${n++}", () => checkParseFails({ ...dmJson, 'realm_uri': '/examplecorp' })); // FAILS - // test("${n++}", () => checkParseFails({ ...streamJson }..remove('recipient_type'))); // FAILS + test("${n++}", () => checkParseFails({ ...streamJson }..remove('recipient_type'))); test("${n++}", () => checkParseFails({ ...streamJson, 'stream_id': '12,34' })); test("${n++}", () => checkParseFails({ ...streamJson, 'stream_id': 'abc' })); test("${n++}", () => checkParseFails({ ...streamJson }..remove('topic'))); - // test("${n++}", () => checkParseFails({ ...groupDmJson }..remove('recipient_type'))); // FAILS + test("${n++}", () => checkParseFails({ ...groupDmJson }..remove('recipient_type'))); test("${n++}", () => checkParseFails({ ...groupDmJson, 'pm_users': 'abc,34' })); test("${n++}", () => checkParseFails({ ...groupDmJson, 'pm_users': '12,abc' })); test("${n++}", () => checkParseFails({ ...groupDmJson, 'pm_users': '12,' })); - // test("${n++}", () => checkParseFails({ ...dmJson }..remove('recipient_type'))); // FAILS - // test("${n++}", () => checkParseFails({ ...dmJson, 'recipient_type': 'nonsense' })); // FAILS + test("${n++}", () => checkParseFails({ ...dmJson }..remove('recipient_type'))); + test("${n++}", () => checkParseFails({ ...dmJson, 'recipient_type': 'nonsense' })); test("${n++}", () => checkParseFails({ ...dmJson }..remove('sender_avatar_url'))); // test("${n++}", () => checkParseFails({ ...dmJson, 'sender_avatar_url': '/avatar/123.jpeg' })); // FAILS