Merge branch 'develop' into perf

This commit is contained in:
Salvatore Giordano
2021-06-08 10:03:27 +02:00
14 changed files with 449 additions and 222 deletions
+50 -9
View File
@@ -354,9 +354,13 @@ class Channel {
}
/// Send a [message] to this channel.
/// If [skipPush] is true the message will not send a push notification
/// Waits for a [_messageAttachmentsUploadCompleter] to complete
/// before actually sending the message.
Future<SendMessageResponse> sendMessage(Message message) async {
Future<SendMessageResponse> sendMessage(
Message message, {
bool skipPush = false,
}) async {
_checkInitialized();
// Cancelling previous completer in case it's called again in the process
// Eg. Updating the message while the previous call is in progress.
@@ -399,7 +403,12 @@ class Channel {
message = await attachmentsUploadCompleter.future;
}
final response = await _client.sendMessage(message, id!, type);
final response = await _client.sendMessage(
message,
id!,
type,
skipPush: skipPush,
);
state!.addMessage(response.message);
return response;
} catch (error) {
@@ -414,6 +423,9 @@ class Channel {
/// Waits for a [_messageAttachmentsUploadCompleter] to complete
/// before actually updating the message.
Future<UpdateMessageResponse> updateMessage(Message message) async {
final currentMessage =
state?.messages.firstWhere((e) => e.id == message.id);
// Cancelling previous completer in case it's called again in the process
// Eg. Updating the message while the previous call is in progress.
_messageAttachmentsUploadCompleter
@@ -458,6 +470,31 @@ class Channel {
state?.addMessage(m);
return response;
} catch (error) {
if (error is DioError && error.type != DioErrorType.response) {
state?.retryQueue?.add([message]);
} else if (error is ApiError) {
if (currentMessage != null) {
state?.addMessage(currentMessage);
}
}
rethrow;
}
}
/// Partially updates the [message] in this channel.
Future<UpdateMessageResponse> partiallyUpdateMessage(
Message message, Map data) async {
try {
final response = await _client.partiallyUpdateMessage(message.id, data);
final m = response.message.copyWith(
ownReactions: message.ownReactions,
);
state?.addMessage(m);
return response;
} catch (error) {
if (error is DioError && error.type != DioErrorType.response) {
@@ -530,17 +567,21 @@ class Channel {
Duration(seconds: timeoutOrExpirationDate.toInt()),
);
}
return updateMessage(
message.copyWith(
pinned: true,
pinExpires: pinExpires,
),
);
return partiallyUpdateMessage(message, {
'set': {
'pinned': true,
if (pinExpires != null) 'pin_expires': pinExpires.toIso8601String(),
}
});
}
/// Unpins provided message
Future<UpdateMessageResponse> unpinMessage(Message message) =>
updateMessage(message.copyWith(pinned: false));
partiallyUpdateMessage(message, {
'set': {
'pinned': false,
}
});
/// Send a file to this channel
Future<SendFileResponse> sendFile(
+37 -16
View File
@@ -315,7 +315,7 @@ class StreamChatClient {
await connectUser(User(id: userId), newToken);
try {
handler.resolve(
return handler.resolve(
await httpClient.request(
err.requestOptions.path,
cancelToken: err.requestOptions.cancelToken,
@@ -343,10 +343,12 @@ class StreamChatClient {
),
);
} on DioError {
handler.reject(err);
return handler.reject(err);
}
}
}
return handler.next(err);
}
LogHandlerFunction _getDefaultLogHandler() {
@@ -1327,11 +1329,15 @@ class StreamChatClient {
Future<SendMessageResponse> sendMessage(
Message message,
String channelId,
String channelType,
) async {
String channelType, {
bool skipPush = false,
}) async {
final response = await post(
'/channels/$channelType/$channelId/message',
data: {'message': message.toJson()},
data: {
'message': message.toJson(),
'skip_push': skipPush,
},
);
return decode(response.data, SendMessageResponse.fromJson);
}
@@ -1345,6 +1351,20 @@ class StreamChatClient {
return decode(response.data, UpdateMessageResponse.fromJson);
}
/// Partially update the given message
/// Use 'set' in map to set values
/// User 'unset' in map to unset values
Future<UpdateMessageResponse> partiallyUpdateMessage(
String id,
Map data,
) async {
final response = await put(
'/messages/$id',
data: data,
);
return decode(response.data, UpdateMessageResponse.fromJson);
}
/// Deletes the given message
Future<EmptyResponse> deleteMessage(Message message) async {
final response = await delete('/messages/${message.id}');
@@ -1383,20 +1403,21 @@ class StreamChatClient {
)
.toUtc();
}
return updateMessage(
message.copyWith(
pinned: true,
pinExpires: pinExpires,
),
);
return partiallyUpdateMessage(message.id, {
'set': {
'pinned': true,
if (pinExpires != null) 'pin_expires': pinExpires.toIso8601String(),
}
});
}
/// Unpins provided message
Future<UpdateMessageResponse> unpinMessage(Message message) => updateMessage(
message.copyWith(
pinned: false,
),
);
Future<UpdateMessageResponse> unpinMessage(Message message) =>
partiallyUpdateMessage(message.id, {
'set': {
'pinned': false,
}
});
}
/// The class that handles the state of the channel listening to the events
@@ -73,7 +73,6 @@ class Message extends Equatable {
this.extraData = const {},
this.deletedAt,
this.status = MessageSendingStatus.sent,
this.skipPush = false,
}) : id = id ?? const Uuid().v4(),
pinExpires = pinExpires?.toUtc(),
createdAt = createdAt ?? DateTime.now(),
@@ -158,10 +157,6 @@ class Message extends Equatable {
@JsonKey(defaultValue: false)
final bool silent;
/// If true the message will not send a push notification
@JsonKey(defaultValue: false)
final bool skipPush;
/// If true the message is shadowed
@JsonKey(
includeIfNull: false,
@@ -253,7 +248,6 @@ class Message extends Equatable {
'pinned_at',
'pin_expires',
'pinned_by',
'skip_push',
];
/// Serialize to json
@@ -291,7 +285,6 @@ class Message extends Equatable {
User? pinnedBy,
Map<String, Object?>? extraData,
MessageSendingStatus? status,
bool? skipPush,
}) {
assert(() {
if (pinExpires is! DateTime &&
@@ -331,7 +324,6 @@ class Message extends Equatable {
pinnedBy: pinnedBy ?? this.pinnedBy,
pinExpires:
pinExpires == _pinExpires ? this.pinExpires : pinExpires as DateTime?,
skipPush: skipPush ?? this.skipPush,
);
}
@@ -366,7 +358,6 @@ class Message extends Equatable {
pinnedAt: other.pinnedAt,
pinExpires: other.pinExpires,
pinnedBy: other.pinnedBy,
skipPush: other.skipPush,
);
@override
@@ -399,7 +390,6 @@ class Message extends Equatable {
pinnedBy,
extraData,
status,
skipPush,
];
}
@@ -67,7 +67,6 @@ Message _$MessageFromJson(Map<String, dynamic> json) {
deletedAt: json['deleted_at'] == null
? null
: DateTime.parse(json['deleted_at'] as String),
skipPush: json['skip_push'] as bool? ?? false,
);
}
@@ -97,7 +96,6 @@ Map<String, dynamic> _$MessageToJson(Message instance) {
writeNotNull('thread_participants', readonly(instance.threadParticipants));
val['show_in_channel'] = instance.showInChannel;
val['silent'] = instance.silent;
val['skip_push'] = instance.skipPush;
writeNotNull('shadowed', readonly(instance.shadowed));
writeNotNull('command', readonly(instance.command));
writeNotNull('created_at', readonly(instance.createdAt));