Only send checkpoint after message reaches phone

This commit is contained in:
Tulir Asokan 2023-08-18 20:43:46 +03:00
parent af212c9339
commit 40f78b8fd6
2 changed files with 33 additions and 1 deletions

View file

@ -25,11 +25,13 @@ import (
"time" "time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"go.mau.fi/util/jsontime"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/mautrix/bridge/status" "maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-gmessages/database"
"go.mau.fi/mautrix-gmessages/libgm/gmproto" "go.mau.fi/mautrix-gmessages/libgm/gmproto"
) )
@ -126,6 +128,26 @@ func (portal *Portal) sendErrorMessage(evt *event.Event, err error, msgType stri
return resp.EventID return resp.EventID
} }
func (portal *Portal) sendCheckpoint(dbMsg *database.Message, err error, delivered bool) {
checkpoint := status.MessageCheckpoint{
EventID: dbMsg.MXID,
RoomID: dbMsg.RoomID,
Step: status.MsgStepRemote,
Timestamp: jsontime.UnixMilliNow(),
Status: "",
ReportedBy: status.MsgReportedByBridge,
}
if err != nil {
checkpoint.Status = status.MsgStatusPermFailure
checkpoint.Info = err.Error()
} else if delivered {
checkpoint.Status = status.MsgStatusDelivered
} else {
checkpoint.Status = status.MsgStatusSuccess
}
go portal.bridge.SendRawMessageCheckpoint(&checkpoint)
}
func (portal *Portal) sendStatusEvent(evtID, lastRetry id.EventID, err error, deliveredTo *[]id.UserID) { func (portal *Portal) sendStatusEvent(evtID, lastRetry id.EventID, err error, deliveredTo *[]id.UserID) {
if !portal.bridge.Config.Bridge.MessageStatusEvents { if !portal.bridge.Config.Bridge.MessageStatusEvents {
return return
@ -209,8 +231,8 @@ func (portal *Portal) sendMessageMetrics(evt *event.Event, err error, part strin
Str("event_type", evt.Type.Type). Str("event_type", evt.Type.Type).
Msg("Handled Matrix event") Msg("Handled Matrix event")
portal.sendDeliveryReceipt(evt.ID) portal.sendDeliveryReceipt(evt.ID)
portal.bridge.SendMessageSuccessCheckpoint(evt, status.MsgStepRemote, ms.getRetryNum())
if msgType != "message" { if msgType != "message" {
portal.bridge.SendMessageSuccessCheckpoint(evt, status.MsgStepRemote, ms.getRetryNum())
portal.sendStatusEvent(origEvtID, evt.ID, nil, nil) portal.sendStatusEvent(origEvtID, evt.ID, nil, nil)
} }
if prevNotice := ms.popNoticeID(); prevNotice != "" { if prevNotice := ms.popNoticeID(); prevNotice != "" {

View file

@ -495,16 +495,24 @@ func (portal *Portal) handleExistingMessageUpdate(ctx context.Context, source *U
} }
case !dbMsg.Status.ReadReceiptSent && portal.IsPrivateChat() && newStatus == gmproto.MessageStatusType_OUTGOING_DISPLAYED: case !dbMsg.Status.ReadReceiptSent && portal.IsPrivateChat() && newStatus == gmproto.MessageStatusType_OUTGOING_DISPLAYED:
dbMsg.Status.ReadReceiptSent = true dbMsg.Status.ReadReceiptSent = true
if !dbMsg.Status.MSSSent {
portal.sendCheckpoint(dbMsg, nil, false)
}
if !dbMsg.Status.MSSDeliverySent { if !dbMsg.Status.MSSDeliverySent {
dbMsg.Status.MSSDeliverySent = true dbMsg.Status.MSSDeliverySent = true
dbMsg.Status.MSSSent = true dbMsg.Status.MSSSent = true
go portal.sendStatusEvent(dbMsg.MXID, "", nil, &[]id.UserID{portal.MainIntent().UserID}) go portal.sendStatusEvent(dbMsg.MXID, "", nil, &[]id.UserID{portal.MainIntent().UserID})
portal.sendCheckpoint(dbMsg, nil, true)
} }
err := portal.MainIntent().MarkRead(portal.MXID, dbMsg.MXID) err := portal.MainIntent().MarkRead(portal.MXID, dbMsg.MXID)
if err != nil { if err != nil {
log.Warn().Err(err).Msg("Failed to mark message as read") log.Warn().Err(err).Msg("Failed to mark message as read")
} }
case !dbMsg.Status.MSSDeliverySent && portal.IsPrivateChat() && newStatus == gmproto.MessageStatusType_OUTGOING_DELIVERED: case !dbMsg.Status.MSSDeliverySent && portal.IsPrivateChat() && newStatus == gmproto.MessageStatusType_OUTGOING_DELIVERED:
if !dbMsg.Status.MSSSent {
portal.sendCheckpoint(dbMsg, nil, false)
}
portal.sendCheckpoint(dbMsg, nil, true)
dbMsg.Status.MSSDeliverySent = true dbMsg.Status.MSSDeliverySent = true
dbMsg.Status.MSSSent = true dbMsg.Status.MSSSent = true
go portal.sendStatusEvent(dbMsg.MXID, "", nil, &[]id.UserID{portal.MainIntent().UserID}) go portal.sendStatusEvent(dbMsg.MXID, "", nil, &[]id.UserID{portal.MainIntent().UserID})
@ -516,8 +524,10 @@ func (portal *Portal) handleExistingMessageUpdate(ctx context.Context, source *U
deliveredTo = &[]id.UserID{} deliveredTo = &[]id.UserID{}
} }
go portal.sendStatusEvent(dbMsg.MXID, "", nil, deliveredTo) go portal.sendStatusEvent(dbMsg.MXID, "", nil, deliveredTo)
portal.sendCheckpoint(dbMsg, nil, false)
case !dbMsg.Status.MSSFailSent && !dbMsg.Status.MSSSent && isFailSendStatus(newStatus): case !dbMsg.Status.MSSFailSent && !dbMsg.Status.MSSSent && isFailSendStatus(newStatus):
go portal.sendStatusEvent(dbMsg.MXID, "", OutgoingStatusError(newStatus), nil) go portal.sendStatusEvent(dbMsg.MXID, "", OutgoingStatusError(newStatus), nil)
portal.sendCheckpoint(dbMsg, OutgoingStatusError(newStatus), false)
// TODO error notice // TODO error notice
default: default:
log.Debug().Msg("Ignored message update") log.Debug().Msg("Ignored message update")