@oxyhq/core 7.0.0 → 7.1.1

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.
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Unified account-list projection — THE single source of truth.
3
+ *
4
+ * Produces the flat `SwitchableAccount[]` every account chooser renders, by
5
+ * merging the device's server-authoritative session set (`DeviceSessionState`
6
+ * from {@link SessionClient}) with the caller's account graph (`AccountNode[]`
7
+ * from `oxyServices.listAccounts()`), deduped by `accountId`. This lives in
8
+ * `@oxyhq/core` so `@oxyhq/services` (RN) and `@oxyhq/auth` (web) — and
9
+ * `auth.oxy.so` — all render the SAME list from the SAME logic and cannot
10
+ * diverge.
11
+ *
12
+ * Pure and I/O-free: the caller resolves per-account profiles via
13
+ * `oxyServices.getUsersByIds(...)` and passes them in as `profilesById`, and
14
+ * binds `resolveAvatarUrl` to `oxyServices.getFileDownloadUrl`. This is the same
15
+ * split the former `@oxyhq/services` `buildSwitchableAccounts` used — hoisted
16
+ * into core, keyed directly on `DeviceSessionState` (whose `activeAccountId` is
17
+ * atomic, so no cross-call current-row reconciliation is needed).
18
+ */
19
+ import { getAccountDisplayName, getAccountFallbackHandle } from '../utils/accountUtils.js';
20
+ /**
21
+ * Pure union of device sign-ins and account-graph nodes into the flat
22
+ * {@link SwitchableAccount}[] every switcher renders.
23
+ *
24
+ * Order: device rows first (in `state.accounts` order, active flagged), then
25
+ * graph-only rows (in graph order). An account present as BOTH a device session
26
+ * and a graph node is deduped into ONE device row enriched with the graph
27
+ * metadata (relationship / kind / parent / membership).
28
+ */
29
+ export function projectSwitchableAccounts(input) {
30
+ const { state, graph, profilesById, activeUser, locale, resolveAvatarUrl } = input;
31
+ const activeAccountId = state?.activeAccountId ?? null;
32
+ const toRow = (accountUser, opts) => {
33
+ const accountId = accountUser.id?.toString() ?? '';
34
+ const handle = getAccountFallbackHandle(accountUser);
35
+ const secondaryHandle = handle ? `@${handle}` : null;
36
+ return {
37
+ accountId,
38
+ sessionId: opts.sessionId,
39
+ authuser: opts.authuser,
40
+ isCurrent: Boolean(accountId) && accountId === activeAccountId,
41
+ onDevice: Boolean(opts.sessionId),
42
+ relationship: opts.relationship,
43
+ kind: opts.kind,
44
+ parentAccountId: opts.parentAccountId,
45
+ callerMembership: opts.callerMembership,
46
+ displayName: getAccountDisplayName(accountUser, locale),
47
+ // Real email, or the `@handle` fallback (NEVER synthesized).
48
+ email: accountUser.email ?? secondaryHandle,
49
+ avatarUrl: resolveAvatarUrl(accountUser.avatar),
50
+ color: accountUser.color ?? null,
51
+ user: accountUser,
52
+ };
53
+ };
54
+ // --- Device rows (from the server-authoritative session set) ---
55
+ const deviceRows = (state?.accounts ?? []).flatMap((account) => {
56
+ const isActive = account.accountId === activeAccountId;
57
+ // The active row prefers the freshest `activeUser` (when supplied), then the
58
+ // batch-resolved profile; every other row uses the batch-resolved profile.
59
+ const accountUser = isActive && activeUser
60
+ ? activeUser
61
+ : profilesById.get(account.accountId);
62
+ if (!accountUser) {
63
+ return [];
64
+ }
65
+ return [toRow(accountUser, { sessionId: account.sessionId, authuser: account.authuser })];
66
+ });
67
+ // --- Merge graph nodes, deduping by account id ---
68
+ const byAccountId = new Map();
69
+ const order = [];
70
+ const remember = (row) => {
71
+ if (!row.accountId || byAccountId.has(row.accountId)) {
72
+ return;
73
+ }
74
+ byAccountId.set(row.accountId, row);
75
+ order.push(row.accountId);
76
+ };
77
+ for (const row of deviceRows) {
78
+ remember(row);
79
+ }
80
+ for (const node of graph) {
81
+ const existing = byAccountId.get(node.accountId);
82
+ if (existing) {
83
+ // On-device account that is ALSO in the graph: enrich the device row with
84
+ // graph metadata; keep its (freshest) profile + sessionId + active flag.
85
+ byAccountId.set(node.accountId, {
86
+ ...existing,
87
+ relationship: node.relationship,
88
+ kind: node.kind,
89
+ parentAccountId: node.parentAccountId,
90
+ callerMembership: node.callerMembership,
91
+ });
92
+ continue;
93
+ }
94
+ // Graph-only account (owned org / shared, not yet a device session).
95
+ remember(toRow(node.account, {
96
+ relationship: node.relationship,
97
+ kind: node.kind,
98
+ parentAccountId: node.parentAccountId,
99
+ callerMembership: node.callerMembership,
100
+ }));
101
+ }
102
+ return order.flatMap((id) => {
103
+ const row = byAccountId.get(id);
104
+ return row ? [row] : [];
105
+ });
106
+ }
107
+ /**
108
+ * Every distinct account id referenced by a device session set AND an account
109
+ * graph, sorted for a stable profile-fetch key. Feed to
110
+ * `oxyServices.getUsersByIds(...)`; graph nodes already embed their `account`
111
+ * document, but including their ids lets the caller pass one id set and lets the
112
+ * projection prefer freshly-fetched profiles uniformly.
113
+ */
114
+ export function switchableAccountIds(state, graph) {
115
+ const ids = new Set();
116
+ for (const account of state?.accounts ?? []) {
117
+ if (account.accountId) {
118
+ ids.add(account.accountId);
119
+ }
120
+ }
121
+ for (const node of graph) {
122
+ if (node.accountId) {
123
+ ids.add(node.accountId);
124
+ }
125
+ }
126
+ return Array.from(ids).sort();
127
+ }
@@ -22,8 +22,15 @@ import { createSessionClientHost } from './sessionClientHost.js';
22
22
  * specifier (bundler-fragile in Metro/Expo-web and Vite against the published
23
23
  * dist). When omitted, the client falls back to the lazy loader.
24
24
  */
25
- export function createSessionClient(oxyServices, transport, socketFactory) {
25
+ export function createSessionClient(oxyServices, transport, socketFactory,
26
+ /**
27
+ * Optional signed-out realtime wiring: `signedOutSocketAuth` (open the socket
28
+ * while signed out so an idle tab receives its device pushes) and
29
+ * `onSessionAppeared` (self-acquire when a sibling signs in). See
30
+ * {@link SessionClientOptions}.
31
+ */
32
+ extra) {
26
33
  const host = createSessionClientHost(oxyServices);
27
- const client = new SessionClient(host, { transport, socketFactory });
34
+ const client = new SessionClient(host, { transport, socketFactory, ...extra });
28
35
  return { client, host };
29
36
  }