Skip to content

Commit

Permalink
Fix message corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Jul 2, 2024
1 parent 5500f87 commit 38c3208
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions snikket/Chat.hx
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,9 @@ class DirectChat extends Chat {

@HaxeCBridge.noemit // on superclass as abstract
public function correctMessage(localId:String, message:ChatMessage) {
final toSend = message.clone();
final toSend = prepareOutgoingMessage(message.clone());
message = prepareOutgoingMessage(message);
message.resetLocalId();
message.versions = [toSend]; // This is a correction
message.localId = localId;
persistence.storeMessage(client.accountId(), message, (corrected) -> {
Expand Down Expand Up @@ -869,8 +870,9 @@ class Channel extends Chat {

@HaxeCBridge.noemit // on superclass as abstract
public function correctMessage(localId:String, message:ChatMessage) {
final toSend = message.clone();
final toSend = prepareOutgoingMessage(message.clone());
message = prepareOutgoingMessage(message);
message.resetLocalId();
message.versions = [toSend]; // This is a correction
message.localId = localId;
persistence.storeMessage(client.accountId(), message, (corrected) -> {
Expand Down
5 changes: 5 additions & 0 deletions snikket/ChatMessage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class ChatMessage {
return this.timestamp = timestamp;
}

@:allow(snikket)
private function resetLocalId() {
Reflect.setField(this, "localId", null);
}

/**
Get HTML version of the message body
**/
Expand Down
2 changes: 1 addition & 1 deletion snikket/persistence/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ const browser = (dbname) => {
const tx = db.transaction(["messages", "reactions"], "readwrite");
const store = tx.objectStore("messages");
return promisifyRequest(store.index("localId").openCursor(IDBKeyRange.only([account, message.localId || [], message.chatId()]))).then((result) => {
if (result?.value && !message.isIncoming() && result?.value.direction === enums.MessageDirection.MessageSent) {
if (result?.value && !message.isIncoming() && result?.value.direction === enums.MessageDirection.MessageSent && message.versions.length < 1) {
// Duplicate, we trust our own sent ids
return promisifyRequest(result.delete());
} else if (result?.value && result.value.sender == message.senderId() && (message.versions.length > 0 || (result.value.versions || []).length > 0)) {
Expand Down

0 comments on commit 38c3208

Please sign in to comment.