fix persistence bugs and update example

This commit is contained in:
Salvatore Giordano
2021-02-01 16:50:52 +01:00
parent fbf5e125c6
commit 711c20b967
5 changed files with 38 additions and 17 deletions
+18 -8
View File
@@ -618,16 +618,26 @@ class Channel {
}
}
final response = await _client.post(path, data: payload);
final updatedState = _client.decode(response.data, ChannelState.fromJson);
try {
final response = await _client.post(path, data: payload);
final updatedState = _client.decode(response.data, ChannelState.fromJson);
if (_id == null) {
_id = updatedState.channel.id;
_cid = updatedState.channel.cid;
if (_id == null) {
_id = updatedState.channel.id;
_cid = updatedState.channel.cid;
}
state?.updateChannelState(updatedState);
return updatedState;
} catch (e) {
if (!_client.persistenceEnabled) {
rethrow;
}
return _client.chatPersistenceClient?.getChannelStateByCid(
cid,
messagePagination: messagesPagination,
);
}
state?.updateChannelState(updatedState);
return updatedState;
}
/// Query channel members
@@ -173,8 +173,10 @@ abstract class ChatPersistenceClient {
final reactions = channelStates.expand((it) => it.messages).expand((it) {
return [
...it.ownReactions.where((r) => r.userId != null),
...it.latestReactions.where((r) => r.userId != null)
if (it.ownReactions != null)
...it.ownReactions.where((r) => r.userId != null),
if (it.latestReactions != null)
...it.latestReactions.where((r) => r.userId != null)
];
}).where((it) => it != null);
@@ -184,12 +186,14 @@ abstract class ChatPersistenceClient {
...cs.messages?.map((m) {
return [
m.user,
...m.latestReactions?.map((r) => r.user),
...m.ownReactions?.map((r) => r.user),
if (m.latestReactions != null)
...m.latestReactions.map((r) => r.user),
if (m.ownReactions != null)
...m.ownReactions.map((r) => r.user),
];
})?.expand((v) => v),
...cs.read?.map((r) => r.user),
...cs.members?.map((m) => m.user),
if (cs.read != null) ...cs.read.map((r) => r.user),
if (cs.members != null) ...cs.members.map((m) => m.user),
])
.expand((it) => it)
.where((it) => it != null);
@@ -202,7 +206,7 @@ abstract class ChatPersistenceClient {
final updateReadsFuture = channelStates.map((it) {
final cid = it.channel.cid;
final reads = it.read.where((it) => it != null);
final reads = it.read?.where((it) => it != null) ?? [];
return updateReads(cid, reads.toList(growable: false));
}).toList(growable: false);
@@ -7,6 +7,8 @@ Future<void> main() async {
/// project dashboard.
final client = StreamChatClient('b67pax5b2wdq');
WidgetsFlutterBinding.ensureInitialized();
/// Set the chatPersistenceClient for offline support
client.chatPersistenceClient = StreamChatPersistenceClient(
logLevel: Level.INFO,
@@ -3,6 +3,7 @@ import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_persistence/src/db/moor_chat_database.dart';
import 'package:stream_chat_persistence/src/entity/messages.dart';
import 'package:stream_chat_persistence/src/entity/users.dart';
import '../mapper/mapper.dart';
part 'message_dao.g.dart';
@@ -136,7 +137,11 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
/// Updates the message data of a particular channel with
/// the new [messageList] data
Future<void> updateMessages(String cid, List<Message> messageList) {
Future<void> updateMessages(String cid, List<Message> messageList) async {
if (messageList == null) {
return;
}
return batch((batch) {
batch.insertAll(
messages,
@@ -47,7 +47,7 @@ extension MessageX on Message {
MessageEntity toEntity({String cid}) {
return MessageEntity(
id: id,
attachments: attachments.map((it) => jsonEncode(it)).toList(),
attachments: attachments?.map((it) => jsonEncode(it))?.toList() ?? [],
channelCid: cid,
type: type,
parentId: parentId,