mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-19 14:54:22 +01:00
notifications: postgres datastore implementation
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
DROP INDEX notification_subscriptions_pubkey_url_key;
|
||||
DROP INDEX notification_subscriptions_pubkey_idx;
|
||||
DROP TABLE public.notification_subscriptions;
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE public.notification_subscriptions (
|
||||
id bigserial primary key,
|
||||
pubkey bytea NOT NULL,
|
||||
url varchar NOT NULL,
|
||||
created_at bigint NOT NULL,
|
||||
refreshed_at bigint NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX notification_subscriptions_pubkey_idx ON public.notification_subscriptions (pubkey);
|
||||
CREATE UNIQUE INDEX notification_subscriptions_pubkey_url_key ON public.notification_subscriptions (pubkey, url);
|
||||
76
postgresql/notifications_store.go
Normal file
76
postgresql/notifications_store.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
type NotificationsStore struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewNotificationsStore(pool *pgxpool.Pool) *NotificationsStore {
|
||||
return &NotificationsStore{pool: pool}
|
||||
}
|
||||
|
||||
func (s *NotificationsStore) Register(
|
||||
ctx context.Context,
|
||||
pubkey string,
|
||||
url string,
|
||||
) error {
|
||||
pk, err := hex.DecodeString(pubkey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now().UnixMicro()
|
||||
_, err = s.pool.Exec(
|
||||
ctx,
|
||||
`INSERT INTO public.notification_subscriptions (pubkey, url, created_at, refreshed_at)
|
||||
values ($1, $2, $3, $4)
|
||||
ON CONFLICT (pubkey, url) DO UPDATE SET refreshed_at = $4`,
|
||||
pk,
|
||||
url,
|
||||
now,
|
||||
now,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *NotificationsStore) GetRegistrations(
|
||||
ctx context.Context,
|
||||
pubkey string,
|
||||
) ([]string, error) {
|
||||
pk, err := hex.DecodeString(pubkey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := s.pool.Query(
|
||||
ctx,
|
||||
`SELECT url
|
||||
FROM public.notification_subscriptions
|
||||
WHERE pubkey = $1`,
|
||||
pk,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []string
|
||||
for rows.Next() {
|
||||
var url string
|
||||
err = rows.Scan(&url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result = append(result, url)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user