Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
33 lines
792 B
Go
33 lines
792 B
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// TrustedSenders reads the senders a mailbox trusts. The webmail writes them
|
|
// through the auth service; delivery only ever asks.
|
|
type TrustedSenders struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewTrustedSenders(pool *pgxpool.Pool) *TrustedSenders {
|
|
return &TrustedSenders{pool: pool}
|
|
}
|
|
|
|
func (t *TrustedSenders) IsTrusted(ctx context.Context, mailboxID uuid.UUID, address string) (bool, error) {
|
|
var trusted bool
|
|
err := t.pool.QueryRow(ctx, `
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM trusted_senders WHERE mailbox_id = $1 AND address = $2
|
|
)
|
|
`, mailboxID, address).Scan(&trusted)
|
|
if err != nil {
|
|
return false, fmt.Errorf("look up a trusted sender: %w", err)
|
|
}
|
|
return trusted, nil
|
|
}
|