Skip to content

api: Add unreadMsgs in initial snapshot #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2023
Merged
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
101 changes: 101 additions & 0 deletions lib/api/model/initial_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class InitialSnapshot {

final List<Subscription> subscriptions;

final UnreadMessagesSnapshot unreadMsgs;

final List<ZulipStream> streams;

// Servers pre-5.0 don't have `user_settings`, and instead provide whatever
Expand Down Expand Up @@ -79,6 +81,7 @@ class InitialSnapshot {
required this.customProfileFields,
required this.recentPrivateConversations,
required this.subscriptions,
required this.unreadMsgs,
required this.streams,
required this.userSettings,
required this.maxFileUploadSizeMib,
Expand Down Expand Up @@ -163,3 +166,101 @@ enum UserSettingName {
static final _byRawString = _$UserSettingNameEnumMap
.map((key, value) => MapEntry(value, key));
}

/// The `unread_msgs` snapshot.
///
/// For docs, search for "unread_msgs:"
/// in <https://zulip.com/api/register-queue>.
@JsonSerializable(fieldRename: FieldRename.snake)
class UnreadMessagesSnapshot {
final int count;

@JsonKey(name: 'pms')
final List<UnreadDmSnapshot> dms;

final List<UnreadStreamSnapshot> streams;
final List<UnreadHuddleSnapshot> huddles;
final List<int> mentions;
final bool oldUnreadsMissing;

UnreadMessagesSnapshot({
required this.count,
required this.dms,
required this.streams,
required this.huddles,
required this.mentions,
required this.oldUnreadsMissing,
});

factory UnreadMessagesSnapshot.fromJson(Map<String, dynamic> json) =>
_$UnreadMessagesSnapshotFromJson(json);

Map<String, dynamic> toJson() => _$UnreadMessagesSnapshotToJson(this);
}

/// An item in [UnreadMessagesSnapshot.dms].
@JsonSerializable(fieldRename: FieldRename.snake)
class UnreadDmSnapshot {
@JsonKey(readValue: _readOtherUserId)
final int otherUserId;

// The doc mistakenly calls this `unread_ids`:
// https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20.60unread_msgs.2Epms.5B.5D.2Eunread_message_ids.60/near/1623940
final List<int> unreadMessageIds;

// other_user_id was introduced at FL 119 as the new name for sender_id:
// https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20When.20was.20.60unread_msgs.2Epms.5B.5D.2Eother_user_id.60.20added.3F/near/1623961
// TODO(server-5): Simplify away.
static _readOtherUserId(Map json, String key) {
return json[key] ?? json['sender_id'];
}

UnreadDmSnapshot({
required this.otherUserId,
required this.unreadMessageIds,
});

factory UnreadDmSnapshot.fromJson(Map<String, dynamic> json) =>
_$UnreadDmSnapshotFromJson(json);

Map<String, dynamic> toJson() => _$UnreadDmSnapshotToJson(this);
}

/// An item in [UnreadMessagesSnapshot.streams].
@JsonSerializable(fieldRename: FieldRename.snake)
class UnreadStreamSnapshot {
final String topic;
final int streamId;
final List<int> unreadMessageIds;

UnreadStreamSnapshot({
required this.topic,
required this.streamId,
required this.unreadMessageIds,
});

factory UnreadStreamSnapshot.fromJson(Map<String, dynamic> json) =>
_$UnreadStreamSnapshotFromJson(json);

Map<String, dynamic> toJson() => _$UnreadStreamSnapshotToJson(this);
}

/// An item in [UnreadMessagesSnapshot.huddles].
@JsonSerializable(fieldRename: FieldRename.snake)
class UnreadHuddleSnapshot {
final String userIdsString;

// The doc mistakenly calls this `unread_ids`:
// https://chat.zulip.org/#narrow/stream/412-api-documentation/topic/register.3A.20.60unread_msgs.2Epms.5B.5D.2Eunread_message_ids.60/near/1623940
final List<int> unreadMessageIds;

UnreadHuddleSnapshot({
required this.userIdsString,
required this.unreadMessageIds,
});

factory UnreadHuddleSnapshot.fromJson(Map<String, dynamic> json) =>
_$UnreadHuddleSnapshotFromJson(json);

Map<String, dynamic> toJson() => _$UnreadHuddleSnapshotToJson(this);
}
81 changes: 81 additions & 0 deletions lib/api/model/initial_snapshot.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/example_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ ZulipStream stream({
);
}

UnreadMessagesSnapshot unreadMsgs({
int? count,
List<UnreadDmSnapshot>? dms,
List<UnreadStreamSnapshot>? streams,
List<UnreadHuddleSnapshot>? huddles,
List<int>? mentions,
bool? oldUnreadsMissing,
}) {
return UnreadMessagesSnapshot(
count: count ?? 0,
dms: dms ?? [],
streams: streams ?? [],
huddles: huddles ?? [],
mentions: mentions ?? [],
oldUnreadsMissing: oldUnreadsMissing ?? false,
);
}
const _unreadMsgs = unreadMsgs;

final _messagePropertiesBase = {
'is_me_message': false,
'recipient_id': 32, // obsolescent in API, and ignored
Expand Down Expand Up @@ -223,6 +242,7 @@ InitialSnapshot initialSnapshot({
List<CustomProfileField>? customProfileFields,
List<RecentDmConversation>? recentPrivateConversations,
List<Subscription>? subscriptions,
UnreadMessagesSnapshot? unreadMsgs,
List<ZulipStream>? streams,
UserSettings? userSettings,
int? maxFileUploadSizeMib,
Expand All @@ -240,6 +260,7 @@ InitialSnapshot initialSnapshot({
customProfileFields: customProfileFields ?? [],
recentPrivateConversations: recentPrivateConversations ?? [],
subscriptions: subscriptions ?? [], // TODO add subscriptions to default
unreadMsgs: unreadMsgs ?? _unreadMsgs(),
streams: streams ?? [], // TODO add streams to default
userSettings: userSettings, // TODO add userSettings to default
maxFileUploadSizeMib: maxFileUploadSizeMib ?? 25,
Expand Down