Cotton code
123 lines · 3.8 KB · GoRaw
package mainimport (	"context"	"errors"	"sync"	"time"	"go.mau.fi/whatsmeow"	"go.mau.fi/whatsmeow/proto/waMmsRetry"	"go.mau.fi/whatsmeow/types"	"go.mau.fi/whatsmeow/types/events")// How long the owner's phone is given to upload a file again. A phone that is off or away answers nothing, and the// file stays "gone" until a later pass asks again.const reuploadWait = 90 * time.Second// expired says WhatsApp's media hosts no longer hold the file (they drop files after some weeks).func expired(err error) bool {	return errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith403) || errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith404) || errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith410)}// After the phone leaves one ask unanswered, the next asks wait this long before trying it again: a phone that is// off would otherwise hold a page of expired files for a minute and a half each.const phoneQuiet = 10 * time.Minute// retries routes the phone's answers to the downloads waiting for them, by message id.type retries struct {	mu         sync.Mutex	waiting    map[string]chan *events.MediaRetry	quietUntil time.Time}// silent says the phone left an ask unanswered a short while ago, so this file is not asked for now.func (r *retries) silent(now time.Time) bool {	r.mu.Lock()	defer r.mu.Unlock()	return now.Before(r.quietUntil)}func (r *retries) unanswered(now time.Time) {	r.mu.Lock()	defer r.mu.Unlock()	r.quietUntil = now.Add(phoneQuiet)}func (r *retries) wait(id string) (chan *events.MediaRetry, func()) {	r.mu.Lock()	defer r.mu.Unlock()	if r.waiting == nil {		r.waiting = map[string]chan *events.MediaRetry{}	}	ch := make(chan *events.MediaRetry, 1)	r.waiting[id] = ch	return ch, func() {		r.mu.Lock()		defer r.mu.Unlock()		if r.waiting[id] == ch {			delete(r.waiting, id)		}	}}// answered hands the phone's answer to the download waiting for it; an answer nobody waits for is dropped.func (r *retries) answered(evt *events.MediaRetry) {	r.mu.Lock()	defer r.mu.Unlock()	if ch, ok := r.waiting[string(evt.MessageID)]; ok {		select {		case ch <- evt:		default:		}	}}// retryPath reads the phone's answer: the new path on WhatsApp's hosts, or ErrGone when the phone no longer has// the file (deleted from the phone, or never downloaded there).func retryPath(evt *events.MediaRetry, mediaKey []byte) (string, error) {	if evt.Error != nil {		return "", ErrGone	}	answer, err := whatsmeow.DecryptMediaRetryNotification(evt, mediaKey)	if err != nil {		return "", Unfetchable{"the phone's answer did not open: " + err.Error()}	}	if answer.GetResult() != waMmsRetry.MediaRetryNotification_SUCCESS || answer.GetDirectPath() == "" {		return "", ErrGone	}	return answer.GetDirectPath(), nil}// reupload asks the owner's phone to upload the file of m's message again and waits for where it went.func (h *Hub) reupload(ctx context.Context, cli *whatsmeow.Client, m *MediaPointer) (string, error) {	if m.MessageID == "" || m.Chat == "" {		// A pointer from before this helper named its message: there is nothing to ask the phone about.		return "", ErrGone	}	chat, err := types.ParseJID(m.Chat)	if err != nil || h.retries.silent(time.Now()) {		return "", ErrGone	}	info := &types.MessageInfo{ID: types.MessageID(m.MessageID), MessageSource: types.MessageSource{Chat: chat, IsFromMe: m.FromMe, IsGroup: m.IsGroup}}	if m.Sender != "" {		if sender, err := types.ParseJID(m.Sender); err == nil {			info.Sender = sender		}	}	answer, done := h.retries.wait(m.MessageID)	defer done()	if err := cli.SendMediaRetryReceipt(ctx, info, m.MediaKey); err != nil {		return "", err	}	wait, cancel := context.WithTimeout(ctx, reuploadWait)	defer cancel()	select {	case evt := <-answer:		return retryPath(evt, m.MediaKey)	case <-wait.Done():		h.retries.unanswered(time.Now())		return "", ErrGone	}}