@tak-ps/node-cot 14.60.0 → 14.61.0

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.
@@ -2,6 +2,8 @@ import Util from '../utils/util.js'
2
2
  import CoT from '../cot.js';
3
3
  import type JSONCoT from '../types/types.js';
4
4
  import type { Static } from '@sinclair/typebox';
5
+ import type { ChatHierarchyNode } from '../types/feature.js';
6
+ import { chatHierarchyDetail, chatHierarchyContacts, chatHierarchyGroup, chatHierarchyParent } from '../utils/chat.js';
5
7
 
6
8
  export type MissionChatMember = {
7
9
  uid: string;
@@ -179,6 +181,8 @@ export type DirectChatInput = {
179
181
 
180
182
  export class DirectChat extends CoT {
181
183
  constructor(chat: DirectChatInput) {
184
+ const messageId = chat.messageId || crypto.randomUUID();
185
+
182
186
  const cot: Static<typeof JSONCoT> = {
183
187
  event: {
184
188
  _attributes: Util.cot_event_attr('b-t-f', 'h-g-i-g-o'),
@@ -188,7 +192,7 @@ export class DirectChat extends CoT {
188
192
  _attributes: {
189
193
  parent: chat.parent || 'RootContactGroup',
190
194
  groupOwner: chat.groupOwner ? 'true' : 'false',
191
- messageId: chat.messageId || crypto.randomUUID(),
195
+ messageId,
192
196
  chatroom: chat.chatroom || chat.to.callsign,
193
197
  id: chat.to.uid,
194
198
  senderCallsign: chat.from.callsign
@@ -205,7 +209,7 @@ export class DirectChat extends CoT {
205
209
  }
206
210
  }
207
211
 
208
- cot.event._attributes.uid = `GeoChat.${chat.from.uid}.${chat.to.uid}.${crypto.randomUUID()}`;
212
+ cot.event._attributes.uid = `GeoChat.${chat.from.uid}.${chat.to.uid}.${messageId}`;
209
213
 
210
214
  cot.event.detail ??= {};
211
215
 
@@ -234,3 +238,150 @@ export class DirectChat extends CoT {
234
238
 
235
239
  }
236
240
  }
241
+
242
+ export type GroupChatInput = {
243
+ from: DirectChatMember;
244
+ group: {
245
+ id: string;
246
+ name: string;
247
+ };
248
+
249
+ /**
250
+ * Every other member of the group - the message is addressed to each of them
251
+ * Omit to broadcast to everyone the sender shares a channel with (ie: "All Chat Rooms")
252
+ */
253
+ members?: Array<DirectChatMember>;
254
+
255
+ /**
256
+ * Full tree of the top level user group that `group` belongs to, rooted at `UserGroups`,
257
+ * in the form `to_geojson` returns as `chat.hierarchy` - required to address a nested group
258
+ * If `members` is omitted the message is addressed to every contact below `group` in the tree
259
+ */
260
+ hierarchy?: ChatHierarchyNode;
261
+
262
+ message: string;
263
+
264
+ parent?: string;
265
+
266
+ /**
267
+ * Only set by the creator of the group - recipients replace the membership
268
+ * of the group with the `hierarchy` of a message from its owner
269
+ */
270
+ groupOwner?: boolean;
271
+
272
+ /**
273
+ * UID of a child group that the sender deleted from `group`
274
+ */
275
+ deleteChild?: string;
276
+ messageId?: string;
277
+ }
278
+
279
+ export const GroupChatParent = {
280
+ root: 'RootContactGroup',
281
+ user: 'UserGroups',
282
+ team: 'TeamGroups'
283
+ } as const;
284
+
285
+ /**
286
+ * GeoChat message to a Group
287
+ *
288
+ * User created groups (parent `UserGroups`) carry the group's `hierarchy` so that
289
+ * a recipient that has never seen the group can create it with the right members
290
+ */
291
+ export class GroupChat extends CoT {
292
+ constructor(chat: GroupChatInput) {
293
+ const messageId = chat.messageId || crypto.randomUUID();
294
+ const addressed = chat.hierarchy ? chatHierarchyGroup(chat.hierarchy, chat.group.id) : undefined;
295
+ const members = (chat.members ?? (addressed ? chatHierarchyContacts(addressed) : []))
296
+ .filter((member) => member.uid !== chat.from.uid);
297
+ const broadcast = members.length === 0;
298
+ const parent = chat.parent
299
+ || (chat.hierarchy ? chatHierarchyParent(chat.hierarchy, chat.group.id) : undefined)
300
+ || (broadcast ? GroupChatParent.root : GroupChatParent.user);
301
+
302
+ const chatgrp: Record<string, string> = {
303
+ uid0: chat.from.uid,
304
+ id: chat.group.id
305
+ };
306
+
307
+ if (!broadcast) {
308
+ members.forEach((member, i) => {
309
+ chatgrp[`uid${i + 1}`] = member.uid;
310
+ });
311
+ } else {
312
+ chatgrp.uid1 = chat.group.id;
313
+ }
314
+
315
+ const cot: Static<typeof JSONCoT> = {
316
+ event: {
317
+ _attributes: Util.cot_event_attr('b-t-f', 'h-g-i-g-o'),
318
+ point: Util.cot_point(),
319
+ detail: {
320
+ __chat: {
321
+ _attributes: {
322
+ parent,
323
+ groupOwner: chat.groupOwner ? 'true' : 'false',
324
+ messageId,
325
+ chatroom: chat.group.name,
326
+ id: chat.group.id,
327
+ senderCallsign: chat.from.callsign,
328
+ ...(chat.deleteChild ? { deleteChild: chat.deleteChild } : {})
329
+ },
330
+ chatgrp: {
331
+ _attributes: chatgrp
332
+ }
333
+ },
334
+ }
335
+ }
336
+ }
337
+
338
+ cot.event._attributes.uid = `GeoChat.${chat.from.uid}.${chat.group.id}.${messageId}`;
339
+
340
+ cot.event.detail ??= {};
341
+
342
+ if (cot.event.detail.__chat && chat.hierarchy) {
343
+ cot.event.detail.__chat.hierarchy = chatHierarchyDetail(chat.hierarchy);
344
+ } else if (cot.event.detail.__chat && !broadcast && parent === GroupChatParent.user) {
345
+ cot.event.detail.__chat.hierarchy = chatHierarchyDetail({
346
+ uid: GroupChatParent.user,
347
+ name: 'Groups',
348
+ type: 'group',
349
+ children: [{
350
+ uid: chat.group.id,
351
+ name: chat.group.name,
352
+ type: 'group',
353
+ children: [chat.from, ...members].map((member) => ({
354
+ uid: member.uid,
355
+ name: member.callsign,
356
+ type: 'contact' as const
357
+ }))
358
+ }]
359
+ });
360
+ }
361
+
362
+ cot.event.detail.link = {
363
+ _attributes: {
364
+ uid: chat.from.uid,
365
+ type: 'a-f-G',
366
+ relation: 'p-p'
367
+ }
368
+ }
369
+
370
+ cot.event.detail.remarks = {
371
+ _attributes: {
372
+ source: chat.from.uid,
373
+ ...(broadcast ? { to: chat.group.id } : {}),
374
+ time: cot.event._attributes.time
375
+ },
376
+ _text: chat.message
377
+ }
378
+
379
+ super(cot)
380
+
381
+ for (const member of members) {
382
+ this.addDest({
383
+ uid: member.uid
384
+ })
385
+ }
386
+ }
387
+ }
@@ -28,6 +28,7 @@ import { destination } from '@turf/destination';
28
28
  import Color from '../utils/color.js';
29
29
  import { jsonClone, toNumber } from '../utils/util.js';
30
30
  import Type2525 from '../utils/2525.js';
31
+ import { chatHierarchy } from '../utils/chat.js';
31
32
  import JSONCoT from '../types/types.js'
32
33
  import CoT from '../cot.js';
33
34
 
@@ -294,6 +295,9 @@ export async function to_geojson(
294
295
  ...raw.event.detail.__chat._attributes,
295
296
  chatgrp
296
297
  }
298
+
299
+ const hierarchy = chatHierarchy(raw.event.detail.__chat.hierarchy);
300
+ if (hierarchy) feat.properties.chat.hierarchy = hierarchy;
297
301
  } else if (raw.event.detail.__chatreceipt) {
298
302
  const chatgrp: unknown = raw.event.detail.__chatreceipt.chatgrp;
299
303
  feat.properties.chat = {
@@ -137,6 +137,24 @@ export const FeaturePropertySpatial = Type.Object({
137
137
  spin: SpatialSpinAttributes
138
138
  });
139
139
 
140
+ export const ChatHierarchy = Type.Object({
141
+ uid: Type.String(),
142
+ name: Type.String(),
143
+ type: Type.Union([Type.Literal('group'), Type.Literal('contact')]),
144
+ children: Type.Optional(Type.Array(Type.Any({
145
+ description: 'Nested ChatHierarchy nodes'
146
+ })))
147
+ }, {
148
+ description: 'Group membership tree sent with a GeoChat to a user created group, rooted at the UserGroups group'
149
+ });
150
+
151
+ export type ChatHierarchyNode = {
152
+ uid: string;
153
+ name: string;
154
+ type: 'group' | 'contact';
155
+ children?: Array<ChatHierarchyNode>;
156
+ };
157
+
140
158
  export const Properties = Type.Object({
141
159
  callsign: Type.String({ default: 'UNKNOWN' }),
142
160
  type: Type.String({ default: 'a-f-G' }),
@@ -201,7 +219,11 @@ export const Properties = Type.Object({
201
219
  chatroom: Type.String(),
202
220
  id: Type.Optional(Type.String()),
203
221
  senderCallsign: Type.String(),
204
- chatgrp: Type.Any()
222
+ deleteChild: Type.Optional(Type.String({
223
+ description: 'UID of a group that the sender deleted from the group this message is addressed to'
224
+ })),
225
+ chatgrp: Type.Any(),
226
+ hierarchy: Type.Optional(ChatHierarchy)
205
227
  })),
206
228
  track: Type.Optional(TrackAttributes),
207
229
  dest: Type.Optional(Type.Union([MartiDestAttributes, Type.Array(MartiDestAttributes)])),
@@ -582,9 +582,11 @@ export const Chat = Type.Object({
582
582
  messageId: Type.Optional(Type.String()),
583
583
  chatroom: Type.String(),
584
584
  id: Type.Optional(Type.String()),
585
- senderCallsign: Type.String()
585
+ senderCallsign: Type.String(),
586
+ deleteChild: Type.Optional(Type.String())
586
587
  }),
587
- chatgrp: Type.Any()
588
+ chatgrp: Type.Any(),
589
+ hierarchy: Type.Optional(Type.Any())
588
590
  })
589
591
 
590
592
  export const TakVersionAttributes = Type.Object({
@@ -0,0 +1,123 @@
1
+ import type { ChatHierarchyNode } from '../types/feature.js';
2
+
3
+ type RawHierarchyNode = {
4
+ _attributes?: { uid?: unknown; name?: unknown };
5
+ group?: unknown;
6
+ contact?: unknown;
7
+ };
8
+
9
+ function isRawNode(raw: unknown): raw is RawHierarchyNode {
10
+ return typeof raw === 'object' && raw !== null;
11
+ }
12
+
13
+ function rawNodes(raw: unknown): Array<RawHierarchyNode> {
14
+ if (Array.isArray(raw)) return raw.filter((item) => isRawNode(item));
15
+ return isRawNode(raw) ? [raw] : [];
16
+ }
17
+
18
+ function toNode(raw: RawHierarchyNode, type: 'group' | 'contact'): ChatHierarchyNode | undefined {
19
+ const uid = raw._attributes?.uid;
20
+ const name = raw._attributes?.name;
21
+
22
+ if (typeof uid !== 'string' || typeof name !== 'string') return undefined;
23
+
24
+ const node: ChatHierarchyNode = { uid, name, type };
25
+
26
+ if (type === 'group') {
27
+ node.children = [
28
+ ...rawNodes(raw.group).map((child) => toNode(child, 'group')),
29
+ ...rawNodes(raw.contact).map((child) => toNode(child, 'contact'))
30
+ ].filter((child) => child !== undefined);
31
+ }
32
+
33
+ return node;
34
+ }
35
+
36
+ /**
37
+ * Convert the `<hierarchy>` of a `__chat` detail into a tree of groups & contacts
38
+ */
39
+ export function chatHierarchy(raw: unknown): ChatHierarchyNode | undefined {
40
+ if (!isRawNode(raw)) return undefined;
41
+
42
+ const roots = rawNodes(raw.group);
43
+ if (roots.length === 0) return undefined;
44
+
45
+ const root = roots[0];
46
+
47
+ return toNode(root, 'group');
48
+ }
49
+
50
+ type HierarchyDetail = {
51
+ _attributes: { uid: string; name: string };
52
+ group?: Array<HierarchyDetail>;
53
+ contact?: Array<HierarchyDetail>;
54
+ };
55
+
56
+ function toDetail(node: ChatHierarchyNode): HierarchyDetail {
57
+ const detail: HierarchyDetail = {
58
+ _attributes: { uid: node.uid, name: node.name }
59
+ };
60
+
61
+ const children = node.children ?? [];
62
+ const groups = children.filter((child) => child.type === 'group');
63
+ const contacts = children.filter((child) => child.type === 'contact');
64
+
65
+ if (groups.length > 0) detail.group = groups.map((group) => toDetail(group));
66
+ if (contacts.length > 0) detail.contact = contacts.map((contact) => toDetail(contact));
67
+
68
+ return detail;
69
+ }
70
+
71
+ /**
72
+ * Convert a tree of groups & contacts into the `<hierarchy>` of a `__chat` detail
73
+ */
74
+ export function chatHierarchyDetail(root: ChatHierarchyNode): { group: HierarchyDetail } {
75
+ return { group: toDetail(root) };
76
+ }
77
+
78
+ /**
79
+ * Every contact below a node of a hierarchy, regardless of how deeply it is nested
80
+ */
81
+ export function chatHierarchyContacts(node: ChatHierarchyNode): Array<{ uid: string; callsign: string }> {
82
+ const contacts = new Map<string, { uid: string; callsign: string }>();
83
+
84
+ const walk = (current: ChatHierarchyNode): void => {
85
+ if (current.type === 'contact') {
86
+ contacts.set(current.uid, { uid: current.uid, callsign: current.name });
87
+ }
88
+
89
+ for (const child of current.children ?? []) walk(child);
90
+ };
91
+
92
+ walk(node);
93
+
94
+ return [...contacts.values()];
95
+ }
96
+
97
+ /**
98
+ * Find a group by UID anywhere in a hierarchy
99
+ */
100
+ export function chatHierarchyGroup(node: ChatHierarchyNode, uid: string): ChatHierarchyNode | undefined {
101
+ if (node.type === 'group' && node.uid === uid) return node;
102
+
103
+ for (const child of node.children ?? []) {
104
+ const found = chatHierarchyGroup(child, uid);
105
+ if (found) return found;
106
+ }
107
+
108
+ return undefined;
109
+ }
110
+
111
+ /**
112
+ * UID of the group that directly contains the group with the given UID
113
+ */
114
+ export function chatHierarchyParent(node: ChatHierarchyNode, uid: string): string | undefined {
115
+ for (const child of node.children ?? []) {
116
+ if (child.type === 'group' && child.uid === uid) return node.uid;
117
+
118
+ const found = chatHierarchyParent(child, uid);
119
+ if (found) return found;
120
+ }
121
+
122
+ return undefined;
123
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tak-ps/node-cot",
3
3
  "type": "module",
4
- "version": "14.60.0",
4
+ "version": "14.61.0",
5
5
  "description": "Lightweight JavaScript library for parsing and manipulating TAK messages",
6
6
  "author": "Nick Ingalls <nick@ingalls.ca>",
7
7
  "types": "index.ts",
@@ -45,7 +45,7 @@
45
45
  "ajv": "^8.12.0",
46
46
  "color": "^5.0.0",
47
47
  "geomagnetism": "^0.2.0",
48
- "json-diff-ts": "^4.0.1",
48
+ "json-diff-ts": "^5.0.0",
49
49
  "mil-std-2525": "^0.2.8",
50
50
  "milstandard-e": "^0.2.14",
51
51
  "milsymbol": "^3.0.2",