Add community node socket: devices, tickets, consent gate, kill switch
Phase 3 of plan.md. Device registration with one-time secrets, scoped single-use socket tickets, the lock-owner WS /nodes/ws with handshake and heartbeat eviction, a single batch ConsentGate, and NodesService which disconnects nodes within one tick when consent, device or the nodes_enabled kill switch changes. A reconcile of 2000 sessions measured about 110 ms after replacing per-session queries with batches. Devii may withdraw but never grant bandwidth_sharing. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -43,6 +43,7 @@ from .moderation import (
|
||||
CONSENTS_TABLE,
|
||||
CONSENT_KINDS,
|
||||
CONSENT_STATES,
|
||||
MEMBER_ONLY_CONSENTS,
|
||||
MATURITY_LEVELS,
|
||||
MATURITY_SOURCES,
|
||||
MATURITY_TABLE,
|
||||
@@ -62,6 +63,7 @@ from .moderation import (
|
||||
band_allows_mature,
|
||||
band_allows_restricted,
|
||||
consent_granted,
|
||||
consent_granted_many,
|
||||
consent_state,
|
||||
get_maturity,
|
||||
get_maturity_by_targets,
|
||||
@@ -261,6 +263,7 @@ __all__ = [
|
||||
"AGE_BANDS",
|
||||
"CONSENTS_TABLE",
|
||||
"CONSENT_KINDS",
|
||||
"MEMBER_ONLY_CONSENTS",
|
||||
"CONSENT_STATES",
|
||||
"MATURITY_LEVELS",
|
||||
"MATURITY_SOURCES",
|
||||
@@ -281,6 +284,7 @@ __all__ = [
|
||||
"band_allows_mature",
|
||||
"band_allows_restricted",
|
||||
"consent_granted",
|
||||
"consent_granted_many",
|
||||
"consent_state",
|
||||
"get_maturity",
|
||||
"get_maturity_by_targets",
|
||||
|
||||
@@ -144,6 +144,7 @@ CONSENT_KINDS: dict[str, str] = {
|
||||
),
|
||||
}
|
||||
CONSENT_STATES: tuple[str, ...] = ("granted", "withdrawn")
|
||||
MEMBER_ONLY_CONSENTS: frozenset[str] = frozenset({"bandwidth_sharing"})
|
||||
|
||||
AGE_BANDS: tuple[str, ...] = ("under_min", "13_15", "16_17", "adult")
|
||||
|
||||
@@ -300,6 +301,23 @@ def consent_granted(owner_kind: str, owner_id: str, kind: str) -> bool:
|
||||
return bool(row and row.get("state") == "granted")
|
||||
|
||||
|
||||
def consent_granted_many(owner_kind: str, owner_ids, kind: str) -> set[str]:
|
||||
unique = list(dict.fromkeys(owner_id for owner_id in owner_ids if owner_id))
|
||||
latest: dict[str, str] = {}
|
||||
for start in range(0, len(unique), 500):
|
||||
placeholders, params = _in_clause(unique[start : start + 500])
|
||||
for row in db.query(
|
||||
f"SELECT owner_id, state FROM {CONSENTS_TABLE} WHERE owner_kind = :owner_kind "
|
||||
f"AND kind = :kind AND deleted_at IS NULL AND owner_id IN ({placeholders}) "
|
||||
"ORDER BY created_at, id",
|
||||
owner_kind=owner_kind,
|
||||
kind=kind,
|
||||
**params,
|
||||
):
|
||||
latest[row["owner_id"]] = row["state"]
|
||||
return {owner_id for owner_id, state in latest.items() if state == "granted"}
|
||||
|
||||
|
||||
def set_consent(
|
||||
owner_kind: str, owner_id: str, kind: str, granted: bool, version: str = "1"
|
||||
) -> dict | None:
|
||||
|
||||
@@ -12,7 +12,7 @@ from sqlalchemy import text
|
||||
from devplacepy.config import SECRET_KEY
|
||||
from devplacepy.services.nodes.spec import DEVICE_KINDS, SPEC
|
||||
|
||||
from .core import _now_iso, db, get_table
|
||||
from .core import _in_clause, _now_iso, db, get_table
|
||||
from .soft_delete import soft_delete
|
||||
|
||||
LIMITS = SPEC.limits
|
||||
@@ -23,6 +23,7 @@ SECRET_BYTES = 32
|
||||
LABEL_MAX = 60
|
||||
EVENT_DETAIL_MAX = 500
|
||||
MEGABYTE = 1024 * 1024
|
||||
BATCH_SIZE = 500
|
||||
HOURS_PATTERN = re.compile(r"^([01]\d|2[0-3]):[0-5]\d$")
|
||||
PLATFORMS = tuple(sorted({item.adapter for item in SPEC.destinations}))
|
||||
FLAG_OPEN = "open"
|
||||
@@ -125,6 +126,37 @@ def get_device(device_uid: str) -> dict | None:
|
||||
return get_table("node_devices").find_one(uid=device_uid, deleted_at=None)
|
||||
|
||||
|
||||
def _batches(values) -> list[list[str]]:
|
||||
unique = list(dict.fromkeys(value for value in values if value))
|
||||
return [unique[i : i + BATCH_SIZE] for i in range(0, len(unique), BATCH_SIZE)]
|
||||
|
||||
|
||||
def get_devices_by_uids(device_uids) -> dict[str, dict]:
|
||||
found: dict[str, dict] = {}
|
||||
for batch in _batches(device_uids):
|
||||
placeholders, params = _in_clause(batch)
|
||||
for row in db.query(
|
||||
f"SELECT * FROM node_devices WHERE uid IN ({placeholders}) "
|
||||
"AND deleted_at IS NULL",
|
||||
**params,
|
||||
):
|
||||
found[row["uid"]] = dict(row)
|
||||
return found
|
||||
|
||||
|
||||
def get_enabled_by_users(user_uids) -> dict[str, bool]:
|
||||
found: dict[str, bool] = {}
|
||||
for batch in _batches(user_uids):
|
||||
placeholders, params = _in_clause(batch)
|
||||
for row in db.query(
|
||||
f"SELECT user_uid, enabled FROM node_settings WHERE user_uid IN ({placeholders}) "
|
||||
"AND deleted_at IS NULL",
|
||||
**params,
|
||||
):
|
||||
found[row["user_uid"]] = bool(row["enabled"])
|
||||
return found
|
||||
|
||||
|
||||
def list_devices(user_uid: str) -> list[dict]:
|
||||
return list(
|
||||
get_table("node_devices").find(
|
||||
|
||||
@@ -498,6 +498,8 @@ def init_db():
|
||||
("uid", ""),
|
||||
("token", ""),
|
||||
("user_uid", ""),
|
||||
("scope", ""),
|
||||
("device_uid", ""),
|
||||
("created_at", ""),
|
||||
("expires_at", ""),
|
||||
("used_at", ""),
|
||||
|
||||
Reference in New Issue
Block a user