Skip to content
Snippets Groups Projects
Commit c565c1ea authored by Jono Wenger's avatar Jono Wenger
Browse files

Merge branch 'XX-4466/webWorkerComms' into 'project/fileUpload'

XX-4466 / File upload - IndexDB Webworker Comms

See merge request !81
parents debdda36 0052d1f7
No related branches found
No related tags found
3 merge requests!81XX-4466 / File upload - IndexDB Webworker Comms,!71Project/file upload,!67fix for latest client release
This commit is part of merge request !71. Comments created here will be created in the context of that merge request.
...@@ -340,8 +340,12 @@ func (m *manager) updateFromUUIDCB(data []byte) ([]byte, error) { ...@@ -340,8 +340,12 @@ func (m *manager) updateFromUUIDCB(data []byte) ([]byte, error) {
status = &msg.Status status = &msg.Status
} }
m.model.UpdateFromUUID( err = m.model.UpdateFromUUID(
msg.UUID, messageID, timestamp, round, pinned, hidden, status) msg.UUID, messageID, timestamp, round, pinned, hidden, status)
if err != nil {
return []byte(err.Error()), nil
}
return nil, nil return nil, nil
} }
...@@ -374,15 +378,21 @@ func (m *manager) updateFromMessageIDCB(data []byte) ([]byte, error) { ...@@ -374,15 +378,21 @@ func (m *manager) updateFromMessageIDCB(data []byte) ([]byte, error) {
status = &msg.Status status = &msg.Status
} }
uuid := m.model.UpdateFromMessageID( var ue wChannels.UuidError
uuid, err := m.model.UpdateFromMessageID(
msg.MessageID, timestamp, round, pinned, hidden, status) msg.MessageID, timestamp, round, pinned, hidden, status)
if err != nil {
ue.Error = []byte(err.Error())
} else {
ue.UUID = uuid
}
uuidData, err := json.Marshal(uuid) data, err = json.Marshal(ue)
if err != nil { if err != nil {
return nil, errors.Errorf("failed to JSON marshal UUID : %+v", err) return nil, errors.Errorf("failed to JSON marshal %T: %+v", ue, err)
} }
return uuidData, nil return data, nil
} }
// getMessageCB is the callback for wasmModel.GetMessage. Returns JSON // getMessageCB is the callback for wasmModel.GetMessage. Returns JSON
......
...@@ -251,10 +251,13 @@ func (w *wasmModel) ReceiveReaction(channelID *id.ID, messageID, ...@@ -251,10 +251,13 @@ func (w *wasmModel) ReceiveReaction(channelID *id.ID, messageID,
// messageID, timestamp, round, pinned, and hidden are all nillable and may be // messageID, timestamp, round, pinned, and hidden are all nillable and may be
// updated based upon the UUID at a later date. If a nil value is passed, then // updated based upon the UUID at a later date. If a nil value is passed, then
// make no update. // make no update.
//
// Returns an error if the message cannot be updated. It must return
// channels.NoMessageErr if the message does not exist.
func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID, func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID,
timestamp *time.Time, round *rounds.Round, pinned, hidden *bool, timestamp *time.Time, round *rounds.Round, pinned, hidden *bool,
status *channels.SentStatus) { status *channels.SentStatus) error {
parentErr := errors.New("failed to UpdateFromUUID") parentErr := "failed to UpdateFromUUID"
// FIXME: this is a bit of race condition without the mux. // FIXME: this is a bit of race condition without the mux.
// This should be done via the transactions (i.e., make a // This should be done via the transactions (i.e., make a
...@@ -268,17 +271,18 @@ func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID, ...@@ -268,17 +271,18 @@ func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID,
// Use the key to get the existing Message // Use the key to get the existing Message
currentMsg, err := impl.Get(w.db, messageStoreName, key) currentMsg, err := impl.Get(w.db, messageStoreName, key)
if err != nil { if err != nil {
jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr, if strings.Contains(err.Error(), impl.ErrDoesNotExist) {
"Unable to get message: %+v", err)) return errors.WithMessage(channels.NoMessageErr, parentErr)
return }
return errors.WithMessage(err, parentErr)
} }
_, err = w.updateMessage(utils.JsToJson(currentMsg), messageID, timestamp, _, err = w.updateMessage(utils.JsToJson(currentMsg), messageID, timestamp,
round, pinned, hidden, status) round, pinned, hidden, status)
if err != nil { if err != nil {
jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr, return errors.WithMessage(err, parentErr)
"Unable to updateMessage: %+v", err))
} }
return nil
} }
// UpdateFromMessageID is called whenever a message with the message ID is // UpdateFromMessageID is called whenever a message with the message ID is
...@@ -290,10 +294,13 @@ func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID, ...@@ -290,10 +294,13 @@ func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID,
// timestamp, round, pinned, and hidden are all nillable and may be updated // timestamp, round, pinned, and hidden are all nillable and may be updated
// based upon the UUID at a later date. If a nil value is passed, then make // based upon the UUID at a later date. If a nil value is passed, then make
// no update. // no update.
//
// Returns an error if the message cannot be updated. It must return
// channels.NoMessageErr if the message does not exist.
func (w *wasmModel) UpdateFromMessageID(messageID message.ID, func (w *wasmModel) UpdateFromMessageID(messageID message.ID,
timestamp *time.Time, round *rounds.Round, pinned, hidden *bool, timestamp *time.Time, round *rounds.Round, pinned, hidden *bool,
status *channels.SentStatus) uint64 { status *channels.SentStatus) (uint64, error) {
parentErr := errors.New("failed to UpdateFromMessageID") parentErr := "failed to UpdateFromMessageID"
// FIXME: this is a bit of race condition without the mux. // FIXME: this is a bit of race condition without the mux.
// This should be done via the transactions (i.e., make a // This should be done via the transactions (i.e., make a
...@@ -304,19 +311,19 @@ func (w *wasmModel) UpdateFromMessageID(messageID message.ID, ...@@ -304,19 +311,19 @@ func (w *wasmModel) UpdateFromMessageID(messageID message.ID,
currentMsgObj, err := impl.GetIndex(w.db, messageStoreName, currentMsgObj, err := impl.GetIndex(w.db, messageStoreName,
messageStoreMessageIndex, impl.EncodeBytes(messageID.Marshal())) messageStoreMessageIndex, impl.EncodeBytes(messageID.Marshal()))
if err != nil { if err != nil {
jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr, if strings.Contains(err.Error(), impl.ErrDoesNotExist) {
"Failed to get message by index: %+v", err)) return 0, errors.WithMessage(channels.NoMessageErr, parentErr)
return 0 }
return 0, errors.WithMessage(err, parentErr)
} }
currentMsg := utils.JsToJson(currentMsgObj) currentMsg := utils.JsToJson(currentMsgObj)
uuid, err := w.updateMessage(currentMsg, &messageID, timestamp, uuid, err := w.updateMessage(currentMsg, &messageID, timestamp,
round, pinned, hidden, status) round, pinned, hidden, status)
if err != nil { if err != nil {
jww.ERROR.Printf("%+v", errors.WithMessagef(parentErr, return 0, errors.WithMessage(err, parentErr)
"Unable to updateMessage: %+v", err))
} }
return uuid return uuid, nil
} }
// updateMessage is a helper for updating a stored message. // updateMessage is a helper for updating a stored message.
......
...@@ -263,9 +263,12 @@ type MessageUpdateInfo struct { ...@@ -263,9 +263,12 @@ type MessageUpdateInfo struct {
// messageID, timestamp, round, pinned, and hidden are all nillable and may be // messageID, timestamp, round, pinned, and hidden are all nillable and may be
// updated based upon the UUID at a later date. If a nil value is passed, then // updated based upon the UUID at a later date. If a nil value is passed, then
// make no update. // make no update.
//
// Returns an error if the message cannot be updated. It must return
// [channels.NoMessageErr] if the message does not exist.
func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID, func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID,
timestamp *time.Time, round *rounds.Round, pinned, hidden *bool, timestamp *time.Time, round *rounds.Round, pinned, hidden *bool,
status *channels.SentStatus) { status *channels.SentStatus) error {
msg := MessageUpdateInfo{UUID: uuid} msg := MessageUpdateInfo{UUID: uuid}
if messageID != nil { if messageID != nil {
msg.MessageID = *messageID msg.MessageID = *messageID
...@@ -294,12 +297,33 @@ func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID, ...@@ -294,12 +297,33 @@ func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID,
data, err := json.Marshal(msg) data, err := json.Marshal(msg)
if err != nil { if err != nil {
jww.ERROR.Printf( return errors.Errorf(
"[CH] Could not JSON marshal payload for UpdateFromUUID: %+v", err) "could not JSON marshal payload for UpdateFromUUID: %+v", err)
return
} }
w.wm.SendMessage(UpdateFromUUIDTag, data, nil) errChan := make(chan error)
w.wm.SendMessage(UpdateFromUUIDTag, data, func(data []byte) {
if data != nil {
errChan <- errors.New(string(data))
} else {
errChan <- nil
}
})
select {
case err = <-errChan:
return err
case <-time.After(worker.ResponseTimeout):
return errors.Errorf("timed out after %s waiting for response from "+
"the worker about UpdateFromUUID", worker.ResponseTimeout)
}
}
// UuidError is JSON marshalled and sent to the worker for
// [wasmModel.UpdateFromMessageID].
type UuidError struct {
UUID uint64 `json:"uuid"`
Error []byte `json:"error"`
} }
// UpdateFromMessageID is called whenever a message with the message ID is // UpdateFromMessageID is called whenever a message with the message ID is
...@@ -313,7 +337,7 @@ func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID, ...@@ -313,7 +337,7 @@ func (w *wasmModel) UpdateFromUUID(uuid uint64, messageID *message.ID,
// no update. // no update.
func (w *wasmModel) UpdateFromMessageID(messageID message.ID, func (w *wasmModel) UpdateFromMessageID(messageID message.ID,
timestamp *time.Time, round *rounds.Round, pinned, hidden *bool, timestamp *time.Time, round *rounds.Round, pinned, hidden *bool,
status *channels.SentStatus) uint64 { status *channels.SentStatus) (uint64, error) {
msg := MessageUpdateInfo{MessageID: messageID, MessageIDSet: true} msg := MessageUpdateInfo{MessageID: messageID, MessageIDSet: true}
if timestamp != nil { if timestamp != nil {
...@@ -339,33 +363,34 @@ func (w *wasmModel) UpdateFromMessageID(messageID message.ID, ...@@ -339,33 +363,34 @@ func (w *wasmModel) UpdateFromMessageID(messageID message.ID,
data, err := json.Marshal(msg) data, err := json.Marshal(msg)
if err != nil { if err != nil {
jww.ERROR.Printf("[CH] Could not JSON marshal payload for "+ return 0, errors.Errorf("could not JSON marshal payload for "+
"UpdateFromMessageID: %+v", err) "UpdateFromMessageID: %+v", err)
return 0
} }
uuidChan := make(chan uint64) uuidChan := make(chan uint64)
errChan := make(chan error)
w.wm.SendMessage(UpdateFromMessageIDTag, data, w.wm.SendMessage(UpdateFromMessageIDTag, data,
func(data []byte) { func(data []byte) {
var uuid uint64 var ue UuidError
err = json.Unmarshal(data, &uuid) if err = json.Unmarshal(data, &ue); err != nil {
if err != nil { errChan <- errors.Errorf("could not JSON unmarshal response "+
jww.ERROR.Printf("[CH] Could not JSON unmarshal response to "+ "to UpdateFromMessageID: %+v", err)
"UpdateFromMessageID: %+v", err) } else if ue.Error != nil {
uuidChan <- 0 errChan <- errors.New(string(ue.Error))
} else {
uuidChan <- ue.UUID
} }
uuidChan <- uuid
}) })
select { select {
case uuid := <-uuidChan: case uuid := <-uuidChan:
return uuid return uuid, nil
case err = <-errChan:
return 0, err
case <-time.After(worker.ResponseTimeout): case <-time.After(worker.ResponseTimeout):
jww.ERROR.Printf("[CH] Timed out after %s waiting for response from "+ return 0, errors.Errorf("timed out after %s waiting for response from "+
"the worker about UpdateFromMessageID", worker.ResponseTimeout) "the worker about UpdateFromMessageID", worker.ResponseTimeout)
} }
return 0
} }
// GetMessageMessage is JSON marshalled and sent to the worker for // GetMessageMessage is JSON marshalled and sent to the worker for
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment