@camstack/server 1.2.924 → 1.2.925
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/CHANGELOG.md +6 -0
- package/package.json +4 -12
- package/system/assets/core-notifier/icon.svg +5 -0
- package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.js +1 -1
- package/system/dist/builtins/addon-pages-aggregator/addon-pages-aggregator.addon.mjs +1 -1
- package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.js +1 -1
- package/system/dist/builtins/addon-widgets-aggregator/addon-widgets-aggregator.addon.mjs +1 -1
- package/system/dist/builtins/alerts/alerts.addon.js +1 -1
- package/system/dist/builtins/alerts/alerts.addon.mjs +1 -1
- package/system/dist/builtins/autotrack/index.js +1 -1
- package/system/dist/builtins/autotrack/index.mjs +1 -1
- package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.js +2 -2
- package/system/dist/builtins/backup-orchestrator/backup-orchestrator.addon.mjs +2 -2
- package/system/dist/builtins/calendars/calendars.addon.js +1 -1
- package/system/dist/builtins/calendars/calendars.addon.mjs +1 -1
- package/system/dist/builtins/camera-grid/index.js +1 -1
- package/system/dist/builtins/camera-grid/index.mjs +1 -1
- package/system/dist/builtins/composer/composer.addon.js +4 -4
- package/system/dist/builtins/composer/composer.addon.mjs +4 -4
- package/system/dist/builtins/console-logging/index.js +1 -1
- package/system/dist/builtins/console-logging/index.mjs +1 -1
- package/system/dist/builtins/core-blocks/core-blocks.addon.js +4 -4
- package/system/dist/builtins/core-blocks/core-blocks.addon.mjs +4 -4
- package/system/dist/builtins/core-notifier/core-notifier.addon.d.ts +7 -0
- package/system/dist/builtins/core-notifier/core-notifier.addon.js +549 -0
- package/system/dist/builtins/core-notifier/core-notifier.addon.mjs +543 -0
- package/system/dist/builtins/core-notifier/group-descriptor.d.ts +16 -0
- package/system/dist/builtins/core-notifier/group-instances.d.ts +20 -0
- package/system/dist/builtins/core-notifier/group-notification-output.d.ts +63 -0
- package/system/dist/builtins/core-notifier/index.d.ts +1 -0
- package/system/dist/builtins/core-notifier/index.js +8 -0
- package/system/dist/builtins/core-notifier/index.mjs +2 -0
- package/system/dist/builtins/core-notifier/member-kinds.d.ts +22 -0
- package/system/dist/builtins/core-notifier/member-ledger.d.ts +40 -0
- package/system/dist/builtins/device-history/device-history.addon.js +2 -2
- package/system/dist/builtins/device-history/device-history.addon.mjs +2 -2
- package/system/dist/builtins/device-manager/device-manager.addon.js +3 -3
- package/system/dist/builtins/device-manager/device-manager.addon.mjs +3 -3
- package/system/dist/builtins/hub-forwarder/index.js +1 -1
- package/system/dist/builtins/hub-forwarder/index.mjs +1 -1
- package/system/dist/builtins/keychain/index.js +4 -4
- package/system/dist/builtins/keychain/index.mjs +4 -4
- package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.js +1 -1
- package/system/dist/builtins/liveness-monitor/liveness-monitor.addon.mjs +1 -1
- package/system/dist/builtins/local-auth/local-auth.addon.js +2 -2
- package/system/dist/builtins/local-auth/local-auth.addon.mjs +2 -2
- package/system/dist/builtins/local-network/local-network.addon.js +3 -3
- package/system/dist/builtins/local-network/local-network.addon.mjs +3 -3
- package/system/dist/builtins/loki-logging/index.js +1 -1
- package/system/dist/builtins/loki-logging/index.mjs +1 -1
- package/system/dist/builtins/native-metrics/native-metrics.addon.js +1 -1
- package/system/dist/builtins/native-metrics/native-metrics.addon.mjs +1 -1
- package/system/dist/builtins/platform-probe/index.js +1 -1
- package/system/dist/builtins/platform-probe/index.mjs +1 -1
- package/system/dist/builtins/record-entries/index.js +1 -1
- package/system/dist/builtins/record-entries/index.mjs +1 -1
- package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.js +1 -1
- package/system/dist/builtins/remote-access-orchestrator/remote-access-orchestrator.addon.mjs +1 -1
- package/system/dist/builtins/snapshot/index.js +1 -1
- package/system/dist/builtins/snapshot/index.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.js +1 -1
- package/system/dist/builtins/sqlite-storage/filesystem-storage.addon.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/index.js +1 -1
- package/system/dist/builtins/sqlite-storage/index.mjs +1 -1
- package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.js +3 -3
- package/system/dist/builtins/sqlite-storage/sqlite-settings.addon.mjs +3 -3
- package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.js +1 -1
- package/system/dist/builtins/storage-orchestrator/storage-orchestrator.addon.mjs +1 -1
- package/system/dist/builtins/system-config/system-config.addon.js +3 -3
- package/system/dist/builtins/system-config/system-config.addon.mjs +284 -284
- package/system/dist/builtins/winston-logging/index.js +1 -1
- package/system/dist/builtins/winston-logging/index.mjs +1 -1
- package/system/dist/{capture-engine-C5SdmaKz.js → capture-engine-BEXh051r.js} +1 -1
- package/system/dist/{capture-engine-DWria-z2.mjs → capture-engine-CWfylyT8.mjs} +1 -1
- package/system/dist/{composition-sources-D7lvBXc8.js → composition-sources-B3_FuKde.js} +1 -1
- package/system/dist/{composition-sources-DheiqGa9.mjs → composition-sources-CcpMUxus.mjs} +1 -1
- package/system/dist/{constants-D3b9Y_CL.js → constants-B0tqNa2l.js} +1 -1
- package/system/dist/{constants-BVlI9ZGT.mjs → constants-fPesBGmF.mjs} +1 -1
- package/system/dist/{device-ca-CN76DeJT.mjs → device-ca-B98SR8Xf.mjs} +1 -1
- package/system/dist/{device-ca-DXPcZMMj.js → device-ca-Bh0GEstW.js} +1 -1
- package/system/dist/{device-row-store-C7JBjEgX.mjs → device-row-store-BBByy029.mjs} +1 -1
- package/system/dist/{device-row-store-taOeGSj6.js → device-row-store-CfWN8mLZ.js} +1 -1
- package/system/dist/{dist-DmqSc6J1.mjs → dist-CH-iCceM.mjs} +715 -8
- package/system/dist/{dist-CISneIO7.js → dist-CVjsCA7D.js} +792 -7
- package/system/dist/file-host-worker.js +1 -1
- package/system/dist/file-host-worker.mjs +1 -1
- package/system/dist/{hub-identity-CK65JBwk.js → hub-identity-BU4XzogT.js} +1 -1
- package/system/dist/{hub-identity-BC_IXmWZ.mjs → hub-identity-DCqJbSVn.mjs} +1 -1
- package/system/dist/{hub-mdns-ciao-DyDtfN7-.js → hub-mdns-ciao-BNzmwsIH.js} +2 -2
- package/system/dist/{hub-mdns-ciao-CHwLEof0.mjs → hub-mdns-ciao-IOM82NRd.mjs} +2 -2
- package/system/dist/index.d.ts +1 -0
- package/system/dist/index.js +11 -8
- package/system/dist/index.mjs +11 -9
- package/system/dist/{legacy-integration-rows-door-Ckvx-mil.js → legacy-integration-rows-door-CRcvhOWb.js} +1 -1
- package/system/dist/{legacy-integration-rows-door-BCtYMhm9.mjs → legacy-integration-rows-door-nQzjUyPU.mjs} +1 -1
- package/system/dist/{rename-composition-block-uFu4-kFk.mjs → rename-composition-block-CJ-C9HoY.mjs} +1 -1
- package/system/dist/{rename-composition-block-c7bj9EWu.js → rename-composition-block-oOfXrUWs.js} +1 -1
- package/system/dist/{restore-archive-JVHCUYE9.js → restore-archive-BSdudYif.js} +1 -1
- package/system/dist/{restore-archive-BQNTXuId.mjs → restore-archive-WK8ttFhW.mjs} +82 -82
- package/system/dist/{wal-maintenance-96iOS2pz.js → wal-maintenance-BRc_MNtZ.js} +0 -0
- package/system/dist/{wal-maintenance-CmzAxnVz.mjs → wal-maintenance-CyOVe3E1.mjs} +0 -0
- package/system/package.json +22 -1
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
import { Ct as GroupMemberSchema, Gi as integrationInstancesCapability, Hu as errMsg, Ii as groupMembersOf, Ja as notificationTargetKinds, Kt as NOTIFIER_GROUP_ADDON_ID, Lc as BaseAddon, Wr as decideMember, Xt as NotificationTargetStore, cs as shapeForMember, p as AlarmPanelStatusSchema, qa as notificationOutputCapability, qt as NOTIFIER_GROUP_KIND, sr as buildNotificationTargetInstances, va as listNotificationTargets, yr as clampToCaps } from "../../dist-CH-iCceM.mjs";
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
//#region src/builtins/core-notifier/group-descriptor.ts
|
|
4
|
+
/**
|
|
5
|
+
* The `group` target kind a core-notifier serves: the descriptor the add flow lists, and the record
|
|
6
|
+
* kinds it is stored in. A group has no secrets and no destination of its own (D634): its body is a
|
|
7
|
+
* name, a switch and the `members` rows, and every member keeps its own credentials.
|
|
8
|
+
*/
|
|
9
|
+
/** The form of a group: one editable list of member rows (the full editor is a custom dialog). */
|
|
10
|
+
function groupConfigSchema() {
|
|
11
|
+
return { sections: [{
|
|
12
|
+
id: "members",
|
|
13
|
+
title: "Members",
|
|
14
|
+
fields: [{
|
|
15
|
+
type: "editable-array",
|
|
16
|
+
key: "members",
|
|
17
|
+
label: "Members",
|
|
18
|
+
description: "The notifiers this group delivers to, each with its own policy.",
|
|
19
|
+
itemFields: [{
|
|
20
|
+
type: "text",
|
|
21
|
+
key: "targetId",
|
|
22
|
+
label: "Notifier",
|
|
23
|
+
required: true
|
|
24
|
+
}, {
|
|
25
|
+
type: "boolean",
|
|
26
|
+
key: "enabled",
|
|
27
|
+
label: "Enabled"
|
|
28
|
+
}]
|
|
29
|
+
}]
|
|
30
|
+
}] };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* What a group declares: every field the notification center can deliver, with generous limits, so
|
|
34
|
+
* nothing is stripped before the group sees it (D1030). The group does not narrow anything itself:
|
|
35
|
+
* each member is clamped to its OWN kind's caps when it is sent to.
|
|
36
|
+
*/
|
|
37
|
+
var GROUP_CAPS = {
|
|
38
|
+
text: {
|
|
39
|
+
title: true,
|
|
40
|
+
body: true
|
|
41
|
+
},
|
|
42
|
+
attachments: {
|
|
43
|
+
mediaTypes: [
|
|
44
|
+
"image",
|
|
45
|
+
"video",
|
|
46
|
+
"gif",
|
|
47
|
+
"audio",
|
|
48
|
+
"icon"
|
|
49
|
+
],
|
|
50
|
+
mode: "both",
|
|
51
|
+
max: 10
|
|
52
|
+
},
|
|
53
|
+
actions: 10,
|
|
54
|
+
actionIcons: true,
|
|
55
|
+
levels: [
|
|
56
|
+
{
|
|
57
|
+
id: "min",
|
|
58
|
+
label: "Minimum",
|
|
59
|
+
ordinal: 1
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: "low",
|
|
63
|
+
label: "Low",
|
|
64
|
+
ordinal: 2
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
id: "default",
|
|
68
|
+
label: "Default",
|
|
69
|
+
ordinal: 3
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
id: "high",
|
|
73
|
+
label: "High",
|
|
74
|
+
ordinal: 4
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
id: "critical",
|
|
78
|
+
label: "Critical",
|
|
79
|
+
ordinal: 5,
|
|
80
|
+
flags: { critical: true }
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
format: [
|
|
84
|
+
"text",
|
|
85
|
+
"markdown",
|
|
86
|
+
"html"
|
|
87
|
+
],
|
|
88
|
+
clickUrl: true,
|
|
89
|
+
sound: true,
|
|
90
|
+
ttl: true,
|
|
91
|
+
bodyMaxLen: 1e4
|
|
92
|
+
};
|
|
93
|
+
function groupTargetKind() {
|
|
94
|
+
return {
|
|
95
|
+
kind: NOTIFIER_GROUP_KIND,
|
|
96
|
+
label: "Group",
|
|
97
|
+
icon: "users",
|
|
98
|
+
addonId: NOTIFIER_GROUP_ADDON_ID,
|
|
99
|
+
configSchema: groupConfigSchema(),
|
|
100
|
+
destinationKeys: [],
|
|
101
|
+
supportsDiscovery: false,
|
|
102
|
+
caps: GROUP_CAPS
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
var groupRecordKinds = notificationTargetKinds([groupTargetKind()]);
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/builtins/core-notifier/group-instances.ts
|
|
108
|
+
/**
|
|
109
|
+
* What the core-notifier hands the shared notification-target instance door: the `group` kind and the
|
|
110
|
+
* save rule of its body. A group is refused when a member row cannot be read, when a target is listed
|
|
111
|
+
* twice, and when a member is itself a group (nesting is never delivered; the send skips it as well).
|
|
112
|
+
*/
|
|
113
|
+
/** The admin widget that draws the member editor of a group (a generic id the client maps to a component). */
|
|
114
|
+
var GROUP_MEMBERS_WIDGET = "notifier/group-members";
|
|
115
|
+
/** The members of a form body, or the reason the body cannot be saved. */
|
|
116
|
+
function validateGroupMembers(config, groupIds) {
|
|
117
|
+
const rows = config["members"];
|
|
118
|
+
if (rows === void 0) return [];
|
|
119
|
+
if (!Array.isArray(rows)) throw new Error("A group needs a list of members");
|
|
120
|
+
const members = [];
|
|
121
|
+
const seen = /* @__PURE__ */ new Set();
|
|
122
|
+
for (const row of rows) {
|
|
123
|
+
const parsed = GroupMemberSchema.safeParse(row);
|
|
124
|
+
if (!parsed.success) throw new Error("A group member row is not valid: it needs a notifier");
|
|
125
|
+
const { targetId } = parsed.data;
|
|
126
|
+
if (seen.has(targetId)) throw new Error(`The notifier ${targetId} is listed twice`);
|
|
127
|
+
seen.add(targetId);
|
|
128
|
+
if (groupIds.has(targetId)) throw new Error("A group cannot contain another group");
|
|
129
|
+
members.push(parsed.data);
|
|
130
|
+
}
|
|
131
|
+
return members;
|
|
132
|
+
}
|
|
133
|
+
/** The save rule that needs no other record: rows readable, no duplicate. Nesting is checked against the store. */
|
|
134
|
+
var NO_GROUPS = /* @__PURE__ */ new Set();
|
|
135
|
+
function groupInstancesSpec(deps) {
|
|
136
|
+
return {
|
|
137
|
+
descriptors: [groupTargetKind()],
|
|
138
|
+
kinds: groupRecordKinds,
|
|
139
|
+
mintId: deps.mintId,
|
|
140
|
+
canProbe: () => false,
|
|
141
|
+
widgets: (kind) => kind === "group" ? [{
|
|
142
|
+
id: GROUP_MEMBERS_WIDGET,
|
|
143
|
+
placement: "connection"
|
|
144
|
+
}] : void 0,
|
|
145
|
+
validate: ({ kind, config }) => {
|
|
146
|
+
if (kind !== "group") return void 0;
|
|
147
|
+
validateGroupMembers(config, NO_GROUPS);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The instances provider with the nested-group rule in front of every write: the ids of the groups
|
|
153
|
+
* are read from the store (async), so the rule is a step before `createInstance` / `updateInstance`
|
|
154
|
+
* rather than the synchronous save rule of the spec.
|
|
155
|
+
*/
|
|
156
|
+
function refuseNestedGroups(provider, listGroupIds) {
|
|
157
|
+
const check = async (values) => {
|
|
158
|
+
if (values === void 0) return;
|
|
159
|
+
validateGroupMembers(values, await listGroupIds());
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
...provider,
|
|
163
|
+
createInstance: async (input, ...rest) => {
|
|
164
|
+
await check(input.values);
|
|
165
|
+
return provider.createInstance(input, ...rest);
|
|
166
|
+
},
|
|
167
|
+
updateInstance: async (input, ...rest) => {
|
|
168
|
+
await check(input.values);
|
|
169
|
+
return provider.updateInstance(input, ...rest);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/builtins/core-notifier/member-ledger.ts
|
|
175
|
+
/**
|
|
176
|
+
* The two pieces of memory a group keeps, both in-process and bounded:
|
|
177
|
+
* - the delivered-set, keyed by (group, notification, member): an outbox retry of a group send
|
|
178
|
+
* re-sends only the members that failed, because the ones that landed are remembered here;
|
|
179
|
+
* - the throttle, keyed by (group, member): at most one delivery per member per window.
|
|
180
|
+
*
|
|
181
|
+
* Neither is durable on purpose: a restart forgets them, which costs one duplicate at worst, never a
|
|
182
|
+
* lost notification.
|
|
183
|
+
*/
|
|
184
|
+
var KEY_SEPARATOR = "\0";
|
|
185
|
+
var MemberLedger = class {
|
|
186
|
+
options;
|
|
187
|
+
delivered = /* @__PURE__ */ new Map();
|
|
188
|
+
lastSent = /* @__PURE__ */ new Map();
|
|
189
|
+
ttlMs;
|
|
190
|
+
maxEntries;
|
|
191
|
+
constructor(options) {
|
|
192
|
+
this.options = options;
|
|
193
|
+
this.ttlMs = options.ttlMs ?? 18e5;
|
|
194
|
+
this.maxEntries = options.maxEntries ?? 5e3;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* The key a retry of one send is recognised by. The rule id (`metadata.ruleId`) is part of it when
|
|
198
|
+
* the notification carries one, so two rules on the same event never share a key. With an event id
|
|
199
|
+
* or tag the key is that id; with neither it is a hash of the content (title, body, device).
|
|
200
|
+
*/
|
|
201
|
+
static notificationKey(input) {
|
|
202
|
+
const rule = input.metadata?.["ruleId"];
|
|
203
|
+
const ruleId = typeof rule === "string" || typeof rule === "number" ? String(rule) : "";
|
|
204
|
+
const id = input.eventId ?? input.tag;
|
|
205
|
+
if (id !== void 0) return [
|
|
206
|
+
id,
|
|
207
|
+
ruleId,
|
|
208
|
+
input.body
|
|
209
|
+
].join(KEY_SEPARATOR);
|
|
210
|
+
return ["content", createHash("sha256").update([
|
|
211
|
+
input.title ?? "",
|
|
212
|
+
input.body,
|
|
213
|
+
String(input.deviceId ?? ""),
|
|
214
|
+
ruleId
|
|
215
|
+
].join(KEY_SEPARATOR)).digest("hex")].join(KEY_SEPARATOR);
|
|
216
|
+
}
|
|
217
|
+
wasDelivered(groupId, notificationKey, memberId) {
|
|
218
|
+
this.sweep(this.delivered, this.ttlMs);
|
|
219
|
+
return this.delivered.has(this.deliveredKey(groupId, notificationKey, memberId));
|
|
220
|
+
}
|
|
221
|
+
markDelivered(groupId, notificationKey, memberId) {
|
|
222
|
+
this.remember(this.delivered, this.deliveredKey(groupId, notificationKey, memberId));
|
|
223
|
+
}
|
|
224
|
+
/** Whether `memberId` already got something inside its throttle window. */
|
|
225
|
+
isThrottled(groupId, memberId, throttleSec) {
|
|
226
|
+
if (throttleSec === void 0 || throttleSec <= 0) return false;
|
|
227
|
+
const last = this.lastSent.get(`${groupId}${KEY_SEPARATOR}${memberId}`);
|
|
228
|
+
return last !== void 0 && this.options.now() - last < throttleSec * 1e3;
|
|
229
|
+
}
|
|
230
|
+
markSent(groupId, memberId) {
|
|
231
|
+
this.remember(this.lastSent, `${groupId}${KEY_SEPARATOR}${memberId}`);
|
|
232
|
+
}
|
|
233
|
+
/** Forget everything of a deleted group. */
|
|
234
|
+
forgetGroup(groupId) {
|
|
235
|
+
const prefix = `${groupId}${KEY_SEPARATOR}`;
|
|
236
|
+
for (const map of [this.delivered, this.lastSent]) for (const key of map.keys()) if (key.startsWith(prefix)) map.delete(key);
|
|
237
|
+
}
|
|
238
|
+
deliveredKey(groupId, notificationKey, memberId) {
|
|
239
|
+
return [
|
|
240
|
+
groupId,
|
|
241
|
+
notificationKey,
|
|
242
|
+
memberId
|
|
243
|
+
].join(KEY_SEPARATOR);
|
|
244
|
+
}
|
|
245
|
+
remember(map, key) {
|
|
246
|
+
map.delete(key);
|
|
247
|
+
map.set(key, this.options.now());
|
|
248
|
+
while (map.size > this.maxEntries) {
|
|
249
|
+
const oldest = map.keys().next();
|
|
250
|
+
if (oldest.done === true) break;
|
|
251
|
+
map.delete(oldest.value);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
sweep(map, ttlMs) {
|
|
255
|
+
const cutoff = this.options.now() - ttlMs;
|
|
256
|
+
for (const [key, at] of map) {
|
|
257
|
+
if (at >= cutoff) break;
|
|
258
|
+
map.delete(key);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
//#endregion
|
|
263
|
+
//#region src/builtins/core-notifier/group-notification-output.ts
|
|
264
|
+
/**
|
|
265
|
+
* The `notification-output` provider of the `group` kind: a send to a group is one send per member.
|
|
266
|
+
*
|
|
267
|
+
* Both senders reach a group with no change, because both address a target by `(addonId, targetId)`
|
|
268
|
+
* read from the instances of `notification-output`: the notification center's `resolveTarget` and the
|
|
269
|
+
* automation `sendToExportedTarget`. The group only has to be one of those instances.
|
|
270
|
+
*
|
|
271
|
+
* Rules: fan-out goes through `ctx.api.notificationOutput.send` (the `sendToMember` port); a member
|
|
272
|
+
* that is itself a group is refused (`nested-group`); every skip is logged with its reason and the
|
|
273
|
+
* group's tags (D391); a retry re-sends only the members that failed (the ledger).
|
|
274
|
+
*/
|
|
275
|
+
var CANNED = {
|
|
276
|
+
title: "CamStack",
|
|
277
|
+
body: "This is a CamStack test notification.",
|
|
278
|
+
format: "text",
|
|
279
|
+
priority: 3
|
|
280
|
+
};
|
|
281
|
+
function summary(outcomes) {
|
|
282
|
+
const failed = outcomes.flatMap((o) => o.status === "failed" ? [`${o.targetId}: ${o.error}`] : []);
|
|
283
|
+
return `group delivery failed for ${failed.length} of ${outcomes.length} members (${failed.join("; ")})`;
|
|
284
|
+
}
|
|
285
|
+
function createGroupNotificationOutputProvider(deps) {
|
|
286
|
+
const { logger } = deps;
|
|
287
|
+
async function deliverMember(group, member, notification, notificationKey, targets, unreadableHolders, byKind) {
|
|
288
|
+
const tags = {
|
|
289
|
+
groupId: group.id,
|
|
290
|
+
...notification.deviceId !== void 0 ? { deviceId: notification.deviceId } : {}
|
|
291
|
+
};
|
|
292
|
+
const skip = (reason) => {
|
|
293
|
+
logger.info("group member skipped", {
|
|
294
|
+
tags,
|
|
295
|
+
meta: {
|
|
296
|
+
groupId: group.id,
|
|
297
|
+
targetId: member.targetId,
|
|
298
|
+
reason
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
return {
|
|
302
|
+
targetId: member.targetId,
|
|
303
|
+
status: "skipped",
|
|
304
|
+
reason
|
|
305
|
+
};
|
|
306
|
+
};
|
|
307
|
+
const target = targets.get(member.targetId);
|
|
308
|
+
if (target === void 0) {
|
|
309
|
+
if (unreadableHolders.length > 0) {
|
|
310
|
+
const error = `target not found, and these holders could not be read: ${unreadableHolders.join(", ")}`;
|
|
311
|
+
logger.warn("group member failed: target not found on a partial read", {
|
|
312
|
+
tags,
|
|
313
|
+
meta: {
|
|
314
|
+
groupId: group.id,
|
|
315
|
+
targetId: member.targetId,
|
|
316
|
+
unreadableHolders
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
return {
|
|
320
|
+
targetId: member.targetId,
|
|
321
|
+
status: "failed",
|
|
322
|
+
error
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
logger.warn("group member skipped: target not found", {
|
|
326
|
+
tags,
|
|
327
|
+
meta: {
|
|
328
|
+
groupId: group.id,
|
|
329
|
+
targetId: member.targetId,
|
|
330
|
+
reason: "unknown-target"
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
return {
|
|
334
|
+
targetId: member.targetId,
|
|
335
|
+
status: "skipped",
|
|
336
|
+
reason: "unknown-target"
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
if (target.kind === "group") return skip("nested-group");
|
|
340
|
+
if (!target.enabled) return skip("target-disabled");
|
|
341
|
+
if (deps.ledger.wasDelivered(group.id, notificationKey, member.targetId)) return skip("already-delivered");
|
|
342
|
+
const alarm = member.when?.kind === "alarm" ? await deps.readAlarmState(member.when.deviceId) : void 0;
|
|
343
|
+
const decision = decideMember({
|
|
344
|
+
member,
|
|
345
|
+
notification,
|
|
346
|
+
nowMs: deps.now(),
|
|
347
|
+
...alarm !== void 0 ? { alarmState: alarm } : {}
|
|
348
|
+
});
|
|
349
|
+
if (!decision.send) return skip(decision.reason);
|
|
350
|
+
if (deps.ledger.isThrottled(group.id, member.targetId, member.throttleSec)) return skip("throttled");
|
|
351
|
+
const shaped = shapeForMember(member, notification);
|
|
352
|
+
const ownCaps = byKind.get(target.kind);
|
|
353
|
+
try {
|
|
354
|
+
const result = await deps.sendToMember({
|
|
355
|
+
addonId: target.addonId,
|
|
356
|
+
targetId: member.targetId,
|
|
357
|
+
notification: ownCaps === void 0 ? shaped : clampToCaps(shaped, ownCaps)
|
|
358
|
+
});
|
|
359
|
+
if (!result.success) {
|
|
360
|
+
const error = result.error ?? "send failed";
|
|
361
|
+
logger.warn("group member delivery failed", {
|
|
362
|
+
tags,
|
|
363
|
+
meta: {
|
|
364
|
+
groupId: group.id,
|
|
365
|
+
targetId: member.targetId,
|
|
366
|
+
error
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
return {
|
|
370
|
+
targetId: member.targetId,
|
|
371
|
+
status: "failed",
|
|
372
|
+
error
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
deps.ledger.markDelivered(group.id, notificationKey, member.targetId);
|
|
376
|
+
deps.ledger.markSent(group.id, member.targetId);
|
|
377
|
+
return {
|
|
378
|
+
targetId: member.targetId,
|
|
379
|
+
status: "sent"
|
|
380
|
+
};
|
|
381
|
+
} catch (err) {
|
|
382
|
+
const error = errMsg(err);
|
|
383
|
+
logger.warn("group member delivery threw", {
|
|
384
|
+
tags,
|
|
385
|
+
meta: {
|
|
386
|
+
groupId: group.id,
|
|
387
|
+
targetId: member.targetId,
|
|
388
|
+
error
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
return {
|
|
392
|
+
targetId: member.targetId,
|
|
393
|
+
status: "failed",
|
|
394
|
+
error
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
async function deliver(group, notification) {
|
|
399
|
+
const { members, invalid } = groupMembersOf(group.config);
|
|
400
|
+
if (invalid > 0) logger.warn("group has member rows that could not be read; they are not delivered to", {
|
|
401
|
+
tags: { groupId: group.id },
|
|
402
|
+
meta: {
|
|
403
|
+
groupId: group.id,
|
|
404
|
+
invalid
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
const read = await deps.listTargets();
|
|
408
|
+
const targets = new Map(read.targets.map((target) => [target.id, target]));
|
|
409
|
+
const unreadableHolders = read.unreadable.map((holder) => holder.addonId);
|
|
410
|
+
const key = MemberLedger.notificationKey(notification);
|
|
411
|
+
const byKind = await deps.listMemberKinds().then((kinds) => new Map(kinds.map((kind) => [kind.kind, kind.caps]))).catch((err) => {
|
|
412
|
+
logger.warn("member kind caps could not be read; members degrade on their own", {
|
|
413
|
+
tags: { groupId: group.id },
|
|
414
|
+
meta: { error: errMsg(err) }
|
|
415
|
+
});
|
|
416
|
+
return /* @__PURE__ */ new Map();
|
|
417
|
+
});
|
|
418
|
+
const outcomes = [];
|
|
419
|
+
for (const member of members) outcomes.push(await deliverMember(group, member, notification, key, targets, unreadableHolders, byKind));
|
|
420
|
+
return {
|
|
421
|
+
groupId: group.id,
|
|
422
|
+
outcomes
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
async function sendTo(group, id, notification) {
|
|
426
|
+
if (group === null) return {
|
|
427
|
+
success: false,
|
|
428
|
+
error: `Unknown target '${id}'`
|
|
429
|
+
};
|
|
430
|
+
const report = await deliver(group, notification);
|
|
431
|
+
return report.outcomes.some((outcome) => outcome.status === "failed") ? {
|
|
432
|
+
success: false,
|
|
433
|
+
error: summary(report.outcomes)
|
|
434
|
+
} : { success: true };
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
provider: {
|
|
438
|
+
listTargetKinds: async () => [groupTargetKind()],
|
|
439
|
+
discoverTargets: async () => [],
|
|
440
|
+
probeTargetConfig: async ({ kind }) => ({
|
|
441
|
+
ok: false,
|
|
442
|
+
resolvedUrl: "",
|
|
443
|
+
error: `Kind '${kind}' does not support credential probing`
|
|
444
|
+
}),
|
|
445
|
+
send: async ({ targetId, notification }) => {
|
|
446
|
+
const group = await deps.store.getById(targetId);
|
|
447
|
+
if (group !== null && !group.enabled) return {
|
|
448
|
+
success: false,
|
|
449
|
+
error: "Target is disabled"
|
|
450
|
+
};
|
|
451
|
+
return sendTo(group, targetId, notification);
|
|
452
|
+
},
|
|
453
|
+
testTarget: async ({ targetId, sample }) => sendTo(await deps.store.getById(targetId), targetId, sample ?? CANNED)
|
|
454
|
+
},
|
|
455
|
+
deliver
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
//#endregion
|
|
459
|
+
//#region src/builtins/core-notifier/member-kinds.ts
|
|
460
|
+
/**
|
|
461
|
+
* The kinds the members of a group can be: the `notification-output` catalogue minus the group's own
|
|
462
|
+
* kind. The group is never a member of a group, so its kind is dropped HERE, at the source of the
|
|
463
|
+
* read, rather than by a flag further down. A failed read throws: a catalogue that could not be read
|
|
464
|
+
* is not an empty one (D49).
|
|
465
|
+
*/
|
|
466
|
+
async function readMemberKinds(api) {
|
|
467
|
+
return (await api.notificationOutput.listTargetKinds.query({})).filter((row) => row.addonId !== NOTIFIER_GROUP_ADDON_ID).map((row) => ({
|
|
468
|
+
kind: row.kind,
|
|
469
|
+
caps: row.caps
|
|
470
|
+
}));
|
|
471
|
+
}
|
|
472
|
+
//#endregion
|
|
473
|
+
//#region src/builtins/core-notifier/core-notifier.addon.ts
|
|
474
|
+
/**
|
|
475
|
+
* Core notifier — owns the `group` notification target (D1071).
|
|
476
|
+
*
|
|
477
|
+
* A group is a target like any other: an instance of `notification-output` of the record kind
|
|
478
|
+
* `notify-group`, listed with the other targets, usable by a notification rule and by the automation
|
|
479
|
+
* "Send notification" step. Delivering to it is one `notificationOutput.send` per member, under the
|
|
480
|
+
* member's own policy. It keeps no credentials: members keep theirs.
|
|
481
|
+
*/
|
|
482
|
+
var CoreNotifierAddon = class extends BaseAddon {
|
|
483
|
+
id = NOTIFIER_GROUP_ADDON_ID;
|
|
484
|
+
constructor() {
|
|
485
|
+
super({});
|
|
486
|
+
}
|
|
487
|
+
async onInitialize() {
|
|
488
|
+
const records = this.ctx.records;
|
|
489
|
+
const api = this.ctx.api;
|
|
490
|
+
if (records === void 0) throw new Error("core-notifier needs ctx.records on the hub");
|
|
491
|
+
if (api === void 0) throw new Error("core-notifier needs ctx.api to reach its members");
|
|
492
|
+
await records.declare(groupRecordKinds);
|
|
493
|
+
const logger = this.ctx.logger.child("notification-output");
|
|
494
|
+
const store = NotificationTargetStore.of(records, NOTIFIER_GROUP_ADDON_ID, groupRecordKinds, [groupTargetKind()], { logger });
|
|
495
|
+
const group = createGroupNotificationOutputProvider({
|
|
496
|
+
store,
|
|
497
|
+
listTargets: () => listNotificationTargets(api),
|
|
498
|
+
listMemberKinds: () => readMemberKinds(api),
|
|
499
|
+
sendToMember: (input) => api.notificationOutput.send.mutate({
|
|
500
|
+
addonId: input.addonId,
|
|
501
|
+
targetId: input.targetId,
|
|
502
|
+
notification: input.notification
|
|
503
|
+
}),
|
|
504
|
+
readAlarmState: async (deviceId) => {
|
|
505
|
+
try {
|
|
506
|
+
const status = AlarmPanelStatusSchema.safeParse(await api.alarmPanel.getStatus.query({ deviceId }));
|
|
507
|
+
if (status.success) return status.data.state;
|
|
508
|
+
logger.warn("alarm panel status is not readable; the member is skipped", {
|
|
509
|
+
tags: { deviceId },
|
|
510
|
+
meta: {
|
|
511
|
+
deviceId,
|
|
512
|
+
error: status.error.message
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
return null;
|
|
516
|
+
} catch (err) {
|
|
517
|
+
logger.warn("alarm panel read failed; the member is skipped", {
|
|
518
|
+
tags: { deviceId },
|
|
519
|
+
meta: {
|
|
520
|
+
deviceId,
|
|
521
|
+
error: errMsg(err)
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
return null;
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
ledger: new MemberLedger({ now: () => Date.now() }),
|
|
528
|
+
now: () => Date.now(),
|
|
529
|
+
logger
|
|
530
|
+
});
|
|
531
|
+
const instances = refuseNestedGroups(buildNotificationTargetInstances(records, groupInstancesSpec({ mintId: () => randomUUID() })), async () => new Set((await store.list()).map((target) => target.id)));
|
|
532
|
+
this.ctx.logger.info("CoreNotifier initialized");
|
|
533
|
+
return { providers: [{
|
|
534
|
+
capability: notificationOutputCapability,
|
|
535
|
+
provider: group.provider
|
|
536
|
+
}, {
|
|
537
|
+
capability: integrationInstancesCapability,
|
|
538
|
+
provider: instances
|
|
539
|
+
}] };
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
//#endregion
|
|
543
|
+
export { CoreNotifierAddon, CoreNotifierAddon as default };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `group` target kind a core-notifier serves: the descriptor the add flow lists, and the record
|
|
3
|
+
* kinds it is stored in. A group has no secrets and no destination of its own (D634): its body is a
|
|
4
|
+
* name, a switch and the `members` rows, and every member keeps its own credentials.
|
|
5
|
+
*/
|
|
6
|
+
import { type NotificationTargetRecordKind, type TargetKind, type TargetKindCaps } from "@camstack/types";
|
|
7
|
+
/** The form of a group: one editable list of member rows (the full editor is a custom dialog). */
|
|
8
|
+
export declare function groupConfigSchema(): TargetKind["configSchema"];
|
|
9
|
+
/**
|
|
10
|
+
* What a group declares: every field the notification center can deliver, with generous limits, so
|
|
11
|
+
* nothing is stripped before the group sees it (D1030). The group does not narrow anything itself:
|
|
12
|
+
* each member is clamped to its OWN kind's caps when it is sent to.
|
|
13
|
+
*/
|
|
14
|
+
export declare const GROUP_CAPS: TargetKindCaps;
|
|
15
|
+
export declare function groupTargetKind(): TargetKind;
|
|
16
|
+
export declare const groupRecordKinds: Readonly<Record<string, NotificationTargetRecordKind>>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the core-notifier hands the shared notification-target instance door: the `group` kind and the
|
|
3
|
+
* save rule of its body. A group is refused when a member row cannot be read, when a target is listed
|
|
4
|
+
* twice, and when a member is itself a group (nesting is never delivered; the send skips it as well).
|
|
5
|
+
*/
|
|
6
|
+
import { type GroupMember, type IIntegrationInstancesProvider, type NotificationTargetInstancesSpec } from "@camstack/types";
|
|
7
|
+
/** The admin widget that draws the member editor of a group (a generic id the client maps to a component). */
|
|
8
|
+
export declare const GROUP_MEMBERS_WIDGET = "notifier/group-members";
|
|
9
|
+
export interface GroupInstancesDeps {
|
|
10
|
+
readonly mintId: () => string;
|
|
11
|
+
}
|
|
12
|
+
/** The members of a form body, or the reason the body cannot be saved. */
|
|
13
|
+
export declare function validateGroupMembers(config: Readonly<Record<string, unknown>>, groupIds: ReadonlySet<string>): readonly GroupMember[];
|
|
14
|
+
export declare function groupInstancesSpec(deps: GroupInstancesDeps): NotificationTargetInstancesSpec;
|
|
15
|
+
/**
|
|
16
|
+
* The instances provider with the nested-group rule in front of every write: the ids of the groups
|
|
17
|
+
* are read from the store (async), so the rule is a step before `createInstance` / `updateInstance`
|
|
18
|
+
* rather than the synchronous save rule of the spec.
|
|
19
|
+
*/
|
|
20
|
+
export declare function refuseNestedGroups(provider: IIntegrationInstancesProvider, listGroupIds: () => Promise<ReadonlySet<string>>): IIntegrationInstancesProvider;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `notification-output` provider of the `group` kind: a send to a group is one send per member.
|
|
3
|
+
*
|
|
4
|
+
* Both senders reach a group with no change, because both address a target by `(addonId, targetId)`
|
|
5
|
+
* read from the instances of `notification-output`: the notification center's `resolveTarget` and the
|
|
6
|
+
* automation `sendToExportedTarget`. The group only has to be one of those instances.
|
|
7
|
+
*
|
|
8
|
+
* Rules: fan-out goes through `ctx.api.notificationOutput.send` (the `sendToMember` port); a member
|
|
9
|
+
* that is itself a group is refused (`nested-group`); every skip is logged with its reason and the
|
|
10
|
+
* group's tags (D391); a retry re-sends only the members that failed (the ledger).
|
|
11
|
+
*/
|
|
12
|
+
import { type AlarmState, type GroupSkipReason, type INotificationOutputProvider, type IScopedLogger, type Notification, type NotificationTargetRef, type NotificationTargetStore, type SendResult, type Target, type TargetKindCaps } from "@camstack/types";
|
|
13
|
+
import { MemberLedger } from "./member-ledger.js";
|
|
14
|
+
/** A target kind and what it delivers. */
|
|
15
|
+
export interface MemberKindCaps {
|
|
16
|
+
readonly kind: string;
|
|
17
|
+
readonly caps: TargetKindCaps;
|
|
18
|
+
}
|
|
19
|
+
/** What happened to one member of one send. */
|
|
20
|
+
export type MemberOutcome = {
|
|
21
|
+
readonly targetId: string;
|
|
22
|
+
readonly status: "sent";
|
|
23
|
+
} | {
|
|
24
|
+
readonly targetId: string;
|
|
25
|
+
readonly status: "skipped";
|
|
26
|
+
readonly reason: GroupSkipReason;
|
|
27
|
+
} | {
|
|
28
|
+
readonly targetId: string;
|
|
29
|
+
readonly status: "failed";
|
|
30
|
+
readonly error: string;
|
|
31
|
+
};
|
|
32
|
+
/** What a send to a group did, member by member. */
|
|
33
|
+
export interface GroupDeliveryReport {
|
|
34
|
+
readonly groupId: string;
|
|
35
|
+
readonly outcomes: readonly MemberOutcome[];
|
|
36
|
+
}
|
|
37
|
+
export interface GroupNotificationOutputDeps {
|
|
38
|
+
readonly store: Pick<NotificationTargetStore, "list" | "getById">;
|
|
39
|
+
/** Every notification target, groups included; `unreadable` names a holder that could not answer. */
|
|
40
|
+
readonly listTargets: () => Promise<{
|
|
41
|
+
readonly targets: readonly NotificationTargetRef[];
|
|
42
|
+
readonly unreadable: readonly {
|
|
43
|
+
readonly addonId: string;
|
|
44
|
+
readonly error: string;
|
|
45
|
+
}[];
|
|
46
|
+
}>;
|
|
47
|
+
/** The kind catalogue without the group's own kind: what each member's kind delivers. */
|
|
48
|
+
readonly listMemberKinds: () => Promise<readonly MemberKindCaps[]>;
|
|
49
|
+
readonly sendToMember: (input: {
|
|
50
|
+
readonly addonId: string;
|
|
51
|
+
readonly targetId: string;
|
|
52
|
+
readonly notification: Notification;
|
|
53
|
+
}) => Promise<SendResult>;
|
|
54
|
+
/** The alarm panel's state; `null` = it could not be read. */
|
|
55
|
+
readonly readAlarmState: (deviceId: number) => Promise<AlarmState | null>;
|
|
56
|
+
readonly ledger: MemberLedger;
|
|
57
|
+
readonly now: () => number;
|
|
58
|
+
readonly logger: IScopedLogger;
|
|
59
|
+
}
|
|
60
|
+
export declare function createGroupNotificationOutputProvider(deps: GroupNotificationOutputDeps): {
|
|
61
|
+
readonly provider: INotificationOutputProvider;
|
|
62
|
+
readonly deliver: (group: Target, notification: Notification) => Promise<GroupDeliveryReport>;
|
|
63
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CoreNotifierAddon as default, CoreNotifierAddon } from "./core-notifier.addon.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
require("../../chunk-Cek0wNdY.js");
|
|
6
|
+
const require_builtins_core_notifier_core_notifier_addon = require("./core-notifier.addon.js");
|
|
7
|
+
exports.CoreNotifierAddon = require_builtins_core_notifier_core_notifier_addon.CoreNotifierAddon;
|
|
8
|
+
exports.default = require_builtins_core_notifier_core_notifier_addon.CoreNotifierAddon;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kinds the members of a group can be: the `notification-output` catalogue minus the group's own
|
|
3
|
+
* kind. The group is never a member of a group, so its kind is dropped HERE, at the source of the
|
|
4
|
+
* read, rather than by a flag further down. A failed read throws: a catalogue that could not be read
|
|
5
|
+
* is not an empty one (D49).
|
|
6
|
+
*/
|
|
7
|
+
import { type TargetKindCaps } from "@camstack/types";
|
|
8
|
+
import type { MemberKindCaps } from "./group-notification-output.js";
|
|
9
|
+
interface KindRow {
|
|
10
|
+
readonly kind: string;
|
|
11
|
+
readonly addonId: string;
|
|
12
|
+
readonly caps: TargetKindCaps;
|
|
13
|
+
}
|
|
14
|
+
export interface MemberKindsApi {
|
|
15
|
+
readonly notificationOutput: {
|
|
16
|
+
readonly listTargetKinds: {
|
|
17
|
+
query(input: Record<string, never>): Promise<readonly KindRow[]>;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export declare function readMemberKinds(api: MemberKindsApi): Promise<readonly MemberKindCaps[]>;
|
|
22
|
+
export {};
|