Skip to content

Commit

Permalink
wip use recipient_type
Browse files Browse the repository at this point in the history
  • Loading branch information
gnprice committed Oct 18, 2023
1 parent c5458ea commit 63c30f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
20 changes: 15 additions & 5 deletions lib/api/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -103,16 +105,21 @@ sealed class FcmMessageRecipient {
FcmMessageRecipient();

factory FcmMessageRecipient.fromJson(Map<String, dynamic> 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.
Expand All @@ -134,6 +141,9 @@ class FcmMessageStreamRecipient extends FcmMessageRecipient {
}

class FcmMessageDmRecipient extends FcmMessageRecipient {
@JsonKey(includeToJson: true)
String get recipientType => 'private';

final List<int> allRecipientIds;

FcmMessageDmRecipient({required this.allRecipientIds});
Expand Down
9 changes: 4 additions & 5 deletions test/api/notifications_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ void main() {
check(parse(json).toJson())
.deepEquals({ ...json }
..remove('content_truncated')
..remove('recipient_type')
..remove('alert'));
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 63c30f1

Please sign in to comment.