diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 46f60da0..2996db07 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,5 +1,9 @@ ## Upcoming +✅ Added + +- Extra properties added to `PaginationParams` to aid in fetching messages. + 🐞 Fixed - `closeConnection()` now uses `normalClosure` status when closing websocket. diff --git a/packages/stream_chat/lib/src/core/api/requests.dart b/packages/stream_chat/lib/src/core/api/requests.dart index 14995b10..6f00b374 100644 --- a/packages/stream_chat/lib/src/core/api/requests.dart +++ b/packages/stream_chat/lib/src/core/api/requests.dart @@ -60,8 +60,11 @@ class PaginationParams extends Equatable { /// ``` const PaginationParams({ this.limit = 10, + this.before = 10, + this.after = 10, this.offset, this.next, + this.idAround, this.greaterThan, this.greaterThanOrEqual, this.lessThan, @@ -78,12 +81,22 @@ class PaginationParams extends Equatable { /// The amount of items requested from the APIs. final int limit; + /// The amount of items requested before message ID from the APIs. + final int before; + + /// The amount of items requested after message ID from the APIs. + final int after; + /// The offset of requesting items. final int? offset; /// A key used to paginate. final String? next; + /// Message ID to fetch messages around + @JsonKey(name: 'id_around') + final String? idAround; + /// Filter on ids greater than the given value. @JsonKey(name: 'id_gt') final String? greaterThan; @@ -106,7 +119,10 @@ class PaginationParams extends Equatable { /// Creates a copy of [PaginationParams] with specified attributes overridden. PaginationParams copyWith({ int? limit, + int? before, + int? after, int? offset, + String? idAround, String? next, String? greaterThan, String? greaterThanOrEqual, @@ -115,7 +131,10 @@ class PaginationParams extends Equatable { }) => PaginationParams( limit: limit ?? this.limit, + before: before ?? this.before, + after: limit ?? this.after, offset: offset ?? this.offset, + idAround: idAround ?? this.idAround, next: next ?? this.next, greaterThan: greaterThan ?? this.greaterThan, greaterThanOrEqual: greaterThanOrEqual ?? this.greaterThanOrEqual, @@ -126,8 +145,11 @@ class PaginationParams extends Equatable { @override List get props => [ limit, + before, + after, offset, next, + idAround, greaterThan, greaterThanOrEqual, lessThan, diff --git a/packages/stream_chat/lib/src/core/api/requests.g.dart b/packages/stream_chat/lib/src/core/api/requests.g.dart index ef995997..7d45ee86 100644 --- a/packages/stream_chat/lib/src/core/api/requests.g.dart +++ b/packages/stream_chat/lib/src/core/api/requests.g.dart @@ -21,8 +21,11 @@ Map _$SortOptionToJson(SortOption instance) => PaginationParams _$PaginationParamsFromJson(Map json) => PaginationParams( limit: json['limit'] as int? ?? 10, + before: json['before'] as int? ?? 10, + after: json['after'] as int? ?? 10, offset: json['offset'] as int?, next: json['next'] as String?, + idAround: json['id_around'] as String?, greaterThan: json['id_gt'] as String?, greaterThanOrEqual: json['id_gte'] as String?, lessThan: json['id_lt'] as String?, @@ -32,6 +35,8 @@ PaginationParams _$PaginationParamsFromJson(Map json) => Map _$PaginationParamsToJson(PaginationParams instance) { final val = { 'limit': instance.limit, + 'before': instance.before, + 'after': instance.after, }; void writeNotNull(String key, dynamic value) { @@ -42,6 +47,7 @@ Map _$PaginationParamsToJson(PaginationParams instance) { writeNotNull('offset', instance.offset); writeNotNull('next', instance.next); + writeNotNull('id_around', instance.idAround); writeNotNull('id_gt', instance.greaterThan); writeNotNull('id_gte', instance.greaterThanOrEqual); writeNotNull('id_lt', instance.lessThan); diff --git a/packages/stream_chat_flutter/CHANGELOG.md b/packages/stream_chat_flutter/CHANGELOG.md index 76dc371d..0088abfa 100644 --- a/packages/stream_chat_flutter/CHANGELOG.md +++ b/packages/stream_chat_flutter/CHANGELOG.md @@ -3,6 +3,7 @@ ✅ Added - `MessageListView` now allows more better control over spacing after messages using `spacingWidgetBuilder`. +- `StreamChannel` can now fetch messages around a message ID with the `queryAroundMessage` call. 🐞 Fixed diff --git a/packages/stream_chat_flutter_core/lib/src/stream_channel.dart b/packages/stream_chat_flutter_core/lib/src/stream_channel.dart index 8117a26e..633c3137 100644 --- a/packages/stream_chat_flutter_core/lib/src/stream_channel.dart +++ b/packages/stream_chat_flutter_core/lib/src/stream_channel.dart @@ -227,13 +227,13 @@ class StreamChannelState extends State { preferOffline: preferOffline, ); - Future> _queryAtMessage({ + Future _queryAtMessage({ String? messageId, int before = 20, int after = 20, bool preferOffline = false, }) async { - if (channel.state == null) return []; + if (channel.state == null) return null; channel.state!.isUpToDate = false; channel.state!.truncate(); @@ -245,23 +245,33 @@ class StreamChannelState extends State { preferOffline: preferOffline, ); channel.state!.isUpToDate = true; - return []; + return null; } - return Future.wait([ - queryBeforeMessage( - messageId, - limit: before, - preferOffline: preferOffline, - ), - queryAfterMessage( - messageId, - limit: after, - preferOffline: preferOffline, - ), - ]); + return queryAroundMessage( + messageId, + before: before, + after: after, + preferOffline: preferOffline, + ); } + /// + Future queryAroundMessage( + String messageId, { + int before = 20, + int after = 20, + bool preferOffline = false, + }) => + channel.query( + messagesPagination: PaginationParams( + idAround: messageId, + before: before, + after: after, + ), + preferOffline: preferOffline, + ); + /// Future queryBeforeMessage( String messageId, {