Skip to content

Commit 594a316

Browse files
committed
api: Drop DmRecipient
This is unused in app code. This is mostly NFC except that we will ignore fields other than "id" from the list of objects from "display_recipient" on message events. We will use this name for a different purpose later.
1 parent a99b657 commit 594a316

File tree

3 files changed

+24
-97
lines changed

3 files changed

+24
-97
lines changed

lib/api/model/model.dart

+17-60
Original file line numberDiff line numberDiff line change
@@ -765,78 +765,35 @@ class StreamMessage extends Message {
765765
Map<String, dynamic> toJson() => _$StreamMessageToJson(this);
766766
}
767767

768-
@JsonSerializable(fieldRename: FieldRename.snake)
769-
class DmRecipient {
770-
final int id;
771-
final String email;
772-
final String fullName;
773-
774-
// final String? shortName; // obsolete, ignore
775-
// final bool? isMirrorDummy; // obsolete, ignore
776-
777-
DmRecipient({required this.id, required this.email, required this.fullName});
778-
779-
factory DmRecipient.fromJson(Map<String, dynamic> json) =>
780-
_$DmRecipientFromJson(json);
781-
782-
Map<String, dynamic> toJson() => _$DmRecipientToJson(this);
783-
784-
@override
785-
String toString() => 'DmRecipient(id: $id, email: $email, fullName: $fullName)';
786-
787-
@override
788-
bool operator ==(Object other) {
789-
if (other is! DmRecipient) return false;
790-
return other.id == id && other.email == email && other.fullName == fullName;
791-
}
792-
793-
@override
794-
int get hashCode => Object.hash('DmRecipient', id, email, fullName);
795-
}
796-
797-
class DmRecipientListConverter extends JsonConverter<List<DmRecipient>, List<dynamic>> {
798-
const DmRecipientListConverter();
799-
800-
@override
801-
List<DmRecipient> fromJson(List<dynamic> json) {
802-
return json.map((e) => DmRecipient.fromJson(e as Map<String, dynamic>))
803-
.toList(growable: false)
804-
..sort((a, b) => a.id.compareTo(b.id));
805-
}
806-
807-
@override
808-
List<dynamic> toJson(List<DmRecipient> object) => object;
809-
}
810-
811768
@JsonSerializable(fieldRename: FieldRename.snake)
812769
class DmMessage extends Message {
813770
@override
814771
@JsonKey(includeToJson: true)
815772
String get type => 'private';
816773

817-
/// The `display_recipient` from the server, sorted by user ID numerically.
774+
/// The user IDs of all users in the thread, sorted numerically, as in
775+
/// the `display_recipient from the server.
776+
///
777+
/// The other fields on `display_recipient` are ignored and won't roundtrip.
818778
///
819779
/// This lists the sender as well as all (other) recipients, and it
820780
/// lists each user just once. In particular the self-user is always
821781
/// included.
822-
///
823-
/// Note the data here is not updated on changes to the users, so everything
824-
/// other than the user IDs may be stale.
825-
/// Consider using [allRecipientIds] instead, and getting user details
826-
/// from the store.
827782
// TODO(server): Document that it's all users. That statement is based on
828783
// reverse-engineering notes in zulip-mobile:src/api/modelTypes.js at PmMessage.
829-
@DmRecipientListConverter()
830-
final List<DmRecipient> displayRecipient;
784+
@JsonKey(name: 'display_recipient', fromJson: _allRecipientIdsFromJson, toJson: _allRecipientIdsToJson)
785+
final List<int> allRecipientIds;
786+
787+
static List<int> _allRecipientIdsFromJson(Object? json) {
788+
return (json as List<dynamic>).map(
789+
(element) => ((element as Map<String, dynamic>)['id'] as num).toInt()
790+
).toList(growable: false)
791+
..sort();
792+
}
831793

832-
/// The user IDs of all users in the thread, sorted numerically.
833-
///
834-
/// This lists the sender as well as all (other) recipients, and it
835-
/// lists each user just once. In particular the self-user is always
836-
/// included.
837-
///
838-
/// This is a result of [List.map], so it has an efficient `length`.
839-
Iterable<int> get allRecipientIds => displayRecipient.map((e) => e.id);
794+
static List<Map<String, dynamic>> _allRecipientIdsToJson(List<int> allRecipientIds) {
795+
return allRecipientIds.map((element) => {'id': element}).toList();
796+
}
840797

841798
DmMessage({
842799
required super.client,
@@ -856,7 +813,7 @@ class DmMessage extends Message {
856813
required super.flags,
857814
required super.matchContent,
858815
required super.matchTopic,
859-
required this.displayRecipient,
816+
required this.allRecipientIds,
860817
});
861818

862819
factory DmMessage.fromJson(Map<String, dynamic> json) =>

lib/api/model/model.g.dart

+4-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/api/model/model_test.dart

+3-20
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ void main() {
172172
return DmMessage.fromJson({ ...baseJson, ...specialJson });
173173
}
174174

175-
Iterable<DmRecipient> asRecipients(Iterable<User> users) {
175+
List<Map<String, dynamic>> asRecipients(Iterable<User> users) {
176176
return users.map((u) =>
177-
DmRecipient(id: u.userId, email: u.email, fullName: u.fullName));
177+
{'id': u.userId, 'email': u.email, 'full_name': u.fullName}).toList();
178178
}
179179

180180
Map<String, dynamic> withRecipients(Iterable<User> recipients) {
@@ -183,31 +183,14 @@ void main() {
183183
'sender_id': from.userId,
184184
'sender_email': from.email,
185185
'sender_full_name': from.fullName,
186-
'display_recipient': asRecipients(recipients).map((r) => r.toJson()).toList(),
186+
'display_recipient': asRecipients(recipients),
187187
};
188188
}
189189

190190
User user2 = eg.user(userId: 2);
191191
User user3 = eg.user(userId: 3);
192192
User user11 = eg.user(userId: 11);
193193

194-
test('displayRecipient', () {
195-
check(parse(withRecipients([user2])).displayRecipient)
196-
.deepEquals(asRecipients([user2]));
197-
198-
check(parse(withRecipients([user2, user3])).displayRecipient)
199-
.deepEquals(asRecipients([user2, user3]));
200-
check(parse(withRecipients([user3, user2])).displayRecipient)
201-
.deepEquals(asRecipients([user2, user3]));
202-
203-
check(parse(withRecipients([user2, user3, user11])).displayRecipient)
204-
.deepEquals(asRecipients([user2, user3, user11]));
205-
check(parse(withRecipients([user3, user11, user2])).displayRecipient)
206-
.deepEquals(asRecipients([user2, user3, user11]));
207-
check(parse(withRecipients([user11, user2, user3])).displayRecipient)
208-
.deepEquals(asRecipients([user2, user3, user11]));
209-
});
210-
211194
test('allRecipientIds', () {
212195
check(parse(withRecipients([user2])).allRecipientIds)
213196
.deepEquals([2]);

0 commit comments

Comments
 (0)