Skip to content

Commit

Permalink
fixup! messagetracking: add detection of 2fa setup required error
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed May 1, 2024
1 parent 930da0a commit 4b02b11
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
8 changes: 4 additions & 4 deletions messagetracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (portal *Portal) sendDeliveryReceipt(eventID id.EventID) {
}
}

func (portal *Portal) sendMessageMetrics(evt *event.Event, err error, part string, ms *metricSender) {
func (portal *Portal) sendMessageMetrics(user *User, evt *event.Event, err error, part string, ms *metricSender) {
var msgType string
switch evt.Type {
case event.EventMessage:
Expand Down Expand Up @@ -180,7 +180,7 @@ func (portal *Portal) sendMessageMetrics(evt *event.Event, err error, part strin
// Detect two_factor_setup_required errors and also set the user's
// state to BAD_CREDENTIALS.
if err.Error() == "two_factor_setup_required" {
portal.bridge.SendBridgeState(context.TODO(), &status.BridgeState{
user.BridgeStates[portal.Key.TeamID].Send(status.BridgeState{
StateEvent: status.StateBadCredentials,
Reason: err.Error(),
})
Expand Down Expand Up @@ -283,13 +283,13 @@ func (ms *metricSender) setNoticeID(evtID id.EventID) {
}
}

func (ms *metricSender) sendMessageMetrics(evt *event.Event, err error, part string, completed bool) {
func (ms *metricSender) sendMessageMetrics(user *User, evt *event.Event, err error, part string, completed bool) {
ms.lock.Lock()
defer ms.lock.Unlock()
if !completed && ms.completed {
return
}
ms.portal.sendMessageMetrics(evt, err, part, ms)
ms.portal.sendMessageMetrics(user, evt, err, part, ms)
ms.retryNum++
ms.completed = completed
}
40 changes: 20 additions & 20 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event, ms *me
userTeam := sender.GetUserTeam(portal.Key.TeamID)
if userTeam == nil {
portal.log.Warnfln("User %s not logged into team %s", sender.MXID, portal.Key.TeamID)
go ms.sendMessageMetrics(evt, errUserNotLoggedIn, "Ignoring", true)
go ms.sendMessageMetrics(sender, evt, errUserNotLoggedIn, "Ignoring", true)
return
}
if userTeam.Client == nil {
Expand All @@ -620,7 +620,7 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event, ms *me
existing := portal.bridge.DB.Message.GetByMatrixID(portal.Key, evt.ID)
if existing != nil {
portal.log.Debugln("not handling duplicate message", evt.ID)
go ms.sendMessageMetrics(evt, nil, "", true)
go ms.sendMessageMetrics(sender, evt, nil, "", true)
return
}

Expand All @@ -637,14 +637,14 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event, ms *me
if errorAfter > 0 {
remainingTime := errorAfter - messageAge
if remainingTime < 0 {
go ms.sendMessageMetrics(evt, errTimeoutBeforeHandling, "Timeout handling", true)
go ms.sendMessageMetrics(sender, evt, errTimeoutBeforeHandling, "Timeout handling", true)
return
} else if remainingTime < 1*time.Second {
portal.log.Warnfln("Message %s was delayed before reaching the bridge, only have %s (of %s timeout) until delay warning", evt.ID, remainingTime, errorAfter)
}
go func() {
time.Sleep(remainingTime)
ms.sendMessageMetrics(evt, errMessageTakingLong, "Timeout handling", false)
ms.sendMessageMetrics(sender, evt, errMessageTakingLong, "Timeout handling", false)
}()
}

Expand All @@ -663,7 +663,7 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event, ms *me
start = time.Now()
var timestamp string
if options == nil && fileUpload == nil {
go ms.sendMessageMetrics(evt, err, "Error converting", true)
go ms.sendMessageMetrics(sender, evt, err, "Error converting", true)
return
} else if options != nil {
portal.log.Debugfln("Sending message %s to Slack %s %s", evt.ID, portal.Key.TeamID, portal.Key.ChannelID)
Expand All @@ -672,15 +672,15 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event, ms *me
slack.MsgOptionAsUser(true),
slack.MsgOptionCompose(options...))
if err != nil {
go ms.sendMessageMetrics(evt, err, "Error sending", true)
go ms.sendMessageMetrics(sender, evt, err, "Error sending", true)
return
}
} else if fileUpload != nil {
portal.log.Debugfln("Uploading file from message %s to Slack %s %s", evt.ID, portal.Key.TeamID, portal.Key.ChannelID)
file, err := userTeam.Client.UploadFile(*fileUpload)
if err != nil {
portal.log.Errorfln("Failed to upload slack attachment: %v", err)
go ms.sendMessageMetrics(evt, errMediaSlackUploadFailed, "Error uploading", true)
go ms.sendMessageMetrics(sender, evt, errMediaSlackUploadFailed, "Error uploading", true)
return
}
var shareInfo slack.ShareFileInfo
Expand All @@ -690,13 +690,13 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event, ms *me
} else if info, found := file.Shares.Public[portal.Key.ChannelID]; found && len(info) > 0 {
shareInfo = info[0]
} else {
go ms.sendMessageMetrics(evt, errMediaSlackUploadFailed, "Error uploading", true)
go ms.sendMessageMetrics(sender, evt, errMediaSlackUploadFailed, "Error uploading", true)
return
}
timestamp = shareInfo.Ts
}
ms.timings.totalSend = time.Since(start)
go ms.sendMessageMetrics(evt, err, "Error sending", true)
go ms.sendMessageMetrics(sender, evt, err, "Error sending", true)
// TODO: store these timings in some way

if timestamp != "" {
Expand Down Expand Up @@ -805,14 +805,14 @@ func (portal *Portal) handleMatrixReaction(sender *User, evt *event.Event, ms *m

userTeam := sender.GetUserTeam(portal.Key.TeamID)
if userTeam == nil {
go ms.sendMessageMetrics(evt, errUserNotLoggedIn, "Ignoring", true)
go ms.sendMessageMetrics(sender, evt, errUserNotLoggedIn, "Ignoring", true)
return
}

reaction := evt.Content.AsReaction()
if reaction.RelatesTo.Type != event.RelAnnotation {
portal.log.Errorfln("Ignoring reaction %s due to unknown m.relates_to data", evt.ID)
ms.sendMessageMetrics(evt, errUnexpectedRelatesTo, "Error sending", true)
ms.sendMessageMetrics(sender, evt, errUnexpectedRelatesTo, "Error sending", true)
return
}

Expand Down Expand Up @@ -842,7 +842,7 @@ func (portal *Portal) handleMatrixReaction(sender *User, evt *event.Event, ms *m
}
if slackID == "" {
portal.log.Debugf("Message %s has not yet been sent to slack", reaction.RelatesTo.EventID)
ms.sendMessageMetrics(evt, errReactionTargetNotFound, "Error sending", true)
ms.sendMessageMetrics(sender, evt, errReactionTargetNotFound, "Error sending", true)
return
}

Expand All @@ -861,7 +861,7 @@ func (portal *Portal) handleMatrixReaction(sender *User, evt *event.Event, ms *m

if emojiID == "" {
portal.log.Errorfln("Couldn't find shortcode for emoji %s", reaction.RelatesTo.Key)
ms.sendMessageMetrics(evt, errEmojiShortcodeNotFound, "Error sending", true)
ms.sendMessageMetrics(sender, evt, errEmojiShortcodeNotFound, "Error sending", true)
return
}

Expand All @@ -883,7 +883,7 @@ func (portal *Portal) handleMatrixReaction(sender *User, evt *event.Event, ms *m
Channel: portal.Key.ChannelID,
Timestamp: slackID,
})
ms.sendMessageMetrics(evt, err, "Error sending", true)
ms.sendMessageMetrics(sender, evt, err, "Error sending", true)
if err != nil {
portal.log.Debugfln("Failed to send reaction %s id:%s: %v", portal.Key, slackID, err)
return
Expand All @@ -906,7 +906,7 @@ func (portal *Portal) handleMatrixRedaction(user *User, evt *event.Event) {

userTeam := user.GetUserTeam(portal.Key.TeamID)
if userTeam == nil {
go portal.sendMessageMetrics(evt, errUserNotLoggedIn, "Ignoring", nil)
go portal.sendMessageMetrics(user, evt, errUserNotLoggedIn, "Ignoring", nil)
return
}
portal.log.Debugfln("Received redaction %s from %s", evt.ID, evt.Sender)
Expand All @@ -921,9 +921,9 @@ func (portal *Portal) handleMatrixRedaction(user *User, evt *event.Event) {
} else {
message.Delete()
}
go portal.sendMessageMetrics(evt, err, "Error sending", nil)
go portal.sendMessageMetrics(user, evt, err, "Error sending", nil)
} else {
go portal.sendMessageMetrics(evt, errTargetNotFound, "Error sending", nil)
go portal.sendMessageMetrics(user, evt, errTargetNotFound, "Error sending", nil)
}
return
}
Expand All @@ -945,15 +945,15 @@ func (portal *Portal) handleMatrixRedaction(user *User, evt *event.Event) {
} else {
reaction.Delete()
}
go portal.sendMessageMetrics(evt, err, "Error sending", nil)
go portal.sendMessageMetrics(user, evt, err, "Error sending", nil)
} else {
go portal.sendMessageMetrics(evt, errUnknownEmoji, "Error sending", nil)
go portal.sendMessageMetrics(user, evt, errUnknownEmoji, "Error sending", nil)
}
return
}

portal.log.Warnfln("Failed to redact %s@%s: no event found", portal.Key, evt.Redacts)
go portal.sendMessageMetrics(evt, errReactionTargetNotFound, "Error sending", nil)
go portal.sendMessageMetrics(user, evt, errReactionTargetNotFound, "Error sending", nil)
}

func typingDiff(prev, new []id.UserID) (started []id.UserID) {
Expand Down

0 comments on commit 4b02b11

Please sign in to comment.