Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.7 KB
Raw Blame
import type { PushNotification } from '@orca-cloud/push-contract'
import type { PushDatabase } from './push-database.js'
import { parsePushDeliveryPayload } from './push-delivery-payload.js'

export async function reconcileQueuedDismissal(
  tx: PushDatabase,
  host: string,
  registrationId: string,
  notification: PushNotification,
  now: number
): Promise<boolean> {
  if (!notification.notificationId) return false
  const key = [host, notification.notificationEpoch, notification.notificationId]
  const [dismissed] = await tx.query(
    'SELECT notification_seq FROM push_dismissed_events WHERE host_fingerprint = ? AND notification_epoch = ? AND notification_id = ?',
    key
  )
  if (notification.kind !== 'dismiss')
    return Number(dismissed?.notification_seq ?? -1) >= notification.notificationSeq
  await tx.query(
    `INSERT INTO push_dismissed_events(host_fingerprint, notification_epoch, notification_id, notification_seq, created_at)
    VALUES (?, ?, ?, ?, ?) ON CONFLICT(host_fingerprint, notification_epoch, notification_id)
    DO UPDATE SET notification_seq = CASE WHEN push_dismissed_events.notification_seq > excluded.notification_seq THEN push_dismissed_events.notification_seq ELSE excluded.notification_seq END, created_at = excluded.created_at`,
    [...key, notification.notificationSeq, now]
  )
  const deliveries = await tx.query(
    "SELECT batch_id, payload_json FROM push_delivery_batches WHERE host_fingerprint = ? AND registration_id = ? AND kind = 'alert' AND state = 'pending' AND lease_until <= ?",
    [host, registrationId, now]
  )
  for (const delivery of deliveries) {
    const queued = parsePushDeliveryPayload(String(delivery.payload_json))
    if (
      queued.notificationEpoch !== notification.notificationEpoch ||
      queued.notificationId !== notification.notificationId ||
      queued.notificationSeq > notification.notificationSeq
    )
      continue
    // The lease guard re-evaluates after a concurrent claim commits; that alert then goes out and
    // this dismissal is delivered after it.
    await tx.query(
      "DELETE FROM push_delivery_batches WHERE batch_id = ? AND state = 'pending' AND lease_until <= ?",
      [delivery.batch_id, now]
    )
  }
  return false
}

export async function isDismissedAlert(
  tx: PushDatabase,
  host: string,
  notification: PushNotification
): Promise<boolean> {
  if (notification.kind === 'dismiss' || !notification.notificationId) return false
  const rows = await tx.query(
    'SELECT notification_seq FROM push_dismissed_events WHERE host_fingerprint = ? AND notification_epoch = ? AND notification_id = ?',
    [host, notification.notificationEpoch, notification.notificationId]
  )
  return Number(rows[0]?.notification_seq ?? -1) >= notification.notificationSeq
}