Overflow Tap: speak Overflow's Dashboard API, as its own helper app does
The first cut guessed a write route on Overflow's public API, which documents
only reads for Tap. With the helper app's bundle in hand (Overflow ProPresenter
Integration 1.2.3, read 2026-10-11) the module now makes exactly the calls the
helper makes, against the Dashboard's own backend:
POST /nonprofit-dashboard/authentication/login/jwt a Dashboard sign-in → JWT
GET /nonprofit-dashboard/propresenter/nonprofits/{id}/keywords
the Keyword Mappings
POST …/nonprofits/{id}/keywords?syncNonprofit=true { keywords } = the switch
So the keyword → destination → Tap groups mapping stays where Overflow keeps
it (Dashboard › Integrations › ProPresenter › Keyword Mappings) and is read
from there: on start, every five minutes, on the tile's refresh button, and
from every trigger's answer. Admin config shrinks to the ProPresenter endpoint,
the Overflow Dashboard login (plus a Nonprofit ID only for an @overflow.co
staff login) and the alert switch; the API-key, Tap-groups, keyword-text and
match-mode fields are gone.
Behaviour follows the helper: both the presentation layer (/v1/status/slide)
and the announcement layer (/v1/announcement/slide_index + /active) are
watched; when a layer's set of matched keywords changes to a non-empty set it
is sent; matching is per word, case-insensitive, punctuation around the word
ignored. The session token is cached across re-inits for the same login so an
admin saving config repeatedly cannot trip Overflow's "too many attempts";
a 401 signs in again once; a refused sign-in is not retried within 20 s.
tools/overflow-tap-mock.js now plays the Dashboard API (any email/password
signs in; --fail-switch, --expire=<s>, --login=<email>:<password>) and gives
the mock ProPresenter an announcement layer (--no-announcements for an older
one). Verified against it: sign-in, keyword load, triggers on both layers,
token expiry → re-login, 500 → NOT SWITCHED + alert + retries, wrong login →
the reason on the tile and in the health line, and recovery. keywords.selftest.js
pins the matching rule (8 cases).
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
6b51044be6
commit
459d8b9c38
@@ -1,78 +1,104 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Overflow API client — the Tap part of Overflow's public REST API.
|
||||
* Overflow Dashboard API — the calls Overflow's own "ProPresenter Integration"
|
||||
* helper app (1.2.3) makes, read from its bundle on 2026-10-11. None of this is
|
||||
* in Overflow's public API reference (which documents only reads for Tap): it
|
||||
* is the Dashboard's own backend, used with a Dashboard login — the same
|
||||
* email and password the helper asks for. Google sign-in does not work here;
|
||||
* the account needs a password (Overflow's article says the same).
|
||||
*
|
||||
* https://docs.overflow.co/api-reference (OpenAPI: server.stage.overflow.co/api/docs/openapi.json)
|
||||
* POST /nonprofit-dashboard/authentication/login/jwt
|
||||
* { email, password } → { token } a JWT; its payload carries
|
||||
* email, nonprofitId, permissions [{ action, subject }] (subject TAP
|
||||
* or all is required), nonprofits [...], exp
|
||||
* GET /nonprofit-dashboard/propresenter/nonprofits/{nonprofitId}/keywords
|
||||
* → [ { keyword, destinationName, tapGroups: [{ id, tapGroupName }], … } ]
|
||||
* the Dashboard's Integrations › ProPresenter › Keyword Mappings
|
||||
* POST /nonprofit-dashboard/propresenter/nonprofits/{nonprofitId}/keywords?syncNonprofit=true
|
||||
* { keywords: ["give"] } → the same list, updated THE SWITCH:
|
||||
* Overflow points the keyword's groups at its destination
|
||||
* GET /nonprofit-dashboard/propresenter/nonprofits?search=&limit=
|
||||
* POST /nonprofit-dashboard/propresenter/nonprofits/{nonprofitId}/select
|
||||
* only for an @overflow.co staff login, which has no nonprofit of its
|
||||
* own and must pick one first
|
||||
*
|
||||
* Authentication is two headers, `x-client-id` and `x-api-key`, from the
|
||||
* Overflow Dashboard's Developers › API Keys. 120 requests per minute per
|
||||
* client; a 429 is answered when the window is spent.
|
||||
* Bearer auth on everything after login. A 401 means the session is gone —
|
||||
* sign in again. The helper's own wording for a refused login is kept here
|
||||
* (401 invalid email or password, 429 too many attempts, 403 locked).
|
||||
*
|
||||
* Reads used here are documented: GET /api/v3/tap/destinations,
|
||||
* GET /api/v3/tap/groups and GET /api/v3/tap/groups/{id} (a group carries the
|
||||
* `destinationId` it currently points at).
|
||||
*
|
||||
* THE SWITCH IS NOT (as of 2026-10-07). The public reference lists reads only
|
||||
* for Tap; Overflow's own ProPresenter helper changes a group's destination
|
||||
* through the Dashboard's private API after signing in with a Dashboard
|
||||
* login. `setGroupDestination` below sends the natural REST shape for this
|
||||
* API — PATCH the group with the new `destinationId`, as PATCH /donors/{id}
|
||||
* updates a donor — and the server confirms every switch by reading the group
|
||||
* back, so an answer that changes nothing is reported, not believed. If
|
||||
* Overflow publishes (or confirms) a different route, this one function is
|
||||
* the only place to change. tools/overflow-tap-mock.js serves the same shape.
|
||||
*
|
||||
* OVERFLOW_TAP_API_BASE=http://127.0.0.1:24720 points the client at that mock.
|
||||
* OVERFLOW_TAP_API_BASE=http://127.0.0.1:24720 points the client at
|
||||
* tools/overflow-tap-mock.js.
|
||||
*/
|
||||
|
||||
const DEFAULT_API_BASE = 'https://server.overflow.co';
|
||||
const API_BASE = String(process.env.OVERFLOW_TAP_API_BASE || DEFAULT_API_BASE).replace(/\/+$/, '');
|
||||
const DASH = '/nonprofit-dashboard';
|
||||
const TIMEOUT_MS = 10000;
|
||||
const PAGE_SIZE = 100;
|
||||
const MAX_PAGES = 20;
|
||||
const ID_RE = /^[0-9a-f]{24}$/i;
|
||||
const TRIGGER_TIMEOUT_MS = 15000;
|
||||
|
||||
class OverflowError extends Error {
|
||||
constructor(message, { status = 0, retryable = false } = {}) {
|
||||
constructor(message, { status = 0, retryable = false, auth = false } = {}) {
|
||||
super(message);
|
||||
this.name = 'OverflowError';
|
||||
this.status = status;
|
||||
this.retryable = retryable;
|
||||
this.auth = auth; // the session was refused: sign in again before retrying
|
||||
}
|
||||
}
|
||||
|
||||
function looksLikeId(s) {
|
||||
return ID_RE.test(String(s || '').trim());
|
||||
/** The JWT's payload, or null when the token is not one. No signature check — we only read our own session. */
|
||||
function decodeJwt(token) {
|
||||
const parts = String(token || '').split('.');
|
||||
if (parts.length !== 3) return null;
|
||||
try {
|
||||
const b64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
|
||||
return JSON.parse(Buffer.from(b64, 'base64').toString('utf8'));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function createOverflowClient({ clientId, apiKey, fetchImpl = globalThis.fetch }) {
|
||||
const id = String(clientId || '').trim();
|
||||
const key = String(apiKey || '').trim();
|
||||
/** What the module needs to know about a session, from its token. */
|
||||
function describeSession(token) {
|
||||
const p = decodeJwt(token) || {};
|
||||
const email = String(p.email || '');
|
||||
const perms = Array.isArray(p.permissions) ? p.permissions : [];
|
||||
const nonprofits = Array.isArray(p.nonprofits)
|
||||
? p.nonprofits.map((n) => ({ id: String((n && n.id) || ''), name: String((n && n.name) || '') })).filter((n) => n.id)
|
||||
: [];
|
||||
return {
|
||||
email,
|
||||
staff: email.toLowerCase().endsWith('@overflow.co'),
|
||||
nonprofitId: p.nonprofitId ? String(p.nonprofitId) : '',
|
||||
nonprofits,
|
||||
hasTap: perms.some((x) => x && (x.subject === 'TAP' || x.subject === 'all')),
|
||||
expiresAt: Number.isFinite(Number(p.exp)) ? Number(p.exp) * 1000 : 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function request(method, path, { query, body } = {}) {
|
||||
if (!id || !key) throw new OverflowError('Overflow client ID and API key are not set', { status: 0 });
|
||||
function createOverflowClient({ fetchImpl = globalThis.fetch } = {}) {
|
||||
async function request(method, path, { query, body, token, timeout = TIMEOUT_MS } = {}) {
|
||||
const url = new URL(API_BASE + path);
|
||||
for (const [k, v] of Object.entries(query || {})) {
|
||||
if (v === undefined || v === null || v === '') continue;
|
||||
url.searchParams.set(k, String(v));
|
||||
}
|
||||
const headers = { Accept: 'application/json', 'x-client-id': id, 'x-api-key': key };
|
||||
const headers = { Accept: 'application/json' };
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
if (body !== undefined) headers['Content-Type'] = 'application/json';
|
||||
const ctl = new AbortController();
|
||||
const timer = setTimeout(() => ctl.abort(), TIMEOUT_MS);
|
||||
const timer = setTimeout(() => ctl.abort(), timeout);
|
||||
let res;
|
||||
try {
|
||||
res = await fetchImpl(url, {
|
||||
method,
|
||||
headers,
|
||||
body: body === undefined ? undefined : JSON.stringify(body),
|
||||
signal: ctl.signal,
|
||||
});
|
||||
res = await fetchImpl(url, { method, headers, body: body === undefined ? undefined : JSON.stringify(body), signal: ctl.signal });
|
||||
} catch (err) {
|
||||
const aborted = err && err.name === 'AbortError';
|
||||
const cause = err && err.cause && (err.cause.code || err.cause.message);
|
||||
throw new OverflowError(aborted ? `Overflow did not answer within ${TIMEOUT_MS / 1000} s` : `Overflow unreachable: ${err.message}${cause ? ` (${cause})` : ''}`, { retryable: true });
|
||||
throw new OverflowError(
|
||||
aborted ? `Overflow did not answer within ${timeout / 1000} s` : `Overflow unreachable: ${err.message}${cause ? ` (${cause})` : ''}`,
|
||||
{ retryable: true },
|
||||
);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
@@ -83,44 +109,66 @@ function createOverflowClient({ clientId, apiKey, fetchImpl = globalThis.fetch }
|
||||
}
|
||||
if (!res.ok) {
|
||||
const detail = data && (data.message || data.error || data.detail);
|
||||
const what = Array.isArray(detail) ? detail.join('; ') : (detail ? String(detail) : res.statusText);
|
||||
let message;
|
||||
if (res.status === 401 || res.status === 403) message = `Overflow refused the credentials (${res.status}${what ? `: ${what}` : ''})`;
|
||||
else if (res.status === 404 || res.status === 405) message = `Overflow has no ${method} ${path} (${res.status})`;
|
||||
else if (res.status === 429) message = 'Overflow is rate-limiting (120 requests a minute)';
|
||||
else message = `Overflow answered ${res.status}${what ? `: ${what}` : ''}`;
|
||||
throw new OverflowError(message, { status: res.status, retryable: res.status === 429 || res.status >= 500 });
|
||||
const what = Array.isArray(detail) ? detail.join('; ') : (detail ? String(detail) : '');
|
||||
throw new OverflowError(what ? `Overflow answered ${res.status}: ${what}` : `Overflow answered ${res.status} ${res.statusText}`.trim(), {
|
||||
status: res.status,
|
||||
auth: res.status === 401,
|
||||
retryable: res.status === 429 || res.status >= 500,
|
||||
});
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/** Every page of a collection, newest-first as the API sorts it. */
|
||||
async function listAll(path, query = {}) {
|
||||
const all = [];
|
||||
for (let page = 1; page <= MAX_PAGES; page += 1) {
|
||||
const res = await request('GET', path, { query: { ...query, limit: PAGE_SIZE, page } });
|
||||
const data = Array.isArray(res && res.data) ? res.data : [];
|
||||
all.push(...data);
|
||||
const total = Number(res && res.totalCount);
|
||||
if (data.length < PAGE_SIZE || (Number.isFinite(total) && all.length >= total)) break;
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
return {
|
||||
base: API_BASE,
|
||||
configured: Boolean(id && key),
|
||||
listDestinations: () => listAll('/api/v3/tap/destinations'),
|
||||
listGroups: () => listAll('/api/v3/tap/groups'),
|
||||
async getGroup(groupId) {
|
||||
const res = await request('GET', `/api/v3/tap/groups/${encodeURIComponent(groupId)}`);
|
||||
return res && res.data ? res.data : res;
|
||||
|
||||
/** Sign in with a Dashboard login; resolves to the JWT. */
|
||||
async login(email, password) {
|
||||
try {
|
||||
const data = await request('POST', `${DASH}/authentication/login/jwt`, { body: { email, password } });
|
||||
const token = data && data.token;
|
||||
if (!token || typeof token !== 'string') throw new OverflowError('Overflow sign-in answered without a token');
|
||||
return token;
|
||||
} catch (err) {
|
||||
if (!(err instanceof OverflowError) || !err.status) throw err;
|
||||
if (err.status === 401) throw new OverflowError('Overflow refused the sign-in: invalid email or password', { status: 401 });
|
||||
if (err.status === 429) throw new OverflowError('Overflow refused the sign-in: too many attempts — wait a while', { status: 429, retryable: true });
|
||||
if (err.status === 403) throw new OverflowError('Overflow refused the sign-in: account locked', { status: 403 });
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
/** See the note at the top of this file: the one unverified call. */
|
||||
setGroupDestination(groupId, destinationId) {
|
||||
return request('PATCH', `/api/v3/tap/groups/${encodeURIComponent(groupId)}`, { body: { destinationId } });
|
||||
|
||||
/** The Keyword Mappings for a nonprofit, as the Dashboard holds them. */
|
||||
async listKeywords(token, nonprofitId) {
|
||||
const data = await request('GET', `${DASH}/propresenter/nonprofits/${encodeURIComponent(nonprofitId)}/keywords`, { token });
|
||||
return Array.isArray(data) ? data : [];
|
||||
},
|
||||
|
||||
/** Tell Overflow these keywords are on screen; it moves their groups. Resolves to the updated list when one comes back. */
|
||||
async trigger(token, nonprofitId, keywords) {
|
||||
const data = await request('POST', `${DASH}/propresenter/nonprofits/${encodeURIComponent(nonprofitId)}/keywords`, {
|
||||
query: { syncNonprofit: 'true' },
|
||||
body: { keywords },
|
||||
token,
|
||||
timeout: TRIGGER_TIMEOUT_MS,
|
||||
});
|
||||
return Array.isArray(data) ? data : null;
|
||||
},
|
||||
|
||||
/** Staff logins only: the nonprofits this login may act for. */
|
||||
async searchNonprofits(token, search = '', limit = 10) {
|
||||
const data = await request('GET', `${DASH}/propresenter/nonprofits`, { token, query: { search, limit } });
|
||||
const list = (data && data.data && data.data.nonprofits) || (data && data.nonprofits) || (data && data.data) || data;
|
||||
return Array.isArray(list)
|
||||
? list.map((n) => ({ id: String((n && n.id) || ''), name: String((n && n.name) || ''), slug: String((n && n.slug) || '') }))
|
||||
: [];
|
||||
},
|
||||
|
||||
/** Staff logins only: bind the session to one nonprofit before reading its keywords. */
|
||||
selectNonprofit(token, nonprofitId) {
|
||||
return request('POST', `${DASH}/propresenter/nonprofits/${encodeURIComponent(nonprofitId)}/select`, { token, body: {} });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { createOverflowClient, OverflowError, looksLikeId, API_BASE, DEFAULT_API_BASE };
|
||||
module.exports = { createOverflowClient, OverflowError, decodeJwt, describeSession, API_BASE, DEFAULT_API_BASE };
|
||||
|
||||
Reference in New Issue
Block a user