directmedia: add more logs and make flag configurable

This commit is contained in:
Tulir Asokan 2024-11-06 16:48:42 +01:00
parent 8ae6198551
commit a55ba03fb2
3 changed files with 17 additions and 3 deletions

View file

@ -45,6 +45,7 @@ type Config struct {
ExtEvPolls bool `yaml:"extev_polls"`
DisableViewOnce bool `yaml:"disable_view_once"`
ForceActiveDeliveryReceipts bool `yaml:"force_active_delivery_receipts"`
DirectMediaAutoRequest bool `yaml:"direct_media_auto_request"`
AnimatedSticker msgconv.AnimatedStickerConfig `yaml:"animated_sticker"`
@ -106,6 +107,7 @@ func upgradeConfig(helper up.Helper) {
helper.Copy(up.Bool, "extev_polls")
helper.Copy(up.Bool, "disable_view_once")
helper.Copy(up.Bool, "force_active_delivery_receipts")
helper.Copy(up.Bool, "direct_media_auto_request")
helper.Copy(up.Str, "animated_sticker", "target")
helper.Copy(up.Int, "animated_sticker", "args", "width")

View file

@ -58,6 +58,8 @@ func (wa *WhatsAppConnector) Download(ctx context.Context, mediaID networkid.Med
if err != nil {
return nil, err
}
log := zerolog.Ctx(ctx).With().Any("message_id", parsedID).Logger()
ctx = log.WithContext(ctx)
msg, err := wa.Bridge.DB.Message.GetFirstPartByID(ctx, receiverID, parsedID.String())
if err != nil {
return nil, fmt.Errorf("failed to get message: %w", err)
@ -99,13 +101,17 @@ func (wa *WhatsAppConnector) Download(ctx context.Context, mediaID networkid.Med
Callback: func(f *os.File) error {
err := waClient.Client.DownloadToFile(keys, f)
if errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith403) || errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith404) || errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith410) {
if _, noReload := params["fi.mau.whatsapp.no_reload_media"]; noReload {
val := params["fi.mau.whatsapp.reload_media"]
if val == "false" || (!wa.Config.DirectMediaAutoRequest && val != "true") {
return ErrReloadNeeded
}
log.Trace().Msg("Media not found for direct download, requesting and waiting")
err = waClient.requestAndWaitDirectMedia(ctx, msg.ID, keys)
if err != nil {
log.Trace().Err(err).Msg("Failed to wait for media for direct download")
return err
}
log.Trace().Msg("Retrying download after successful retry")
err = waClient.Client.DownloadToFile(keys, f)
}
if errors.Is(err, whatsmeow.ErrFileLengthMismatch) || errors.Is(err, whatsmeow.ErrInvalidMediaSHA256) {
@ -142,7 +148,7 @@ func (wa *WhatsAppClient) getDirectMediaRetryState(msgID networkid.MessageID, cr
}
func (wa *WhatsAppClient) requestAndWaitDirectMedia(ctx context.Context, rawMsgID networkid.MessageID, keys *msgconv.FailedMediaKeys) error {
state, err := wa.requestDirectMedia(rawMsgID, keys.Key)
state, err := wa.requestDirectMedia(ctx, rawMsgID, keys.Key)
if err != nil {
return err
}
@ -165,16 +171,19 @@ func (wa *WhatsAppClient) requestAndWaitDirectMedia(ctx context.Context, rawMsgI
}
}
func (wa *WhatsAppClient) requestDirectMedia(rawMsgID networkid.MessageID, key []byte) (*directMediaRetry, error) {
func (wa *WhatsAppClient) requestDirectMedia(ctx context.Context, rawMsgID networkid.MessageID, key []byte) (*directMediaRetry, error) {
state := wa.getDirectMediaRetryState(rawMsgID, true)
state.Lock()
defer state.Unlock()
if !state.requested {
zerolog.Ctx(ctx).Debug().Msg("Sending request for missing media in direct download")
err := wa.sendMediaRequestDirect(rawMsgID, key)
if err != nil {
return nil, fmt.Errorf("failed to send media retry request: %w", err)
}
state.requested = true
} else {
zerolog.Ctx(ctx).Debug().Msg("Media retry request already sent previously, just waiting for response")
}
return state, nil
}

View file

@ -54,6 +54,9 @@ disable_view_once: false
# By default, the bridge acts like WhatsApp web, which only sends active delivery
# receipts when it's in the foreground.
force_active_delivery_receipts: false
# When direct media is enabled and a piece of media isn't available on the WhatsApp servers,
# should it be automatically requested from the phone?
direct_media_auto_request: true
# Settings for converting animated stickers.
animated_sticker: