nixamp 0.9.6 → 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/partyline.d.ts +39 -1
- package/dist/partyline.js +143 -18
- package/dist/server.d.ts +12 -0
- package/dist/server.js +104 -0
- package/package.json +1 -1
- package/src/partyline.ts +149 -20
- package/src/server.ts +111 -0
- package/web/dist/assets/{hls-3VKVEQE3-CyT3WOu5.js → hls-3VKVEQE3-D83F7MHN.js} +1 -1
- package/web/dist/assets/index-Cn1xz1VT.js +1 -0
- package/web/dist/assets/{mpegts-C1Y0UKD_.js → mpegts-C8FXaw1J.js} +1 -1
- package/web/dist/assets/{mpegts-LO6RVLD6-BpeB1zGZ.js → mpegts-LO6RVLD6-C7akmB6x.js} +1 -1
- package/web/dist/index.html +1 -1
- package/web/dist/sw.js +5 -5
- package/web/dist/assets/index-BGIa9JsR.js +0 -1
package/dist/partyline.d.ts
CHANGED
|
@@ -99,6 +99,23 @@ export declare function roomCodeFrom(entered: unknown): string;
|
|
|
99
99
|
export declare function pacificTime(at: number): string;
|
|
100
100
|
/** How a code is read back: one digit at a time, because 482917 is not a number. */
|
|
101
101
|
export declare function spokenCode(code: string): string;
|
|
102
|
+
/**
|
|
103
|
+
* A track's name as it should be read out.
|
|
104
|
+
*
|
|
105
|
+
* What is playing is a filename more often than a title: "02 - ...And Justice
|
|
106
|
+
* For All.mp3" read aloud is "zero two dash dot dot dot", and the extension is
|
|
107
|
+
* a noise the caller does not need. The track number and extension go; what
|
|
108
|
+
* is left is close enough to a title to say.
|
|
109
|
+
*/
|
|
110
|
+
export declare function spokenTitle(nowPlaying: string): string;
|
|
111
|
+
/**
|
|
112
|
+
* How many people are in a room, said to the one who just walked in.
|
|
113
|
+
*
|
|
114
|
+
* The count includes them: "there are 3 people in here" is what you say to the
|
|
115
|
+
* third person, and "you are the first one here" is what you say to the first,
|
|
116
|
+
* who would otherwise be told there is one person in an empty room.
|
|
117
|
+
*/
|
|
118
|
+
export declare function peopleHere(callers: number): string;
|
|
102
119
|
export interface TelnyxEvent {
|
|
103
120
|
event_type?: string;
|
|
104
121
|
payload?: Record<string, unknown>;
|
|
@@ -119,6 +136,10 @@ export declare class PartyLine {
|
|
|
119
136
|
private readonly legFrom;
|
|
120
137
|
/** Legs that heard "press 1", and which stream they would be reminded about. */
|
|
121
138
|
private readonly pendingReminder;
|
|
139
|
+
/** How many times each leg has been asked for a code, so it is not forever. */
|
|
140
|
+
private readonly asks;
|
|
141
|
+
/** Legs whose voice failed once, which hear the plain one from then on. */
|
|
142
|
+
private readonly plainVoice;
|
|
122
143
|
/** Who to text when a stream returns, by stream code. */
|
|
123
144
|
private readonly reminders;
|
|
124
145
|
/**
|
|
@@ -216,9 +237,24 @@ export declare class PartyLine {
|
|
|
216
237
|
}): Promise<number>;
|
|
217
238
|
/** How many numbers are waiting to hear that a code is live. */
|
|
218
239
|
waitingOn(code: string): number;
|
|
219
|
-
/**
|
|
240
|
+
/**
|
|
241
|
+
* Put a leg into a room, making the conference if it is the first one there.
|
|
242
|
+
*
|
|
243
|
+
* `welcome` is what the caller hears once they are in, before the count of
|
|
244
|
+
* who else is; a stream's room names the stream, an ordinary room reads its
|
|
245
|
+
* code back.
|
|
246
|
+
*/
|
|
220
247
|
private join;
|
|
221
248
|
private enter;
|
|
249
|
+
/**
|
|
250
|
+
* Say hello to somebody who just joined, and only to them.
|
|
251
|
+
*
|
|
252
|
+
* Spoken into the conference rather than at the leg, because the leg is in
|
|
253
|
+
* the conference now and a speak on it is what the join interrupts. Telnyx
|
|
254
|
+
* addresses conference speech to particular participants, so the others in
|
|
255
|
+
* the room do not hear every arrival welcomed.
|
|
256
|
+
*/
|
|
257
|
+
private greet;
|
|
222
258
|
/**
|
|
223
259
|
* How many people are on the phone for a stream.
|
|
224
260
|
*
|
|
@@ -239,6 +275,8 @@ export declare class PartyLine {
|
|
|
239
275
|
*/
|
|
240
276
|
private room;
|
|
241
277
|
private get voice();
|
|
278
|
+
/** The voice for this leg: the good one, unless it has already failed them. */
|
|
279
|
+
private voiceFor;
|
|
242
280
|
private get maxParticipants();
|
|
243
281
|
/** One call-control command. True when Telnyx accepted it. */
|
|
244
282
|
private command;
|
package/dist/partyline.js
CHANGED
|
@@ -40,6 +40,33 @@ const TELNYX_API = "https://api.telnyx.com/v2";
|
|
|
40
40
|
* configuration rather than a constant for exactly that reason.
|
|
41
41
|
*/
|
|
42
42
|
const DEFAULT_VOICE = "Telnyx.KokoroTTS.af";
|
|
43
|
+
/**
|
|
44
|
+
* The voice a leg falls back to when the good one fails.
|
|
45
|
+
*
|
|
46
|
+
* Telnyx's Kokoro voice answered a prompt with a 500 once (2026-09-09), and
|
|
47
|
+
* what the caller got was a gather with no speech in it, ended at once with
|
|
48
|
+
* no digits, and asked again -- a line that "just repeats itself", silently,
|
|
49
|
+
* every ninety seconds. The plain Telnyx voice is older and worse and has not
|
|
50
|
+
* been seen to fail, which is the quality that matters on the second try.
|
|
51
|
+
*/
|
|
52
|
+
const FALLBACK_VOICE = "female";
|
|
53
|
+
/**
|
|
54
|
+
* How long a caller has between digits before the code is treated as done.
|
|
55
|
+
*
|
|
56
|
+
* Telnyx's default is five seconds and did not fire: a caller who keyed five
|
|
57
|
+
* digits (one was lost in the keypad tone over the prompt) waited ten seconds
|
|
58
|
+
* in silence and hung up. Set explicitly so the partial code comes back to us
|
|
59
|
+
* quickly and we can say how many digits we got.
|
|
60
|
+
*/
|
|
61
|
+
const INTER_DIGIT_MS = 4000;
|
|
62
|
+
/**
|
|
63
|
+
* How many times a caller is asked for a code before being let go.
|
|
64
|
+
*
|
|
65
|
+
* A gather that ends with nothing three times is a caller who cannot or will
|
|
66
|
+
* not key a code -- or a voice that is not being heard at all. Asking a
|
|
67
|
+
* fourth time is the loop that was reported; saying goodbye is not.
|
|
68
|
+
*/
|
|
69
|
+
const MAX_ASKS = 3;
|
|
43
70
|
/** How long a signed webhook stays acceptable. Telnyx's own SDKs use five minutes. */
|
|
44
71
|
const SIGNATURE_TOLERANCE_MS = 5 * 60 * 1000;
|
|
45
72
|
/** A conference Telnyx will discard on its own, so we stop trusting ours first. */
|
|
@@ -92,6 +119,35 @@ export function pacificTime(at) {
|
|
|
92
119
|
export function spokenCode(code) {
|
|
93
120
|
return code.split("").join(", ");
|
|
94
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* A track's name as it should be read out.
|
|
124
|
+
*
|
|
125
|
+
* What is playing is a filename more often than a title: "02 - ...And Justice
|
|
126
|
+
* For All.mp3" read aloud is "zero two dash dot dot dot", and the extension is
|
|
127
|
+
* a noise the caller does not need. The track number and extension go; what
|
|
128
|
+
* is left is close enough to a title to say.
|
|
129
|
+
*/
|
|
130
|
+
export function spokenTitle(nowPlaying) {
|
|
131
|
+
return nowPlaying
|
|
132
|
+
.replace(/\.[a-z0-9]{2,4}$/i, "")
|
|
133
|
+
.replace(/^\s*\d{1,3}\s*[-._]\s*/, "")
|
|
134
|
+
.replace(/^[\s.]+/, "")
|
|
135
|
+
.trim();
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* How many people are in a room, said to the one who just walked in.
|
|
139
|
+
*
|
|
140
|
+
* The count includes them: "there are 3 people in here" is what you say to the
|
|
141
|
+
* third person, and "you are the first one here" is what you say to the first,
|
|
142
|
+
* who would otherwise be told there is one person in an empty room.
|
|
143
|
+
*/
|
|
144
|
+
export function peopleHere(callers) {
|
|
145
|
+
if (callers <= 1)
|
|
146
|
+
return "You are the first one here. Say hello when somebody joins.";
|
|
147
|
+
if (callers === 2)
|
|
148
|
+
return "There is one other person in here. Say hello.";
|
|
149
|
+
return `There are ${callers} people in here. Say hello.`;
|
|
150
|
+
}
|
|
95
151
|
/**
|
|
96
152
|
* Telnyx signs `${timestamp}|${body}` with ed25519 and sends both back in
|
|
97
153
|
* headers. Node will not take a bare 32-byte key, so it is wrapped in the
|
|
@@ -135,6 +191,10 @@ export class PartyLine {
|
|
|
135
191
|
legFrom = new Map();
|
|
136
192
|
/** Legs that heard "press 1", and which stream they would be reminded about. */
|
|
137
193
|
pendingReminder = new Map();
|
|
194
|
+
/** How many times each leg has been asked for a code, so it is not forever. */
|
|
195
|
+
asks = new Map();
|
|
196
|
+
/** Legs whose voice failed once, which hear the plain one from then on. */
|
|
197
|
+
plainVoice = new Set();
|
|
138
198
|
/** Who to text when a stream returns, by stream code. */
|
|
139
199
|
reminders = new Map();
|
|
140
200
|
/**
|
|
@@ -244,8 +304,20 @@ export class PartyLine {
|
|
|
244
304
|
await this.ask(leg);
|
|
245
305
|
return;
|
|
246
306
|
}
|
|
307
|
+
if (type === "call.speak.failed") {
|
|
308
|
+
// The voice, not the caller, failed. Everything this leg hears from now
|
|
309
|
+
// on is in the plain voice; the gather this speech belonged to ends on
|
|
310
|
+
// its own and is asked again, audibly this time.
|
|
311
|
+
this.plainVoice.add(leg);
|
|
312
|
+
this.options.onEvent?.(" a prompt could not be spoken; using the plain voice.");
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
247
315
|
if (type === "call.gather.ended") {
|
|
248
316
|
const digits = typeof payload["digits"] === "string" ? payload["digits"] : "";
|
|
317
|
+
// A gather that ended because the caller hung up is not an answer, and
|
|
318
|
+
// anything sent to that leg now is a 422 for the log.
|
|
319
|
+
if (payload["status"] === "call_hangup")
|
|
320
|
+
return;
|
|
249
321
|
// A leg that was just offered a reminder is answering that, not keying a
|
|
250
322
|
// room code -- the same event carries both, so the question we asked is
|
|
251
323
|
// what decides how to read it.
|
|
@@ -259,9 +331,14 @@ export class PartyLine {
|
|
|
259
331
|
if (!code) {
|
|
260
332
|
// Re-ask rather than guess. Anything that is not six digits is not a
|
|
261
333
|
// room, and picking the nearest one would be picking a stranger's.
|
|
262
|
-
|
|
334
|
+
// Say what we got: five digits and silence is a caller who thinks
|
|
335
|
+
// the line is broken, and a caller who keyed nothing does not need
|
|
336
|
+
// telling their nothing was not six digits.
|
|
337
|
+
const got = digits.replace(/\D/g, "").length;
|
|
338
|
+
await this.ask(leg, got > 0 ? `I only got ${got} digits. ` : "I did not get a code. ");
|
|
263
339
|
return;
|
|
264
340
|
}
|
|
341
|
+
this.asks.delete(leg);
|
|
265
342
|
// A code that belongs to a stream is answered as a stream. Anything else
|
|
266
343
|
// is an ordinary room, which is what this line was before.
|
|
267
344
|
if (await this.stream(leg, code))
|
|
@@ -273,6 +350,8 @@ export class PartyLine {
|
|
|
273
350
|
this.release(leg);
|
|
274
351
|
this.legFrom.delete(leg);
|
|
275
352
|
this.pendingReminder.delete(leg);
|
|
353
|
+
this.asks.delete(leg);
|
|
354
|
+
this.plainVoice.delete(leg);
|
|
276
355
|
return;
|
|
277
356
|
}
|
|
278
357
|
}
|
|
@@ -290,16 +369,33 @@ export class PartyLine {
|
|
|
290
369
|
* to press anything after; # is there for the ones who do it anyway.
|
|
291
370
|
*/
|
|
292
371
|
async ask(leg, prefix = "") {
|
|
293
|
-
const
|
|
294
|
-
|
|
372
|
+
const asked = (this.asks.get(leg) ?? 0) + 1;
|
|
373
|
+
this.asks.set(leg, asked);
|
|
374
|
+
if (asked > MAX_ASKS) {
|
|
375
|
+
// Three gathers with no code in them is not a caller who needs a fourth
|
|
376
|
+
// prompt. Whatever is wrong -- their keypad, our voice -- repeating
|
|
377
|
+
// ourselves is the failure that was reported, so this ends instead.
|
|
378
|
+
await this.command(leg, "speak", {
|
|
379
|
+
payload: "I did not get a room code. Goodbye.",
|
|
380
|
+
voice: this.voiceFor(leg),
|
|
381
|
+
});
|
|
382
|
+
await this.command(leg, "hangup", {});
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
// Short, because callers key the code over the prompt and a long one
|
|
386
|
+
// costs digits: a tone pressed as the speech starts was not heard. The
|
|
387
|
+
// first time gets the welcome; a re-ask has already been welcomed.
|
|
388
|
+
const greeting = this.options.greeting ?? "Welcome to the nixamp party line. Enter the six digit room code.";
|
|
389
|
+
const payload = asked === 1 ? `${prefix}${greeting}` : `${prefix}Enter the six digit room code.`;
|
|
295
390
|
await this.command(leg, "gather_using_speak", {
|
|
296
|
-
payload
|
|
297
|
-
voice: this.
|
|
391
|
+
payload,
|
|
392
|
+
voice: this.voiceFor(leg),
|
|
298
393
|
valid_digits: "0123456789",
|
|
299
394
|
minimum_digits: CODE_LENGTH,
|
|
300
395
|
maximum_digits: CODE_LENGTH,
|
|
301
396
|
terminating_digit: "#",
|
|
302
397
|
timeout_millis: 20000,
|
|
398
|
+
inter_digit_timeout_millis: INTER_DIGIT_MS,
|
|
303
399
|
});
|
|
304
400
|
}
|
|
305
401
|
/**
|
|
@@ -324,13 +420,13 @@ export class PartyLine {
|
|
|
324
420
|
return false;
|
|
325
421
|
const live = streams.liveByCode(code);
|
|
326
422
|
if (live !== undefined) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
}
|
|
333
|
-
await this.join(leg, code);
|
|
423
|
+
// The welcome is said once they are in the room, not before: a speak
|
|
424
|
+
// on the leg followed by the conference join was cut off by the join --
|
|
425
|
+
// Telnyx reported it started and ended in the same millisecond -- and
|
|
426
|
+
// the caller heard twenty seconds of nothing and hung up.
|
|
427
|
+
const title = spokenTitle(live.nowPlaying);
|
|
428
|
+
const what = title ? `, playing ${title}` : "";
|
|
429
|
+
await this.join(leg, code, `Welcome to the live room for ${live.name}${what}. `);
|
|
334
430
|
return true;
|
|
335
431
|
}
|
|
336
432
|
const ended = streams.endedByCode(code);
|
|
@@ -345,7 +441,7 @@ export class PartyLine {
|
|
|
345
441
|
`The live stream ended at ${pacificTime(ended.endedAt)}. ` +
|
|
346
442
|
"Call back later when they stream again. " +
|
|
347
443
|
"Press 1 to get a text message when they do.",
|
|
348
|
-
voice: this.
|
|
444
|
+
voice: this.voiceFor(leg),
|
|
349
445
|
valid_digits: "1",
|
|
350
446
|
minimum_digits: 1,
|
|
351
447
|
maximum_digits: 1,
|
|
@@ -370,7 +466,7 @@ export class PartyLine {
|
|
|
370
466
|
this.options.onEvent?.(` a caller asked to be told when ${code} is live again.`);
|
|
371
467
|
await this.command(leg, "speak", {
|
|
372
468
|
payload: "Got it. We will text you when they are live again. Goodbye.",
|
|
373
|
-
voice: this.
|
|
469
|
+
voice: this.voiceFor(leg),
|
|
374
470
|
});
|
|
375
471
|
await this.command(leg, "hangup", {});
|
|
376
472
|
}
|
|
@@ -408,13 +504,19 @@ export class PartyLine {
|
|
|
408
504
|
waitingOn(code) {
|
|
409
505
|
return this.reminders.get(code)?.size ?? 0;
|
|
410
506
|
}
|
|
411
|
-
/**
|
|
412
|
-
|
|
507
|
+
/**
|
|
508
|
+
* Put a leg into a room, making the conference if it is the first one there.
|
|
509
|
+
*
|
|
510
|
+
* `welcome` is what the caller hears once they are in, before the count of
|
|
511
|
+
* who else is; a stream's room names the stream, an ordinary room reads its
|
|
512
|
+
* code back.
|
|
513
|
+
*/
|
|
514
|
+
async join(leg, code, welcome = `Welcome to room ${spokenCode(code)}. `) {
|
|
413
515
|
const room = this.room(code);
|
|
414
516
|
if (room.callers >= this.maxParticipants) {
|
|
415
517
|
await this.command(leg, "speak", {
|
|
416
518
|
payload: "That room is full. Goodbye.",
|
|
417
|
-
voice: this.
|
|
519
|
+
voice: this.voiceFor(leg),
|
|
418
520
|
});
|
|
419
521
|
await this.command(leg, "hangup", {});
|
|
420
522
|
return;
|
|
@@ -427,6 +529,7 @@ export class PartyLine {
|
|
|
427
529
|
const joined = await this.request(`/conferences/${encodeURIComponent(room.conferenceId)}/actions/join`, { call_control_id: leg, start_conference_on_enter: true });
|
|
428
530
|
if (joined !== null) {
|
|
429
531
|
this.enter(room, leg);
|
|
532
|
+
await this.greet(room, leg, welcome);
|
|
430
533
|
return;
|
|
431
534
|
}
|
|
432
535
|
// The id was stale in a way the clock did not predict -- an operator
|
|
@@ -445,7 +548,7 @@ export class PartyLine {
|
|
|
445
548
|
if (typeof id !== "string") {
|
|
446
549
|
await this.command(leg, "speak", {
|
|
447
550
|
payload: "Sorry, that room could not be opened. Goodbye.",
|
|
448
|
-
voice: this.
|
|
551
|
+
voice: this.voiceFor(leg),
|
|
449
552
|
});
|
|
450
553
|
await this.command(leg, "hangup", {});
|
|
451
554
|
this.legRoom.delete(leg);
|
|
@@ -454,6 +557,7 @@ export class PartyLine {
|
|
|
454
557
|
room.conferenceId = id;
|
|
455
558
|
room.startedAt = this.now();
|
|
456
559
|
this.enter(room, leg);
|
|
560
|
+
await this.greet(room, leg, welcome);
|
|
457
561
|
}
|
|
458
562
|
enter(room, leg) {
|
|
459
563
|
if (room.legs.has(leg))
|
|
@@ -462,6 +566,23 @@ export class PartyLine {
|
|
|
462
566
|
room.callers = room.legs.size;
|
|
463
567
|
this.options.onEvent?.(` a caller joined a room (${room.callers} on the line).`);
|
|
464
568
|
}
|
|
569
|
+
/**
|
|
570
|
+
* Say hello to somebody who just joined, and only to them.
|
|
571
|
+
*
|
|
572
|
+
* Spoken into the conference rather than at the leg, because the leg is in
|
|
573
|
+
* the conference now and a speak on it is what the join interrupts. Telnyx
|
|
574
|
+
* addresses conference speech to particular participants, so the others in
|
|
575
|
+
* the room do not hear every arrival welcomed.
|
|
576
|
+
*/
|
|
577
|
+
async greet(room, leg, welcome) {
|
|
578
|
+
if (room.conferenceId === null)
|
|
579
|
+
return;
|
|
580
|
+
await this.request(`/conferences/${encodeURIComponent(room.conferenceId)}/actions/speak`, {
|
|
581
|
+
payload: `${welcome}${peopleHere(room.callers)}`,
|
|
582
|
+
voice: this.voiceFor(leg),
|
|
583
|
+
call_control_ids: [leg],
|
|
584
|
+
});
|
|
585
|
+
}
|
|
465
586
|
/**
|
|
466
587
|
* How many people are on the phone for a stream.
|
|
467
588
|
*
|
|
@@ -515,6 +636,10 @@ export class PartyLine {
|
|
|
515
636
|
get voice() {
|
|
516
637
|
return this.options.voice ?? DEFAULT_VOICE;
|
|
517
638
|
}
|
|
639
|
+
/** The voice for this leg: the good one, unless it has already failed them. */
|
|
640
|
+
voiceFor(leg) {
|
|
641
|
+
return this.plainVoice.has(leg) ? FALLBACK_VOICE : this.voice;
|
|
642
|
+
}
|
|
518
643
|
get maxParticipants() {
|
|
519
644
|
return this.options.maxParticipants ?? 50;
|
|
520
645
|
}
|
package/dist/server.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { Catalogs } from "./catalogs.ts";
|
|
|
17
17
|
import { Names } from "./names.ts";
|
|
18
18
|
import { Certs } from "./certs.ts";
|
|
19
19
|
import { type Throttle } from "@profullstack/throttle";
|
|
20
|
+
import { type Notification } from "./notify.ts";
|
|
20
21
|
import { type Tools, type Track } from "./audio.ts";
|
|
21
22
|
import { type Command, type RemoteTrack, type Snapshot } from "./protocol.ts";
|
|
22
23
|
export declare const SERVE_BAND_COUNT = 24;
|
|
@@ -536,6 +537,17 @@ export interface HandlerOptions {
|
|
|
536
537
|
follows?: Follows;
|
|
537
538
|
/** The servers an account hearted. nixamp.com only, like follows. */
|
|
538
539
|
favorites?: Favorites;
|
|
540
|
+
/**
|
|
541
|
+
* How an invite is sent: by email, by text, and the site the watch link is
|
|
542
|
+
* built on. nixamp.com only; a personal nixamp has no mail to send from.
|
|
543
|
+
*/
|
|
544
|
+
invites?: {
|
|
545
|
+
email?: (to: string, note: Notification) => Promise<boolean>;
|
|
546
|
+
sms?: {
|
|
547
|
+
send(to: string, text: string): Promise<boolean>;
|
|
548
|
+
};
|
|
549
|
+
site: string;
|
|
550
|
+
};
|
|
539
551
|
/** Names under `<handle>.<zone>` for an account's servers. nixamp.com only. */
|
|
540
552
|
names?: Names;
|
|
541
553
|
/** One wildcard certificate per handle, issued and renewed here. nixamp.com only. */
|
package/dist/server.js
CHANGED
|
@@ -47,6 +47,7 @@ import { forbiddenLibrary, readLibrary } from "./library.js";
|
|
|
47
47
|
import { createThrottle, presentedCredential } from "@profullstack/throttle";
|
|
48
48
|
import { Durable } from "./durable.js";
|
|
49
49
|
import { notifyAll, resendEmail, webPush } from "./notify.js";
|
|
50
|
+
import { inviteSubject, inviteText, isEmail, isPhone, watchLink } from "./invite.js";
|
|
50
51
|
import { confirm, DEFAULT_DIRECTORY, Publisher } from "./publish.js";
|
|
51
52
|
import { applyRemoteConfig, createPaywall, FREE_LISTENERS, paywallFromEnv, } from "./paywall.js";
|
|
52
53
|
import { isRemote, playsInBrowser, sourceLabel } from "./sources.js";
|
|
@@ -1066,6 +1067,80 @@ export function createHandler(engine, options) {
|
|
|
1066
1067
|
json(response, 202, { status: "issuing", host });
|
|
1067
1068
|
return;
|
|
1068
1069
|
}
|
|
1070
|
+
// --- an invite: "so-and-so is streaming", by text or by email ------------
|
|
1071
|
+
//
|
|
1072
|
+
// The page had a Send button and nothing answered it: the message was
|
|
1073
|
+
// written (invite.ts) and never given a route. Sending needs an account,
|
|
1074
|
+
// because a text costs money and lands on somebody's phone, and the link
|
|
1075
|
+
// sent is always the listen link -- an admin link handed out by mistake
|
|
1076
|
+
// would hand out the server.
|
|
1077
|
+
if (path === "/api/v1/invite" && options.invites && options.accounts) {
|
|
1078
|
+
if (request.method !== "POST") {
|
|
1079
|
+
json(response, 405, { error: "POST only" });
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
const me = await options.accounts.whoIs(tokenFrom(request.headers));
|
|
1083
|
+
if (me === null) {
|
|
1084
|
+
json(response, 401, { error: "sign in to send an invite" });
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
let body = {};
|
|
1088
|
+
try {
|
|
1089
|
+
body = JSON.parse(await readBody(request));
|
|
1090
|
+
}
|
|
1091
|
+
catch {
|
|
1092
|
+
json(response, 400, { error: "bad JSON" });
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
const to = String(body.to ?? "").trim();
|
|
1096
|
+
const stream = String(body.stream ?? "").trim();
|
|
1097
|
+
const byEmail = isEmail(to);
|
|
1098
|
+
const bySms = !byEmail && isPhone(to);
|
|
1099
|
+
if (!byEmail && !bySms) {
|
|
1100
|
+
json(response, 400, { error: "give a phone number or an email address" });
|
|
1101
|
+
return;
|
|
1102
|
+
}
|
|
1103
|
+
let origin = "";
|
|
1104
|
+
try {
|
|
1105
|
+
origin = new URL(stream).origin;
|
|
1106
|
+
}
|
|
1107
|
+
catch {
|
|
1108
|
+
json(response, 400, { error: "that is not a stream link" });
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
// What the directory knows about this server names it and gives the
|
|
1112
|
+
// listen link and the phone code. An unlisted server is still sendable,
|
|
1113
|
+
// as long as the link given is not the one that drives it.
|
|
1114
|
+
const listed = options.directory?.list().find((one) => {
|
|
1115
|
+
try {
|
|
1116
|
+
return new URL(one.url).origin === origin;
|
|
1117
|
+
}
|
|
1118
|
+
catch {
|
|
1119
|
+
return false;
|
|
1120
|
+
}
|
|
1121
|
+
});
|
|
1122
|
+
const link = listed?.url ?? stream;
|
|
1123
|
+
if (/\/admin\//.test(link) || /[?&]k=/.test(link) && !listed) {
|
|
1124
|
+
json(response, 400, { error: "send the view link, not the admin link" });
|
|
1125
|
+
return;
|
|
1126
|
+
}
|
|
1127
|
+
const invite = {
|
|
1128
|
+
name: listed?.name ?? new URL(link).hostname,
|
|
1129
|
+
link: watchLink(link, options.invites.site),
|
|
1130
|
+
phone: listed ? CALL_IN_NUMBER : "",
|
|
1131
|
+
code: listed?.code ?? "",
|
|
1132
|
+
};
|
|
1133
|
+
const sender = byEmail ? options.invites.email : options.invites.sms;
|
|
1134
|
+
if (!sender) {
|
|
1135
|
+
json(response, 503, { error: byEmail ? "this site cannot send email yet" : "this site cannot send texts yet" });
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
const ok = byEmail
|
|
1139
|
+
? await options.invites.email(to, { title: inviteSubject(invite), body: inviteText(invite), url: invite.link })
|
|
1140
|
+
: await options.invites.sms.send(to, inviteText(invite));
|
|
1141
|
+
json(response, ok ? 200 : 502, ok ? { sent: to } : { error: "the message did not go" });
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1069
1144
|
// Favourites: the servers you hearted, kept against your account. Reading
|
|
1070
1145
|
// the directory and listening need no account; remembering where you
|
|
1071
1146
|
// listened does, because there has to be somebody to remember it for.
|
|
@@ -3266,6 +3341,8 @@ export async function serve(argv, version = "0.1.0") {
|
|
|
3266
3341
|
// a guess with an Authorization header buys itself the bigger budget.
|
|
3267
3342
|
{ path: "/api/v1/auth/", limit: 20, credential: false },
|
|
3268
3343
|
{ path: "/api/v1/dns/", limit: 30 },
|
|
3344
|
+
// A text costs money and lands on a phone: ten a minute is plenty.
|
|
3345
|
+
{ path: "/api/v1/invite", limit: 10 },
|
|
3269
3346
|
{ path: "/api/v1/dns", limit: 30 },
|
|
3270
3347
|
{ path: "/api/v1/certs", limit: 30 },
|
|
3271
3348
|
{ path: "/api/health", open: true },
|
|
@@ -3538,6 +3615,33 @@ export async function serve(argv, version = "0.1.0") {
|
|
|
3538
3615
|
...(directory ? { directory } : {}),
|
|
3539
3616
|
...(follows ? { follows, vapidPublicKey } : {}),
|
|
3540
3617
|
...(favorites ? { favorites } : {}),
|
|
3618
|
+
// Invites go out the same way follow notifications do, and only from a
|
|
3619
|
+
// site that has somebody to send them for.
|
|
3620
|
+
...(pool
|
|
3621
|
+
? {
|
|
3622
|
+
invites: {
|
|
3623
|
+
site: (process.env["NIXAMP_SITE"] ?? DEFAULT_DIRECTORY).replace(/\/+$/, ""),
|
|
3624
|
+
...(process.env["RESEND_API_KEY"]
|
|
3625
|
+
? {
|
|
3626
|
+
email: resendEmail({
|
|
3627
|
+
apiKey: process.env["RESEND_API_KEY"],
|
|
3628
|
+
from: process.env["NIXAMP_MAIL_FROM"] ?? "nixamp <notifications@nixamp.com>",
|
|
3629
|
+
onEvent: (message) => console.log(message),
|
|
3630
|
+
}),
|
|
3631
|
+
}
|
|
3632
|
+
: {}),
|
|
3633
|
+
...(process.env["TELNYX_API_KEY"] && process.env["PARTYLINE_SMS_FROM"]
|
|
3634
|
+
? {
|
|
3635
|
+
sms: telnyxSms({
|
|
3636
|
+
apiKey: process.env["TELNYX_API_KEY"],
|
|
3637
|
+
from: process.env["PARTYLINE_SMS_FROM"],
|
|
3638
|
+
onEvent: (message) => console.log(message),
|
|
3639
|
+
}),
|
|
3640
|
+
}
|
|
3641
|
+
: {}),
|
|
3642
|
+
},
|
|
3643
|
+
}
|
|
3644
|
+
: {}),
|
|
3541
3645
|
...(names ? { names } : {}),
|
|
3542
3646
|
...(certs ? { certs } : {}),
|
|
3543
3647
|
dnsZone: zoneName,
|