commit 184e4de6a4897b14d1f75b9a95ff5b7eb957f543 from: Oliver Lowe date: Wed Mar 13 04:09:32 2024 UTC apub: tighten up content-type on marshalling into mail Clearer: set a body to read from, then change it when necessary. Fewer instructions! Also, mail clients don't really know what to do with text/markdown, so just use text/plain; that's what Gruber would want. Adds basic test case too commit - 519acee1bbf60fee276fd734f7a8254edd803d06 commit + 184e4de6a4897b14d1f75b9a95ff5b7eb957f543 blob - c6912852868287f8120ed9ccadf48ce62e7f59d2 blob + 987fe41861005415060cbedec17837b1a16591b5 --- mail.go +++ mail.go @@ -59,20 +59,18 @@ func MarshalMail(activity *Activity) ([]byte, error) { fmt.Fprintf(buf, "References: <%s>\n", activity.InReplyTo) } + body := &activity.Content if activity.Source.Content != "" && activity.Source.MediaType == "text/markdown" { + body = &activity.Source.Content fmt.Fprintln(buf, "Content-Type: text/plain; charset=utf-8") - } else if activity.MediaType != "" { - fmt.Fprintln(buf, "Content-Type:", activity.MediaType) + } else if activity.MediaType == "text/markdown" { + fmt.Fprintln(buf, "Content-Type: text/plain; charset=utf-8") } else { fmt.Fprintln(buf, "Content-Type:", "text/html; charset=utf-8") } fmt.Fprintln(buf, "Subject:", activity.Name) fmt.Fprintln(buf) - if activity.Source.Content != "" && activity.Source.MediaType == "text/markdown" { - fmt.Fprintln(buf, activity.Source.Content) - } else { - fmt.Fprintln(buf, activity.Content) - } + fmt.Fprintln(buf, *body) _, err = mail.ReadMessage(bytes.NewReader(buf.Bytes())) return buf.Bytes(), err } blob - 61bf051284c44bab0c9c0ee8b84609ae5bcdff77 blob + fe6864206e60fa17becbdec36ffe8bd64e15b13f --- mail_test.go +++ mail_test.go @@ -75,9 +75,24 @@ func TestMarshalMail(t *testing.T) { t.Errorf("%s: marshal to mail message: %v", name, err) continue } - if _, err := mail.ReadMessage(bytes.NewReader(b)); err != nil { + msg, err := mail.ReadMessage(bytes.NewReader(b)) + if err != nil { t.Errorf("%s: read back message from marshalled activity: %v", name, err) + continue } + p := make([]byte, 8) + n, err := msg.Body.Read(p) + if err != nil { + t.Errorf("%s: read message body: %v", name, err) + } + if n != len(p) { + if a.Type == "Page" { + // Pages have no content, so skip this case + continue + } + t.Errorf("%s: short read from body", name) + t.Log(string(p)) + } } }