import albedo_sqlite import albedo_sqlite/marmot_script import albedo_sqlite/schema import gleam/dynamic/decode import gleam/option.{type Option} import sqlight /// Shared return type for queries in this directory: /// src/albedo_ext_webhooks/ledger/sql/configure.sql /// src/albedo_ext_webhooks/ledger/sql/delete.sql /// src/albedo_ext_webhooks/ledger/sql/find.sql /// src/albedo_ext_webhooks/ledger/sql/find_hook_by_name.sql /// src/albedo_ext_webhooks/ledger/sql/find_other_hook_by_name.sql /// src/albedo_ext_webhooks/ledger/sql/find_owned.sql /// src/albedo_ext_webhooks/ledger/sql/for_sessions.sql /// src/albedo_ext_webhooks/ledger/sql/insert_hook_within_limit.sql /// src/albedo_ext_webhooks/ledger/sql/list.sql /// src/albedo_ext_webhooks/ledger/sql/list_all.sql /// src/albedo_ext_webhooks/ledger/sql/page_all.sql /// src/albedo_ext_webhooks/ledger/sql/page_session.sql /// src/albedo_ext_webhooks/ledger/sql/rotate.sql /// src/albedo_ext_webhooks/ledger/sql/set_enabled.sql /// src/albedo_ext_webhooks/ledger/sql/update_hook_at_revision.sql pub type HookRow { HookRow( id: String, session: String, name: String, enabled: Int, signature_header: String, signature_prefix: String, revision: Int, ) } fn hook_row_decoder() -> decode.Decoder(HookRow) { use id <- decode.field(0, decode.string) use session <- decode.field(1, decode.string) use name <- decode.field(2, decode.string) use enabled <- decode.field(3, decode.int) use signature_header <- decode.field(4, decode.string) use signature_prefix <- decode.field(5, decode.string) use revision <- decode.field(6, decode.int) decode.success(HookRow( id:, session:, name:, enabled:, signature_header:, signature_prefix:, revision:, )) } /// Generated from src/albedo_ext_webhooks/ledger/sql/add_hook_columns.sql pub fn add_hook_columns( db db: sqlight.Connection, ) -> Result(List(String), String) { let #(table, additions) = marmot_script.additions( "src/albedo_ext_webhooks/ledger/sql/add_hook_columns.sql", #("webhook_hooks", [ #("moved_to", "ALTER TABLE webhook_hooks ADD COLUMN moved_to TEXT;"), #( "moved_noted", "ALTER TABLE webhook_hooks ADD COLUMN moved_noted INTEGER NOT NULL DEFAULT 0;", ), ]), ) schema.add_columns(on: db, table:, additions:) } /// Generated from src/albedo_ext_webhooks/ledger/sql/add_revision.sql pub fn add_revision(db db: sqlight.Connection) -> Result(List(String), String) { let #(table, additions) = marmot_script.additions( "src/albedo_ext_webhooks/ledger/sql/add_revision.sql", #("webhook_permissions", [ #( "revision", "ALTER TABLE webhook_permissions ADD COLUMN revision INTEGER NOT NULL DEFAULT 1;", ), ]), ) schema.add_columns(on: db, table:, additions:) } /// Generated from src/albedo_ext_webhooks/ledger/sql/configure.sql pub fn configure( db db: sqlight.Connection, header header: String, prefix prefix: String, id id: String, session session: String, revision revision: Int, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "UPDATE webhook_hooks SET signature_header=@header,signature_prefix=@prefix,revision=revision+1 WHERE id=@id AND session=@session AND revision=@revision RETURNING id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision", on: db, with: [ sqlight.text(header), sqlight.text(prefix), sqlight.text(id), sqlight.text(session), sqlight.int(revision), ], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/delete.sql pub fn delete( db db: sqlight.Connection, id id: String, session session: String, revision revision: Int, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "DELETE FROM webhook_hooks WHERE id=@id AND session=@session AND revision=@revision RETURNING id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision", on: db, with: [sqlight.text(id), sqlight.text(session), sqlight.int(revision)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/delivery_owners.sql pub type DeliveryOwnersRow { DeliveryOwnersRow(id: String, hook: String) } /// Generated from src/albedo_ext_webhooks/ledger/sql/delivery_owners.sql pub fn delivery_owners( db db: sqlight.Connection, hooks hooks: String, ids ids: String, ) -> Result(List(DeliveryOwnersRow), String) { albedo_sqlite.rows( "SELECT id AS id,hook AS hook FROM webhook_deliveries WHERE hook IN (SELECT value FROM json_each(@hooks)) AND id IN (SELECT value FROM json_each(@ids))", on: db, with: [sqlight.text(hooks), sqlight.text(ids)], expecting: { use id <- decode.field(0, decode.string) use hook <- decode.field(1, decode.string) decode.success(DeliveryOwnersRow(id:, hook:)) }, ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/enqueue.sql pub fn enqueue( db db: sqlight.Connection, id id: Option(String), hook hook: String, name name: String, session session: String, body body: BitArray, digest digest: String, event_key event_key: Option(String), ) -> Result(List(String), String) { albedo_sqlite.rows( "INSERT INTO webhook_deliveries(id,hook,name,session,body,body_sha256,event_key) VALUES(@id,@hook,@name,@session,@body,@digest,@event_key) ON CONFLICT DO NOTHING RETURNING id AS id", on: db, with: [ sqlight.nullable(sqlight.text, id), sqlight.text(hook), sqlight.text(name), sqlight.text(session), sqlight.blob(body), sqlight.text(digest), sqlight.nullable(sqlight.text, event_key), ], expecting: decode.field(0, decode.string, decode.success), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/enqueue_new.sql pub type EnqueueNewRow { EnqueueNewRow(id: String, body_sha256: String) } /// Generated from src/albedo_ext_webhooks/ledger/sql/enqueue_new.sql pub fn enqueue_new( db db: sqlight.Connection, id id: String, key key: String, ) -> Result(List(EnqueueNewRow), String) { albedo_sqlite.rows( "SELECT id AS id,body_sha256 FROM webhook_deliveries WHERE hook=@id AND event_key=@key", on: db, with: [sqlight.text(id), sqlight.text(key)], expecting: { use id <- decode.field(0, decode.string) use body_sha256 <- decode.field(1, decode.string) decode.success(EnqueueNewRow(id:, body_sha256:)) }, ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/events_since.sql pub type EventsSinceRow { EventsSinceRow(hook: String, event_key: String, id: String) } /// Generated from src/albedo_ext_webhooks/ledger/sql/events_since.sql pub fn events_since( db db: sqlight.Connection, sessions sessions: String, since since: Float, ) -> Result(List(EventsSinceRow), String) { albedo_sqlite.rows( "SELECT hook,event_key AS event_key,id AS id FROM webhook_deliveries WHERE event_key IS NOT NULL AND session IN (SELECT value FROM json_each(@sessions)) AND received_at>=strftime('%Y-%m-%dT%H:%M:%fZ',@since,'unixepoch')", on: db, with: [sqlight.text(sessions), sqlight.float(since)], expecting: { use hook <- decode.field(0, decode.string) use event_key <- decode.field(1, decode.string) use id <- decode.field(2, decode.string) decode.success(EventsSinceRow(hook:, event_key:, id:)) }, ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/find.sql pub fn find( db db: sqlight.Connection, id id: String, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks WHERE id=@id", on: db, with: [sqlight.text(id)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/find_hook_by_name.sql pub fn find_hook_by_name( db db: sqlight.Connection, session session: String, name name: String, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks WHERE session=@session AND name=@name", on: db, with: [sqlight.text(session), sqlight.text(name)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/find_hook_for_delivery.sql pub type FindHookForDeliveryRow { FindHookForDeliveryRow( session: String, name: String, secret: String, signature_header: String, signature_prefix: String, moved_to: Option(String), moved_noted: Int, ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/find_hook_for_delivery.sql pub fn find_hook_for_delivery( db db: sqlight.Connection, id id: String, ) -> Result(List(FindHookForDeliveryRow), String) { albedo_sqlite.rows( "SELECT session,name,secret,signature_header,signature_prefix,moved_to,moved_noted FROM webhook_hooks WHERE id=@id AND (enabled=1 OR moved_to IS NOT NULL)", on: db, with: [sqlight.text(id)], expecting: { use session <- decode.field(0, decode.string) use name <- decode.field(1, decode.string) use secret <- decode.field(2, decode.string) use signature_header <- decode.field(3, decode.string) use signature_prefix <- decode.field(4, decode.string) use moved_to <- decode.field(5, decode.optional(decode.string)) use moved_noted <- decode.field(6, decode.int) decode.success(FindHookForDeliveryRow( session:, name:, secret:, signature_header:, signature_prefix:, moved_to:, moved_noted:, )) }, ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/find_other_hook_by_name.sql pub fn find_other_hook_by_name( db db: sqlight.Connection, session session: String, name name: String, id id: String, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks WHERE session=@session AND name=@name AND id<>@id", on: db, with: [sqlight.text(session), sqlight.text(name), sqlight.text(id)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/find_owned.sql pub fn find_owned( db db: sqlight.Connection, id id: String, session session: String, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks WHERE id=@id AND session=@session", on: db, with: [sqlight.text(id), sqlight.text(session)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/for_sessions.sql pub fn for_sessions( db db: sqlight.Connection, sessions_json sessions_json: String, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks WHERE moved_to IS NULL AND session IN (SELECT value FROM json_each(@sessions_json)) ORDER BY name", on: db, with: [sqlight.text(sessions_json)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/initialise.sql pub fn initialise( db db: sqlight.Connection, done_at done_at: Int, ) -> Result(Nil, String) { albedo_sqlite.run( "INSERT INTO webhook_upgrades(name,done_at) VALUES('filed',@done_at)", on: db, with: [sqlight.int(done_at)], ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/initialise_indexes.sql pub fn initialise_indexes(db db: sqlight.Connection) -> Result(Nil, String) { albedo_sqlite.exec( marmot_script.source( "src/albedo_ext_webhooks/ledger/sql/initialise_indexes.sql", "CREATE INDEX IF NOT EXISTS webhook_deliveries_hook ON webhook_deliveries(hook,id);\nCREATE INDEX IF NOT EXISTS webhook_delivered_before ON webhook_deliveries(hook,id) WHERE delivered_at IS NOT NULL;\nCREATE INDEX IF NOT EXISTS webhook_received ON webhook_deliveries(received_at,id);\nCREATE INDEX IF NOT EXISTS webhook_session_received ON webhook_deliveries(session,received_at,id);\nDROP INDEX IF EXISTS webhook_pending;\nDROP INDEX IF EXISTS webhook_deliveries_session;", ), on: db, ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/initialise_schema.sql pub fn initialise_schema(db db: sqlight.Connection) -> Result(Nil, String) { albedo_sqlite.exec( marmot_script.source( "src/albedo_ext_webhooks/ledger/sql/initialise_schema.sql", "CREATE TABLE IF NOT EXISTS webhook_permissions (\n session TEXT PRIMARY KEY REFERENCES sessions(id) ON DELETE CASCADE,\n agent_manage INTEGER NOT NULL DEFAULT 0 CHECK(agent_manage IN (0,1)),\n revision INTEGER NOT NULL DEFAULT 1\n);\nCREATE TABLE IF NOT EXISTS webhook_hooks (\n id TEXT PRIMARY KEY,\n session TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,\n name TEXT NOT NULL,\n secret TEXT NOT NULL,\n enabled INTEGER NOT NULL DEFAULT 1 CHECK(enabled IN (0,1)),\n signature_header TEXT NOT NULL DEFAULT 'x-albedo-signature',\n signature_prefix TEXT NOT NULL DEFAULT 'sha256=',\n revision INTEGER NOT NULL DEFAULT 1,\n UNIQUE(session,name)\n);\nCREATE INDEX IF NOT EXISTS webhook_hooks_session ON webhook_hooks(session);\nCREATE TABLE IF NOT EXISTS webhook_deliveries (\n id TEXT PRIMARY KEY,\n hook TEXT NOT NULL,\n name TEXT NOT NULL,\n session TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,\n body BLOB NOT NULL,\n body_sha256 TEXT NOT NULL,\n event_key TEXT,\n received_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),\n delivered_at TEXT,\n attempts INTEGER NOT NULL DEFAULT 0,\n last_error TEXT\n);\nCREATE UNIQUE INDEX IF NOT EXISTS webhook_event_key ON webhook_deliveries(hook,event_key) WHERE event_key IS NOT NULL;\nCREATE TABLE IF NOT EXISTS webhook_upgrades (\n name TEXT PRIMARY KEY,\n done_at INTEGER NOT NULL\n);", ), on: db, ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/insert_hook_within_limit.sql pub fn insert_hook_within_limit( db db: sqlight.Connection, id id: String, session session: String, name name: String, secret secret: String, enabled enabled: Int, header header: String, prefix prefix: String, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "INSERT INTO webhook_hooks(id,session,name,secret,enabled,signature_header,signature_prefix) SELECT @id,@session,@name,@secret,@enabled,@header,@prefix WHERE (SELECT count(*) FROM webhook_hooks WHERE session=@session) < 32 RETURNING id AS id,session,name,enabled,signature_header,signature_prefix,revision", on: db, with: [ sqlight.text(id), sqlight.text(session), sqlight.text(name), sqlight.text(secret), sqlight.int(enabled), sqlight.text(header), sqlight.text(prefix), ], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/list.sql pub fn list( db db: sqlight.Connection, session session: String, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks WHERE session=@session ORDER BY name LIMIT 100", on: db, with: [sqlight.text(session)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/list_all.sql pub fn list_all(db db: sqlight.Connection) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks ORDER BY session,name", on: db, with: [], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/mark_move_noted.sql pub fn mark_move_noted( db db: sqlight.Connection, id id: String, ) -> Result(Nil, String) { albedo_sqlite.run( "UPDATE webhook_hooks SET moved_to=coalesce(moved_to,''),moved_noted=1 WHERE id=@id", on: db, with: [sqlight.text(id)], ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/page_all.sql pub fn page_all( db db: sqlight.Connection, after after: String, limit limit: Int, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks WHERE id>@after ORDER BY id LIMIT @limit", on: db, with: [sqlight.text(after), sqlight.int(limit)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/page_session.sql pub fn page_session( db db: sqlight.Connection, session session: String, after after: String, limit limit: Int, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "SELECT id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision FROM webhook_hooks WHERE session=@session AND id>@after ORDER BY id LIMIT @limit", on: db, with: [sqlight.text(session), sqlight.text(after), sqlight.int(limit)], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/permission_on.sql pub type PermissionOnRow { PermissionOnRow(agent_manage: Int, revision: Int) } /// Generated from src/albedo_ext_webhooks/ledger/sql/permission_on.sql pub fn permission_on( db db: sqlight.Connection, session session: String, ) -> Result(List(PermissionOnRow), String) { albedo_sqlite.rows( "SELECT agent_manage,revision FROM webhook_permissions WHERE session=@session", on: db, with: [sqlight.text(session)], expecting: { use agent_manage <- decode.field(0, decode.int) use revision <- decode.field(1, decode.int) decode.success(PermissionOnRow(agent_manage:, revision:)) }, ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/refuse_delivery.sql pub fn refuse_delivery( db db: sqlight.Connection, delivery delivery: String, ) -> Result(List(String), String) { albedo_sqlite.rows( "UPDATE webhook_hooks SET moved_to=coalesce(moved_to,''),moved_noted=1 WHERE moved_noted=0 AND id=(SELECT hook FROM webhook_deliveries WHERE id=@delivery) RETURNING name", on: db, with: [sqlight.text(delivery)], expecting: decode.field(0, decode.string, decode.success), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/retire.sql pub fn retire( db db: sqlight.Connection, url url: Option(String), id id: String, ) -> Result(Nil, String) { albedo_sqlite.run( "UPDATE webhook_hooks SET moved_to=@url WHERE id=@id AND moved_to IS NULL", on: db, with: [sqlight.nullable(sqlight.text, url), sqlight.text(id)], ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/rotate.sql pub fn rotate( db db: sqlight.Connection, secret secret: String, id id: String, session session: String, revision revision: Int, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "UPDATE webhook_hooks SET secret=@secret,revision=revision+1 WHERE id=@id AND session=@session AND revision=@revision RETURNING id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision", on: db, with: [ sqlight.text(secret), sqlight.text(id), sqlight.text(session), sqlight.int(revision), ], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/set_enabled.sql pub fn set_enabled( db db: sqlight.Connection, enabled enabled: Int, id id: String, session session: String, revision revision: Int, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "UPDATE webhook_hooks SET enabled=@enabled,revision=revision+1 WHERE id=@id AND session=@session AND revision=@revision RETURNING id AS id,session AS session,name,enabled,signature_header,signature_prefix,revision", on: db, with: [ sqlight.int(enabled), sqlight.text(id), sqlight.text(session), sqlight.int(revision), ], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/update_hook_at_revision.sql pub fn update_hook_at_revision( db db: sqlight.Connection, name name: String, enabled enabled: Int, signature_header signature_header: String, prefix prefix: String, id id: String, session session: String, revision revision: Int, ) -> Result(List(HookRow), String) { albedo_sqlite.rows( "UPDATE webhook_hooks SET name=@name,enabled=@enabled,signature_header=@signature_header,signature_prefix=@prefix,revision=revision+1 WHERE id=@id AND session=@session AND revision=@revision RETURNING id AS id,session,name,enabled,signature_header,signature_prefix,revision", on: db, with: [ sqlight.text(name), sqlight.int(enabled), sqlight.text(signature_header), sqlight.text(prefix), sqlight.text(id), sqlight.text(session), sqlight.int(revision), ], expecting: hook_row_decoder(), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/upgrade_done.sql pub fn upgrade_done(db db: sqlight.Connection) -> Result(List(Int), String) { albedo_sqlite.rows( "SELECT CAST(1 AS INTEGER) AS present FROM webhook_upgrades WHERE name='filed'", on: db, with: [], expecting: decode.field(0, decode.int, decode.success), ) } /// Generated from src/albedo_ext_webhooks/ledger/sql/upsert_permission.sql pub fn upsert_permission( db db: sqlight.Connection, session session: Option(String), enabled enabled: Int, ) -> Result(Nil, String) { albedo_sqlite.run( "INSERT INTO webhook_permissions(session,agent_manage,revision) VALUES(@session,@enabled,1) ON CONFLICT(session) DO UPDATE SET agent_manage=excluded.agent_manage,revision=revision+1", on: db, with: [sqlight.nullable(sqlight.text, session), sqlight.int(enabled)], ) }