Full pipeline: IMAP ingress -> OpenClaw dispatch -> callback -> SMTP reply. SQLite stateful storage with idempotency, threading, and retry logic. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package database
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// FindByMessageID returns the task with the given message_id, or nil if not found.
|
|
func FindByMessageID(db *gorm.DB, messageID string) (*Task, error) {
|
|
var task Task
|
|
err := db.Where("message_id = ?", messageID).First(&task).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &task, nil
|
|
}
|
|
|
|
// FindLatestByThreadID returns the most recent COMPLETED task in the thread.
|
|
func FindLatestByThreadID(db *gorm.DB, threadID string) (*Task, error) {
|
|
var task Task
|
|
err := db.Where("thread_id = ? AND status = ?", threadID, StatusCompleted).
|
|
Order("created_at DESC").First(&task).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &task, nil
|
|
}
|
|
|
|
// FindByTaskUUID returns the task with the given task_uuid.
|
|
func FindByTaskUUID(db *gorm.DB, taskUUID string) (*Task, error) {
|
|
var task Task
|
|
err := db.Where("task_uuid = ?", taskUUID).First(&task).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &task, nil
|
|
}
|
|
|
|
// ThreadHistory returns all COMPLETED tasks for a thread, ordered chronologically.
|
|
func ThreadHistory(db *gorm.DB, threadID string) ([]Task, error) {
|
|
var tasks []Task
|
|
err := db.Where("thread_id = ? AND status = ?", threadID, StatusCompleted).
|
|
Order("created_at ASC").Find(&tasks).Error
|
|
return tasks, err
|
|
}
|
|
|
|
// CreateTask inserts a new task record.
|
|
func CreateTask(db *gorm.DB, task *Task) error {
|
|
return db.Create(task).Error
|
|
}
|
|
|
|
// UpdateTask saves changes to an existing task record.
|
|
func UpdateTask(db *gorm.DB, task *Task) error {
|
|
return db.Save(task).Error
|
|
}
|