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

Make the send tracking code clearer

parent 2dce69c4
No related branches found
No related tags found
2 merge requests!564Clean up the send tracking code, and properly mark DMs as delivered,!515Release
......@@ -81,19 +81,19 @@ func NewDMClient(myID *codename.PrivateIdentity, receiver EventModel,
}
// Register the listener
dmc.register(receiver, dmc.st.CheckIfSent)
dmc.register(receiver, dmc.st)
return dmc
}
// Register registers a listener for direct messages.
func (dc *dmClient) register(apiReceiver EventModel,
checkSent messageReceiveFunc) error {
tracker SendTracker) error {
beginningOfTime := time.Time{}
r := &receiver{
c: dc,
api: apiReceiver,
checkSent: checkSent,
sendTracker: tracker,
}
// Initialize Send Tracking
......
......@@ -277,4 +277,10 @@ type SendTracker interface {
//CheckIfSent checks if the given message was a sent message
CheckIfSent(messageID cryptoMessage.ID, r rounds.Round) bool
//Delivered marks a message delivered
Delivered(msgID cryptoMessage.ID, round rounds.Round) bool
//StopTracking stops tracking a message
StopTracking(msgID cryptoMessage.ID, round rounds.Round) bool
}
......@@ -31,7 +31,7 @@ import (
type receiver struct {
c *dmClient
api EventModel
checkSent messageReceiveFunc
sendTracker SendTracker
}
type dmProcessor struct {
......@@ -78,7 +78,9 @@ func (dp *dmProcessor) Process(msg format.Message,
msgID := message.DeriveDirectMessageID(myID, directMsg)
// Check if we sent the message and ignore triggering if we sent
if dp.r.checkSent(msgID, round) {
// This will happen when DM'ing with oneself, but the receive self
// processor will update the status to delivered, so we do nothing here.
if dp.r.sendTracker.CheckIfSent(msgID, round) {
return
}
......@@ -172,8 +174,22 @@ func (sp *selfProcessor) Process(msg format.Message,
msgID := message.DeriveDirectMessageID(partnerID, directMsg)
// Check if we sent the message and ignore triggering if we sent
if sp.r.checkSent(msgID, round) {
// Check if we sent the message and ignore triggering if we
// sent, but mark the message as delivered
if sp.r.sendTracker.CheckIfSent(msgID, round) {
go func() {
ok := sp.r.sendTracker.Delivered(msgID, round)
if !ok {
jww.WARN.Printf("[DM] Couldn't mark delivered"+
": %s %v)",
msgID, round)
}
sp.r.sendTracker.StopTracking(msgID, round)
if !ok {
jww.WARN.Printf("[DM] Coulnd't StopTracking: "+
"%s, %v", msgID, round)
}
}()
return
}
......
......@@ -319,20 +319,39 @@ func (st *sendTracker) handleSendFailed(uuid uint64) (*tracked, error) {
}
// CheckIfSent is used when a message is received to check if the message was
// sent by this user. If it was, the correct signal is sent to the event model
// and the function returns true, notifying the caller to not process the
// message.
// sent by this user.
func (st *sendTracker) CheckIfSent(
messageID message.ID, round rounds.Round) bool {
st.mux.RLock()
// Skip if already added
defer st.mux.RUnlock()
_, existsMessage := st.byMessageID[messageID]
st.mux.RUnlock()
return existsMessage
}
// Delivered calls the event model update function to tell it that this
// message was delivered. (after this is called successfully, it is safe to
// stop tracking this message).
// returns true if the update sent status func was called.
func (st *sendTracker) Delivered(messageID message.ID,
round rounds.Round) bool {
st.mux.RLock()
defer st.mux.RUnlock()
msgData, existsMessage := st.byMessageID[messageID]
if !existsMessage {
return false
}
ts := message.MutateTimestamp(round.Timestamps[states.QUEUED],
messageID)
st.updateStatus(msgData.UUID, messageID, ts,
round, Sent)
return true
}
// StopTracking deletes this message id/round combination from the
// send tracking. returns true if it was removed, false otherwise.
func (st *sendTracker) StopTracking(messageID message.ID,
round rounds.Round) bool {
st.mux.Lock()
defer st.mux.Unlock()
msgData, existsMessage := st.byMessageID[messageID]
......@@ -359,11 +378,6 @@ func (st *sendTracker) CheckIfSent(
}
}
ts := message.MutateTimestamp(round.Timestamps[states.QUEUED],
messageID)
go st.updateStatus(msgData.UUID, messageID, ts,
round, Sent)
if err := st.storeSent(); err != nil {
jww.FATAL.Panicf("failed to store the updated sent list: %+v",
err)
......
......@@ -84,6 +84,8 @@ func TestSendTracker_MessageReceive(t *testing.T) {
require.NoError(t, err)
process = st.CheckIfSent(mid, r)
st.Delivered(mid, r)
st.StopTracking(mid, r)
require.True(t, process)
directMessage2 := &DirectMessage{
......@@ -102,6 +104,8 @@ func TestSendTracker_MessageReceive(t *testing.T) {
require.NoError(t, err)
process = st.CheckIfSent(mid, r)
require.True(t, process)
st.Delivered(mid, r)
st.StopTracking(mid, r)
}
// Test failedSend function, confirming that data is stored appropriately and
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment