- posts table: one row per post queued to a list (message id, subject, time, recipients, sent, failed, dropped), deleted with the list and forgotten after 400 days when a new post is recorded - EnqueuePost and EnqueueFromHeld record the post in the same transaction and stamp every delivery with its id; a refused duplicate records nothing; other mail keeps post_id 0 - CompleteOutbox counts sent, the new DropOutbox counts dropped and FailOutbox counts failed, each in the transaction that changes the delivery; a delivery that already failed is never counted twice - outbox priority column; DueOutbox orders by priority, next_at, id - ListPosts newest first (default 50, at most 200) with Pending derived - additive migration of the outbox columns, tested on an old database
78 lines
2.5 KiB
Go
78 lines
2.5 KiB
Go
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<?`, now.Add(-postTTL).Unix()); err != nil {
|
|
return 0, err
|
|
}
|
|
subject, _ := summarize(msg)
|
|
var messageID string
|
|
if m, err := mail.ReadMessage(bytes.NewReader(msg)); err == nil {
|
|
messageID = cleanSummary(m.Header.Get("Message-ID"))
|
|
}
|
|
res, err := tx.Exec(`INSERT INTO posts(list_id, message_id, subject, created_at, recipients) VALUES(?,?,?,?,?)`,
|
|
listID, messageID, subject, now.Unix(), recipients)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
// ListPosts returns the posts of a list, newest first: up to limit (50 when limit <= 0, at most 200).
|
|
func (s *Store) ListPosts(listID int64, limit int) ([]Post, error) {
|
|
if limit <= 0 {
|
|
limit = defaultPostsLimit
|
|
}
|
|
if limit > 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()
|
|
}
|