Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 143x 60x 60x 60x 60x 60x 29x 4x 4x 3x 3x 3x 3x 3x 3x 4x 60x 31x 31x 60x 60x 143x 143x 30x 30x 30x 30x 30x 26x 26x 26x 26x 26x 26x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 1x 1x 1x 2x 4x 4x 143x 143x 143x 143x 143x 143x 143x 19x 19x 19x 19x 19x 19x 11x 19x 19x 19x 19x 19x 19x 143x 143x 143x 11x 11x 11x 11x 7x 7x 11x 11x 11x 11x 11x 11x 143x 143x 143x 143x 80x 80x 80x 80x 13x 13x 13x 13x 13x 13x 13x 13x 143x 143x 143x 143x 143x 143x 143x 143x 143x 194x 143x 143x | import { Notification, ObjectReference, User } from '../interfaces';
import { Events } from '../events/eventRenderers';
import type { StoreAsset } from '../store/Store';
import {
PublicationHandler,
SocketSubscription,
subscribeToSocket
} from './SocketService';
/**
* Typed access to our realtime topics. SocketService owns the shared
* connection and per-channel fan-out; this module owns how topics map to
* channel names, including the page identity (org and user uuids) needed to
* address user-scoped channels, and what each topic publishes.
*
* The identity arrives via temba-store (hydrated from the page template), so
* user-scoped subscriptions requested before the store mounts are queued and
* activate when the context is set. On pages with no authenticated context
* they simply never activate.
*
* Payload types below describe the wire, which is raw JSON - timestamps are
* strings here even where the rendered equivalents in events.ts carry Dates.
*/
export interface RealtimeSubscription {
unsubscribe(): void;
}
/** Anything published on any of our topics. */
export interface RealtimeEvent {
type: string;
}
/**
* org:<org-uuid> - workspace-wide state every component on the page shares.
* An asset changed somewhere, so anything displaying it can update.
*/
export interface AssetChangedEvent extends RealtimeEvent {
type: 'asset_changed';
asset: StoreAsset;
}
export type OrganizationEvent = AssetChangedEvent;
/**
* flow:<flow-uuid> - published once per committed sprint batch while a flow
* is running. It carries no counts, it just says they moved: the editor reads
* the current numbers over http when it sees one.
*/
export interface FlowActivityEvent extends RealtimeEvent {
type: 'activity';
}
export type FlowEvent = FlowActivityEvent;
/**
* history:<contact-uuid> and history:<contact-uuid>:<ticket-uuid> - the
* engine's contact events (the payloads in events.ts, before their dates are
* parsed) plus the ephemeral ones below, which are never persisted.
*/
export interface ContactHistoryEvent extends RealtimeEvent {
uuid?: string;
created_on?: string;
_user?: User;
// the rest of the payload varies by type. The contact-state events - the
// ones we read fields off rather than hand straight to the renderers - are
// typed below; events.ts describes the rendered shape of the others.
// unknown rather than any, so reading a field nobody declared is a type
// error at the point of use instead of silently spreading
[key: string]: unknown;
}
/**
* The events that change what a contact *is*, as opposed to recording
* something that happened to them. These are the ones ContactWatch applies to
* the contact it holds, so their payloads are what a server-side rename would
* break.
*/
export interface ContactNameChangedEvent extends ContactHistoryEvent {
type: Events.CONTACT_NAME_CHANGED;
name: string;
}
export interface ContactLanguageChangedEvent extends ContactHistoryEvent {
type: Events.CONTACT_LANGUAGE_CHANGED;
language: string;
}
export interface ContactStatusChangedEvent extends ContactHistoryEvent {
type: Events.CONTACT_STATUS_CHANGED;
status: string;
}
export interface ContactFlowChangedEvent extends ContactHistoryEvent {
type: Events.CONTACT_FLOW_CHANGED;
flow: ObjectReference | null;
}
export interface ContactLastSeenChangedEvent extends ContactHistoryEvent {
type: Events.CONTACT_LAST_SEEN_CHANGED;
last_seen_on: string;
}
export interface ContactFieldChangedEvent extends ContactHistoryEvent {
type: Events.CONTACT_FIELD_CHANGED;
field: { key: string; name: string };
// engine field values always carry text; typed representations are present
// when the value parses as that type (see goflow's Value)
value: { text: string; datetime?: string; number?: string } | null;
}
export interface ContactGroupsChangedEvent extends ContactHistoryEvent {
type: Events.CONTACT_GROUPS_CHANGED;
groups_added?: ObjectReference[];
groups_removed?: ObjectReference[];
}
export interface ContactURNsChangedEvent extends ContactHistoryEvent {
type: Events.CONTACT_URNS_CHANGED;
urns: string[];
}
export type ContactStateEvent =
| ContactNameChangedEvent
| ContactLanguageChangedEvent
| ContactStatusChangedEvent
| ContactFlowChangedEvent
| ContactLastSeenChangedEvent
| ContactFieldChangedEvent
| ContactGroupsChangedEvent
| ContactURNsChangedEvent;
/**
* Published by agents as they compose, and echoed back to the publisher, so
* consumers filter out their own by _user.
*/
export interface TypingEvent extends ContactHistoryEvent {
type: Events.TYPING_STARTED | Events.TYPING_STOPPED;
direction?: string;
// whatsapp expresses typing as an operation on the contact's last incoming
// message, so publications carry its external id when we have one
msg_external_id?: string;
}
export interface RealtimeContext {
org: string;
user: string;
}
interface PendingSubscription {
resolveChannel: (ctx: RealtimeContext) => string;
onPublication: PublicationHandler;
onSubscribed?: () => void;
sub: SocketSubscription;
cancelled: boolean;
}
let context: RealtimeContext = null;
const pending: PendingSubscription[] = [];
/**
* Sets the page's realtime identity, flushing any subscriptions that were
* waiting on it. Set once per page load - an org switch is a full page
* load, so a real page never changes or clears its context. Passing null is
* a full reset for tests: it discards the context AND any still-queued
* subscriptions, so handles handed out before the reset never activate.
* Returns the previous context.
*/
export const setRealtimeContext = (
ctx: RealtimeContext | null
): RealtimeContext | null => {
const previous = context;
context = ctx;
if (ctx) {
while (pending.length > 0) {
const p = pending.shift();
if (!p.cancelled) {
p.sub = subscribeToSocket(
p.resolveChannel(ctx),
p.onPublication,
p.onSubscribed
);
}
}
} else {
pending.length = 0;
}
return previous;
};
const subscribeWhenReady = (
resolveChannel: (ctx: RealtimeContext) => string,
onPublication: PublicationHandler,
onSubscribed?: () => void
): RealtimeSubscription => {
if (context) {
return subscribeToSocket(
resolveChannel(context),
onPublication,
onSubscribed
);
}
const p: PendingSubscription = {
resolveChannel,
onPublication,
onSubscribed,
sub: null,
cancelled: false
};
pending.push(p);
return {
unsubscribe: () => {
p.cancelled = true;
if (p.sub) {
p.sub.unsubscribe();
p.sub = null;
}
}
};
};
/**
* The current user's notifications in the current workspace. onSubscribed
* fires on every (re)subscribe, including after reconnects, so subscribers
* can catch up on anything missed while offline.
*/
export const subscribeToNotifications = (
onNotification: (notification: Notification) => void,
onSubscribed?: () => void
): RealtimeSubscription => {
return subscribeWhenReady(
(ctx) => `notifications:${ctx.org}:${ctx.user}`,
(data) => onNotification(data as Notification),
onSubscribed
);
};
/**
* Workspace-wide state changes shared by every component on the page.
*/
export const subscribeToOrganization = (
onEvent: (event: OrganizationEvent) => void,
onSubscribed?: () => void
): RealtimeSubscription => {
return subscribeWhenReady(
(ctx) => `org:${ctx.org}`,
(data) => onEvent(data as OrganizationEvent),
onSubscribed
);
};
/**
* Realtime events for a flow open in the editor. Needs no page context
* because the flow UUID uniquely identifies the authorized channel.
*/
export const subscribeToFlow = (
flow: string,
onEvent: (event: FlowEvent) => void,
onSubscribed?: () => void
): RealtimeSubscription => {
return subscribeToSocket(
`flow:${flow}`,
(data) => onEvent(data as FlowEvent),
onSubscribed
);
};
/**
* A contact's history events, or a ticket's detail events when a ticket is
* given. Needs no page context so subscribes immediately.
*/
export const subscribeToContactHistory = (
contact: string,
ticket: string | null,
onEvent: (event: ContactHistoryEvent) => void,
onSubscribed?: () => void
): RealtimeSubscription => {
return subscribeToSocket(
ticket ? `history:${contact}:${ticket}` : `history:${contact}`,
(data) => onEvent(data as ContactHistoryEvent),
onSubscribed
);
};
|