From 87de6649e1c9beadc2a63d3774f5f8e221b84d39 Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Wed, 12 Jan 2022 10:24:53 -0500 Subject: [PATCH] webhooks: Add FilterWebhooks function --- webhooks/config.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/webhooks/config.go b/webhooks/config.go index 9faa70fb471070fdae93c5cb0d9db521615318fd..af28840eebeec33cb76cacf2aedb2ec391127fae 100644 --- a/webhooks/config.go +++ b/webhooks/config.go @@ -7,6 +7,7 @@ import ( "time" "git.sr.ht/~sircmpwn/core-go/auth" + sq "github.com/Masterminds/squirrel" ) // The following invariants apply to AuthConfig: @@ -59,3 +60,21 @@ func NewAuthConfig(ctx context.Context) (AuthConfig, error) { } panic("Unreachable") } + +// Returns an SQL expression to filter webhooks for the authenticated user. +func FilterWebhooks(ctx context.Context) (sq.Sqlizer, error) { + ac, err := NewAuthConfig(ctx) + if err != nil { + return nil, err + } + if ac.ClientID != nil { + // XXX: Should we maybe return all webhooks configured by client ID? + return sq.And{ + sq.Expr(`NOW() at time zone 'utc' < expires`), + sq.Expr(`token_hash = ?`, ac.TokenHash), + sq.Expr(`client_id = ?`, *ac.ClientID), + }, nil + } else { + return sq.Expr(`NOW() at time zone 'utc' < expires`), nil + } +}