Skip to content
Snippets Groups Projects
Commit 9b9a2cb3 authored by Jake Taylor's avatar Jake Taylor :lips:
Browse files

added deleteMessage impl to dm EM impl

parent e4ed3939
No related branches found
No related tags found
2 merge requests!130added deleteMessage impl to dm EM impl,!109Project/haven beta
......@@ -179,13 +179,7 @@ func (w *wasmModel) UpdateSentStatus(uuid uint64, messageID message.ID,
}
// Extract the existing Message and update the Status
newMessage := &Message{}
err = json.Unmarshal([]byte(utils.JsToJson(currentMsg)), newMessage)
if err != nil {
jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr,
"Could not JSON unmarshal message: %+v", err))
return
}
newMessage, err := valueToMessage(currentMsg)
newMessage.Status = uint8(status)
if !messageID.Equals(message.ID{}) {
......@@ -365,6 +359,45 @@ func (w *wasmModel) setBlocked(senderPubKey ed25519.PublicKey, isBlocked bool) e
resultConvo.Token, resultConvo.CodesetVersion, timeBlocked)
}
// DeleteMessage deletes the message with the given message.ID belonging to
// the sender. If the message exists and belongs to the sender, then it is
// deleted and DeleteMessage returns true. If it does not exist, it returns
// false.
func (w *wasmModel) DeleteMessage(messageID message.ID, senderPubKey ed25519.PublicKey) bool {
parentErr := "failed to DeleteMessage"
msgId := impl.EncodeBytes(messageID.Marshal())
// Use the key to get the existing Message
currentMsg, err := impl.GetIndex(w.db, messageStoreName,
messageStoreMessageIndex, msgId)
if err != nil {
jww.ERROR.Printf("%s: %+v", parentErr, err)
return false
}
// Convert the js.Value to a proper object
msgObj, err := valueToMessage(currentMsg)
if err != nil {
jww.ERROR.Printf("%s: %+v", parentErr, err)
return false
}
// Ensure the public keys match
if !bytes.Equal(msgObj.SenderPubKey, senderPubKey) {
jww.ERROR.Printf("%s: %s", parentErr, "Public keys do not match")
return false
}
// Perform the delete
err = impl.DeleteIndex(w.db, messageStoreName, messageStoreMessageIndex,
msgPkeyName, msgId)
if err != nil {
jww.ERROR.Printf("%s: %+v", parentErr, err)
return false
}
return true
}
// GetConversation returns the conversation held by the model (receiver).
func (w *wasmModel) GetConversation(senderPubKey ed25519.PublicKey) *dm.ModelConversation {
parentErr := "failed to GetConversation"
......@@ -426,3 +459,9 @@ func (w *wasmModel) GetConversations() []dm.ModelConversation {
}
return conversations
}
// valueToMessage is a helper for converting js.Value to Message.
func valueToMessage(msgObj js.Value) (*Message, error) {
resultMsg := &Message{}
return resultMsg, json.Unmarshal([]byte(utils.JsToJson(msgObj)), resultMsg)
}
......@@ -14,6 +14,7 @@ import (
"crypto/ed25519"
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"gitlab.com/elixxir/client/v4/cmix/rounds"
"gitlab.com/elixxir/client/v4/dm"
"gitlab.com/elixxir/crypto/message"
......@@ -158,3 +159,36 @@ func TestWasmModel_BlockSender(t *testing.T) {
t.Fatal("Expected blocked to be false")
}
}
// Test failed and successful deletes
func TestWasmModel_DeleteMessage(t *testing.T) {
m, err := newWASMModel("TestWasmModel_DeleteMessage", nil, dummyReceivedMessageCB)
if err != nil {
t.Fatal(err.Error())
}
// Insert test message
testBytes := []byte("test")
testBadBytes := []byte("uwu")
testMsgId := message.DeriveChannelMessageID(&id.ID{1}, 0, testBytes)
testMsg := &Message{
MessageID: testMsgId.Marshal(),
ConversationPubKey: testBytes,
ParentMessageID: nil,
Timestamp: time.Now(),
SenderPubKey: testBytes,
CodesetVersion: 5,
Status: 5,
Text: "",
Type: 5,
Round: 5,
}
_, err = m.upsertMessage(testMsg)
require.NoError(t, err)
// Non-matching pub key, should fail to delete
require.False(t, m.DeleteMessage(testMsgId, testBadBytes))
// Correct pub key, should have deleted
require.True(t, m.DeleteMessage(testMsgId, testBytes))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment