diff --git a/imapserver/fetch.go b/imapserver/fetch.go index c23d440..db3ae03 100644 --- a/imapserver/fetch.go +++ b/imapserver/fetch.go @@ -143,7 +143,7 @@ func (c *conn) cmdxFetch(isUID bool, tag, cmdstr string, p *parser) { var uids []store.UID var vanishedUIDs []store.UID - cmd := &fetchCmd{conn: c, isUID: isUID, hasChangedSince: haveChangedSince} + cmd := &fetchCmd{conn: c, isUID: isUID, hasChangedSince: haveChangedSince, newPreviews: map[store.UID]string{}} defer func() { if cmd.rtx == nil { diff --git a/imapserver/fetch_test.go b/imapserver/fetch_test.go index f5a6a07..88eadbf 100644 --- a/imapserver/fetch_test.go +++ b/imapserver/fetch_test.go @@ -444,6 +444,42 @@ Content-Transfer-Encoding: Quoted-printable tc.client.Unselect() tc.client.Examine("inbox") + // Preview + preview := "Hello Joe, do you think we can meet at 3:30 tomorrow?" + tc.transactf("ok", "fetch 1 preview") + tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{uid1, imapclient.FetchPreview{Preview: &preview}}}) + + tc.transactf("ok", "fetch 1 preview (lazy)") + tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{uid1, imapclient.FetchPreview{Preview: &preview}}}) + + // On-demand preview and saving on first request. + err = tc.account.DB.Write(ctxbg, func(tx *bstore.Tx) error { + m := store.Message{ID: 1} + err := tx.Get(&m) + tcheck(t, err, "get message") + if m.UID != 1 { + t.Fatalf("uid %d instead of 1", m.UID) + } + m.Preview = nil + err = tx.Update(&m) + tcheck(t, err, "remove preview from message") + return nil + }) + tcheck(t, err, "remove preview from database") + + tc.transactf("ok", "fetch 1 preview") + tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{uid1, imapclient.FetchPreview{Preview: &preview}}}) + m := store.Message{ID: 1} + err = tc.account.DB.Get(ctxbg, &m) + tcheck(t, err, "get message") + if m.Preview == nil { + t.Fatalf("preview missing") + } else if *m.Preview != preview+"\n" { + t.Fatalf("got preview %q, expected %q", *m.Preview, preview+"\n") + } + + tc.transactf("bad", "fetch 1 preview (bogus)") + // Start a second session. Use it to remove the message. First session should still // be able to access the messages. tc2 := startNoSwitchboard(t) @@ -466,15 +502,5 @@ Content-Transfer-Encoding: Quoted-printable tc.transactf("ok", "fetch 1 rfc822") tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{uid1, rfc1}}) - // Preview - preview := "Hello Joe, do you think we can meet at 3:30 tomorrow?" - tc.transactf("ok", "fetch 1 preview") - tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{uid1, imapclient.FetchPreview{Preview: &preview}}}) - - tc.transactf("ok", "fetch 1 preview (lazy)") - tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{uid1, imapclient.FetchPreview{Preview: &preview}}}) - - tc.transactf("bad", "fetch 1 preview (bogus)") - tc.client.Logout() } diff --git a/store/account.go b/store/account.go index a3fcc0d..09dc006 100644 --- a/store/account.go +++ b/store/account.go @@ -569,7 +569,8 @@ type Message struct { // through the IMAP fetch attribute "PREVIEW" (without "LAZY")), and stored with // the message at that time. // The preview is at most 256 characters (can be more bytes), with detected quoted - // text replaced with "[..."]. + // text replaced with "[...]". Previews typically end with a newline, callers may + // want to strip whitespace. Preview *string // ParsedBuf message structure. Currently saved as JSON of message.Part because bstore diff --git a/webmail/api.json b/webmail/api.json index 41e511c..1ebd035 100644 --- a/webmail/api.json +++ b/webmail/api.json @@ -2608,7 +2608,7 @@ }, { "Name": "Preview", - "Docs": "If non-nil, a preview of the message based on text and/or html parts of the message. Used in the webmail and IMAP PREVIEW extension. If non-nil, it is empty if no preview could be created, or the message has not textual content or couldn't be parsed. Previews are typically created when delivering a message, but not when importing messages, for speed. Previews are generated on first request (in the webmail, or through the IMAP fetch attribute \"PREVIEW\" (without \"LAZY\")), and stored with the message at that time. The preview is at most 256 characters (can be more bytes), with detected quoted text replaced with \"[...\"].", + "Docs": "If non-nil, a preview of the message based on text and/or html parts of the message. Used in the webmail and IMAP PREVIEW extension. If non-nil, it is empty if no preview could be created, or the message has not textual content or couldn't be parsed. Previews are typically created when delivering a message, but not when importing messages, for speed. Previews are generated on first request (in the webmail, or through the IMAP fetch attribute \"PREVIEW\" (without \"LAZY\")), and stored with the message at that time. The preview is at most 256 characters (can be more bytes), with detected quoted text replaced with \"[...]\".", "Typewords": [ "nullable", "string" diff --git a/webmail/api.ts b/webmail/api.ts index 8dbb9e0..35df357 100644 --- a/webmail/api.ts +++ b/webmail/api.ts @@ -377,7 +377,7 @@ export interface Message { Size: number TrainedJunk?: boolean | null // If nil, no training done yet. Otherwise, true is trained as junk, false trained as nonjunk. MsgPrefix?: string | null // Typically holds received headers and/or header separator. - Preview?: string | null // If non-nil, a preview of the message based on text and/or html parts of the message. Used in the webmail and IMAP PREVIEW extension. If non-nil, it is empty if no preview could be created, or the message has not textual content or couldn't be parsed. Previews are typically created when delivering a message, but not when importing messages, for speed. Previews are generated on first request (in the webmail, or through the IMAP fetch attribute "PREVIEW" (without "LAZY")), and stored with the message at that time. The preview is at most 256 characters (can be more bytes), with detected quoted text replaced with "[..."]. + Preview?: string | null // If non-nil, a preview of the message based on text and/or html parts of the message. Used in the webmail and IMAP PREVIEW extension. If non-nil, it is empty if no preview could be created, or the message has not textual content or couldn't be parsed. Previews are typically created when delivering a message, but not when importing messages, for speed. Previews are generated on first request (in the webmail, or through the IMAP fetch attribute "PREVIEW" (without "LAZY")), and stored with the message at that time. The preview is at most 256 characters (can be more bytes), with detected quoted text replaced with "[...]". ParsedBuf?: string | null // ParsedBuf message structure. Currently saved as JSON of message.Part because bstore cannot yet store recursive types. Created when first needed, and saved in the database. todo: once replaced with non-json storage, remove date fixup in ../message/part.go. }