Skip to content
Snippets Groups Projects
Commit cfb301bb authored by Richard T. Carback III's avatar Richard T. Carback III
Browse files

Merge branch 'XX-4536/UpdateConvoCallback' into 'release'

added updateConversation flag to messageCallback; made message reception...

See merge request !86
parents ca5701e7 5c12f547
No related branches found
No related tags found
2 merge requests!86added updateConversation flag to messageCallback; made message reception...,!67fix for latest client release
...@@ -80,13 +80,14 @@ func (m *manager) newWASMEventModelCB(data []byte) ([]byte, error) { ...@@ -80,13 +80,14 @@ func (m *manager) newWASMEventModelCB(data []byte) ([]byte, error) {
// main thread. // main thread.
// //
// messageReceivedCallback adhere to the MessageReceivedCallback type. // messageReceivedCallback adhere to the MessageReceivedCallback type.
func (m *manager) messageReceivedCallback( func (m *manager) messageReceivedCallback(uuid uint64, pubKey ed25519.PublicKey,
uuid uint64, pubKey ed25519.PublicKey, update bool) { messageUpdate, conversationUpdate bool) {
// Package parameters for sending // Package parameters for sending
msg := &wDm.MessageReceivedCallbackMessage{ msg := &wDm.MessageReceivedCallbackMessage{
UUID: uuid, UUID: uuid,
PubKey: pubKey, PubKey: pubKey,
Update: update, MessageUpdate: messageUpdate,
ConversationUpdate: conversationUpdate,
} }
data, err := json.Marshal(msg) data, err := json.Marshal(msg)
if err != nil { if err != nil {
......
...@@ -39,10 +39,10 @@ type wasmModel struct { ...@@ -39,10 +39,10 @@ type wasmModel struct {
updateMux sync.Mutex updateMux sync.Mutex
} }
// joinConversation is used for joining new conversations. // upsertConversation is used for joining or updating a Conversation.
func (w *wasmModel) joinConversation(nickname string, func (w *wasmModel) upsertConversation(nickname string,
pubKey ed25519.PublicKey, dmToken uint32, codeset uint8) error { pubKey ed25519.PublicKey, dmToken uint32, codeset uint8, blocked bool) error {
parentErr := errors.New("failed to joinConversation") parentErr := errors.New("failed to upsertConversation")
// Build object // Build object
newConvo := Conversation{ newConvo := Conversation{
...@@ -50,7 +50,7 @@ func (w *wasmModel) joinConversation(nickname string, ...@@ -50,7 +50,7 @@ func (w *wasmModel) joinConversation(nickname string,
Nickname: nickname, Nickname: nickname,
Token: dmToken, Token: dmToken,
CodesetVersion: codeset, CodesetVersion: codeset,
Blocked: false, Blocked: blocked,
} }
// Convert to jsObject // Convert to jsObject
...@@ -212,9 +212,10 @@ func (w *wasmModel) UpdateSentStatus(uuid uint64, messageID message.ID, ...@@ -212,9 +212,10 @@ func (w *wasmModel) UpdateSentStatus(uuid uint64, messageID message.ID,
return return
} }
jww.TRACE.Printf("[DM indexedDB] Calling ReceiveMessageCB(%v, %v, t)", jww.TRACE.Printf("[DM indexedDB] Calling ReceiveMessageCB(%v, %v, t, f)",
uuid, newMessage.ConversationPubKey) uuid, newMessage.ConversationPubKey)
go w.receivedMessageCB(uuid, newMessage.ConversationPubKey, true) go w.receivedMessageCB(uuid, newMessage.ConversationPubKey,
true, false)
} }
// receiveWrapper is a higher-level wrapper of receiveHelper. // receiveWrapper is a higher-level wrapper of receiveHelper.
...@@ -222,21 +223,34 @@ func (w *wasmModel) receiveWrapper(messageID message.ID, parentID *message.ID, n ...@@ -222,21 +223,34 @@ func (w *wasmModel) receiveWrapper(messageID message.ID, parentID *message.ID, n
data string, partnerKey, senderKey ed25519.PublicKey, dmToken uint32, codeset uint8, data string, partnerKey, senderKey ed25519.PublicKey, dmToken uint32, codeset uint8,
timestamp time.Time, round rounds.Round, mType dm.MessageType, status dm.Status) (uint64, error) { timestamp time.Time, round rounds.Round, mType dm.MessageType, status dm.Status) (uint64, error) {
// If there is no extant Conversation, create one. // Keep track of whether Conversation was altered
_, err := impl.Get(w.db, conversationStoreName, impl.EncodeBytes(partnerKey)) conversationUpdated := false
if err != nil { result, err := w.getConversation(partnerKey)
if strings.Contains(err.Error(), impl.ErrDoesNotExist) {
err = w.joinConversation(nickname, partnerKey, dmToken,
codeset)
if err != nil { if err != nil {
if !strings.Contains(err.Error(), impl.ErrDoesNotExist) {
return 0, err return 0, err
}
} else { } else {
// If there is no extant Conversation, create one.
err = w.upsertConversation(nickname, partnerKey, dmToken,
codeset, false)
if err != nil {
return 0, err return 0, err
} }
conversationUpdated = true
}
} else { } else {
jww.DEBUG.Printf( jww.DEBUG.Printf(
"[DM indexedDB] Conversation with %s already joined", nickname) "[DM indexedDB] Conversation with %s already joined", nickname)
// Update Conversation if nickname was altered
if result.Nickname != nickname {
err = w.upsertConversation(nickname, result.Pubkey, result.Token,
result.CodesetVersion, result.Blocked)
if err != nil {
return 0, err
}
conversationUpdated = true
}
} }
// Handle encryption, if it is present // Handle encryption, if it is present
...@@ -261,9 +275,9 @@ func (w *wasmModel) receiveWrapper(messageID message.ID, parentID *message.ID, n ...@@ -261,9 +275,9 @@ func (w *wasmModel) receiveWrapper(messageID message.ID, parentID *message.ID, n
return 0, err return 0, err
} }
jww.TRACE.Printf("[DM indexedDB] Calling ReceiveMessageCB(%v, %v, f)", jww.TRACE.Printf("[DM indexedDB] Calling ReceiveMessageCB(%v, %v, f, %t)",
uuid, partnerKey) uuid, partnerKey, conversationUpdated)
go w.receivedMessageCB(uuid, partnerKey, false) go w.receivedMessageCB(uuid, partnerKey, false, conversationUpdated)
return uuid, nil return uuid, nil
} }
...@@ -280,7 +294,7 @@ func (w *wasmModel) receiveHelper( ...@@ -280,7 +294,7 @@ func (w *wasmModel) receiveHelper(
return 0, errors.Errorf("Unable to marshal Message: %+v", err) return 0, errors.Errorf("Unable to marshal Message: %+v", err)
} }
// Unset the primaryKey for inserts so that it can be auto-populated and // Unset the primaryKey for inserts so that it can be autopopulated and
// incremented // incremented
if !isUpdate { if !isUpdate {
messageObj.Delete("id") messageObj.Delete("id")
...@@ -357,21 +371,9 @@ func (w *wasmModel) setBlocked(senderPubKey ed25519.PublicKey, isBlocked bool) e ...@@ -357,21 +371,9 @@ func (w *wasmModel) setBlocked(senderPubKey ed25519.PublicKey, isBlocked bool) e
if err != nil { if err != nil {
return err return err
} }
resultConvo.Blocked = isBlocked
// Convert back to js.Value
newMessageJson, err := json.Marshal(resultConvo)
if err != nil {
return err
}
convoObj, err := utils.JsonToJS(newMessageJson)
if err != nil {
return err
}
// Insert into storage return w.upsertConversation(resultConvo.Nickname, resultConvo.Pubkey,
_, err = impl.Put(w.db, conversationStoreName, convoObj) resultConvo.Token, resultConvo.CodesetVersion, isBlocked)
return err
} }
// GetConversation returns the conversation held by the model (receiver). // GetConversation returns the conversation held by the model (receiver).
......
...@@ -33,9 +33,10 @@ const ( ...@@ -33,9 +33,10 @@ const (
// MessageReceivedCallback is called any time a message is received or updated. // MessageReceivedCallback is called any time a message is received or updated.
// //
// update is true if the row is old and was edited. // messageUpdate is true if the Message already exists and was edited.
// conversationUpdate is true if the Conversation was created or modified.
type MessageReceivedCallback func( type MessageReceivedCallback func(
uuid uint64, pubKey ed25519.PublicKey, update bool) uuid uint64, pubKey ed25519.PublicKey, messageUpdate, conversationUpdate bool)
// storeDatabaseNameFn matches storage.StoreIndexedDb so that the data can be // storeDatabaseNameFn matches storage.StoreIndexedDb so that the data can be
// sent between the worker and main thread. // sent between the worker and main thread.
......
...@@ -26,8 +26,8 @@ import ( ...@@ -26,8 +26,8 @@ import (
// MessageReceivedCallback is called any time a message is received or updated. // MessageReceivedCallback is called any time a message is received or updated.
// //
// update is true if the row is old and was edited. // update is true if the row is old and was edited.
type MessageReceivedCallback func( type MessageReceivedCallback func(uuid uint64, pubKey ed25519.PublicKey,
uuid uint64, pubKey ed25519.PublicKey, update bool) messageUpdate, conversationUpdate bool)
// NewWASMEventModelMessage is JSON marshalled and sent to the worker for // NewWASMEventModelMessage is JSON marshalled and sent to the worker for
// [NewWASMEventModel]. // [NewWASMEventModel].
...@@ -93,7 +93,8 @@ func NewWASMEventModel(path, wasmJsPath string, encryption cryptoChannel.Cipher, ...@@ -93,7 +93,8 @@ func NewWASMEventModel(path, wasmJsPath string, encryption cryptoChannel.Cipher,
type MessageReceivedCallbackMessage struct { type MessageReceivedCallbackMessage struct {
UUID uint64 `json:"uuid"` UUID uint64 `json:"uuid"`
PubKey ed25519.PublicKey `json:"pubKey"` PubKey ed25519.PublicKey `json:"pubKey"`
Update bool `json:"update"` MessageUpdate bool `json:"message_update"`
ConversationUpdate bool `json:"conversation_update"`
} }
// messageReceivedCallbackHandler returns a handler to manage messages for the // messageReceivedCallbackHandler returns a handler to manage messages for the
...@@ -107,7 +108,7 @@ func messageReceivedCallbackHandler(cb MessageReceivedCallback) func(data []byte ...@@ -107,7 +108,7 @@ func messageReceivedCallbackHandler(cb MessageReceivedCallback) func(data []byte
"MessageReceivedCallback message from worker: %+v", err) "MessageReceivedCallback message from worker: %+v", err)
return return
} }
cb(msg.UUID, msg.PubKey, msg.Update) cb(msg.UUID, msg.PubKey, msg.MessageUpdate, msg.ConversationUpdate)
} }
} }
......
...@@ -210,8 +210,9 @@ func newDMClientWithIndexedDb(cmixID int, wasmJsPath string, ...@@ -210,8 +210,9 @@ func newDMClientWithIndexedDb(cmixID int, wasmJsPath string,
privateIdentity []byte, cb js.Value, cipher *bindings.DMDbCipher) any { privateIdentity []byte, cb js.Value, cipher *bindings.DMDbCipher) any {
messageReceivedCB := func(uuid uint64, pubKey ed25519.PublicKey, messageReceivedCB := func(uuid uint64, pubKey ed25519.PublicKey,
update bool) { messageUpdate, conversationUpdate bool) {
cb.Invoke(uuid, utils.CopyBytesToJS(pubKey[:]), update) cb.Invoke(uuid, utils.CopyBytesToJS(pubKey[:]),
messageUpdate, conversationUpdate)
} }
promiseFn := func(resolve, reject func(args ...any) js.Value) { promiseFn := func(resolve, reject func(args ...any) js.Value) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment