From 27d49417d7d83e105fbb1d4bdf95149d884aee29 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sun, 12 Mar 2023 14:27:20 +1300 Subject: [PATCH] API: Return received datetime when message does not contain a date header --- storage/database.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/storage/database.go b/storage/database.go index 0adcbcc..21e71fb 100644 --- a/storage/database.go +++ b/storage/database.go @@ -381,7 +381,8 @@ func Search(search string, start, limit int) ([]MessageSummary, error) { return results, err } -// GetMessage returns a data.Message generated from the mailbox_data collection. +// GetMessage returns a Message generated from the mailbox_data collection. +// If the message lacks a date header, then the received datetime is used. func GetMessage(id string) (*Message, error) { raw, err := GetMessageRaw(id) if err != nil { @@ -403,7 +404,35 @@ func GetMessage(id string) (*Message, error) { from = &mail.Address{Name: env.GetHeader("From")} } - date, _ := env.Date() + date, err := env.Date() + if err != nil { + // return received datetime when message does not contain a date header + q := sqlf.From("mailbox"). + Select(`Data`). + OrderBy("Sort DESC"). + Where(`ID = ?`, id) + + if err := q.QueryAndClose(nil, db, func(row *sql.Rows) { + var summary string + em := MessageSummary{} + + if err := row.Scan(&summary); err != nil { + logger.Log().Error(err) + return + } + + if err := json.Unmarshal([]byte(summary), &em); err != nil { + logger.Log().Error(err) + return + } + + logger.Log().Debugf("[db] %s does not contain a date header, using received datetime", id) + + date = em.Created + }); err != nil { + logger.Log().Error(err) + } + } obj := Message{ ID: id,