move func PartStructure from webhook to queue, so it isn't tracked anymore for apidiff changes

the types in webhook should be subjected to apidiff'ing, this was a shared
function. it is better off in package queue. also change the apidiff script so
it leaves apidiff/next.txt empty when there aren't any changes. makes it easier
to rotate the files after releases where nothing changed (a common occurrence).
This commit is contained in:
Mechiel Lukkien
2024-12-07 13:57:07 +01:00
parent 0871bf5219
commit 69a4995449
7 changed files with 62 additions and 53 deletions

View File

@ -3,6 +3,7 @@ package queue
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
@ -796,7 +797,7 @@ func Incoming(ctx context.Context, log mlog.Log, acc *store.Account, messageID s
log.Debug("composing webhook for incoming message")
structure, err := webhook.PartStructure(log, &part)
structure, err := PartStructure(log, &part)
if err != nil {
return fmt.Errorf("parsing part structure: %v", err)
}
@ -912,6 +913,38 @@ func Incoming(ctx context.Context, log mlog.Log, acc *store.Account, messageID s
return nil
}
// PartStructure returns a webhook.Structure for a parsed message part.
func PartStructure(log mlog.Log, p *message.Part) (webhook.Structure, error) {
parts := make([]webhook.Structure, len(p.Parts))
for i := range p.Parts {
var err error
parts[i], err = PartStructure(log, &p.Parts[i])
if err != nil && !errors.Is(err, message.ErrParamEncoding) {
return webhook.Structure{}, err
}
}
disp, filename, err := p.DispositionFilename()
if err != nil && errors.Is(err, message.ErrParamEncoding) {
log.Debugx("parsing disposition/filename", err)
} else if err != nil {
return webhook.Structure{}, err
}
s := webhook.Structure{
ContentType: strings.ToLower(p.MediaType + "/" + p.MediaSubType),
ContentTypeParams: p.ContentTypeParams,
ContentID: p.ContentID,
ContentDisposition: strings.ToLower(disp),
Filename: filename,
DecodedSize: p.DecodedSize,
Parts: parts,
}
// Replace nil map with empty map, for easier to use JSON.
if s.ContentTypeParams == nil {
s.ContentTypeParams = map[string]string{}
}
return s, nil
}
func isAutomated(h textproto.MIMEHeader) bool {
l := []string{"List-Id", "List-Unsubscribe", "List-Unsubscribe-Post", "Precedence"}
for _, k := range l {

View File

@ -82,7 +82,7 @@ func TestHookIncoming(t *testing.T) {
tcheck(t, err, "decode incoming webhook")
in.Meta.Received = in.Meta.Received.Local() // For TZ UTC.
structure, err := webhook.PartStructure(pkglog, &part)
structure, err := PartStructure(pkglog, &part)
tcheck(t, err, "part structure")
expIncoming := webhook.Incoming{