fix persistence bugs and update example
This commit is contained in:
@@ -618,16 +618,26 @@ class Channel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final response = await _client.post(path, data: payload);
|
try {
|
||||||
final updatedState = _client.decode(response.data, ChannelState.fromJson);
|
final response = await _client.post(path, data: payload);
|
||||||
|
final updatedState = _client.decode(response.data, ChannelState.fromJson);
|
||||||
|
|
||||||
if (_id == null) {
|
if (_id == null) {
|
||||||
_id = updatedState.channel.id;
|
_id = updatedState.channel.id;
|
||||||
_cid = updatedState.channel.cid;
|
_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
|
/// Query channel members
|
||||||
|
|||||||
@@ -173,8 +173,10 @@ abstract class ChatPersistenceClient {
|
|||||||
|
|
||||||
final reactions = channelStates.expand((it) => it.messages).expand((it) {
|
final reactions = channelStates.expand((it) => it.messages).expand((it) {
|
||||||
return [
|
return [
|
||||||
...it.ownReactions.where((r) => r.userId != null),
|
if (it.ownReactions != null)
|
||||||
...it.latestReactions.where((r) => r.userId != null)
|
...it.ownReactions.where((r) => r.userId != null),
|
||||||
|
if (it.latestReactions != null)
|
||||||
|
...it.latestReactions.where((r) => r.userId != null)
|
||||||
];
|
];
|
||||||
}).where((it) => it != null);
|
}).where((it) => it != null);
|
||||||
|
|
||||||
@@ -184,12 +186,14 @@ abstract class ChatPersistenceClient {
|
|||||||
...cs.messages?.map((m) {
|
...cs.messages?.map((m) {
|
||||||
return [
|
return [
|
||||||
m.user,
|
m.user,
|
||||||
...m.latestReactions?.map((r) => r.user),
|
if (m.latestReactions != null)
|
||||||
...m.ownReactions?.map((r) => r.user),
|
...m.latestReactions.map((r) => r.user),
|
||||||
|
if (m.ownReactions != null)
|
||||||
|
...m.ownReactions.map((r) => r.user),
|
||||||
];
|
];
|
||||||
})?.expand((v) => v),
|
})?.expand((v) => v),
|
||||||
...cs.read?.map((r) => r.user),
|
if (cs.read != null) ...cs.read.map((r) => r.user),
|
||||||
...cs.members?.map((m) => m.user),
|
if (cs.members != null) ...cs.members.map((m) => m.user),
|
||||||
])
|
])
|
||||||
.expand((it) => it)
|
.expand((it) => it)
|
||||||
.where((it) => it != null);
|
.where((it) => it != null);
|
||||||
@@ -202,7 +206,7 @@ abstract class ChatPersistenceClient {
|
|||||||
|
|
||||||
final updateReadsFuture = channelStates.map((it) {
|
final updateReadsFuture = channelStates.map((it) {
|
||||||
final cid = it.channel.cid;
|
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));
|
return updateReads(cid, reads.toList(growable: false));
|
||||||
}).toList(growable: false);
|
}).toList(growable: false);
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ Future<void> main() async {
|
|||||||
/// project dashboard.
|
/// project dashboard.
|
||||||
final client = StreamChatClient('b67pax5b2wdq');
|
final client = StreamChatClient('b67pax5b2wdq');
|
||||||
|
|
||||||
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
/// Set the chatPersistenceClient for offline support
|
/// Set the chatPersistenceClient for offline support
|
||||||
client.chatPersistenceClient = StreamChatPersistenceClient(
|
client.chatPersistenceClient = StreamChatPersistenceClient(
|
||||||
logLevel: Level.INFO,
|
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/db/moor_chat_database.dart';
|
||||||
import 'package:stream_chat_persistence/src/entity/messages.dart';
|
import 'package:stream_chat_persistence/src/entity/messages.dart';
|
||||||
import 'package:stream_chat_persistence/src/entity/users.dart';
|
import 'package:stream_chat_persistence/src/entity/users.dart';
|
||||||
|
|
||||||
import '../mapper/mapper.dart';
|
import '../mapper/mapper.dart';
|
||||||
|
|
||||||
part 'message_dao.g.dart';
|
part 'message_dao.g.dart';
|
||||||
@@ -136,7 +137,11 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
|
|||||||
|
|
||||||
/// Updates the message data of a particular channel with
|
/// Updates the message data of a particular channel with
|
||||||
/// the new [messageList] data
|
/// 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) {
|
return batch((batch) {
|
||||||
batch.insertAll(
|
batch.insertAll(
|
||||||
messages,
|
messages,
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ extension MessageX on Message {
|
|||||||
MessageEntity toEntity({String cid}) {
|
MessageEntity toEntity({String cid}) {
|
||||||
return MessageEntity(
|
return MessageEntity(
|
||||||
id: id,
|
id: id,
|
||||||
attachments: attachments.map((it) => jsonEncode(it)).toList(),
|
attachments: attachments?.map((it) => jsonEncode(it))?.toList() ?? [],
|
||||||
channelCid: cid,
|
channelCid: cid,
|
||||||
type: type,
|
type: type,
|
||||||
parentId: parentId,
|
parentId: parentId,
|
||||||
|
|||||||
Reference in New Issue
Block a user