Revert "Revert "more message match improvements""

This reverts commit e7e73af99d99a843444a915370d3ad077b23d76b.
This commit is contained in:
zlshames
2024-09-12 09:31:06 -04:00
parent 12ba51f56f
commit bd80bbafa2
3 changed files with 48 additions and 16 deletions

View File

@ -27,6 +27,12 @@ class _DeliveredIndicatorState extends CustomState<DeliveredIndicator, void, Mes
void initState() {
forceDelete = false;
super.initState();
eventDispatcher.stream.listen((event) {
if (event.item1 == "message-updated-${message.guid}") {
setState(() {});
}
});
}
bool get shouldShow {

View File

@ -282,7 +282,7 @@ class Message {
set dateDelivered(DateTime? d) => _dateDelivered.value = d;
final RxBool _isDelivered = RxBool(false);
bool get isDelivered => _isDelivered.value;
bool get isDelivered => (dateDelivered != null) ? true : _isDelivered.value;
set isDelivered(bool b) => _isDelivered.value = b;
final Rxn<DateTime> _dateEdited = Rxn<DateTime>();
@ -1113,6 +1113,9 @@ class Message {
}
bool isNewerThan(Message other) {
// If the other message has an error, we want to show that.
if (error == 0 && other.error != 0) return false;
// Check null dates in order of what should be filled in first -> last
if (dateCreated == null && other.dateCreated != null) return false;
if (dateCreated != null && other.dateCreated == null) return true;

View File

@ -66,27 +66,36 @@ class ActionHandler extends GetxService {
return messages;
}
Future<void> matchMessageWithExisting(Chat chat, Message existing, Message replacement) async {
Future<void> matchMessageWithExisting(Chat chat, String existingGuid, Message replacement, {Message? existing}) async {
// First, try to find a matching message with the replacement's GUID.
// We check this first because if an event came in for that GUID, we should be able to ignore
// the API response.
final existingReplacementMessage = Message.findOne(guid: replacement.guid);
if (existingReplacementMessage != null) {
Logger.debug("Found existing message with GUID ${replacement.guid}! Not replacing...", tag: "MessageStatus");
Logger.debug("Found existing message with GUID ${replacement.guid}...", tag: "MessageStatus");
if (replacement.isNewerThan(existingReplacementMessage)) {
Logger.debug("Replacing existing message with newer message (GUID: ${replacement.guid})...", tag: "MessageStatus");
await Message.replaceMessage(replacement.guid, replacement);
} else {
Logger.debug("Existing message with GUID ${replacement.guid} is newer than the replacement...", tag: "MessageStatus");
}
// Delete the temp message if it exists
if (existing.guid != replacement.guid) {
Logger.debug("Deleting temp message with GUID ${existing.guid}...", tag: "MessageStatus");
final existingTempMessage = Message.findOne(guid: existing.guid);
if (existingGuid != replacement.guid) {
Logger.debug("Deleting temp message with GUID $existingGuid...", tag: "MessageStatus");
final existingTempMessage = Message.findOne(guid: existingGuid);
if (existingTempMessage != null) {
Message.delete(existingTempMessage.guid!);
ms(chat.guid).removeMessage(existing);
if (existing != null) {
ms(chat.guid).removeMessage(existing);
}
}
}
} else {
// If we didn't find a matching message with the replacement's GUID, replace the existing message.
Logger.debug("Replacing message with GUID ${existing.guid} with ${replacement.guid}...", tag: "MessageStatus");
await Message.replaceMessage(existing.guid, replacement);
Logger.debug("Replacing message with GUID $existingGuid with ${replacement.guid}...", tag: "MessageStatus");
await Message.replaceMessage(existingGuid, replacement);
}
}
@ -111,7 +120,7 @@ class ActionHandler extends GetxService {
).then((response) async {
final newMessage = Message.fromMap(response.data['data']);
try {
await matchMessageWithExisting(c, m, newMessage);
await matchMessageWithExisting(c, m.guid!, newMessage, existing: m);
} catch (_) {
Logger.warn("Failed to find message match for ${m.guid} -> ${newMessage.guid}!", tag: "MessageStatus");
}
@ -133,7 +142,7 @@ class ActionHandler extends GetxService {
http.sendTapback(c.guid, selected!.text ?? "", selected.guid!, r, partIndex: m.associatedMessagePart).then((response) async {
final newMessage = Message.fromMap(response.data['data']);
try {
await matchMessageWithExisting(c, m, newMessage);
await matchMessageWithExisting(c, m.guid!, newMessage, existing: m);
} catch (_) {
Logger.warn("Failed to find message match for ${m.guid} -> ${newMessage.guid}!", tag: "MessageStatus");
}
@ -172,7 +181,7 @@ class ActionHandler extends GetxService {
).then((response) async {
final newMessage = Message.fromMap(response.data['data']);
try {
await matchMessageWithExisting(c, m, newMessage);
await matchMessageWithExisting(c, m.guid!, newMessage, existing: m);
} catch (_) {
Logger.warn("Failed to find message match for ${m.guid} -> ${newMessage.guid}!", tag: "MessageStatus");
}
@ -237,10 +246,16 @@ class ActionHandler extends GetxService {
for (Attachment? a in newMessage.attachments) {
if (a == null) continue;
Attachment.replaceAttachment(m.guid, a);
try {
Attachment.replaceAttachment(m.guid, a);
} catch (e) {
Logger.warn("Failed to replace attachment ${a.guid}!", tag: "MessageStatus");
}
}
try {
await matchMessageWithExisting(c, m, newMessage);
await matchMessageWithExisting(c, m.guid!, newMessage, existing: m);
} catch (_) {
Logger.warn("Failed to find message match for ${m.guid} -> ${newMessage.guid}!", tag: "MessageStatus");
}
@ -296,13 +311,21 @@ class ActionHandler extends GetxService {
}
}
Logger.info("Updated message: [${m.text}] ${m.getLastUpdate().toLowerCase()} - for chat [${c.guid}]", tag: "ActionHandler");
// update any attachments
for (Attachment? a in m.attachments) {
if (a == null) continue;
Attachment.replaceAttachment(tempGuid ?? m.guid, a);
try {
Attachment.replaceAttachment(tempGuid ?? m.guid, a);
} catch (e) {
Logger.warn("Failed to replace attachment ${a.guid}!", tag: "MessageStatus");
}
}
// update the message in the DB
await Message.replaceMessage(tempGuid ?? m.guid, m);
await matchMessageWithExisting(c, tempGuid ?? m.guid!, m);
eventDispatcher.emit("message-updated-${m.guid}");
}
Future<Chat> handleNewOrUpdatedChat(Chat partialData) async {