From 57e77e5d4ed54b8044ae150e19e62bb36f6c2a5b Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Mon, 28 Apr 2025 20:22:11 -0600 Subject: [PATCH] each feed version has webhooks to do whatever with it --- src/cmd/cron/main_test.go | 2 +- src/feeds/db.go | 79 ++++++++++----------------------------- src/feeds/db_test.go | 12 ++++-- src/feeds/http.go | 1 - src/feeds/item.go | 1 - 5 files changed, 29 insertions(+), 66 deletions(-) diff --git a/src/cmd/cron/main_test.go b/src/cmd/cron/main_test.go index 01b09b9..96d8935 100644 --- a/src/cmd/cron/main_test.go +++ b/src/cmd/cron/main_test.go @@ -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) } } diff --git a/src/feeds/db.go b/src/feeds/db.go index 750d36b..7bab751 100644 --- a/src/feeds/db.go +++ b/src/feeds/db.go @@ -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, diff --git a/src/feeds/db_test.go b/src/feeds/db_test.go index 8840669..ce5efa2 100644 --- a/src/feeds/db_test.go +++ b/src/feeds/db_test.go @@ -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() { diff --git a/src/feeds/http.go b/src/feeds/http.go index e909c02..38b6dbd 100644 --- a/src/feeds/http.go +++ b/src/feeds/http.go @@ -100,7 +100,6 @@ func (feed Feed) Fetch(ctx context.Context) (Items, error) { Links: links, Preview: preview, Body: body, - Tag: feed.Version.Tag, }) } diff --git a/src/feeds/item.go b/src/feeds/item.go index 228de64..6c9f33d 100644 --- a/src/feeds/item.go +++ b/src/feeds/item.go @@ -7,5 +7,4 @@ type Item struct { Links []string Preview string Body string - Tag string }