Skip to content

Commit

Permalink
Add new SetHmacKeys API method
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Feb 29, 2024
1 parent 47481e5 commit 394be1d
Show file tree
Hide file tree
Showing 16 changed files with 784 additions and 112 deletions.
33 changes: 25 additions & 8 deletions mocks/Installations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 64 additions & 14 deletions mocks/Subscriptions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (s *ApiServer) Unsubscribe(
return connect.NewResponse(&emptypb.Empty{}), nil
}

func (s *ApiServer) SubscribeWithMetadata(ctx context.Context, req *connect.Request[proto.SubscribeWithMetadataRequest]) (*connect.Response[emptypb.Empty], error) {
return nil, nil
}

func convertDeliveryMechanism(mechanism *proto.DeliveryMechanism) *interfaces.DeliveryMechanism {
if mechanism == nil {
return nil
Expand All @@ -152,3 +156,23 @@ func convertDeliveryMechanism(mechanism *proto.DeliveryMechanism) *interfaces.De
return &interfaces.DeliveryMechanism{Kind: interfaces.FCM, Token: fcmToken}
}
}

// func convertHmacUpdates(protoUpdates []*proto.Subscription_HmacKey) (interfaces.HmacUpdates, error) {
// out := make(interfaces.HmacUpdates)
// for _, update := range protoUpdates {
// if update == nil {
// continue
// }
// if _, exists := out[update.Topic]; exists {
// return nil, fmt.Errorf("duplicate topic: %s", update.Topic)
// }
// for _, key := range update.HmacKey {
// out[update.Topic] = append(out[update.Topic], interfaces.HmacKey{
// ThirtyDayPeriodsSinceEpoch: int(key.ThirtyDayPeriodsSinceEpoch),
// Key: key.Key,
// })
// }
// }

// return out, nil
// }
13 changes: 13 additions & 0 deletions pkg/db/migrations/20240229061721_add-hmac-keys.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SET statement_timeout = 0;

--bun:split

DROP TABLE subscription_hmac_keys;

--bun:split

ALTER TABLE subscriptions DROP COLUMN is_silent;

--bun:split

DROP INDEX CONCURRENTLY IF EXISTS subscriptions_installation_id_topic_idx;
21 changes: 21 additions & 0 deletions pkg/db/migrations/20240229061721_add-hmac-keys.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SET statement_timeout = 0;

--bun:split

CREATE TABLE subscription_hmac_keys (
subscription_id INTEGER NOT NULL,
thirty_day_periods_since_epoch INTEGER NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
key BYTEA NOT NULL,
PRIMARY KEY (subscription_id, thirty_day_periods_since_epoch),
FOREIGN KEY (subscription_id) REFERENCES subscriptions(id) ON DELETE CASCADE
);

--bun:split

ALTER TABLE subscriptions ADD COLUMN is_silent BOOLEAN DEFAULT FALSE;

--bun:split

CREATE UNIQUE INDEX CONCURRENTLY subscriptions_installation_id_topic_idx ON subscriptions (installation_id, topic);
21 changes: 16 additions & 5 deletions pkg/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,20 @@ type DeviceDeliveryMechanism struct {
type Subscription struct {
bun.BaseModel `bun:"table:subscriptions"`

Id int64 `bun:",pk,autoincrement"`
CreatedAt time.Time `bun:"created_at,notnull,default:current_timestamp"`
InstallationId string `bun:"installation_id,notnull"`
Topic string `bun:"topic,notnull"`
IsActive bool `bun:"is_active,notnull"`
Id int64 `bun:",pk,autoincrement"`
CreatedAt time.Time `bun:"created_at,notnull"`
InstallationId string `bun:"installation_id,notnull"`
Topic string `bun:"topic,notnull"`
IsActive bool `bun:"is_active,notnull"`
IsSilent bool `bun:"is_silent,notnull"`
HmacKeys []*SubscriptionHmacKeys `bun:"rel:has-many,join:id=subscription_id"`
}

type SubscriptionHmacKeys struct {
bun.BaseModel `bun:"table:subscription_hmac_keys"`
SubscriptionId int64 `bun:"subscription_id,notnull,pk"`
ThirtyDayPeriodsSinceEpoch int32 `bun:"thirty_day_periods_since_epoch,notnull,pk"`
Key []byte `bun:"key,notnull,type:bytea"`
CreatedAt time.Time `bun:"created_at,notnull"`
UpdatedAt time.Time `bun:"updated_at,notnull,default:current_timestamp"`
}
22 changes: 21 additions & 1 deletion pkg/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Subscription struct {
InstallationId string
Topic string
IsActive bool
IsSilent bool
HmacKey *HmacKey
}

type SendRequest struct {
Expand All @@ -49,18 +51,36 @@ type SendRequest struct {
Message *v1.Envelope
}

type HmacKey struct {
ThirtyDayPeriodsSinceEpoch int
Key []byte
}

type SubscriptionInput struct {
Topic string
IsSilent bool
HmacKeys []HmacKey
}

type HmacUpdates map[string][]HmacKey

// Pluggable Installation Service interface
//
//go:generate mockery --dir ../interfaces --name Installations --output ../../mocks --outpkg mocks
type Installations interface {
Register(ctx context.Context, installation Installation) (*RegisterResponse, error)
Delete(ctx context.Context, installationId string) error
GetInstallations(ctx context.Context, installationIds []string) ([]Installation, error)
}

// This interface is not expected to be pluggable
//
//go:generate mockery --dir ../interfaces --name Subscriptions --output ../../mocks --outpkg mocks
type Subscriptions interface {
Subscribe(ctx context.Context, installationId string, topics []string) error
Unsubscribe(ctx context.Context, installationId string, topics []string) error
GetSubscriptions(ctx context.Context, topic string) ([]Subscription, error)
GetSubscriptions(ctx context.Context, topic string, thirtyDayPeriod int) ([]Subscription, error)
SubscribeWithMetadata(ctx context.Context, installationId string, subscriptions []SubscriptionInput) error
}

// Pluggable interface for sending push notifications
Expand Down
Loading

0 comments on commit 394be1d

Please sign in to comment.