Skip to content

Commit

Permalink
Merge pull request #113 from sendbird/v4.2.18
Browse files Browse the repository at this point in the history
Add 4.2.18.
  • Loading branch information
sf-tyler-jeong authored Jun 26, 2024
2 parents 1418013 + 99abe08 commit c959466
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 80 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v4.2.18 (Jun 26, 2024)

### Improvements
- Fixed the bugs regarding edge cases during `resetMyHistory()` processing
- Fixed a bug that the filtering is not worked on multi `GroupChannelCollection`s
- Fixed a bug that channel changelogs are not upserted in db
- Fixed a bug regarding the result of `init()` in `SendbirdChat`

## v4.2.17 (Jun 18, 2024)

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Before installing Sendbird Chat SDK, you need to create a Sendbird application o

```yaml
dependencies:
sendbird_chat_sdk: ^4.2.17
sendbird_chat_sdk: ^4.2.18
```
- Run `flutter pub get` command in your project directory.
Expand Down
16 changes: 14 additions & 2 deletions lib/src/internal/db/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,20 @@ class DB {
}

// GroupChannel
Future<void> upsertGroupChannel(GroupChannel channel) async {
await CGroupChannel.upsert(_chat, _isar, channel);
Future<void> upsertGroupChannel(
GroupChannel channel, {
bool forceUpsert = false,
}) async {
bool upsert = forceUpsert;

if (!upsert) {
final channelInDb = await getGroupChannel(channel.channelUrl);
upsert = await channelInDb?.canUpdate(channel) ?? true;

if (upsert) {
await CGroupChannel.upsert(_chat, _isar, channel);
}
}
}

Future<GroupChannel?> getGroupChannel(String channelUrl) async {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/internal/main/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ part 'chat_notifications.dart';
part 'chat_push.dart';
part 'chat_user.dart';

const sdkVersion = '4.2.17';
const sdkVersion = '4.2.18';

// Internal implementation for main class. Do not directly access this class.
class Chat with WidgetsBindingObserver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,21 @@ extension GroupChannelCollectionManager on CollectionManager {
// Need to compare channel properties with updatedChannel
// when eventSource is CollectionEventSource.channelChangeLogs (?)

if (await channelCollection.canAddChannel(
eventSource, updatedChannel,
checkToUpdateChannel: true)) {
channelCollection.channelList[index] = updatedChannel;
updatedChannelsForEvent.add(updatedChannel);
} else {
channelCollection.channelList.removeAt(index);
deletedChannelUrlsForEvent.add(updatedChannel.channelUrl);
if (await channel.canUpdate(updatedChannel)) {
if (await channelCollection.canAddChannel(
eventSource,
updatedChannel,
checkToUpdateChannel: true,
)) {
channelCollection.channelList[index] = updatedChannel;
updatedChannelsForEvent.add(updatedChannel);
} else {
channelCollection.channelList.removeAt(index);
deletedChannelUrlsForEvent.add(updatedChannel.channelUrl);
}

isUpdatedChannelInChannelList = true;
}

isUpdatedChannelInChannelList = true;
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ extension MessageCollectionManager on CollectionManager {
}

//------------------------------//
// resetMyHistory
// updateMessageOffsetTimestamp
//------------------------------//
Future<void> resetMyHistory({
Future<void> updateMessageOffsetTimestamp({
required String channelUrl,
int? messageOffsetTimestamp,
required int messageOffsetTimestamp,
}) async {
sbLog.d(StackTrace.current, 'resetMyHistory()');
sbLog.d(StackTrace.current, 'updateMessageOffsetTimestamp()');

for (final collection in baseMessageCollections) {
if (collection is MessageCollection) {
if (collection.isInitialized) {
if (collection.baseChannel.channelUrl == channelUrl) {
await collection.resetMyHistory(
await collection.updateMessageOffsetTimestamp(
channelUrl: channelUrl,
messageOffsetTimestamp: messageOffsetTimestamp,
);
Expand Down Expand Up @@ -610,7 +610,7 @@ extension MessageCollectionManager on CollectionManager {
List<RootMessage>? updatedMessages,
List<dynamic>? deletedMessageIds,
bool doNotSendDeleteEvent = false,
bool isResetMyHistory = false,
bool isMessageOffsetTimestampUpdated = false,
}) async {
sbLog.d(StackTrace.current,
'channelUrl: ${messageCollection.baseChannel.channelUrl}, ${eventSource.toString()}');
Expand All @@ -623,7 +623,8 @@ extension MessageCollectionManager on CollectionManager {
if (_chat.dbManager.isEnabled()) {
// [First] delete
if (deletedMessageIds != null && deletedMessageIds.isNotEmpty) {
if (eventSource != CollectionEventSource.messageInitialize &&
if (!(eventSource == CollectionEventSource.messageInitialize &&
deletedMessageIds.length == addedMessages?.length) && // Check
eventSource != CollectionEventSource.messageLoadPrevious &&
eventSource != CollectionEventSource.messageLoadNext) {
List<String> deletedStringIds = deletedMessageIds
Expand Down Expand Up @@ -772,7 +773,8 @@ extension MessageCollectionManager on CollectionManager {
}

//+ [DBManager]
if (eventSource == CollectionEventSource.messageFill || isResetMyHistory) {
if (eventSource == CollectionEventSource.messageFill ||
isMessageOffsetTimestampUpdated) {
messageCollection.setValuesFromMessageList(); // Check
}
//- [DBManager]
Expand Down
18 changes: 13 additions & 5 deletions lib/src/internal/main/chat_manager/command_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CommandManager {
final Map<String, Completer<Command?>> _completerMap = {};
final Map<String, Timer> _ackTimerMap = {};
final Map<String, int> _readMap = {};
final Map<String, Completer<int?>> messageOffsetTsCompleterMap = {};

final Chat _chat;

Expand Down Expand Up @@ -84,6 +85,7 @@ class CommandManager {
}
_ackTimerMap.clear();
_readMap.clear();
messageOffsetTsCompleterMap.clear();
}

void clearCompleterMap({SendbirdException? e}) {
Expand Down Expand Up @@ -417,11 +419,17 @@ class CommandManager {

final GroupChannel? groupChannel = _eitherGroupOrFeed(channel);
if (groupChannel != null) {
if (groupChannel.messageOffsetTimestamp != null &&
message.createdAt <= groupChannel.messageOffsetTimestamp!) {
sbLog.d(StackTrace.current,
'A received message before messageOffsetTimestamp is ignored.');
return; // Check
int? messageOffsetTs =
(await messageOffsetTsCompleterMap[groupChannel.channelUrl]
?.future) ??
groupChannel.messageOffsetTimestamp;

if (messageOffsetTs != null) {
if (message.createdAt < messageOffsetTs) {
sbLog.d(StackTrace.current,
'A received message before messageOffsetTimestamp is ignored.');
return; // Check
}
}

if (groupChannel.hiddenState ==
Expand Down
7 changes: 5 additions & 2 deletions lib/src/internal/main/chat_manager/db_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,13 @@ class DBManager {
return (await _db.getGroupChannelCount() > 0);
}

Future<void> upsertGroupChannels(List<GroupChannel> channels) async {
Future<void> upsertGroupChannels(
List<GroupChannel> channels, {
bool forceUpsert = false,
}) async {
if (isEnabled()) {
for (final channel in channels) {
await _db.upsertGroupChannel(channel);
await _db.upsertGroupChannel(channel, forceUpsert: forceUpsert);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/src/public/core/channel/group_channel/group_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ class GroupChannel extends BaseChannel {
chat.channelCache.setCachedDeliveryStatus(channelUrl, deliveryStatus);
}

Future<bool> canUpdate(GroupChannel channel) async {
if (messageOffsetTimestamp != null && messageOffsetTimestamp != 0) {
if (channel.messageOffsetTimestamp == null ||
channel.messageOffsetTimestamp! < messageOffsetTimestamp!) {
sbLog.d(StackTrace.current,
'Can not update regarding messageOffsetTimestamp in channel.');
return false;
}
}
return true;
}

factory GroupChannel.fromJson(Map<String, dynamic> json) {
return _$GroupChannelFromJson(json)
..set(SendbirdChat().chat); // Set the singleton chat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,66 @@ extension GroupChannelConfiguration on GroupChannel {
sbLog.i(StackTrace.current,
'hidePreviousMessage: $hidePreviousMessage, allowAutoUnhide: $allowAutoUnhide');

final res = await chat.apiClient.send(GroupChannelHideRequest(
chat,
channelUrl,
hidePreviousMessages: hidePreviousMessage,
allowAutoUnhide: allowAutoUnhide,
));
if (chat.commandManager.messageOffsetTsCompleterMap[channelUrl] != null) {
return;
}
chat.commandManager.messageOffsetTsCompleterMap[channelUrl] =
Completer<int?>();

messageOffsetTimestamp = res?['ts_message_offset'] as int?;
if (hidePreviousMessage) clearUnreadCount();
try {
bool isOffsetChanged = false;

isHidden = true;
hiddenState = allowAutoUnhide
? GroupChannelHiddenState.allowAutoUnhide
: GroupChannelHiddenState.preventAutoUnhide;
final res = await chat.apiClient.send(GroupChannelHideRequest(
chat,
channelUrl,
hidePreviousMessages: hidePreviousMessage,
allowAutoUnhide: allowAutoUnhide,
));

final offset = res?['ts_message_offset'] as int?;
if (offset != null && offset != messageOffsetTimestamp) {
messageOffsetTimestamp = offset;
isOffsetChanged = true;

if (lastMessage?.message != null) {
if (lastMessage!.createdAt < messageOffsetTimestamp!) {
lastMessage = null;
}
}

if (hidePreviousMessage) {
clearUnreadCount();
}

isHidden = true;
hiddenState = allowAutoUnhide
? GroupChannelHiddenState.allowAutoUnhide
: GroupChannelHiddenState.preventAutoUnhide;

saveToCache(chat);

//+ [DBManager]
if (chat.dbManager.isEnabled()) {
await chat.dbManager.upsertGroupChannels([this], forceUpsert: true);
}
//- [DBManager]
}

if (isOffsetChanged) {
await chat.collectionManager.updateMessageOffsetTimestamp(
channelUrl: channelUrl,
messageOffsetTimestamp: messageOffsetTimestamp!,
);
}
} catch (_) {
rethrow;
} finally {
if (chat.commandManager.messageOffsetTsCompleterMap[channelUrl] != null) {
chat.commandManager.messageOffsetTsCompleterMap[channelUrl]!
.complete(messageOffsetTimestamp);
chat.commandManager.messageOffsetTsCompleterMap.remove(channelUrl);
}
}
}

/// Unhides this channel from the current `User`'s `GroupChannel` list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,51 @@ extension GroupChannelOperation on GroupChannel {
Future<void> resetMyHistory() async {
sbLog.i(StackTrace.current);

final offset = await chat.apiClient
.send(GroupChannelHistoryResetRequest(chat, channelUrl: channelUrl));
if (offset != null) {
messageOffsetTimestamp = offset;
if (chat.commandManager.messageOffsetTsCompleterMap[channelUrl] != null) {
return;
}
chat.commandManager.messageOffsetTsCompleterMap[channelUrl] =
Completer<int?>();

await chat.collectionManager.resetMyHistory(
channelUrl: channelUrl,
messageOffsetTimestamp: messageOffsetTimestamp,
);
try {
bool isOffsetChanged = false;

final offset = await chat.apiClient
.send(GroupChannelHistoryResetRequest(chat, channelUrl: channelUrl));

if (offset != null && offset != messageOffsetTimestamp) {
messageOffsetTimestamp = offset;
isOffsetChanged = true;

if (lastMessage?.message != null) {
if (lastMessage!.createdAt < messageOffsetTimestamp!) {
lastMessage = null;
}
}

saveToCache(chat);

//+ [DBManager]
if (chat.dbManager.isEnabled()) {
await chat.dbManager.upsertGroupChannels([this], forceUpsert: true);
}
//- [DBManager]
}

if (isOffsetChanged) {
await chat.collectionManager.updateMessageOffsetTimestamp(
channelUrl: channelUrl,
messageOffsetTimestamp: messageOffsetTimestamp!,
);
}
} catch (_) {
rethrow;
} finally {
if (chat.commandManager.messageOffsetTsCompleterMap[channelUrl] != null) {
chat.commandManager.messageOffsetTsCompleterMap[channelUrl]!
.complete(messageOffsetTimestamp);
chat.commandManager.messageOffsetTsCompleterMap.remove(channelUrl);
}
}
}
}
2 changes: 1 addition & 1 deletion lib/src/public/main/chat/sendbird_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SendbirdChat {
required String appId,
SendbirdChatOptions? options,
}) async {
bool result = false;
bool result = true;

_instance._chat.chatContext.init(
chat: _instance._chat,
Expand Down
Loading

0 comments on commit c959466

Please sign in to comment.