Files
Memoh/internal/db/sqlc/email_outbox.sql.go
BBQ cc5f00355f feat: add email service with multi-adapter support (#146)
* feat: add email service with multi-adapter support

Implement a full-stack email service with global provider management,
per-bot bindings with granular read/write permissions, outbox audit
storage, and MCP tool integration for direct mailbox access.

Backend:
- Email providers: CRUD with dynamic config schema (generic SMTP/IMAP, Mailgun)
- Generic adapter: go-mail (SMTP) + go-imap/v2 (IMAP IDLE real-time push via
  UnilateralDataHandler + UID-based tracking + periodic check fallback)
- Mailgun adapter: mailgun-go/v5 with dual inbound mode (webhook + poll)
- Bot email bindings: per-bot provider binding with independent r/w permissions
- Outbox: outbound email audit log with status tracking
- Trigger: inbound emails push notification to bot_inbox (from/subject only,
  LLM reads full content on demand via MCP tools)
- MailboxReader interface: on-demand IMAP queries for listing/reading emails
- MCP tools: email_accounts, email_send, email_list (paginated mailbox),
  email_read (by UID) — all with multi-binding and provider_id selection
- Webhook: /email/mailgun/webhook/:config_id (JWT-skipped, signature-verified)
- DB migration: 0019_add_email (email_providers, bot_email_bindings, email_outbox)

Frontend:
- Email Providers page: /email-providers with MasterDetailSidebarLayout
- Dynamic config form rendered from ordered provider meta schema with i18n keys
- Bot detail: Email tab with bindings management + outbox audit table
- Sidebar navigation entry
- Full i18n support (en + zh)
- Auto-generated SDK from Swagger

Closes #17

* feat(email): trigger bot conversation immediately on inbound email

Instead of only storing an inbox item and waiting for the next chat,
the email trigger now proactively invokes the conversation resolver
so the bot processes new emails right away — aligned with the
schedule/heartbeat trigger pattern.

* fix: lint

---------

Co-authored-by: Acbox <acbox0328@gmail.com>
2026-02-28 21:03:59 +08:00

191 lines
4.6 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: email_outbox.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const countEmailOutboxByBot = `-- name: CountEmailOutboxByBot :one
SELECT count(*) FROM email_outbox
WHERE bot_id = $1
`
func (q *Queries) CountEmailOutboxByBot(ctx context.Context, botID pgtype.UUID) (int64, error) {
row := q.db.QueryRow(ctx, countEmailOutboxByBot, botID)
var count int64
err := row.Scan(&count)
return count, err
}
const createEmailOutbox = `-- name: CreateEmailOutbox :one
INSERT INTO email_outbox (provider_id, bot_id, from_address, to_addresses, subject, body_text, body_html, attachments, status)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9
)
RETURNING id, provider_id, bot_id, message_id, from_address, to_addresses, subject, body_text, body_html, attachments, status, error, sent_at, created_at
`
type CreateEmailOutboxParams struct {
ProviderID pgtype.UUID `json:"provider_id"`
BotID pgtype.UUID `json:"bot_id"`
FromAddress string `json:"from_address"`
ToAddresses []byte `json:"to_addresses"`
Subject string `json:"subject"`
BodyText string `json:"body_text"`
BodyHtml string `json:"body_html"`
Attachments []byte `json:"attachments"`
Status string `json:"status"`
}
func (q *Queries) CreateEmailOutbox(ctx context.Context, arg CreateEmailOutboxParams) (EmailOutbox, error) {
row := q.db.QueryRow(ctx, createEmailOutbox,
arg.ProviderID,
arg.BotID,
arg.FromAddress,
arg.ToAddresses,
arg.Subject,
arg.BodyText,
arg.BodyHtml,
arg.Attachments,
arg.Status,
)
var i EmailOutbox
err := row.Scan(
&i.ID,
&i.ProviderID,
&i.BotID,
&i.MessageID,
&i.FromAddress,
&i.ToAddresses,
&i.Subject,
&i.BodyText,
&i.BodyHtml,
&i.Attachments,
&i.Status,
&i.Error,
&i.SentAt,
&i.CreatedAt,
)
return i, err
}
const getEmailOutboxByID = `-- name: GetEmailOutboxByID :one
SELECT id, provider_id, bot_id, message_id, from_address, to_addresses, subject, body_text, body_html, attachments, status, error, sent_at, created_at FROM email_outbox WHERE id = $1
`
func (q *Queries) GetEmailOutboxByID(ctx context.Context, id pgtype.UUID) (EmailOutbox, error) {
row := q.db.QueryRow(ctx, getEmailOutboxByID, id)
var i EmailOutbox
err := row.Scan(
&i.ID,
&i.ProviderID,
&i.BotID,
&i.MessageID,
&i.FromAddress,
&i.ToAddresses,
&i.Subject,
&i.BodyText,
&i.BodyHtml,
&i.Attachments,
&i.Status,
&i.Error,
&i.SentAt,
&i.CreatedAt,
)
return i, err
}
const listEmailOutboxByBot = `-- name: ListEmailOutboxByBot :many
SELECT id, provider_id, bot_id, message_id, from_address, to_addresses, subject, body_text, body_html, attachments, status, error, sent_at, created_at FROM email_outbox
WHERE bot_id = $1
ORDER BY created_at DESC
LIMIT $3 OFFSET $2
`
type ListEmailOutboxByBotParams struct {
BotID pgtype.UUID `json:"bot_id"`
Off int32 `json:"off"`
Lim int32 `json:"lim"`
}
func (q *Queries) ListEmailOutboxByBot(ctx context.Context, arg ListEmailOutboxByBotParams) ([]EmailOutbox, error) {
rows, err := q.db.Query(ctx, listEmailOutboxByBot, arg.BotID, arg.Off, arg.Lim)
if err != nil {
return nil, err
}
defer rows.Close()
var items []EmailOutbox
for rows.Next() {
var i EmailOutbox
if err := rows.Scan(
&i.ID,
&i.ProviderID,
&i.BotID,
&i.MessageID,
&i.FromAddress,
&i.ToAddresses,
&i.Subject,
&i.BodyText,
&i.BodyHtml,
&i.Attachments,
&i.Status,
&i.Error,
&i.SentAt,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateEmailOutboxFailed = `-- name: UpdateEmailOutboxFailed :exec
UPDATE email_outbox
SET status = 'failed', error = $1
WHERE id = $2
`
type UpdateEmailOutboxFailedParams struct {
Error string `json:"error"`
ID pgtype.UUID `json:"id"`
}
func (q *Queries) UpdateEmailOutboxFailed(ctx context.Context, arg UpdateEmailOutboxFailedParams) error {
_, err := q.db.Exec(ctx, updateEmailOutboxFailed, arg.Error, arg.ID)
return err
}
const updateEmailOutboxSent = `-- name: UpdateEmailOutboxSent :exec
UPDATE email_outbox
SET message_id = $1, status = 'sent', sent_at = now()
WHERE id = $2
`
type UpdateEmailOutboxSentParams struct {
MessageID string `json:"message_id"`
ID pgtype.UUID `json:"id"`
}
func (q *Queries) UpdateEmailOutboxSent(ctx context.Context, arg UpdateEmailOutboxSentParams) error {
_, err := q.db.Exec(ctx, updateEmailOutboxSent, arg.MessageID, arg.ID)
return err
}