fix(llc): fix message order when sent quickly.

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2023-06-17 02:53:56 +05:30
committed by xsahil03x
parent b341d66a89
commit c39c27b616
3 changed files with 175 additions and 67 deletions
@@ -64,8 +64,11 @@ class Message extends Equatable {
this.showInChannel,
this.command,
DateTime? createdAt,
this.localCreatedAt,
DateTime? updatedAt,
this.deletedAt,
this.localUpdatedAt,
DateTime? deletedAt,
this.localDeletedAt,
this.user,
this.pinned = false,
this.pinnedAt,
@@ -76,8 +79,9 @@ class Message extends Equatable {
this.i18n,
}) : id = id ?? const Uuid().v4(),
pinExpires = pinExpires?.toUtc(),
_createdAt = createdAt,
_updatedAt = updatedAt,
remoteCreatedAt = createdAt,
remoteUpdatedAt = updatedAt,
remoteDeletedAt = deletedAt,
_quotedMessageId = quotedMessageId;
/// Create a new instance from JSON.
@@ -161,21 +165,49 @@ class Message extends Equatable {
@JsonKey(includeToJson: false)
final String? command;
final DateTime? _createdAt;
/// Reserved field indicating when the message was deleted.
/// Indicates when the message was created.
///
/// Returns the latest between [localCreatedAt] and [remoteCreatedAt].
/// If both are null, returns [DateTime.now].
@JsonKey(includeToJson: false)
final DateTime? deletedAt;
DateTime get createdAt => localCreatedAt ?? remoteCreatedAt ?? DateTime.now();
/// Reserved field indicating when the message was created.
/// Indicates when the message was created locally.
@JsonKey(includeToJson: false, includeFromJson: false)
final DateTime? localCreatedAt;
/// Indicates when the message was created on the server.
@JsonKey(includeToJson: false, includeFromJson: false)
final DateTime? remoteCreatedAt;
/// Indicates when the message was updated last time.
///
/// Returns the latest between [localUpdatedAt] and [remoteUpdatedAt].
/// If both are null, returns [createdAt].
@JsonKey(includeToJson: false)
DateTime get createdAt => _createdAt ?? DateTime.now();
DateTime get updatedAt => localUpdatedAt ?? remoteUpdatedAt ?? createdAt;
final DateTime? _updatedAt;
/// Indicates when the message was updated locally.
@JsonKey(includeToJson: false, includeFromJson: false)
final DateTime? localUpdatedAt;
/// Reserved field indicating when the message was updated last time.
/// Indicates when the message was updated on the server.
@JsonKey(includeToJson: false, includeFromJson: false)
final DateTime? remoteUpdatedAt;
/// Indicates when the message was deleted.
///
/// Returns the latest between [localDeletedAt] and [remoteDeletedAt].
@JsonKey(includeToJson: false)
DateTime get updatedAt => _updatedAt ?? DateTime.now();
DateTime? get deletedAt => localDeletedAt ?? remoteDeletedAt;
/// Indicates when the message was deleted locally.
@JsonKey(includeToJson: false, includeFromJson: false)
final DateTime? localDeletedAt;
/// Indicates when the message was deleted on the server.
@JsonKey(includeToJson: false, includeFromJson: false)
final DateTime? remoteDeletedAt;
/// User who sent the message.
@JsonKey(includeToJson: false)
@@ -273,8 +305,11 @@ class Message extends Equatable {
bool? showInChannel,
String? command,
DateTime? createdAt,
DateTime? localCreatedAt,
DateTime? updatedAt,
DateTime? localUpdatedAt,
DateTime? deletedAt,
DateTime? localDeletedAt,
User? user,
bool? pinned,
DateTime? pinnedAt,
@@ -338,9 +373,12 @@ class Message extends Equatable {
threadParticipants: threadParticipants ?? this.threadParticipants,
showInChannel: showInChannel ?? this.showInChannel,
command: command ?? this.command,
createdAt: createdAt ?? _createdAt,
updatedAt: updatedAt ?? _updatedAt,
deletedAt: deletedAt ?? this.deletedAt,
createdAt: createdAt ?? remoteCreatedAt,
localCreatedAt: localCreatedAt ?? this.localCreatedAt,
updatedAt: updatedAt ?? remoteUpdatedAt,
localUpdatedAt: localUpdatedAt ?? this.localUpdatedAt,
deletedAt: deletedAt ?? remoteDeletedAt,
localDeletedAt: localDeletedAt ?? this.localDeletedAt,
user: user ?? this.user,
pinned: pinned ?? this.pinned,
pinnedAt: pinnedAt ?? this.pinnedAt,
@@ -374,9 +412,12 @@ class Message extends Equatable {
threadParticipants: other.threadParticipants,
showInChannel: other.showInChannel,
command: other.command,
createdAt: other.createdAt,
updatedAt: other.updatedAt,
deletedAt: other.deletedAt,
createdAt: other.remoteCreatedAt,
localCreatedAt: other.localCreatedAt,
updatedAt: other.remoteUpdatedAt,
localUpdatedAt: other.localUpdatedAt,
deletedAt: other.remoteDeletedAt,
localDeletedAt: other.localDeletedAt,
user: other.user,
pinned: other.pinned,
pinnedAt: other.pinnedAt,
@@ -387,6 +428,28 @@ class Message extends Equatable {
i18n: other.i18n,
);
/// Returns a new [Message] that is [other] with local changes applied to it.
///
/// This ensures that the local sync changes are not lost when the message is
/// updated on the server.
///
/// For example, when a message is sent, it is immediately shown
/// optimistically in the UI. When the message is received from the server,
/// it will not contain the local changes. This method can be used to merge
/// the local changes back into the message.
///
/// This also helps in maintaining the order of the messages in the channel
/// when the messages are sorted by the [createdAt] field.
Message syncWith(Message? other) {
if (other == null) return this;
return copyWith(
localCreatedAt: other.localCreatedAt,
localUpdatedAt: other.localUpdatedAt,
localDeletedAt: other.localDeletedAt,
);
}
@override
List<Object?> get props => [
id,
@@ -407,8 +470,8 @@ class Message extends Equatable {
shadowed,
silent,
command,
_createdAt,
_updatedAt,
createdAt,
updatedAt,
deletedAt,
user,
pinned,