apollo-backend/internal/repository/postgres_device.go

218 lines
5.7 KiB
Go
Raw Permalink Normal View History

2021-07-26 16:34:26 +00:00
package repository
import (
"context"
"fmt"
2022-03-28 21:05:01 +00:00
"time"
2021-07-26 16:34:26 +00:00
"github.com/christianselig/apollo-backend/internal/domain"
)
type postgresDeviceRepository struct {
conn Connection
2021-07-26 16:34:26 +00:00
}
func NewPostgresDevice(conn Connection) domain.DeviceRepository {
return &postgresDeviceRepository{conn: conn}
2021-07-26 16:34:26 +00:00
}
func (p *postgresDeviceRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]domain.Device, error) {
rows, err := p.conn.Query(ctx, query, args...)
2021-07-26 16:34:26 +00:00
if err != nil {
return nil, err
}
defer rows.Close()
var devs []domain.Device
for rows.Next() {
var dev domain.Device
if err := rows.Scan(
&dev.ID,
&dev.APNSToken,
&dev.Sandbox,
2022-03-28 21:05:01 +00:00
&dev.ExpiresAt,
&dev.GracePeriodExpiresAt,
2021-07-26 16:34:26 +00:00
); err != nil {
return nil, err
}
devs = append(devs, dev)
}
return devs, nil
}
2021-09-25 16:56:01 +00:00
func (p *postgresDeviceRepository) GetByID(ctx context.Context, id int64) (domain.Device, error) {
query := `
2022-03-28 21:05:01 +00:00
SELECT id, apns_token, sandbox, expires_at, grace_period_expires_at
2021-09-25 16:56:01 +00:00
FROM devices
WHERE id = $1`
devs, err := p.fetch(ctx, query, id)
if err != nil {
return domain.Device{}, err
}
if len(devs) == 0 {
return domain.Device{}, domain.ErrNotFound
}
return devs[0], nil
}
2021-07-26 16:34:26 +00:00
func (p *postgresDeviceRepository) GetByAPNSToken(ctx context.Context, token string) (domain.Device, error) {
query := `
2022-03-28 21:05:01 +00:00
SELECT id, apns_token, sandbox, expires_at, grace_period_expires_at
2021-07-26 16:34:26 +00:00
FROM devices
2021-08-08 18:19:47 +00:00
WHERE apns_token = $1`
2021-07-26 16:34:26 +00:00
devs, err := p.fetch(ctx, query, token)
if err != nil {
return domain.Device{}, err
}
if len(devs) == 0 {
return domain.Device{}, domain.ErrNotFound
}
return devs[0], nil
}
2021-08-14 17:42:28 +00:00
func (p *postgresDeviceRepository) GetByAccountID(ctx context.Context, id int64) ([]domain.Device, error) {
query := `
2022-03-28 21:05:01 +00:00
SELECT devices.id, apns_token, sandbox, expires_at, grace_period_expires_at
2021-08-14 17:42:28 +00:00
FROM devices
INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id
WHERE devices_accounts.account_id = $1`
return p.fetch(ctx, query, id)
}
2022-03-12 17:50:05 +00:00
func (p *postgresDeviceRepository) GetInboxNotifiableByAccountID(ctx context.Context, id int64) ([]domain.Device, error) {
query := `
2022-03-28 21:05:01 +00:00
SELECT devices.id, apns_token, sandbox, expires_at, grace_period_expires_at
FROM devices
INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id
2022-03-26 16:52:10 +00:00
WHERE devices_accounts.account_id = $1 AND
devices_accounts.inbox_notifiable = TRUE AND
2022-05-07 16:05:47 +00:00
grace_period_expires_at > NOW()`
2022-03-12 17:50:05 +00:00
return p.fetch(ctx, query, id)
}
func (p *postgresDeviceRepository) GetWatcherNotifiableByAccountID(ctx context.Context, id int64) ([]domain.Device, error) {
query := `
2022-03-28 21:05:01 +00:00
SELECT devices.id, apns_token, sandbox, expires_at, grace_period_expires_at
2022-03-12 17:50:05 +00:00
FROM devices
INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id
2022-03-26 16:52:10 +00:00
WHERE devices_accounts.account_id = $1 AND
devices_accounts.watcher_notifiable = TRUE AND
2022-05-07 16:05:47 +00:00
grace_period_expires_at > NOW()`
return p.fetch(ctx, query, id)
}
2021-07-26 16:34:26 +00:00
func (p *postgresDeviceRepository) CreateOrUpdate(ctx context.Context, dev *domain.Device) error {
query := `
2022-03-28 21:05:01 +00:00
INSERT INTO devices (apns_token, sandbox, expires_at, grace_period_expires_at)
VALUES ($1, $2, $3, $4)
2021-08-08 18:19:47 +00:00
ON CONFLICT(apns_token) DO
2022-03-28 21:05:01 +00:00
UPDATE SET expires_at = $3, grace_period_expires_at = $4
2021-07-26 16:34:26 +00:00
RETURNING id`
return p.conn.QueryRow(
2021-07-26 16:34:26 +00:00
ctx,
query,
dev.APNSToken,
dev.Sandbox,
2022-03-28 21:05:01 +00:00
&dev.ExpiresAt,
&dev.GracePeriodExpiresAt,
2021-08-08 18:19:47 +00:00
).Scan(&dev.ID)
2021-07-26 16:34:26 +00:00
}
func (p *postgresDeviceRepository) Create(ctx context.Context, dev *domain.Device) error {
2021-10-12 14:18:40 +00:00
if err := dev.Validate(); err != nil {
return err
}
2021-07-26 16:34:26 +00:00
query := `
INSERT INTO devices
2022-03-28 21:05:01 +00:00
(apns_token, sandbox, expires_at, grace_period_expires_at)
VALUES ($1, $2, $3, $4)
2021-07-26 16:34:26 +00:00
RETURNING id`
return p.conn.QueryRow(
2021-07-26 16:34:26 +00:00
ctx,
query,
dev.APNSToken,
dev.Sandbox,
2022-03-28 21:05:01 +00:00
dev.ExpiresAt,
dev.GracePeriodExpiresAt,
2021-08-08 18:19:47 +00:00
).Scan(&dev.ID)
2021-07-26 16:34:26 +00:00
}
func (p *postgresDeviceRepository) Update(ctx context.Context, dev *domain.Device) error {
2021-10-12 14:18:40 +00:00
if err := dev.Validate(); err != nil {
return err
}
2021-07-26 16:34:26 +00:00
query := `
UPDATE devices
2022-03-28 21:05:01 +00:00
SET expires_at = $2, grace_period_expires_at = $3
2021-07-26 16:34:26 +00:00
WHERE id = $1`
res, err := p.conn.Exec(ctx, query, dev.ID, dev.ExpiresAt, dev.GracePeriodExpiresAt)
2021-07-26 16:34:26 +00:00
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err
}
func (p *postgresDeviceRepository) Delete(ctx context.Context, token string) error {
query := `DELETE FROM devices WHERE apns_token = $1`
res, err := p.conn.Exec(ctx, query, token)
2021-07-26 16:34:26 +00:00
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err
}
2021-08-14 16:08:17 +00:00
func (p *postgresDeviceRepository) SetNotifiable(ctx context.Context, dev *domain.Device, acct *domain.Account, inbox, watcher, global bool) error {
query := `
UPDATE devices_accounts
2022-03-12 17:50:05 +00:00
SET
inbox_notifiable = $1,
watcher_notifiable = $2,
global_mute = $3
WHERE device_id = $4 AND account_id = $5`
res, err := p.conn.Exec(ctx, query, inbox, watcher, global, dev.ID, acct.ID)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err
}
func (p *postgresDeviceRepository) GetNotifiable(ctx context.Context, dev *domain.Device, acct *domain.Account) (bool, bool, bool, error) {
2022-03-12 17:50:05 +00:00
query := `
SELECT inbox_notifiable, watcher_notifiable, global_mute
2022-03-12 17:50:05 +00:00
FROM devices_accounts
WHERE device_id = $1 AND account_id = $2`
2022-03-26 17:40:51 +00:00
var inbox, watcher, global bool
if err := p.conn.QueryRow(ctx, query, dev.ID, acct.ID).Scan(&inbox, &watcher, &global); err != nil {
2022-03-26 17:40:51 +00:00
return false, false, false, domain.ErrNotFound
2022-03-12 17:50:05 +00:00
}
2022-03-26 17:40:51 +00:00
return inbox, watcher, global, nil
2022-03-12 17:50:05 +00:00
}
2022-03-28 21:05:01 +00:00
func (p *postgresDeviceRepository) PruneStale(ctx context.Context, expiry time.Time) (int64, error) {
query := `DELETE FROM devices WHERE grace_period_expires_at < $1`
2021-08-14 16:08:17 +00:00
res, err := p.conn.Exec(ctx, query, expiry)
2021-08-14 16:08:17 +00:00
return res.RowsAffected(), err
}