add comments

This commit is contained in:
Salvatore Giordano
2021-06-22 14:20:43 +02:00
parent 00e184f422
commit c987e8b488
18 changed files with 89 additions and 91 deletions
+12 -14
View File
@@ -6,33 +6,31 @@ import 'package:dio/dio.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
import 'package:stream_chat/src/client/channel.dart'; import 'package:stream_chat/src/client/channel.dart';
import 'package:stream_chat/src/core/util/utils.dart'; import 'package:stream_chat/src/client/retry_policy.dart';
import 'package:stream_chat/src/core/error/error.dart';
import 'package:stream_chat/src/location.dart';
import 'package:stream_chat/src/ws/connection_status.dart';
import 'package:stream_chat/src/core/api/attachment_file_uploader.dart'; import 'package:stream_chat/src/core/api/attachment_file_uploader.dart';
import 'package:stream_chat/src/core/api/requests.dart'; import 'package:stream_chat/src/core/api/requests.dart';
import 'package:stream_chat/src/core/api/responses.dart'; import 'package:stream_chat/src/core/api/responses.dart';
import 'package:stream_chat/src/client/retry_policy.dart';
import 'package:stream_chat/src/ws/websocket.dart';
import 'package:stream_chat/src/core/api/stream_chat_api.dart'; import 'package:stream_chat/src/core/api/stream_chat_api.dart';
import 'package:stream_chat/src/core/error/error.dart';
import 'package:stream_chat/src/core/http/connection_id_manager.dart'; import 'package:stream_chat/src/core/http/connection_id_manager.dart';
import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/http/token.dart'; import 'package:stream_chat/src/core/http/token.dart';
import 'package:stream_chat/src/core/http/token_manager.dart'; import 'package:stream_chat/src/core/http/token_manager.dart';
import 'package:stream_chat/src/db/chat_persistence_client.dart';
import 'package:stream_chat/src/event_type.dart';
import 'package:stream_chat/src/core/models/attachment_file.dart'; import 'package:stream_chat/src/core/models/attachment_file.dart';
import 'package:stream_chat/src/core/models/channel_model.dart'; import 'package:stream_chat/src/core/models/channel_model.dart';
import 'package:stream_chat/src/core/models/channel_state.dart'; import 'package:stream_chat/src/core/models/channel_state.dart';
import 'package:stream_chat/src/core/models/event.dart'; import 'package:stream_chat/src/core/models/event.dart';
import 'package:stream_chat/src/core/models/filter.dart';
import 'package:stream_chat/src/core/models/member.dart';
import 'package:stream_chat/src/core/models/message.dart'; import 'package:stream_chat/src/core/models/message.dart';
import 'package:stream_chat/src/core/models/own_user.dart'; import 'package:stream_chat/src/core/models/own_user.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
import 'package:stream_chat/src/core/util/utils.dart';
import 'package:stream_chat/src/core/models/filter.dart'; import 'package:stream_chat/src/db/chat_persistence_client.dart';
import 'package:stream_chat/src/event_type.dart';
import 'package:stream_chat/src/core/models/member.dart'; import 'package:stream_chat/src/location.dart';
import 'package:stream_chat/src/ws/connection_status.dart';
import 'package:stream_chat/src/ws/websocket.dart';
/// Handler function used for logging records. Function requires a single /// Handler function used for logging records. Function requires a single
/// [LogRecord] as the only parameter. /// [LogRecord] as the only parameter.
@@ -137,10 +135,10 @@ class StreamChatClient {
late final RetryPolicy _retryPolicy; late final RetryPolicy _retryPolicy;
// sync state of the channels present inside state, defaults to false /// sync state of the channels present inside state, defaults to false
bool _synced = false; bool _synced = false;
// the last dateTime at the which all the channels were synced /// the last dateTime at the which all the channels were synced
DateTime? _lastSyncedAt; DateTime? _lastSyncedAt;
/// The retry policy options getter /// The retry policy options getter
@@ -2,6 +2,12 @@ import 'package:stream_chat/src/client/client.dart';
import 'package:stream_chat/src/core/error/error.dart'; import 'package:stream_chat/src/core/error/error.dart';
/// The retry options /// The retry options
/// When sending/updating/deleting a message any temporary error will trigger the retry policy
/// The retry policy exposes 2 methods
/// - shouldRetry: returns a boolean if the request should be retried
/// - retryTimeout: How many milliseconds to wait till the next attempt
///
/// maxRetryAttempts is a hard limit on maximum retry attempts before giving up
class RetryPolicy { class RetryPolicy {
/// Instantiate a new RetryPolicy /// Instantiate a new RetryPolicy
RetryPolicy({ RetryPolicy({
@@ -9,9 +9,9 @@ import 'package:stream_chat/src/core/models/event.dart';
import 'package:stream_chat/src/core/models/filter.dart'; import 'package:stream_chat/src/core/models/filter.dart';
import 'package:stream_chat/src/core/models/message.dart'; import 'package:stream_chat/src/core/models/message.dart';
/// /// Defines the api dedicated to channel operations
class ChannelApi { class ChannelApi {
/// /// Initialize a new channel api
ChannelApi(this._client); ChannelApi(this._client);
final StreamHttpClient _client; final StreamHttpClient _client;
@@ -19,9 +19,9 @@ extension PushProviderX on PushProvider {
}[this]!; }[this]!;
} }
/// /// Defines the api dedicated to device operations
class DeviceApi { class DeviceApi {
/// /// Initialize a new device api
DeviceApi(this._client); DeviceApi(this._client);
final StreamHttpClient _client; final StreamHttpClient _client;
@@ -6,9 +6,9 @@ import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/models/filter.dart'; import 'package:stream_chat/src/core/models/filter.dart';
import 'package:stream_chat/src/core/models/member.dart'; import 'package:stream_chat/src/core/models/member.dart';
/// /// Defines the api dedicated to general operations
class GeneralApi { class GeneralApi {
/// /// Initialize a new general api
GeneralApi(this._client); GeneralApi(this._client);
final StreamHttpClient _client; final StreamHttpClient _client;
@@ -2,14 +2,14 @@ import 'package:stream_chat/src/core/api/responses.dart';
import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
/// /// Defines the api dedicated to guest users operations
class GuestApi { class GuestApi {
/// /// Initialize a new guest api
GuestApi(this._client); GuestApi(this._client);
final StreamHttpClient _client; final StreamHttpClient _client;
/// /// Returns the information about guest user
Future<ConnectGuestUserResponse> getGuestUser(User user) async { Future<ConnectGuestUserResponse> getGuestUser(User user) async {
final response = await _client.post( final response = await _client.post(
'/guest', '/guest',
@@ -3,9 +3,9 @@ import 'package:stream_chat/src/core/api/responses.dart';
import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/models/message.dart'; import 'package:stream_chat/src/core/models/message.dart';
/// /// Defines the api dedicated to messages operations
class MessageApi { class MessageApi {
/// /// Initialize a new message api
MessageApi(this._client); MessageApi(this._client);
final StreamHttpClient _client; final StreamHttpClient _client;
@@ -1,9 +1,9 @@
import 'package:stream_chat/src/core/api/responses.dart'; import 'package:stream_chat/src/core/api/responses.dart';
import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart';
/// /// Defines the api dedicated to moderation operations
class ModerationApi { class ModerationApi {
/// /// Initialize a new moderation api
ModerationApi(this._client); ModerationApi(this._client);
final StreamHttpClient _client; final StreamHttpClient _client;
@@ -1,5 +1,6 @@
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
import 'package:stream_chat/src/client/client.dart'; import 'package:stream_chat/src/client/client.dart';
import 'package:stream_chat/src/core/error/error.dart';
import 'package:stream_chat/src/core/models/channel_model.dart'; import 'package:stream_chat/src/core/models/channel_model.dart';
import 'package:stream_chat/src/core/models/channel_state.dart'; import 'package:stream_chat/src/core/models/channel_state.dart';
import 'package:stream_chat/src/core/models/device.dart'; import 'package:stream_chat/src/core/models/device.dart';
@@ -9,7 +10,6 @@ import 'package:stream_chat/src/core/models/message.dart';
import 'package:stream_chat/src/core/models/reaction.dart'; import 'package:stream_chat/src/core/models/reaction.dart';
import 'package:stream_chat/src/core/models/read.dart'; import 'package:stream_chat/src/core/models/read.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
import 'package:stream_chat/src/core/error/error.dart';
part 'responses.g.dart'; part 'responses.g.dart';
@@ -20,17 +20,17 @@ class _BaseResponse {
/// Model response for [StreamChatNetworkError] data /// Model response for [StreamChatNetworkError] data
@JsonSerializable() @JsonSerializable()
class ErrorResponse extends _BaseResponse { class ErrorResponse extends _BaseResponse {
/// /// The http error code
int? code; int? code;
/// /// The message associated to the error code
String? message; String? message;
/// /// The backend error code
@JsonKey(name: 'StatusCode') @JsonKey(name: 'StatusCode')
int? statusCode; int? statusCode;
/// /// A detailed message about the error
String? moreInfo; String? moreInfo;
/// Create a new instance from a json /// Create a new instance from a json
@@ -1,21 +1,21 @@
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:stream_chat/src/core/api/attachment_file_uploader.dart';
import 'package:stream_chat/src/core/api/channel_api.dart'; import 'package:stream_chat/src/core/api/channel_api.dart';
import 'package:stream_chat/src/core/api/device_api.dart'; import 'package:stream_chat/src/core/api/device_api.dart';
import 'package:stream_chat/src/core/api/general_api.dart'; import 'package:stream_chat/src/core/api/general_api.dart';
import 'package:stream_chat/src/core/api/guest_api.dart'; import 'package:stream_chat/src/core/api/guest_api.dart';
import 'package:stream_chat/src/core/api/message_api.dart'; import 'package:stream_chat/src/core/api/message_api.dart';
import 'package:stream_chat/src/core/api/moderation_api.dart'; import 'package:stream_chat/src/core/api/moderation_api.dart';
import 'package:stream_chat/src/core/api/user_api.dart';
import 'package:stream_chat/src/core/http/connection_id_manager.dart'; import 'package:stream_chat/src/core/http/connection_id_manager.dart';
import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/api/user_api.dart';
import 'package:stream_chat/src/core/http/token_manager.dart'; import 'package:stream_chat/src/core/http/token_manager.dart';
import 'package:stream_chat/src/core/api/attachment_file_uploader.dart';
export 'device_api.dart' show PushProvider; export 'device_api.dart' show PushProvider;
/// /// ApiClient that wraps every other specific api
class StreamChatApi { class StreamChatApi {
/// /// Initialize a new stream chat api
StreamChatApi( StreamChatApi(
String apiKey, { String apiKey, {
StreamHttpClient? client, StreamHttpClient? client,
@@ -38,42 +38,42 @@ class StreamChatApi {
UserApi? _user; UserApi? _user;
/// /// Api dedicated to users operations
UserApi get user => _user ??= UserApi(_client); UserApi get user => _user ??= UserApi(_client);
GuestApi? _guest; GuestApi? _guest;
/// /// Api dedicated to guest operations
GuestApi get guest => _guest ??= GuestApi(_client); GuestApi get guest => _guest ??= GuestApi(_client);
MessageApi? _message; MessageApi? _message;
/// /// Api dedicated to message operations
MessageApi get message => _message ??= MessageApi(_client); MessageApi get message => _message ??= MessageApi(_client);
ChannelApi? _channel; ChannelApi? _channel;
/// /// Api dedicated to channel operations
ChannelApi get channel => _channel ??= ChannelApi(_client); ChannelApi get channel => _channel ??= ChannelApi(_client);
DeviceApi? _device; DeviceApi? _device;
/// /// Api dedicated to device operations
DeviceApi get device => _device ??= DeviceApi(_client); DeviceApi get device => _device ??= DeviceApi(_client);
ModerationApi? _moderation; ModerationApi? _moderation;
/// /// Api dedicated to moderation operations
ModerationApi get moderation => _moderation ??= ModerationApi(_client); ModerationApi get moderation => _moderation ??= ModerationApi(_client);
GeneralApi? _general; GeneralApi? _general;
/// /// Api dedicated to general operations
GeneralApi get general => _general ??= GeneralApi(_client); GeneralApi get general => _general ??= GeneralApi(_client);
AttachmentFileUploader? _fileUploader; AttachmentFileUploader? _fileUploader;
/// /// Class responsible for uploading images and files to a given channel
AttachmentFileUploader get fileUploader => AttachmentFileUploader get fileUploader =>
_fileUploader ??= StreamAttachmentFileUploader(_client); _fileUploader ??= StreamAttachmentFileUploader(_client);
} }
@@ -6,9 +6,9 @@ import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/models/filter.dart'; import 'package:stream_chat/src/core/models/filter.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
/// /// Defines the api dedicated to users operations
class UserApi { class UserApi {
/// /// Initialize a new user api
UserApi(this._client); UserApi(this._client);
final StreamHttpClient _client; final StreamHttpClient _client;
@@ -1,26 +1,26 @@
// ignore_for_file: use_setters_to_change_properties // ignore_for_file: use_setters_to_change_properties
/// /// Handles the connection id of the websocket connection
class ConnectionIdManager { class ConnectionIdManager {
/// /// Initialize a new connection id manager
ConnectionIdManager({ ConnectionIdManager({
String? connectionId, String? connectionId,
}) : _connectionId = connectionId; }) : _connectionId = connectionId;
String? _connectionId; String? _connectionId;
/// /// Get the current connection id
String? get connectionId => _connectionId; String? get connectionId => _connectionId;
/// /// True if there is a connection id
bool get hasConnectionId => _connectionId != null; bool get hasConnectionId => _connectionId != null;
/// /// Set the connection id
void setConnectionId(String connectionId) { void setConnectionId(String connectionId) {
_connectionId = connectionId; _connectionId = connectionId;
} }
/// /// Clear the connection id
void reset() { void reset() {
_connectionId = null; _connectionId = null;
} }
@@ -1,20 +1,20 @@
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:stream_chat/src/core/api/responses.dart'; import 'package:stream_chat/src/core/api/responses.dart';
import 'package:stream_chat/src/core/error/error.dart';
import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart'; import 'package:stream_chat/src/core/http/stream_chat_dio_error.dart';
import 'package:stream_chat/src/core/http/stream_http_client.dart'; import 'package:stream_chat/src/core/http/stream_http_client.dart';
import 'package:stream_chat/src/core/http/token.dart'; import 'package:stream_chat/src/core/http/token.dart';
import 'package:stream_chat/src/core/http/token_manager.dart'; import 'package:stream_chat/src/core/http/token_manager.dart';
import 'package:stream_chat/src/core/error/error.dart';
/// /// Authentication interceptor that refreshes the token if
/// an auth error is received
class AuthInterceptor extends Interceptor { class AuthInterceptor extends Interceptor {
/// /// Initialize a new auth interceptor
AuthInterceptor(this._client, this._tokenManager); AuthInterceptor(this._client, this._tokenManager);
final StreamHttpClient _client; final StreamHttpClient _client;
/// /// The token manager used in the client
final TokenManager _tokenManager; final TokenManager _tokenManager;
@override @override
@@ -1,8 +1,7 @@
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:stream_chat/src/core/http/connection_id_manager.dart'; import 'package:stream_chat/src/core/http/connection_id_manager.dart';
/// /// Interceptor that injects the connection id in the request params
class ConnectionIdInterceptor extends Interceptor { class ConnectionIdInterceptor extends Interceptor {
/// ///
ConnectionIdInterceptor(this.connectionIdManager); ConnectionIdInterceptor(this.connectionIdManager);
@@ -5,26 +5,26 @@ import 'dart:math' as math;
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
/// /// Step where we're logging
enum InterceptStep { enum InterceptStep {
/// /// Request
request, request,
/// /// Response
response, response,
/// /// Error
error, error,
} }
/// /// Function used to print the log
typedef LogPrint = void Function(InterceptStep step, Object object); typedef LogPrint = void Function(InterceptStep step, Object object);
void _defaultLogPrint(InterceptStep step, Object object) => print(object); void _defaultLogPrint(InterceptStep step, Object object) => print(object);
/// /// Interceptor dedicated to logging
class LoggingInterceptor extends Interceptor { class LoggingInterceptor extends Interceptor {
/// /// Initialize a new logging interceptor
LoggingInterceptor({ LoggingInterceptor({
this.request = true, this.request = true,
this.requestHeader = false, this.requestHeader = false,
@@ -310,13 +310,6 @@ class LoggingInterceptor extends Interceptor {
}); });
} }
/* bool _canFlattenMap(Map map) =>
map.values.where((dynamic val) => val is Map || val is List).isEmpty &&
map.toString().length < maxWidth;
bool _canFlattenList(List list) =>
list.length < 10 && list.toString().length < maxWidth;*/
void _printMapAsTable( void _printMapAsTable(
void Function(Object) logPrint, void Function(Object) logPrint,
Map? map, { Map? map, {
@@ -1,9 +1,9 @@
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:stream_chat/src/core/error/error.dart'; import 'package:stream_chat/src/core/error/error.dart';
/// /// Error class specific to StreamChat and Dio
class StreamChatDioError extends DioError { class StreamChatDioError extends DioError {
/// /// Initialize a stream chat dio error
StreamChatDioError({ StreamChatDioError({
required this.error, required this.error,
required RequestOptions requestOptions, required RequestOptions requestOptions,
@@ -5,21 +5,22 @@ import 'package:jose/jose.dart';
import 'package:stream_chat/src/core/models/user.dart'; import 'package:stream_chat/src/core/models/user.dart';
import 'package:stream_chat/src/core/util/utils.dart'; import 'package:stream_chat/src/core/util/utils.dart';
/// /// A function which can be used to request a Stream Chat API token from your
/// own backend server
typedef GuestTokenProvider = Future<String> Function(User user); typedef GuestTokenProvider = Future<String> Function(User user);
/// /// Authentication type
enum AuthType { enum AuthType {
/// /// JWT token
jwt, jwt,
/// /// Anonymous user
anonymous, anonymous,
} }
/// /// Extension for returning the AuthType as a string
extension AuthTypeX on AuthType { extension AuthTypeX on AuthType {
/// /// Returns the AuthType as a string
String get raw => { String get raw => {
AuthType.jwt: 'jwt', AuthType.jwt: 'jwt',
AuthType.anonymous: 'anonymous', AuthType.anonymous: 'anonymous',
@@ -71,13 +72,13 @@ class Token extends Equatable {
return Token.fromRawValue(rawToken); return Token.fromRawValue(rawToken);
} }
/// /// Authentication type of this token
final AuthType authType; final AuthType authType;
/// /// String value of the token
final String rawValue; final String rawValue;
/// /// User id associated with this token
final String userId; final String userId;
@override @override
@@ -1,12 +1,13 @@
import 'package:stream_chat/src/core/http/token.dart'; import 'package:stream_chat/src/core/http/token.dart';
/// A function which can be used to request a Stream Chat API token from your /// A function which can be used to request a Stream Chat API token from your
/// own backend server. Function requires a single [userId]. /// own backend server.
/// Function requires a single [userId].
typedef TokenProvider = Future<String> Function(String userId); typedef TokenProvider = Future<String> Function(String userId);
/// /// Handles common token operations
class TokenManager { class TokenManager {
/// /// Initialize a new token manager
TokenManager({ TokenManager({
String? userId, String? userId,
Token? token, Token? token,
@@ -25,10 +26,10 @@ class TokenManager {
/// User id to which this TokenManager is configured to /// User id to which this TokenManager is configured to
String? get userId => _userId; String? get userId => _userId;
/// /// True if it's a static token
bool get isStatic => _type == 'static'; bool get isStatic => _type == 'static';
/// /// Set a token or a token provider
Future<Token> setTokenOrProvider( Future<Token> setTokenOrProvider(
String userId, { String userId, {
Token? token, Token? token,
@@ -58,7 +59,7 @@ class TokenManager {
return loadToken(); return loadToken();
} }
/// /// Returns the token refreshing the existing one if [refresh] is true
Future<Token> loadToken({bool refresh = false}) async { Future<Token> loadToken({bool refresh = false}) async {
assert( assert(
_userId != null && _type != null, _userId != null && _type != null,
@@ -71,7 +72,7 @@ class TokenManager {
return _token!; return _token!;
} }
/// /// Resets the token manager
void reset() { void reset() {
_userId = null; _userId = null;
_token = null; _token = null;