package store import ( "bytes" "database/sql" "net/mail" "time" ) // Post is a post queued to a list, with the outcome of its deliveries so far. type Post struct { ID int64 ListID int64 MessageID string // the Message-ID header, at most 200 characters Subject string // decoded, at most 200 characters CreatedAt time.Time Recipients int // deliveries queued Sent int // accepted by the relay Failed int // given up for good Dropped int // the mailbox does not exist, or discarded by the admin Pending int // still in the queue: Recipients - Sent - Failed - Dropped } // postTTL is how long a post and its counters are kept. const postTTL = 400 * 24 * time.Hour // Limits of ListPosts. const ( defaultPostsLimit = 50 maxPostsLimit = 200 ) // recordPostTx stores the posts row of a post queued to a list and returns its id. It forgets // posts older than postTTL on the way, so the table never grows without bound. func recordPostTx(tx *sql.Tx, now time.Time, listID int64, msg []byte, recipients int) (int64, error) { if _, err := tx.Exec(`DELETE FROM posts WHERE created_at maxPostsLimit { limit = maxPostsLimit } rows, err := s.db.Query(`SELECT id, list_id, message_id, subject, created_at, recipients, sent, failed, dropped FROM posts WHERE list_id=? ORDER BY created_at DESC, id DESC LIMIT ?`, listID, limit) if err != nil { return nil, err } defer rows.Close() var out []Post for rows.Next() { var p Post var created int64 if err := rows.Scan(&p.ID, &p.ListID, &p.MessageID, &p.Subject, &created, &p.Recipients, &p.Sent, &p.Failed, &p.Dropped); err != nil { return nil, err } p.CreatedAt = time.Unix(created, 0) p.Pending = p.Recipients - p.Sent - p.Failed - p.Dropped out = append(out, p) } return out, rows.Err() }