Skip to content

Commit c959466

Browse files
Merge pull request #113 from sendbird/v4.2.18
Add 4.2.18.
2 parents 1418013 + 99abe08 commit c959466

File tree

15 files changed

+223
-80
lines changed

15 files changed

+223
-80
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## v4.2.18 (Jun 26, 2024)
2+
3+
### Improvements
4+
- Fixed the bugs regarding edge cases during `resetMyHistory()` processing
5+
- Fixed a bug that the filtering is not worked on multi `GroupChannelCollection`s
6+
- Fixed a bug that channel changelogs are not upserted in db
7+
- Fixed a bug regarding the result of `init()` in `SendbirdChat`
8+
19
## v4.2.17 (Jun 18, 2024)
210

311
### Improvements

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Before installing Sendbird Chat SDK, you need to create a Sendbird application o
5050

5151
```yaml
5252
dependencies:
53-
sendbird_chat_sdk: ^4.2.17
53+
sendbird_chat_sdk: ^4.2.18
5454
```
5555
5656
- Run `flutter pub get` command in your project directory.

lib/src/internal/db/db.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,20 @@ class DB {
234234
}
235235

236236
// GroupChannel
237-
Future<void> upsertGroupChannel(GroupChannel channel) async {
238-
await CGroupChannel.upsert(_chat, _isar, channel);
237+
Future<void> upsertGroupChannel(
238+
GroupChannel channel, {
239+
bool forceUpsert = false,
240+
}) async {
241+
bool upsert = forceUpsert;
242+
243+
if (!upsert) {
244+
final channelInDb = await getGroupChannel(channel.channelUrl);
245+
upsert = await channelInDb?.canUpdate(channel) ?? true;
246+
247+
if (upsert) {
248+
await CGroupChannel.upsert(_chat, _isar, channel);
249+
}
250+
}
239251
}
240252

241253
Future<GroupChannel?> getGroupChannel(String channelUrl) async {

lib/src/internal/main/chat/chat.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ part 'chat_notifications.dart';
6262
part 'chat_push.dart';
6363
part 'chat_user.dart';
6464

65-
const sdkVersion = '4.2.17';
65+
const sdkVersion = '4.2.18';
6666

6767
// Internal implementation for main class. Do not directly access this class.
6868
class Chat with WidgetsBindingObserver {

lib/src/internal/main/chat_manager/collection_manager/group_channel_collection_manager.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,21 @@ extension GroupChannelCollectionManager on CollectionManager {
236236
// Need to compare channel properties with updatedChannel
237237
// when eventSource is CollectionEventSource.channelChangeLogs (?)
238238

239-
if (await channelCollection.canAddChannel(
240-
eventSource, updatedChannel,
241-
checkToUpdateChannel: true)) {
242-
channelCollection.channelList[index] = updatedChannel;
243-
updatedChannelsForEvent.add(updatedChannel);
244-
} else {
245-
channelCollection.channelList.removeAt(index);
246-
deletedChannelUrlsForEvent.add(updatedChannel.channelUrl);
239+
if (await channel.canUpdate(updatedChannel)) {
240+
if (await channelCollection.canAddChannel(
241+
eventSource,
242+
updatedChannel,
243+
checkToUpdateChannel: true,
244+
)) {
245+
channelCollection.channelList[index] = updatedChannel;
246+
updatedChannelsForEvent.add(updatedChannel);
247+
} else {
248+
channelCollection.channelList.removeAt(index);
249+
deletedChannelUrlsForEvent.add(updatedChannel.channelUrl);
250+
}
251+
252+
isUpdatedChannelInChannelList = true;
247253
}
248-
249-
isUpdatedChannelInChannelList = true;
250254
break;
251255
}
252256
}

lib/src/internal/main/chat_manager/collection_manager/message_collection_manager.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,19 @@ extension MessageCollectionManager on CollectionManager {
8484
}
8585

8686
//------------------------------//
87-
// resetMyHistory
87+
// updateMessageOffsetTimestamp
8888
//------------------------------//
89-
Future<void> resetMyHistory({
89+
Future<void> updateMessageOffsetTimestamp({
9090
required String channelUrl,
91-
int? messageOffsetTimestamp,
91+
required int messageOffsetTimestamp,
9292
}) async {
93-
sbLog.d(StackTrace.current, 'resetMyHistory()');
93+
sbLog.d(StackTrace.current, 'updateMessageOffsetTimestamp()');
9494

9595
for (final collection in baseMessageCollections) {
9696
if (collection is MessageCollection) {
9797
if (collection.isInitialized) {
9898
if (collection.baseChannel.channelUrl == channelUrl) {
99-
await collection.resetMyHistory(
99+
await collection.updateMessageOffsetTimestamp(
100100
channelUrl: channelUrl,
101101
messageOffsetTimestamp: messageOffsetTimestamp,
102102
);
@@ -610,7 +610,7 @@ extension MessageCollectionManager on CollectionManager {
610610
List<RootMessage>? updatedMessages,
611611
List<dynamic>? deletedMessageIds,
612612
bool doNotSendDeleteEvent = false,
613-
bool isResetMyHistory = false,
613+
bool isMessageOffsetTimestampUpdated = false,
614614
}) async {
615615
sbLog.d(StackTrace.current,
616616
'channelUrl: ${messageCollection.baseChannel.channelUrl}, ${eventSource.toString()}');
@@ -623,7 +623,8 @@ extension MessageCollectionManager on CollectionManager {
623623
if (_chat.dbManager.isEnabled()) {
624624
// [First] delete
625625
if (deletedMessageIds != null && deletedMessageIds.isNotEmpty) {
626-
if (eventSource != CollectionEventSource.messageInitialize &&
626+
if (!(eventSource == CollectionEventSource.messageInitialize &&
627+
deletedMessageIds.length == addedMessages?.length) && // Check
627628
eventSource != CollectionEventSource.messageLoadPrevious &&
628629
eventSource != CollectionEventSource.messageLoadNext) {
629630
List<String> deletedStringIds = deletedMessageIds
@@ -772,7 +773,8 @@ extension MessageCollectionManager on CollectionManager {
772773
}
773774

774775
//+ [DBManager]
775-
if (eventSource == CollectionEventSource.messageFill || isResetMyHistory) {
776+
if (eventSource == CollectionEventSource.messageFill ||
777+
isMessageOffsetTimestampUpdated) {
776778
messageCollection.setValuesFromMessageList(); // Check
777779
}
778780
//- [DBManager]

lib/src/internal/main/chat_manager/command_manager.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class CommandManager {
4747
final Map<String, Completer<Command?>> _completerMap = {};
4848
final Map<String, Timer> _ackTimerMap = {};
4949
final Map<String, int> _readMap = {};
50+
final Map<String, Completer<int?>> messageOffsetTsCompleterMap = {};
5051

5152
final Chat _chat;
5253

@@ -84,6 +85,7 @@ class CommandManager {
8485
}
8586
_ackTimerMap.clear();
8687
_readMap.clear();
88+
messageOffsetTsCompleterMap.clear();
8789
}
8890

8991
void clearCompleterMap({SendbirdException? e}) {
@@ -417,11 +419,17 @@ class CommandManager {
417419

418420
final GroupChannel? groupChannel = _eitherGroupOrFeed(channel);
419421
if (groupChannel != null) {
420-
if (groupChannel.messageOffsetTimestamp != null &&
421-
message.createdAt <= groupChannel.messageOffsetTimestamp!) {
422-
sbLog.d(StackTrace.current,
423-
'A received message before messageOffsetTimestamp is ignored.');
424-
return; // Check
422+
int? messageOffsetTs =
423+
(await messageOffsetTsCompleterMap[groupChannel.channelUrl]
424+
?.future) ??
425+
groupChannel.messageOffsetTimestamp;
426+
427+
if (messageOffsetTs != null) {
428+
if (message.createdAt < messageOffsetTs) {
429+
sbLog.d(StackTrace.current,
430+
'A received message before messageOffsetTimestamp is ignored.');
431+
return; // Check
432+
}
425433
}
426434

427435
if (groupChannel.hiddenState ==

lib/src/internal/main/chat_manager/db_manager.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,13 @@ class DBManager {
264264
return (await _db.getGroupChannelCount() > 0);
265265
}
266266

267-
Future<void> upsertGroupChannels(List<GroupChannel> channels) async {
267+
Future<void> upsertGroupChannels(
268+
List<GroupChannel> channels, {
269+
bool forceUpsert = false,
270+
}) async {
268271
if (isEnabled()) {
269272
for (final channel in channels) {
270-
await _db.upsertGroupChannel(channel);
273+
await _db.upsertGroupChannel(channel, forceUpsert: forceUpsert);
271274
}
272275
}
273276
}

lib/src/public/core/channel/group_channel/group_channel.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@ class GroupChannel extends BaseChannel {
421421
chat.channelCache.setCachedDeliveryStatus(channelUrl, deliveryStatus);
422422
}
423423

424+
Future<bool> canUpdate(GroupChannel channel) async {
425+
if (messageOffsetTimestamp != null && messageOffsetTimestamp != 0) {
426+
if (channel.messageOffsetTimestamp == null ||
427+
channel.messageOffsetTimestamp! < messageOffsetTimestamp!) {
428+
sbLog.d(StackTrace.current,
429+
'Can not update regarding messageOffsetTimestamp in channel.');
430+
return false;
431+
}
432+
}
433+
return true;
434+
}
435+
424436
factory GroupChannel.fromJson(Map<String, dynamic> json) {
425437
return _$GroupChannelFromJson(json)
426438
..set(SendbirdChat().chat); // Set the singleton chat

lib/src/public/core/channel/group_channel/group_channel_configuration.dart

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,66 @@ extension GroupChannelConfiguration on GroupChannel {
1616
sbLog.i(StackTrace.current,
1717
'hidePreviousMessage: $hidePreviousMessage, allowAutoUnhide: $allowAutoUnhide');
1818

19-
final res = await chat.apiClient.send(GroupChannelHideRequest(
20-
chat,
21-
channelUrl,
22-
hidePreviousMessages: hidePreviousMessage,
23-
allowAutoUnhide: allowAutoUnhide,
24-
));
19+
if (chat.commandManager.messageOffsetTsCompleterMap[channelUrl] != null) {
20+
return;
21+
}
22+
chat.commandManager.messageOffsetTsCompleterMap[channelUrl] =
23+
Completer<int?>();
2524

26-
messageOffsetTimestamp = res?['ts_message_offset'] as int?;
27-
if (hidePreviousMessage) clearUnreadCount();
25+
try {
26+
bool isOffsetChanged = false;
2827

29-
isHidden = true;
30-
hiddenState = allowAutoUnhide
31-
? GroupChannelHiddenState.allowAutoUnhide
32-
: GroupChannelHiddenState.preventAutoUnhide;
28+
final res = await chat.apiClient.send(GroupChannelHideRequest(
29+
chat,
30+
channelUrl,
31+
hidePreviousMessages: hidePreviousMessage,
32+
allowAutoUnhide: allowAutoUnhide,
33+
));
34+
35+
final offset = res?['ts_message_offset'] as int?;
36+
if (offset != null && offset != messageOffsetTimestamp) {
37+
messageOffsetTimestamp = offset;
38+
isOffsetChanged = true;
39+
40+
if (lastMessage?.message != null) {
41+
if (lastMessage!.createdAt < messageOffsetTimestamp!) {
42+
lastMessage = null;
43+
}
44+
}
45+
46+
if (hidePreviousMessage) {
47+
clearUnreadCount();
48+
}
49+
50+
isHidden = true;
51+
hiddenState = allowAutoUnhide
52+
? GroupChannelHiddenState.allowAutoUnhide
53+
: GroupChannelHiddenState.preventAutoUnhide;
54+
55+
saveToCache(chat);
56+
57+
//+ [DBManager]
58+
if (chat.dbManager.isEnabled()) {
59+
await chat.dbManager.upsertGroupChannels([this], forceUpsert: true);
60+
}
61+
//- [DBManager]
62+
}
63+
64+
if (isOffsetChanged) {
65+
await chat.collectionManager.updateMessageOffsetTimestamp(
66+
channelUrl: channelUrl,
67+
messageOffsetTimestamp: messageOffsetTimestamp!,
68+
);
69+
}
70+
} catch (_) {
71+
rethrow;
72+
} finally {
73+
if (chat.commandManager.messageOffsetTsCompleterMap[channelUrl] != null) {
74+
chat.commandManager.messageOffsetTsCompleterMap[channelUrl]!
75+
.complete(messageOffsetTimestamp);
76+
chat.commandManager.messageOffsetTsCompleterMap.remove(channelUrl);
77+
}
78+
}
3379
}
3480

3581
/// Unhides this channel from the current `User`'s `GroupChannel` list.

0 commit comments

Comments
 (0)