Files
claw-email/internal/database/repository.go
T
thuanleandClaude Opus 4.7 8815c20c13 Initial commit: email-openclaw bridge v1
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>
2026-04-27 18:16:57 +07:00

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
}