Skip to content

Commit 3a2b3e3

Browse files
committed
Merge remote-tracking branch 'origin/master' into v10.0.0
# Conflicts: # packages/stream_chat/CHANGELOG.md # packages/stream_chat_flutter_core/CHANGELOG.md
2 parents 74bb460 + 367d3f8 commit 3a2b3e3

31 files changed

+185
-57
lines changed

packages/stream_chat/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## Upcoming
2+
3+
🐞 Fixed
4+
5+
- Fixed cached messages are cleared from channels with unread messages when accessed
6+
offline. [[#2083]](https://github.com/GetStream/stream-chat-flutter/issues/2083)
7+
8+
🔄 Changed
9+
10+
- Deprecated `SortOption.new` constructor in favor of `SortOption.desc` and `SortOption.asc`.
11+
112
## 10.0.0-beta.2
213

314
- Included the changes from version [`9.13.0`](https://pub.dev/packages/stream_chat/changelog).

packages/stream_chat/lib/src/client/channel.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,18 @@ class Channel {
17911791
if (this.state == null) {
17921792
_initState(channelState);
17931793
} else {
1794-
// Otherwise, update the channel state.
1794+
// Otherwise, we update the existing state with the new channel state.
1795+
//
1796+
// But, before updating the state, we check if we are querying around a
1797+
// message, If we are, we have to truncate the state to avoid potential
1798+
// gaps in the message sequence.
1799+
final isQueryingAround = switch (messagesPagination) {
1800+
PaginationParams(idAround: _?) => true,
1801+
PaginationParams(createdAtAround: _?) => true,
1802+
_ => false,
1803+
};
1804+
1805+
if (isQueryingAround) this.state?.truncate();
17951806
this.state?.updateChannelState(channelState);
17961807
}
17971808

packages/stream_chat/lib/src/core/api/requests.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ class PaginationParams extends Equatable {
137137
greaterThanOrEqual,
138138
lessThan,
139139
lessThanOrEqual,
140+
createdAtAfterOrEqual,
141+
createdAtAfter,
142+
createdAtBeforeOrEqual,
143+
createdAtBefore,
144+
createdAtAround,
140145
];
141146
}
142147

packages/stream_chat/lib/src/core/api/sort_order.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class SortOption<T extends ComparableFieldProvider> {
4141
/// ```dart
4242
/// final sorting = SortOption("last_message_at") // Default: descending order
4343
/// ```
44+
@Deprecated('Use SortOption.desc or SortOption.asc instead')
4445
const SortOption(
4546
this.field, {
4647
this.direction = SortOption.DESC,

packages/stream_chat/test/src/client/channel_test.dart

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ void main() {
12731273

12741274
test('should work fine with `query`', () async {
12751275
const query = 'test-search-query';
1276-
const sort = [SortOption('test-sort-field')];
1276+
const sort = [SortOption.asc('test-sort-field')];
12771277
const pagination = PaginationParams();
12781278

12791279
final results = List.generate(3, (index) => GetMessageResponse());
@@ -1306,7 +1306,7 @@ void main() {
13061306

13071307
test('should work fine with `messageFilters`', () async {
13081308
final messageFilters = Filter.query('key', 'text');
1309-
const sort = [SortOption('test-sort-field')];
1309+
const sort = [SortOption.desc('test-sort-field')];
13101310
const pagination = PaginationParams();
13111311

13121312
final results = List.generate(3, (index) => GetMessageResponse());
@@ -2749,6 +2749,121 @@ void main() {
27492749
),
27502750
).called(1);
27512751
});
2752+
2753+
test('should truncate state when querying around message id', () async {
2754+
final initialMessages = [
2755+
Message(id: 'msg1', text: 'Hello 1'),
2756+
Message(id: 'msg2', text: 'Hello 2'),
2757+
Message(id: 'msg3', text: 'Hello 3'),
2758+
];
2759+
2760+
final stateWithMessages = _generateChannelState(
2761+
channelId,
2762+
channelType,
2763+
).copyWith(messages: initialMessages);
2764+
2765+
channel.state!.updateChannelState(stateWithMessages);
2766+
expect(channel.state!.messages, hasLength(3));
2767+
2768+
final newState = _generateChannelState(
2769+
channelId,
2770+
channelType,
2771+
).copyWith(messages: [
2772+
Message(id: 'msg-before-1', text: 'Message before 1'),
2773+
Message(id: 'msg-before-2', text: 'Message before 2'),
2774+
Message(id: 'target-message-id', text: 'Target message'),
2775+
Message(id: 'msg-after-1', text: 'Message after 1'),
2776+
Message(id: 'msg-after-2', text: 'Message after 2'),
2777+
]);
2778+
2779+
when(
2780+
() => client.queryChannel(
2781+
channelType,
2782+
channelId: channelId,
2783+
channelData: any(named: 'channelData'),
2784+
messagesPagination: any(named: 'messagesPagination'),
2785+
membersPagination: any(named: 'membersPagination'),
2786+
watchersPagination: any(named: 'watchersPagination'),
2787+
),
2788+
).thenAnswer((_) async => newState);
2789+
2790+
const pagination = PaginationParams(idAround: 'target-message-id');
2791+
2792+
final res = await channel.query(messagesPagination: pagination);
2793+
2794+
expect(res, isNotNull);
2795+
expect(channel.state!.messages, hasLength(5));
2796+
expect(channel.state!.messages[2].id, 'target-message-id');
2797+
2798+
verify(
2799+
() => client.queryChannel(
2800+
channelType,
2801+
channelId: channelId,
2802+
channelData: any(named: 'channelData'),
2803+
messagesPagination: pagination,
2804+
membersPagination: any(named: 'membersPagination'),
2805+
watchersPagination: any(named: 'watchersPagination'),
2806+
),
2807+
).called(1);
2808+
});
2809+
2810+
test('should truncate state when querying around created date', () async {
2811+
final initialMessages = [
2812+
Message(id: 'msg1', text: 'Hello 1'),
2813+
Message(id: 'msg2', text: 'Hello 2'),
2814+
Message(id: 'msg3', text: 'Hello 3'),
2815+
];
2816+
2817+
final stateWithMessages = _generateChannelState(
2818+
channelId,
2819+
channelType,
2820+
).copyWith(messages: initialMessages);
2821+
2822+
channel.state!.updateChannelState(stateWithMessages);
2823+
expect(channel.state!.messages, hasLength(3));
2824+
2825+
final targetDate = DateTime.now();
2826+
final newState = _generateChannelState(
2827+
channelId,
2828+
channelType,
2829+
).copyWith(messages: [
2830+
Message(id: 'msg-before-1', text: 'Message before 1'),
2831+
Message(id: 'msg-before-2', text: 'Message before 2'),
2832+
Message(id: 'target-message', text: 'Target message'),
2833+
Message(id: 'msg-after-1', text: 'Message after 1'),
2834+
Message(id: 'msg-after-2', text: 'Message after 2'),
2835+
]);
2836+
2837+
when(
2838+
() => client.queryChannel(
2839+
channelType,
2840+
channelId: channelId,
2841+
channelData: any(named: 'channelData'),
2842+
messagesPagination: any(named: 'messagesPagination'),
2843+
membersPagination: any(named: 'membersPagination'),
2844+
watchersPagination: any(named: 'watchersPagination'),
2845+
),
2846+
).thenAnswer((_) async => newState);
2847+
2848+
final pagination = PaginationParams(createdAtAround: targetDate);
2849+
2850+
final res = await channel.query(messagesPagination: pagination);
2851+
2852+
expect(res, isNotNull);
2853+
expect(channel.state!.messages, hasLength(5));
2854+
expect(channel.state!.messages[2].id, 'target-message');
2855+
2856+
verify(
2857+
() => client.queryChannel(
2858+
channelType,
2859+
channelId: channelId,
2860+
channelData: any(named: 'channelData'),
2861+
messagesPagination: pagination,
2862+
membersPagination: any(named: 'membersPagination'),
2863+
watchersPagination: any(named: 'watchersPagination'),
2864+
),
2865+
).called(1);
2866+
});
27522867
});
27532868

27542869
test('`.queryMembers`', () async {

packages/stream_chat/test/src/client/client_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,7 +2243,7 @@ void main() {
22432243

22442244
test('`.queryPolls`', () async {
22452245
final filter = Filter.in_('id', const ['test-poll-id']);
2246-
final sort = [const SortOption<Poll>('created_at')];
2246+
final sort = [const SortOption<Poll>.desc('created_at')];
22472247
const pagination = PaginationParams(limit: 20);
22482248

22492249
final polls = List.generate(
@@ -2285,7 +2285,7 @@ void main() {
22852285
test('`.queryPollVotes`', () async {
22862286
const pollId = 'test-poll-id';
22872287
final filter = Filter.in_('id', const ['test-vote-id']);
2288-
final sort = [const SortOption<PollVote>('created_at')];
2288+
final sort = [const SortOption<PollVote>.desc('created_at')];
22892289
const pagination = PaginationParams(limit: 20);
22902290

22912291
final votes = List.generate(

packages/stream_chat/test/src/core/api/channel_api_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void main() {
122122
const channelType = 'test-channel-type';
123123

124124
final filter = Filter.in_('cid', const ['test-cid']);
125-
const sort = [SortOption<ChannelState>('test-field')];
125+
const sort = [SortOption<ChannelState>.desc('test-field')];
126126
const memberLimit = 33;
127127
const messageLimit = 33;
128128

packages/stream_chat/test/src/core/api/general_api_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void main() {
8686
'should throw if `pagination.offset` and `sort` both are provided',
8787
() async {
8888
final filter = Filter.in_('cid', const ['test-cid-1', 'test-cid-2']);
89-
const sort = [SortOption('test-field')];
89+
const sort = [SortOption.desc('test-field')];
9090
const pagination = PaginationParams(offset: 10);
9191
try {
9292
await generalApi.searchMessages(
@@ -103,7 +103,7 @@ void main() {
103103
test('should run successfully with `query`', () async {
104104
final filter = Filter.in_('cid', const ['test-cid-1', 'test-cid-2']);
105105
const query = 'test-query';
106-
const sort = [SortOption('test-field')];
106+
const sort = [SortOption.desc('test-field')];
107107
const pagination = PaginationParams();
108108

109109
const path = '/search';
@@ -142,7 +142,7 @@ void main() {
142142

143143
test('should run successfully with `messageFilter`', () async {
144144
final filter = Filter.in_('cid', const ['test-cid-1', 'test-cid-2']);
145-
const sort = [SortOption('test-field')];
145+
const sort = [SortOption.desc('test-field')];
146146
final messageFilter = Filter.query('key', 'text');
147147
const pagination = PaginationParams();
148148

@@ -187,7 +187,7 @@ void main() {
187187
const channelId = 'test-channel-id';
188188
final filter = Filter.in_('cid', const ['test-cid-1', 'test-cid-2']);
189189
const pagination = PaginationParams();
190-
const sort = [SortOption<Member>('test-field')];
190+
const sort = [SortOption<Member>.desc('test-field')];
191191

192192
const path = '/members';
193193

@@ -234,7 +234,7 @@ void main() {
234234
const channelType = 'test-channel-type';
235235
final filter = Filter.in_('cid', const ['test-cid-1', 'test-cid-2']);
236236
const pagination = PaginationParams();
237-
const sort = [SortOption<Member>('test-field')];
237+
const sort = [SortOption<Member>.desc('test-field')];
238238

239239
const path = '/members';
240240

packages/stream_chat/test/src/core/api/polls_api_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void main() {
331331
test('queryPolls', () async {
332332
const path = '/polls/query';
333333
final filter = Filter.in_('cid', const ['test-cid-1', 'test-cid-2']);
334-
const sort = [SortOption<Poll>('test-field')];
334+
const sort = [SortOption<Poll>.desc('test-field')];
335335
const pagination = PaginationParams(limit: 20);
336336

337337
final payload = jsonEncode({
@@ -381,7 +381,7 @@ void main() {
381381
test('queryPollVotes', () async {
382382
const pollId = 'test-poll-id';
383383
final filter = Filter.in_('cid', const ['test-cid-1', 'test-cid-2']);
384-
const sort = [SortOption<PollVote>('test-field')];
384+
const sort = [SortOption<PollVote>.desc('test-field')];
385385
const pagination = PaginationParams(limit: 20);
386386

387387
const path = '/polls/$pollId/votes';

packages/stream_chat/test/src/core/api/reminders_api_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void main() {
6868
test('should query reminders with filter, sort, and pagination', () async {
6969
const path = '/reminders/query';
7070
final filter = Filter.equal('userId', 'test-user-id');
71-
const sort = [SortOption<MessageReminder>('remindAt')];
71+
const sort = [SortOption<MessageReminder>.desc('remindAt')];
7272
const pagination = PaginationParams(limit: 10, offset: 5);
7373

7474
final expectedPayload = jsonEncode({

0 commit comments

Comments
 (0)