Add better errors for rejected edits

This commit is contained in:
Tulir Asokan 2023-04-06 14:15:44 +03:00
parent c2bda58ad6
commit 5ffd92731b
4 changed files with 32 additions and 6 deletions

2
go.mod
View file

@ -11,7 +11,7 @@ require (
github.com/prometheus/client_golang v1.14.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/tidwall/gjson v1.14.4
go.mau.fi/whatsmeow v0.0.0-20230406104207-886f40e6f4f2
go.mau.fi/whatsmeow v0.0.0-20230406111517-0120a5e3f989
golang.org/x/image v0.6.0
golang.org/x/net v0.8.0
google.golang.org/protobuf v1.28.1

4
go.sum
View file

@ -67,8 +67,8 @@ github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mau.fi/libsignal v0.1.0 h1:vAKI/nJ5tMhdzke4cTK1fb0idJzz1JuEIpmjprueC+c=
go.mau.fi/libsignal v0.1.0/go.mod h1:R8ovrTezxtUNzCQE5PH30StOQWWeBskBsWE55vMfY9I=
go.mau.fi/whatsmeow v0.0.0-20230406104207-886f40e6f4f2 h1:A5KW5iiMn8e2JOntwbwLTbyaMHBEQYnibaKiOqvba1c=
go.mau.fi/whatsmeow v0.0.0-20230406104207-886f40e6f4f2/go.mod h1:zoTtv1CupGEyTew7TOwnBmTbHB4pVad2OzjTf5CVwa0=
go.mau.fi/whatsmeow v0.0.0-20230406111517-0120a5e3f989 h1:fa9OHOLT/+bhRSThxGLQ7Qeyb5ayghsJke/Naage/zM=
go.mau.fi/whatsmeow v0.0.0-20230406111517-0120a5e3f989/go.mod h1:zoTtv1CupGEyTew7TOwnBmTbHB4pVad2OzjTf5CVwa0=
go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto=
go.mau.fi/zeroconfig v0.1.2/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

View file

@ -55,6 +55,11 @@ var (
errPollMissingQuestion = errors.New("poll message is missing question")
errPollDuplicateOption = errors.New("poll options must be unique")
errEditUnknownTarget = errors.New("unknown edit target message")
errEditUnknownTargetType = errors.New("unsupported edited message type")
errEditDifferentSender = errors.New("can't edit message sent by another user")
errEditTooOld = errors.New("message is too old to be edited")
errBroadcastReactionNotSupported = errors.New("reacting to status messages is not currently supported")
errBroadcastSendDisabled = errors.New("sending status messages is disabled")
@ -78,7 +83,13 @@ func errorToStatusReason(err error) (reason event.MessageStatusReason, status ev
return event.MessageStatusUnsupported, event.MessageStatusFail, true, true, ""
case errors.Is(err, errMNoticeDisabled):
return event.MessageStatusUnsupported, event.MessageStatusFail, true, false, ""
case errors.Is(err, errMediaUnsupportedType), errors.Is(err, errPollMissingQuestion), errors.Is(err, errPollDuplicateOption):
case errors.Is(err, errMediaUnsupportedType),
errors.Is(err, errPollMissingQuestion),
errors.Is(err, errPollDuplicateOption),
errors.Is(err, errEditDifferentSender),
errors.Is(err, errEditTooOld),
errors.Is(err, errEditUnknownTarget),
errors.Is(err, errEditUnknownTargetType):
return event.MessageStatusUnsupported, event.MessageStatusFail, true, true, err.Error()
case errors.Is(err, errTimeoutBeforeHandling):
return event.MessageStatusTooOld, event.MessageStatusRetriable, true, true, "the message was too old when it reached the bridge, so it was not handled"

View file

@ -3774,6 +3774,21 @@ type extraConvertMeta struct {
EditRootMsg *database.Message
}
func getEditError(rootMsg *database.Message, editer *User) error {
switch {
case rootMsg == nil:
return errEditUnknownTarget
case rootMsg.Type != database.MsgNormal || rootMsg.IsFakeJID():
return errEditUnknownTargetType
case rootMsg.Sender.User != editer.JID.User:
return errEditDifferentSender
case time.Since(rootMsg.Timestamp) > whatsmeow.EditWindow:
return errEditTooOld
default:
return nil
}
}
func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, evt *event.Event) (*waProto.Message, *User, *extraConvertMeta, error) {
if evt.Type == TypeMSC3381PollResponse || evt.Type == TypeMSC3381V2PollResponse {
return portal.convertMatrixPollVote(ctx, sender, evt)
@ -3788,8 +3803,8 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, ev
var editRootMsg *database.Message
if editEventID := content.RelatesTo.GetReplaceID(); editEventID != "" && portal.bridge.Config.Bridge.SendWhatsAppEdits {
editRootMsg = portal.bridge.DB.Message.GetByMXID(editEventID)
if editRootMsg == nil || editRootMsg.Type != database.MsgNormal || editRootMsg.IsFakeJID() || editRootMsg.Sender.User != sender.JID.User {
return nil, sender, extraMeta, fmt.Errorf("edit rejected") // TODO more specific error message
if editErr := getEditError(editRootMsg, sender); editErr != nil {
return nil, sender, extraMeta, editErr
}
extraMeta.EditRootMsg = editRootMsg
if content.NewContent != nil {