each feed version has webhooks to do whatever with it

main
Bel LaPointe 2025-04-28 20:22:11 -06:00
parent 0628a678d8
commit 57e77e5d4e
5 changed files with 29 additions and 66 deletions

View File

@ -65,7 +65,7 @@ func TestOne(t *testing.T) {
ctx := db.Test(t, ctx)
for i := 0; i < 2; i++ {
if _, err := feeds.Insert(ctx, fmt.Sprintf("%s?idx=%d", sURL, i), "* * * * *", "matches", "tag"); err != nil {
if _, err := feeds.Insert(ctx, fmt.Sprintf("%s?idx=%d", sURL, i), "* * * * *", "matches", http.MethodHead, fmt.Sprintf("%s?idx=1%d", sURL, i), "{{.Title}}"); err != nil {
t.Fatal(err)
}
}

View File

@ -26,11 +26,13 @@ type (
}
Version struct {
Created time.Time
URL string
Cron string
Pattern string
Tag string
Created time.Time
URL string
Cron string
Pattern string
WebhookMethod string
WebhookURL string
WebhookBody string
}
Execution struct {
@ -98,7 +100,9 @@ func Get(ctx context.Context, id string) (Feed, error) {
versions.url AS "Version.URL",
versions.cron AS "Version.Cron",
versions.pattern AS "Version.Pattern",
versions.tag AS "Version.Tag",
versions.webhook_method AS "Version.WebhookMethod",
versions.webhook_url AS "Version.WebhookURL",
versions.webhook_body AS "Version.WebhookBody",
(
SELECT executed_at
FROM "feed.executions"
@ -121,55 +125,6 @@ func Get(ctx context.Context, id string) (Feed, error) {
`, id, id)
}
func oldGet(ctx context.Context, id string) (Feed, error) {
if err := initDB(ctx); err != nil {
return Feed{}, err
}
entry, err := getEntry(ctx, id)
if err != nil {
return Feed{}, err
}
version, err := db.QueryOne[Version](ctx, `
SELECT
"feed.current_versions".versions_created_at AS Created,
"feed.current_versions".url AS URL,
"feed.current_versions".cron AS Cron,
"feed.current_versions".pattern AS Pattern,
"feed.current_versions".tag AS Tag
FROM
"feed.current_versions"
JOIN
"feed.versions" versions_a ON
"feed.current_versions".entries_id=versions_a.entries_id
JOIN
"feed.versions" versions_b ON
"feed.current_versions".versions_created_at=versions_b.created_at
WHERE
"feed.current_versions".entries_id = ?
`, id)
if err != nil {
return Feed{}, err
}
execution, err := db.QueryOne[Execution](ctx, `
SELECT
"feed.executed_at" AS Executed,
"feed.versions_created_at" AS VersionsCreated
FROM
"feed.executions"
WHERE
"feed.executions".entries_id = ?
ORDER BY "feed.executions".executed_at DESC
`, id)
if err != nil {
return Feed{}, err
}
return Feed{}, fmt.Errorf("%+v, %+v, %+v", entry, version, execution)
}
func (feed Feed) Executed(ctx context.Context) error {
if err := initDB(ctx); err != nil {
return err
@ -190,7 +145,7 @@ func (feed Feed) Executed(ctx context.Context) error {
)
}
func Insert(ctx context.Context, url, cron, pattern, tag string) (string, error) {
func Insert(ctx context.Context, url, cron, pattern, webhookMethod, webhookURL, webhookBody string) (string, error) {
if err := initDB(ctx); err != nil {
return "", err
}
@ -210,12 +165,14 @@ func Insert(ctx context.Context, url, cron, pattern, tag string) (string, error)
url,
cron,
pattern,
tag
) VALUES ($4, $5, $6, $7, $8, $9);
webhook_method,
webhook_url,
webhook_body
) VALUES ($4, $5, $6, $7, $8, $9, $10, $11);
COMMIT;
`,
id, now, now,
id, now, url, cron, pattern, tag,
id, now, url, cron, pattern, webhookMethod, webhookURL, webhookBody,
)
}
@ -280,7 +237,9 @@ func initDB(ctx context.Context) error {
`ALTER TABLE "feed.versions" ADD COLUMN url TEXT NOT NULL`,
`ALTER TABLE "feed.versions" ADD COLUMN cron TEXT NOT NULL DEFAULT '0 0 * * *'`,
`ALTER TABLE "feed.versions" ADD COLUMN pattern TEXT NOT NULL DEFAULT ''`,
`ALTER TABLE "feed.versions" ADD COLUMN tag TEXT NOT NULL DEFAULT ''`,
`ALTER TABLE "feed.versions" ADD COLUMN webhook_method TEXT NOT NULL DEFAULT ''`,
`ALTER TABLE "feed.versions" ADD COLUMN webhook_url TEXT NOT NULL DEFAULT ''`,
`ALTER TABLE "feed.versions" ADD COLUMN webhook_body TEXT NOT NULL DEFAULT ''`,
`CREATE TABLE "feed.executions" (
entries_id TEXT,

View File

@ -15,7 +15,7 @@ func TestFeeds(t *testing.T) {
t.Run("crud", func(t *testing.T) {
ctx := db.Test(t, ctx)
id, err := feeds.Insert(ctx, "url", "cron", "pattern", "tag")
id, err := feeds.Insert(ctx, "url", "cron", "pattern", "wmethod", "wurl", "wbody")
if err != nil {
t.Fatal("cannot insert:", err)
}
@ -51,8 +51,14 @@ func TestFeeds(t *testing.T) {
if got.Version.Pattern != "pattern" {
t.Error("bad version.pattern")
}
if got.Version.Tag != "tag" {
t.Error("bad version.tag")
if got.Version.WebhookMethod != "wmethod" {
t.Error("bad version.webhookMethod")
}
if got.Version.WebhookURL != "wurl" {
t.Error("bad version.webhookURL")
}
if got.Version.WebhookBody != "wbody" {
t.Error("bad version.webhookBody")
}
if !got.Execution.Executed.IsZero() {

View File

@ -100,7 +100,6 @@ func (feed Feed) Fetch(ctx context.Context) (Items, error) {
Links: links,
Preview: preview,
Body: body,
Tag: feed.Version.Tag,
})
}

View File

@ -7,5 +7,4 @@ type Item struct {
Links []string
Preview string
Body string
Tag string
}