add client_test.dart

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-06-16 22:19:00 +05:30
parent 7c9bc3b950
commit e8e1d53aa3
12 changed files with 2356 additions and 75 deletions
@@ -22,9 +22,9 @@ class Channel {
this._client,
this._type,
this._id, {
Map<String, Object?> extraData = const {},
Map<String, Object?>? extraData,
}) : _cid = _id != null ? '$_type:$_id' : null,
_extraData = extraData {
_extraData = extraData ?? {} {
_client.logger.info('New Channel instance not initialized created');
}
@@ -202,8 +202,13 @@ class Channel {
}
/// Channel extra data
Map<String, dynamic> get extraData =>
state?._channelState.channel?.extraData ?? _extraData;
Map<String, Object?> get extraData {
var data = state?._channelState.channel?.extraData;
if (data == null || data.isEmpty) {
data = _extraData;
}
return data;
}
/// Channel extra data as a stream
Stream<Map<String, dynamic>> get extraDataStream {
@@ -942,7 +947,7 @@ class Channel {
state = ChannelClientState(this, channelState);
if (cid != null) {
client.state.channels[cid!] = this;
client.state.channels = {cid!: this};
}
if (!_initializedCompleter.isCompleted) {
_initializedCompleter.complete(true);
@@ -959,8 +964,8 @@ class Channel {
/// Set [preferOffline] to true to avoid the api call if the data is already
/// in the offline storage
Future<QueryRepliesResponse> getReplies(
String parentId,
PaginationParams options, {
String parentId, {
PaginationParams? options,
bool preferOffline = false,
}) async {
final cachedReplies = await _client.chatPersistenceClient?.getReplies(
@@ -973,19 +978,22 @@ class Channel {
return QueryRepliesResponse()..messages = cachedReplies;
}
}
final repliesResponse = await _client.getReplies(parentId, options);
final repliesResponse = await _client.getReplies(
parentId,
options: options,
);
state?.updateThreadInfo(parentId, repliesResponse.messages);
return repliesResponse;
}
/// List the reactions for a message in the channel
Future<QueryReactionsResponse> getReactions(
String messageId,
PaginationParams options,
) =>
String messageId, {
PaginationParams? options,
}) =>
_client.getReactions(
messageId,
options,
options: options,
);
/// Retrieves a list of messages by ID
+39 -42
View File
@@ -63,7 +63,7 @@ class StreamChatClient {
/// application.
StreamChatClient(
String apiKey, {
this.logLevel = Level.ALL,
this.logLevel = Level.WARNING,
LogHandlerFunction? logHandlerFunction,
RetryPolicy? retryPolicy,
Location? location,
@@ -71,9 +71,11 @@ class StreamChatClient {
Duration connectTimeout = const Duration(seconds: 6),
Duration receiveTimeout = const Duration(seconds: 6),
StreamChatApi? chatApi,
WebSocket? ws,
AttachmentFileUploader? attachmentFileUploader,
}) {
_setupLogger(logHandlerFunction);
this.logHandlerFunction = logHandlerFunction ?? _defaultLogHandler;
logger.info('Initiating new StreamChatClient');
final options = StreamHttpClientOptions(
baseUrl: baseURL,
@@ -92,13 +94,14 @@ class StreamChatClient {
logger: detachedLogger('🕸️'),
);
_ws = WebSocket(
apiKey: apiKey,
baseUrl: options.baseUrl,
tokenManager: _tokenManager,
handler: handleEvent,
logger: detachedLogger('🔌'),
);
_ws = ws ??
WebSocket(
apiKey: apiKey,
baseUrl: options.baseUrl,
tokenManager: _tokenManager,
handler: handleEvent,
logger: detachedLogger('🔌'),
);
_retryPolicy = retryPolicy ??
RetryPolicy(
@@ -107,8 +110,6 @@ class StreamChatClient {
);
state = ClientState(this);
logger.info('instantiating new client');
}
late final StreamChatApi _chatApi;
@@ -160,7 +161,7 @@ class StreamChatClient {
/// Client specific logger instance.
/// Refer to the class [Logger] to learn more about the specific
/// implementation.
final Logger logger = Logger.detached('📡');
late final Logger logger = detachedLogger('📡');
/// A function that has a parameter of type [LogRecord].
/// This is called on every new log record.
@@ -185,7 +186,7 @@ class StreamChatClient {
final _eventController = BehaviorSubject<Event>();
/// Stream of [Event] coming from websocket connection
/// Stream of [Event] coming from [_ws] connection
/// Listen to this or use the [on] method to filter specific event types
Stream<Event> get eventStream => _eventController.stream;
@@ -195,12 +196,12 @@ class StreamChatClient {
set _wsConnectionStatus(ConnectionStatus status) =>
_wsConnectionStatusController.add(status);
/// The current status value of the websocket connection
/// The current status value of the [_ws] connection
ConnectionStatus get wsConnectionStatus =>
_wsConnectionStatusController.value;
/// This notifies the connection status of the websocket connection.
/// Listen to this to get notified when the websocket tries to reconnect.
/// This notifies the connection status of the [_ws] connection.
/// Listen to this to get notified when the [_ws] tries to reconnect.
Stream<ConnectionStatus> get wsConnectionStatusStream =>
_wsConnectionStatusController.stream.distinct();
@@ -215,19 +216,9 @@ class StreamChatClient {
};
///
Logger detachedLogger(
String name,
) =>
Logger.detached(name)
..level = logLevel
..onRecord.listen(logHandlerFunction);
void _setupLogger(LogHandlerFunction? logHandlerFunction) {
logger.level = logLevel;
this.logHandlerFunction = logHandlerFunction ?? _defaultLogHandler;
logger.onRecord.listen(this.logHandlerFunction);
logger.info('logger setup');
}
Logger detachedLogger(String name) => Logger.detached(name)
..level = logLevel
..onRecord.listen(logHandlerFunction);
/// Connects the current user, this triggers a connection to the API.
/// It returns a [Future] that resolves when the connection is setup.
@@ -325,9 +316,13 @@ class StreamChatClient {
_wsConnectionStatus = ConnectionStatus.connecting;
_connectionStatusSubscription = _ws.connectionStatusStream.listen(
_connectionStatusHandler,
);
// skipping `ws` seed connection status -> ConnectionStatus.disconnected
// otherwise `client.wsConnectionStatusStream` will emit in order
// 1. ConnectionStatus.disconnected -> client seed status
// 2. ConnectionStatus.connecting -> client connecting status
// 3. ConnectionStatus.disconnected -> ws seed status
_connectionStatusSubscription =
_ws.connectionStatusStream.skip(1).listen(_connectionStatusHandler);
try {
return await _ws.connect(user);
@@ -768,13 +763,13 @@ class StreamChatClient {
_chatApi.device.removeDevice(id);
/// Get a development token
String devToken(String userId) => Token.development(userId).rawValue;
Token devToken(String userId) => Token.development(userId);
/// Returns a channel client with the given type, id and custom data.
Channel channel(
String type, {
String? id,
Map<String, Object?> extraData = const {},
Map<String, Object?>? extraData,
}) {
if (id != null && state.channels.containsKey('$type:$id')) {
return state.channels['$type:$id']!;
@@ -796,6 +791,7 @@ class StreamChatClient {
);
/// watches the provided channel
/// Creates first if not yet created
Future<ChannelState> watchChannel(
String channelType, {
String? channelId,
@@ -809,6 +805,7 @@ class StreamChatClient {
);
/// Query the API, get messages, members or other channel fields
/// Creates the channel first if not yet created
Future<ChannelState> queryChannel(
String channelType, {
bool state = true,
@@ -1139,22 +1136,22 @@ class StreamChatClient {
/// Lists all the message replies for the [parentId]
Future<QueryRepliesResponse> getReplies(
String parentId,
PaginationParams options,
) =>
String parentId, {
PaginationParams? options,
}) =>
_chatApi.message.getReplies(
parentId,
options,
options: options,
);
/// Get all the reactions for a [messageId]
Future<QueryReactionsResponse> getReactions(
String messageId,
PaginationParams options,
) =>
String messageId, {
PaginationParams? options,
}) =>
_chatApi.message.getReactions(
messageId,
options,
options: options,
);
/// Update the given message
@@ -142,13 +142,13 @@ class MessageApi {
/// Get all the reactions for a [messageId]
Future<QueryReactionsResponse> getReactions(
String messageId,
PaginationParams options,
) async {
String messageId, {
PaginationParams? options,
}) async {
final response = await _client.get(
'/messages/$messageId/reactions',
queryParameters: {
...options.toJson(),
if (options != null) ...options.toJson(),
},
);
return QueryReactionsResponse.fromJson(response.data);
@@ -168,13 +168,13 @@ class MessageApi {
/// Lists all the message replies for the [parentId]
Future<QueryRepliesResponse> getReplies(
String parentId,
PaginationParams options,
) async {
String parentId, {
PaginationParams? options,
}) async {
final response = await _client.get(
'/messages/$parentId/replies',
queryParameters: {
...options.toJson(),
if (options != null) ...options.toJson(),
},
);
return QueryRepliesResponse.fromJson(response.data);