mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 08:34:42 +00:00
660 lines
21 KiB
Go
660 lines
21 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: telemetry_requests.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const clearNoPasswordBlocked = `-- name: ClearNoPasswordBlocked :execrows
|
|
UPDATE polling_targets SET no_password_blocked = false WHERE target_public_key = $1 AND no_password_blocked
|
|
`
|
|
|
|
// Run under LockPollingTarget.
|
|
func (q *Queries) ClearNoPasswordBlocked(ctx context.Context, targetPublicKey []byte) (int64, error) {
|
|
result, err := q.db.Exec(ctx, clearNoPasswordBlocked, targetPublicKey)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const countUserRequests = `-- name: CountUserRequests :one
|
|
SELECT count(*)::int FROM telemetry_requests WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) CountUserRequests(ctx context.Context, userID uuid.UUID) (int32, error) {
|
|
row := q.db.QueryRow(ctx, countUserRequests, userID)
|
|
var column_1 int32
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|
|
|
|
const deletePollingAdminStop = `-- name: DeletePollingAdminStop :execrows
|
|
DELETE FROM polling_admin_stops WHERE target_public_key = $1 AND observer_public_key = $2
|
|
`
|
|
|
|
type DeletePollingAdminStopParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
}
|
|
|
|
func (q *Queries) DeletePollingAdminStop(ctx context.Context, arg DeletePollingAdminStopParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deletePollingAdminStop, arg.TargetPublicKey, arg.ObserverPublicKey)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deleteTelemetryRequest = `-- name: DeleteTelemetryRequest :exec
|
|
DELETE FROM telemetry_requests WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteTelemetryRequest(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteTelemetryRequest, id)
|
|
return err
|
|
}
|
|
|
|
const ensurePollingTarget = `-- name: EnsurePollingTarget :exec
|
|
INSERT INTO polling_targets (target_public_key) VALUES ($1) ON CONFLICT (target_public_key) DO NOTHING
|
|
`
|
|
|
|
func (q *Queries) EnsurePollingTarget(ctx context.Context, targetPublicKey []byte) error {
|
|
_, err := q.db.Exec(ctx, ensurePollingTarget, targetPublicKey)
|
|
return err
|
|
}
|
|
|
|
const getPollingTarget = `-- name: GetPollingTarget :one
|
|
SELECT active_request_id, ver, ver_changed_at, no_password_blocked FROM polling_targets WHERE target_public_key = $1
|
|
`
|
|
|
|
type GetPollingTargetRow struct {
|
|
ActiveRequestID pgtype.UUID `json:"active_request_id"`
|
|
Ver int64 `json:"ver"`
|
|
VerChangedAt pgtype.Timestamptz `json:"ver_changed_at"`
|
|
NoPasswordBlocked bool `json:"no_password_blocked"`
|
|
}
|
|
|
|
func (q *Queries) GetPollingTarget(ctx context.Context, targetPublicKey []byte) (GetPollingTargetRow, error) {
|
|
row := q.db.QueryRow(ctx, getPollingTarget, targetPublicKey)
|
|
var i GetPollingTargetRow
|
|
err := row.Scan(
|
|
&i.ActiveRequestID,
|
|
&i.Ver,
|
|
&i.VerChangedAt,
|
|
&i.NoPasswordBlocked,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertPollingAdminStop = `-- name: InsertPollingAdminStop :execrows
|
|
INSERT INTO polling_admin_stops (target_public_key, observer_public_key, reported_at) VALUES ($1, $2, $3)
|
|
ON CONFLICT (target_public_key, observer_public_key) DO NOTHING
|
|
`
|
|
|
|
type InsertPollingAdminStopParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
ReportedAt pgtype.Timestamptz `json:"reported_at"`
|
|
}
|
|
|
|
func (q *Queries) InsertPollingAdminStop(ctx context.Context, arg InsertPollingAdminStopParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, insertPollingAdminStop, arg.TargetPublicKey, arg.ObserverPublicKey, arg.ReportedAt)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const insertTelemetryRequest = `-- name: InsertTelemetryRequest :exec
|
|
INSERT INTO telemetry_requests (id, user_id, target_public_key, status, status_reason, password_ct, password_nonce,
|
|
password_failed, created_at, updated_at, enabled_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
`
|
|
|
|
type InsertTelemetryRequestParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Status string `json:"status"`
|
|
StatusReason string `json:"status_reason"`
|
|
PasswordCt []byte `json:"password_ct"`
|
|
PasswordNonce []byte `json:"password_nonce"`
|
|
PasswordFailed bool `json:"password_failed"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
EnabledAt pgtype.Timestamptz `json:"enabled_at"`
|
|
}
|
|
|
|
func (q *Queries) InsertTelemetryRequest(ctx context.Context, arg InsertTelemetryRequestParams) error {
|
|
_, err := q.db.Exec(ctx, insertTelemetryRequest,
|
|
arg.ID,
|
|
arg.UserID,
|
|
arg.TargetPublicKey,
|
|
arg.Status,
|
|
arg.StatusReason,
|
|
arg.PasswordCt,
|
|
arg.PasswordNonce,
|
|
arg.PasswordFailed,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
arg.EnabledAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listNoPasswordBlocked = `-- name: ListNoPasswordBlocked :many
|
|
SELECT target_public_key FROM polling_targets WHERE no_password_blocked ORDER BY target_public_key
|
|
`
|
|
|
|
func (q *Queries) ListNoPasswordBlocked(ctx context.Context) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, listNoPasswordBlocked)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var target_public_key []byte
|
|
if err := rows.Scan(&target_public_key); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, target_public_key)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listPollingAdminStops = `-- name: ListPollingAdminStops :many
|
|
SELECT target_public_key, observer_public_key, reported_at FROM polling_admin_stops ORDER BY reported_at DESC, target_public_key, observer_public_key
|
|
`
|
|
|
|
func (q *Queries) ListPollingAdminStops(ctx context.Context) ([]PollingAdminStop, error) {
|
|
rows, err := q.db.Query(ctx, listPollingAdminStops)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []PollingAdminStop{}
|
|
for rows.Next() {
|
|
var i PollingAdminStop
|
|
if err := rows.Scan(&i.TargetPublicKey, &i.ObserverPublicKey, &i.ReportedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listRequestedAssignees = `-- name: ListRequestedAssignees :many
|
|
SELECT observer_public_key, rank FROM polling_assignments WHERE target_public_key = $1 AND requested
|
|
`
|
|
|
|
type ListRequestedAssigneesRow struct {
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
Rank int16 `json:"rank"`
|
|
}
|
|
|
|
// The observers handed this target as a requested one, with their rank (0 polls).
|
|
func (q *Queries) ListRequestedAssignees(ctx context.Context, targetPublicKey []byte) ([]ListRequestedAssigneesRow, error) {
|
|
rows, err := q.db.Query(ctx, listRequestedAssignees, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListRequestedAssigneesRow{}
|
|
for rows.Next() {
|
|
var i ListRequestedAssigneesRow
|
|
if err := rows.Scan(&i.ObserverPublicKey, &i.Rank); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listTargetAdminStops = `-- name: ListTargetAdminStops :many
|
|
SELECT observer_public_key FROM polling_admin_stops WHERE target_public_key = $1
|
|
`
|
|
|
|
func (q *Queries) ListTargetAdminStops(ctx context.Context, targetPublicKey []byte) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, listTargetAdminStops, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var observer_public_key []byte
|
|
if err := rows.Scan(&observer_public_key); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, observer_public_key)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listTargetOtherStates = `-- name: ListTargetOtherStates :many
|
|
SELECT observer_public_key, seen_at FROM polling_other_states WHERE target_public_key = $1
|
|
`
|
|
|
|
type ListTargetOtherStatesRow struct {
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
SeenAt pgtype.Timestamptz `json:"seen_at"`
|
|
}
|
|
|
|
func (q *Queries) ListTargetOtherStates(ctx context.Context, targetPublicKey []byte) ([]ListTargetOtherStatesRow, error) {
|
|
rows, err := q.db.Query(ctx, listTargetOtherStates, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListTargetOtherStatesRow{}
|
|
for rows.Next() {
|
|
var i ListTargetOtherStatesRow
|
|
if err := rows.Scan(&i.ObserverPublicKey, &i.SeenAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listTargetRequests = `-- name: ListTargetRequests :many
|
|
SELECT id, user_id, status, status_reason, password_ct, password_nonce, password_failed, created_at, updated_at, enabled_at
|
|
FROM telemetry_requests WHERE target_public_key = $1 ORDER BY created_at, id
|
|
`
|
|
|
|
type ListTargetRequestsRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
Status string `json:"status"`
|
|
StatusReason string `json:"status_reason"`
|
|
PasswordCt []byte `json:"password_ct"`
|
|
PasswordNonce []byte `json:"password_nonce"`
|
|
PasswordFailed bool `json:"password_failed"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
EnabledAt pgtype.Timestamptz `json:"enabled_at"`
|
|
}
|
|
|
|
func (q *Queries) ListTargetRequests(ctx context.Context, targetPublicKey []byte) ([]ListTargetRequestsRow, error) {
|
|
rows, err := q.db.Query(ctx, listTargetRequests, targetPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListTargetRequestsRow{}
|
|
for rows.Next() {
|
|
var i ListTargetRequestsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.UserID,
|
|
&i.Status,
|
|
&i.StatusReason,
|
|
&i.PasswordCt,
|
|
&i.PasswordNonce,
|
|
&i.PasswordFailed,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.EnabledAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listUserTelemetryRequests = `-- name: ListUserTelemetryRequests :many
|
|
SELECT r.id, r.target_public_key, r.status, r.status_reason, (r.password_ct IS NOT NULL)::boolean AS has_password,
|
|
r.created_at, r.updated_at,
|
|
n.id AS node_id, COALESCE(n.name, '')::text AS node_name,
|
|
COALESCE(p.observer_id, '00000000-0000-0000-0000-000000000000'::uuid) AS poller_id, COALESCE(p.name, '')::text AS poller_name,
|
|
(SELECT max(x.sampled_at) FROM repeater_readings x WHERE x.target_public_key = r.target_public_key AND x.trusted)::timestamptz AS latest_reading_at
|
|
FROM telemetry_requests r
|
|
LEFT JOIN nodes n ON n.public_key = r.target_public_key
|
|
LEFT JOIN LATERAL (
|
|
SELECT o.id AS observer_id, COALESCE(NULLIF(o.display_name, ''), left(encode(o.public_key, 'hex'), 8)) AS name
|
|
FROM polling_assignments a JOIN observers o ON o.public_key = a.observer_public_key
|
|
WHERE a.target_public_key = r.target_public_key AND a.rank = 0
|
|
LIMIT 1
|
|
) p ON true
|
|
WHERE r.user_id = $1
|
|
ORDER BY r.created_at, r.id
|
|
`
|
|
|
|
type ListUserTelemetryRequestsRow struct {
|
|
ID uuid.UUID `json:"id"`
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Status string `json:"status"`
|
|
StatusReason string `json:"status_reason"`
|
|
HasPassword bool `json:"has_password"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
NodeID pgtype.UUID `json:"node_id"`
|
|
NodeName string `json:"node_name"`
|
|
PollerID uuid.UUID `json:"poller_id"`
|
|
PollerName string `json:"poller_name"`
|
|
LatestReadingAt pgtype.Timestamptz `json:"latest_reading_at"`
|
|
}
|
|
|
|
// A user's requests, oldest first, with the node's current id and name, the observer that polls the repeater and
|
|
// the newest trusted reading. No password column is read; poller_id is the nil uuid when nobody polls it.
|
|
func (q *Queries) ListUserTelemetryRequests(ctx context.Context, userID uuid.UUID) ([]ListUserTelemetryRequestsRow, error) {
|
|
rows, err := q.db.Query(ctx, listUserTelemetryRequests, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListUserTelemetryRequestsRow{}
|
|
for rows.Next() {
|
|
var i ListUserTelemetryRequestsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.TargetPublicKey,
|
|
&i.Status,
|
|
&i.StatusReason,
|
|
&i.HasPassword,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.NodeID,
|
|
&i.NodeName,
|
|
&i.PollerID,
|
|
&i.PollerName,
|
|
&i.LatestReadingAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockUserForRequests = `-- name: LockUserForRequests :one
|
|
SELECT id FROM users WHERE id = $1 FOR NO KEY UPDATE
|
|
`
|
|
|
|
// Serializes one user's request creation (the limit) without blocking the foreign-key checks on their rows.
|
|
func (q *Queries) LockUserForRequests(ctx context.Context, id uuid.UUID) (uuid.UUID, error) {
|
|
row := q.db.QueryRow(ctx, lockUserForRequests, id)
|
|
var id_2 uuid.UUID
|
|
err := row.Scan(&id_2)
|
|
return id_2, err
|
|
}
|
|
|
|
const offlineRequestTargets = `-- name: OfflineRequestTargets :many
|
|
SELECT r.target_public_key::bytea AS target_public_key
|
|
FROM telemetry_requests r
|
|
LEFT JOIN nodes n ON n.public_key = r.target_public_key
|
|
WHERE r.status <> 'disabled'
|
|
GROUP BY r.target_public_key, n.last_seen
|
|
HAVING GREATEST(max(r.enabled_at), COALESCE(n.last_seen, '-infinity'::timestamptz)) < $1::timestamptz
|
|
`
|
|
|
|
// Requested targets whose node no observer has heard since cutoff and whose newest enabling is older than cutoff.
|
|
// Requests with no node row count from the enabling alone.
|
|
func (q *Queries) OfflineRequestTargets(ctx context.Context, cutoff pgtype.Timestamptz) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, offlineRequestTargets, cutoff)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var target_public_key []byte
|
|
if err := rows.Scan(&target_public_key); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, target_public_key)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const pollingAdminRequestCounts = `-- name: PollingAdminRequestCounts :many
|
|
SELECT r.target_public_key::bytea AS target_public_key, count(*)::int AS requests,
|
|
bool_or(t.active_request_id IS NOT NULL)::boolean AS requested
|
|
FROM telemetry_requests r
|
|
LEFT JOIN polling_targets t ON t.target_public_key = r.target_public_key
|
|
WHERE r.target_public_key = ANY($1::bytea[])
|
|
GROUP BY r.target_public_key
|
|
`
|
|
|
|
type PollingAdminRequestCountsRow struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Requests int32 `json:"requests"`
|
|
Requested bool `json:"requested"`
|
|
}
|
|
|
|
func (q *Queries) PollingAdminRequestCounts(ctx context.Context, targets [][]byte) ([]PollingAdminRequestCountsRow, error) {
|
|
rows, err := q.db.Query(ctx, pollingAdminRequestCounts, targets)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []PollingAdminRequestCountsRow{}
|
|
for rows.Next() {
|
|
var i PollingAdminRequestCountsRow
|
|
if err := rows.Scan(&i.TargetPublicKey, &i.Requests, &i.Requested); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const pollingTargetExists = `-- name: PollingTargetExists :one
|
|
SELECT EXISTS (SELECT 1 FROM polling_targets WHERE target_public_key = $1)
|
|
`
|
|
|
|
func (q *Queries) PollingTargetExists(ctx context.Context, targetPublicKey []byte) (bool, error) {
|
|
row := q.db.QueryRow(ctx, pollingTargetExists, targetPublicKey)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const requestNode = `-- name: RequestNode :one
|
|
SELECT public_key, node_type, COALESCE(name, '')::text AS name FROM nodes WHERE id = $1
|
|
`
|
|
|
|
type RequestNodeRow struct {
|
|
PublicKey []byte `json:"public_key"`
|
|
NodeType int16 `json:"node_type"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func (q *Queries) RequestNode(ctx context.Context, id uuid.UUID) (RequestNodeRow, error) {
|
|
row := q.db.QueryRow(ctx, requestNode, id)
|
|
var i RequestNodeRow
|
|
err := row.Scan(&i.PublicKey, &i.NodeType, &i.Name)
|
|
return i, err
|
|
}
|
|
|
|
const requestedPasswords = `-- name: RequestedPasswords :many
|
|
SELECT t.target_public_key::bytea AS target_public_key, t.ver, r.id AS request_id, r.password_ct, r.password_nonce
|
|
FROM polling_targets t
|
|
JOIN telemetry_requests r ON r.id = t.active_request_id
|
|
WHERE t.target_public_key = ANY($1::bytea[])
|
|
`
|
|
|
|
type RequestedPasswordsRow struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
Ver int64 `json:"ver"`
|
|
RequestID uuid.UUID `json:"request_id"`
|
|
PasswordCt []byte `json:"password_ct"`
|
|
PasswordNonce []byte `json:"password_nonce"`
|
|
}
|
|
|
|
// The password in use for each requested target among $1, sealed. A target whose option in use is the blank login
|
|
// has no ciphertext.
|
|
func (q *Queries) RequestedPasswords(ctx context.Context, targets [][]byte) ([]RequestedPasswordsRow, error) {
|
|
rows, err := q.db.Query(ctx, requestedPasswords, targets)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []RequestedPasswordsRow{}
|
|
for rows.Next() {
|
|
var i RequestedPasswordsRow
|
|
if err := rows.Scan(
|
|
&i.TargetPublicKey,
|
|
&i.Ver,
|
|
&i.RequestID,
|
|
&i.PasswordCt,
|
|
&i.PasswordNonce,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const telemetryRequestTarget = `-- name: TelemetryRequestTarget :one
|
|
SELECT target_public_key FROM telemetry_requests WHERE id = $1 AND user_id = $2
|
|
`
|
|
|
|
type TelemetryRequestTargetParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) TelemetryRequestTarget(ctx context.Context, arg TelemetryRequestTargetParams) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, telemetryRequestTarget, arg.ID, arg.UserID)
|
|
var target_public_key []byte
|
|
err := row.Scan(&target_public_key)
|
|
return target_public_key, err
|
|
}
|
|
|
|
const touchPollingOtherState = `-- name: TouchPollingOtherState :exec
|
|
INSERT INTO polling_other_states (target_public_key, observer_public_key, seen_at) VALUES ($1, $2, $3)
|
|
ON CONFLICT (target_public_key, observer_public_key) DO UPDATE SET seen_at = EXCLUDED.seen_at
|
|
`
|
|
|
|
type TouchPollingOtherStateParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ObserverPublicKey []byte `json:"observer_public_key"`
|
|
SeenAt pgtype.Timestamptz `json:"seen_at"`
|
|
}
|
|
|
|
func (q *Queries) TouchPollingOtherState(ctx context.Context, arg TouchPollingOtherStateParams) error {
|
|
_, err := q.db.Exec(ctx, touchPollingOtherState, arg.TargetPublicKey, arg.ObserverPublicKey, arg.SeenAt)
|
|
return err
|
|
}
|
|
|
|
const updatePollingTarget = `-- name: UpdatePollingTarget :exec
|
|
UPDATE polling_targets SET active_request_id = $2, ver = $3, ver_changed_at = $4, no_password_blocked = $5
|
|
WHERE target_public_key = $1
|
|
`
|
|
|
|
type UpdatePollingTargetParams struct {
|
|
TargetPublicKey []byte `json:"target_public_key"`
|
|
ActiveRequestID pgtype.UUID `json:"active_request_id"`
|
|
Ver int64 `json:"ver"`
|
|
VerChangedAt pgtype.Timestamptz `json:"ver_changed_at"`
|
|
NoPasswordBlocked bool `json:"no_password_blocked"`
|
|
}
|
|
|
|
func (q *Queries) UpdatePollingTarget(ctx context.Context, arg UpdatePollingTargetParams) error {
|
|
_, err := q.db.Exec(ctx, updatePollingTarget,
|
|
arg.TargetPublicKey,
|
|
arg.ActiveRequestID,
|
|
arg.Ver,
|
|
arg.VerChangedAt,
|
|
arg.NoPasswordBlocked,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const updateTelemetryRequest = `-- name: UpdateTelemetryRequest :exec
|
|
UPDATE telemetry_requests SET status = $2, status_reason = $3, password_ct = $4, password_nonce = $5,
|
|
password_failed = $6, updated_at = $7, enabled_at = $8
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateTelemetryRequestParams struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Status string `json:"status"`
|
|
StatusReason string `json:"status_reason"`
|
|
PasswordCt []byte `json:"password_ct"`
|
|
PasswordNonce []byte `json:"password_nonce"`
|
|
PasswordFailed bool `json:"password_failed"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
EnabledAt pgtype.Timestamptz `json:"enabled_at"`
|
|
}
|
|
|
|
func (q *Queries) UpdateTelemetryRequest(ctx context.Context, arg UpdateTelemetryRequestParams) error {
|
|
_, err := q.db.Exec(ctx, updateTelemetryRequest,
|
|
arg.ID,
|
|
arg.Status,
|
|
arg.StatusReason,
|
|
arg.PasswordCt,
|
|
arg.PasswordNonce,
|
|
arg.PasswordFailed,
|
|
arg.UpdatedAt,
|
|
arg.EnabledAt,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const userRequestTargets = `-- name: UserRequestTargets :many
|
|
SELECT DISTINCT target_public_key FROM telemetry_requests WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) UserRequestTargets(ctx context.Context, userID uuid.UUID) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, userRequestTargets, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var target_public_key []byte
|
|
if err := rows.Scan(&target_public_key); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, target_public_key)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|