~bigbes/core-go

87de6649e1c9beadc2a63d3774f5f8e221b84d39 — Adnan Maolood 4 years ago 21deda8
webhooks: Add FilterWebhooks function
1 files changed, 19 insertions(+), 0 deletions(-)

M webhooks/config.go
M webhooks/config.go => webhooks/config.go +19 -0
@@ 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
	}
}