From 83d397900f133d20671395e9fb87fc5bdd5962cf Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Tue, 5 Apr 2022 15:35:02 -0600 Subject: [PATCH] media: add better error when media couldn't be retrieved due to WA deleting it from their servers --- portal.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/portal.go b/portal.go index c98efda..d905e93 100644 --- a/portal.go +++ b/portal.go @@ -1900,7 +1900,7 @@ func shallowCopyMap(data map[string]interface{}) map[string]interface{} { return newMap } -func (portal *Portal) makeMediaBridgeFailureMessage(info *types.MessageInfo, bridgeErr error, converted *ConvertedMessage, keys *FailedMediaKeys) *ConvertedMessage { +func (portal *Portal) makeMediaBridgeFailureMessage(info *types.MessageInfo, bridgeErr error, converted *ConvertedMessage, keys *FailedMediaKeys, userFriendlyError string) *ConvertedMessage { portal.log.Errorfln("Failed to bridge media for %s: %v", info.ID, bridgeErr) if keys != nil { meta := &FailedMediaMeta{ @@ -1913,9 +1913,13 @@ func (portal *Portal) makeMediaBridgeFailureMessage(info *types.MessageInfo, bri portal.mediaErrorCache[info.ID] = meta } converted.Type = event.EventMessage + body := userFriendlyError + if body == "" { + body = fmt.Sprintf("Failed to bridge media: %v", bridgeErr) + } converted.Content = &event.MessageEventContent{ MsgType: event.MsgNotice, - Body: fmt.Sprintf("Failed to bridge media: %v", bridgeErr), + Body: body, } return converted } @@ -2164,24 +2168,24 @@ func (portal *Portal) convertMediaMessage(intent *appservice.IntentAPI, source * Type: whatsmeow.GetMediaType(msg), SHA256: msg.GetFileSha256(), EncSHA256: msg.GetFileEncSha256(), - }) + }, "Old photo or attachment. This will sync in a future update.") } else if errors.Is(err, whatsmeow.ErrNoURLPresent) { portal.log.Debugfln("No URL present error for media message %s, ignoring...", info.ID) return nil } else if errors.Is(err, whatsmeow.ErrFileLengthMismatch) || errors.Is(err, whatsmeow.ErrInvalidMediaSHA256) { portal.log.Warnfln("Mismatching media checksums in %s: %v. Ignoring because WhatsApp seems to ignore them too", info.ID, err) } else if err != nil { - return portal.makeMediaBridgeFailureMessage(info, err, converted, nil) + return portal.makeMediaBridgeFailureMessage(info, err, converted, nil, "") } err = portal.uploadMedia(intent, data, converted.Content) if err != nil { if errors.Is(err, mautrix.MTooLarge) { - return portal.makeMediaBridgeFailureMessage(info, errors.New("homeserver rejected too large file"), converted, nil) + return portal.makeMediaBridgeFailureMessage(info, errors.New("homeserver rejected too large file"), converted, nil, "") } else if httpErr, ok := err.(mautrix.HTTPError); ok && httpErr.IsStatus(413) { - return portal.makeMediaBridgeFailureMessage(info, errors.New("proxy rejected too large file"), converted, nil) + return portal.makeMediaBridgeFailureMessage(info, errors.New("proxy rejected too large file"), converted, nil, "") } else { - return portal.makeMediaBridgeFailureMessage(info, fmt.Errorf("failed to upload media: %w", err), converted, nil) + return portal.makeMediaBridgeFailureMessage(info, fmt.Errorf("failed to upload media: %w", err), converted, nil, "") } } return converted