@cobbai/react-native-sdk 1.0.0-beta.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.
- package/CobbaiMobileRnSdk.podspec +22 -0
- package/LICENSE +32 -0
- package/README.md +386 -0
- package/dist/ChatPlaceholder.d.ts +13 -0
- package/dist/ChatPlaceholder.d.ts.map +1 -0
- package/dist/ChatPlaceholder.js +87 -0
- package/dist/CobbaiChatScreen.d.ts +4 -0
- package/dist/CobbaiChatScreen.d.ts.map +1 -0
- package/dist/CobbaiChatScreen.js +196 -0
- package/dist/CobbaiInAppBanner.d.ts +15 -0
- package/dist/CobbaiInAppBanner.d.ts.map +1 -0
- package/dist/CobbaiInAppBanner.js +159 -0
- package/dist/CobbaiLauncher.d.ts +19 -0
- package/dist/CobbaiLauncher.d.ts.map +1 -0
- package/dist/CobbaiLauncher.js +149 -0
- package/dist/CobbaiWebView.d.ts +28 -0
- package/dist/CobbaiWebView.d.ts.map +1 -0
- package/dist/CobbaiWebView.js +141 -0
- package/dist/adapters/expoAdapter.d.ts +85 -0
- package/dist/adapters/expoAdapter.d.ts.map +1 -0
- package/dist/adapters/expoAdapter.js +152 -0
- package/dist/adapters/firebaseAdapter.d.ts +41 -0
- package/dist/adapters/firebaseAdapter.d.ts.map +1 -0
- package/dist/adapters/firebaseAdapter.js +139 -0
- package/dist/adapters/iosRelayAdapter.d.ts +26 -0
- package/dist/adapters/iosRelayAdapter.d.ts.map +1 -0
- package/dist/adapters/iosRelayAdapter.js +101 -0
- package/dist/androidKeyboardFrame.d.ts +16 -0
- package/dist/androidKeyboardFrame.d.ts.map +1 -0
- package/dist/androidKeyboardFrame.js +34 -0
- package/dist/bridge/messages.d.ts +20 -0
- package/dist/bridge/messages.d.ts.map +1 -0
- package/dist/bridge/messages.js +20 -0
- package/dist/bridge/nativeEvent.d.ts +10 -0
- package/dist/bridge/nativeEvent.d.ts.map +1 -0
- package/dist/bridge/nativeEvent.js +15 -0
- package/dist/bridge/parser.d.ts +29 -0
- package/dist/bridge/parser.d.ts.map +1 -0
- package/dist/bridge/parser.js +109 -0
- package/dist/chatPresentation.d.ts +19 -0
- package/dist/chatPresentation.d.ts.map +1 -0
- package/dist/chatPresentation.js +51 -0
- package/dist/chatScreen/useInAppBannerQueue.d.ts +22 -0
- package/dist/chatScreen/useInAppBannerQueue.d.ts.map +1 -0
- package/dist/chatScreen/useInAppBannerQueue.js +60 -0
- package/dist/chatScreen/useKeyboardFrame.d.ts +14 -0
- package/dist/chatScreen/useKeyboardFrame.d.ts.map +1 -0
- package/dist/chatScreen/useKeyboardFrame.js +63 -0
- package/dist/chatScreen/useLauncherPresentation.d.ts +20 -0
- package/dist/chatScreen/useLauncherPresentation.d.ts.map +1 -0
- package/dist/chatScreen/useLauncherPresentation.js +76 -0
- package/dist/chatScreen/useNativeToWebSync.d.ts +17 -0
- package/dist/chatScreen/useNativeToWebSync.d.ts.map +1 -0
- package/dist/chatScreen/useNativeToWebSync.js +36 -0
- package/dist/chatScreen/usePushIntegration.d.ts +19 -0
- package/dist/chatScreen/usePushIntegration.d.ts.map +1 -0
- package/dist/chatScreen/usePushIntegration.js +129 -0
- package/dist/chatScreen/useReadyLifecycle.d.ts +33 -0
- package/dist/chatScreen/useReadyLifecycle.d.ts.map +1 -0
- package/dist/chatScreen/useReadyLifecycle.js +138 -0
- package/dist/chatScreen/useSheetPresentation.d.ts +25 -0
- package/dist/chatScreen/useSheetPresentation.d.ts.map +1 -0
- package/dist/chatScreen/useSheetPresentation.js +98 -0
- package/dist/chatUnavailable.d.ts +23 -0
- package/dist/chatUnavailable.d.ts.map +1 -0
- package/dist/chatUnavailable.js +36 -0
- package/dist/externalLinks.d.ts +4 -0
- package/dist/externalLinks.d.ts.map +1 -0
- package/dist/externalLinks.js +48 -0
- package/dist/host/buildHostHtml.d.ts +7 -0
- package/dist/host/buildHostHtml.d.ts.map +1 -0
- package/dist/host/buildHostHtml.js +101 -0
- package/dist/host/hostPageKey.d.ts +7 -0
- package/dist/host/hostPageKey.d.ts.map +1 -0
- package/dist/host/hostPageKey.js +18 -0
- package/dist/inAppBanner.d.ts +19 -0
- package/dist/inAppBanner.d.ts.map +1 -0
- package/dist/inAppBanner.js +42 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/launcher.d.ts +25 -0
- package/dist/launcher.d.ts.map +1 -0
- package/dist/launcher.js +55 -0
- package/dist/launcherGlyph.d.ts +2 -0
- package/dist/launcherGlyph.d.ts.map +1 -0
- package/dist/launcherGlyph.js +5 -0
- package/dist/openChatScreen.d.ts +13 -0
- package/dist/openChatScreen.d.ts.map +1 -0
- package/dist/openChatScreen.js +63 -0
- package/dist/push.d.ts +59 -0
- package/dist/push.d.ts.map +1 -0
- package/dist/push.js +309 -0
- package/dist/pushIntegration.d.ts +76 -0
- package/dist/pushIntegration.d.ts.map +1 -0
- package/dist/pushIntegration.js +244 -0
- package/dist/pushLogout.d.ts +57 -0
- package/dist/pushLogout.d.ts.map +1 -0
- package/dist/pushLogout.js +225 -0
- package/dist/types.d.ts +42 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +10 -0
- package/ios/CobbaiPush.m +96 -0
- package/ios/CobbaiPushRelay.h +59 -0
- package/ios/CobbaiPushRelay.m +364 -0
- package/package.json +69 -0
- package/src/ChatPlaceholder.tsx +132 -0
- package/src/CobbaiChatScreen.tsx +348 -0
- package/src/CobbaiInAppBanner.tsx +237 -0
- package/src/CobbaiLauncher.tsx +223 -0
- package/src/CobbaiWebView.tsx +242 -0
- package/src/adapters/expoAdapter.ts +227 -0
- package/src/adapters/firebaseAdapter.ts +201 -0
- package/src/adapters/iosRelayAdapter.ts +137 -0
- package/src/androidKeyboardFrame.ts +56 -0
- package/src/bridge/messages.ts +24 -0
- package/src/bridge/nativeEvent.ts +12 -0
- package/src/bridge/parser.ts +159 -0
- package/src/chatPresentation.ts +73 -0
- package/src/chatScreen/useInAppBannerQueue.ts +91 -0
- package/src/chatScreen/useKeyboardFrame.ts +89 -0
- package/src/chatScreen/useLauncherPresentation.ts +121 -0
- package/src/chatScreen/useNativeToWebSync.ts +54 -0
- package/src/chatScreen/usePushIntegration.ts +191 -0
- package/src/chatScreen/useReadyLifecycle.ts +186 -0
- package/src/chatScreen/useSheetPresentation.ts +147 -0
- package/src/chatUnavailable.ts +58 -0
- package/src/externalLinks.ts +56 -0
- package/src/host/buildHostHtml.ts +117 -0
- package/src/host/hostPageKey.ts +17 -0
- package/src/inAppBanner.ts +58 -0
- package/src/index.ts +53 -0
- package/src/launcher.ts +79 -0
- package/src/launcherGlyph.ts +3 -0
- package/src/openChatScreen.ts +78 -0
- package/src/push.ts +424 -0
- package/src/pushIntegration.ts +338 -0
- package/src/pushLogout.ts +277 -0
- package/src/types.ts +46 -0
- package/src/version.ts +8 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PUSH_LOGOUT_STORAGE_KEY = exports.PUSH_LOGOUT_TIMEOUT_MS = void 0;
|
|
4
|
+
exports.setPushLogoutStorage = setPushLogoutStorage;
|
|
5
|
+
exports.rememberUnregisterSecret = rememberUnregisterSecret;
|
|
6
|
+
exports.logoutCobbai = logoutCobbai;
|
|
7
|
+
exports.__resetPushLogoutForTests = __resetPushLogoutForTests;
|
|
8
|
+
exports.__getPushLogoutStateForTests = __getPushLogoutStateForTests;
|
|
9
|
+
const buildHostHtml_1 = require("./host/buildHostHtml");
|
|
10
|
+
const version_1 = require("./version");
|
|
11
|
+
const STATE_KEY = '__COBBAI_PUSH_LOGOUT__';
|
|
12
|
+
/**
|
|
13
|
+
* A logout that got no answer by then is kept and retried later. Without it, a server or captive
|
|
14
|
+
* portal that never answers would hold this request, every logout queued behind it, and
|
|
15
|
+
* `logoutCobbai()`, forever (React Native's fetch has no default timeout).
|
|
16
|
+
*/
|
|
17
|
+
exports.PUSH_LOGOUT_TIMEOUT_MS = 15000;
|
|
18
|
+
exports.PUSH_LOGOUT_STORAGE_KEY = 'cobbai.push.logout.v1';
|
|
19
|
+
const PUSH_TOKENS_PATH = '/connectors/chats/push_tokens';
|
|
20
|
+
function getState() {
|
|
21
|
+
const globalObject = globalThis;
|
|
22
|
+
if (!globalObject[STATE_KEY]) {
|
|
23
|
+
globalObject[STATE_KEY] = {
|
|
24
|
+
storage: null,
|
|
25
|
+
loaded: null,
|
|
26
|
+
registrations: [],
|
|
27
|
+
pending: [],
|
|
28
|
+
flushing: null,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return globalObject[STATE_KEY];
|
|
32
|
+
}
|
|
33
|
+
function registrationKey(registration) {
|
|
34
|
+
return [
|
|
35
|
+
registration.baseUrl,
|
|
36
|
+
registration.chatId,
|
|
37
|
+
registration.provider,
|
|
38
|
+
registration.token,
|
|
39
|
+
].join('|');
|
|
40
|
+
}
|
|
41
|
+
/** Keeps the last entry per key, in the order of their last appearance. */
|
|
42
|
+
function uniqueLatest(registrations) {
|
|
43
|
+
const byKey = new Map();
|
|
44
|
+
registrations.forEach((registration) => {
|
|
45
|
+
const key = registrationKey(registration);
|
|
46
|
+
byKey.delete(key);
|
|
47
|
+
byKey.set(key, registration);
|
|
48
|
+
});
|
|
49
|
+
return [...byKey.values()];
|
|
50
|
+
}
|
|
51
|
+
function isRegistration(value) {
|
|
52
|
+
if (!value || typeof value !== 'object')
|
|
53
|
+
return false;
|
|
54
|
+
const record = value;
|
|
55
|
+
return (typeof record.baseUrl === 'string' &&
|
|
56
|
+
typeof record.chatId === 'string' &&
|
|
57
|
+
typeof record.token === 'string' &&
|
|
58
|
+
(record.provider === 'apns' || record.provider === 'fcm') &&
|
|
59
|
+
typeof record.secret === 'string');
|
|
60
|
+
}
|
|
61
|
+
function warn(message) {
|
|
62
|
+
if (__DEV__) {
|
|
63
|
+
console.warn(`[cobbai][push][logout] ${message}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function errorMessage(error) {
|
|
67
|
+
return error instanceof Error ? error.message : String(error);
|
|
68
|
+
}
|
|
69
|
+
/** Merges what the storage holds into memory, once per storage. */
|
|
70
|
+
function load(state) {
|
|
71
|
+
if (state.loaded)
|
|
72
|
+
return state.loaded;
|
|
73
|
+
const storage = state.storage;
|
|
74
|
+
state.loaded = (async () => {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
if (!storage)
|
|
77
|
+
return;
|
|
78
|
+
try {
|
|
79
|
+
const raw = await storage.getItem(exports.PUSH_LOGOUT_STORAGE_KEY);
|
|
80
|
+
if (!raw)
|
|
81
|
+
return;
|
|
82
|
+
const stored = JSON.parse(raw);
|
|
83
|
+
const storedRegistrations = ((_a = stored.registrations) !== null && _a !== void 0 ? _a : []).filter(isRegistration);
|
|
84
|
+
const storedPending = ((_b = stored.pending) !== null && _b !== void 0 ? _b : []).filter(isRegistration);
|
|
85
|
+
// Memory is newer than the storage.
|
|
86
|
+
state.registrations = uniqueLatest([...storedRegistrations, ...state.registrations]);
|
|
87
|
+
state.pending = uniqueLatest([...storedPending, ...state.pending]);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
warn(`could not read the stored logout state: ${errorMessage(error)}`);
|
|
91
|
+
}
|
|
92
|
+
})();
|
|
93
|
+
return state.loaded;
|
|
94
|
+
}
|
|
95
|
+
async function persist(state) {
|
|
96
|
+
if (!state.storage)
|
|
97
|
+
return;
|
|
98
|
+
try {
|
|
99
|
+
await state.storage.setItem(exports.PUSH_LOGOUT_STORAGE_KEY, JSON.stringify({ registrations: state.registrations, pending: state.pending }));
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
warn(`could not store the logout state: ${errorMessage(error)}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** The server answered for good: a network error, 429 or 5xx is tried again later. */
|
|
106
|
+
function isFinalAnswer(status) {
|
|
107
|
+
return status !== 429 && status < 500;
|
|
108
|
+
}
|
|
109
|
+
async function sendLogout(registration) {
|
|
110
|
+
const controller = typeof AbortController === 'function' ? new AbortController() : null;
|
|
111
|
+
let timer = null;
|
|
112
|
+
// Settles even where fetch ignores the abort signal.
|
|
113
|
+
const timeout = new Promise((resolve) => {
|
|
114
|
+
timer = setTimeout(() => {
|
|
115
|
+
controller === null || controller === void 0 ? void 0 : controller.abort();
|
|
116
|
+
resolve(null);
|
|
117
|
+
}, exports.PUSH_LOGOUT_TIMEOUT_MS);
|
|
118
|
+
});
|
|
119
|
+
try {
|
|
120
|
+
const request = fetch(`${registration.baseUrl}${PUSH_TOKENS_PATH}`, {
|
|
121
|
+
method: 'DELETE',
|
|
122
|
+
// No cookies: on Android the native HTTP client shares the WebView's cookie jar, and the chat
|
|
123
|
+
// session cookie must not travel with another User-Agent (the secret is the only proof).
|
|
124
|
+
credentials: 'omit',
|
|
125
|
+
headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
|
|
126
|
+
body: JSON.stringify({
|
|
127
|
+
chat_id: registration.chatId,
|
|
128
|
+
token: registration.token,
|
|
129
|
+
provider: registration.provider,
|
|
130
|
+
unregister_secret: registration.secret,
|
|
131
|
+
sdk_version: version_1.SDK_VERSION,
|
|
132
|
+
}),
|
|
133
|
+
signal: controller === null || controller === void 0 ? void 0 : controller.signal,
|
|
134
|
+
});
|
|
135
|
+
// An abort or network error after the timeout must not surface as an unhandled rejection.
|
|
136
|
+
request.catch(() => { });
|
|
137
|
+
const response = await Promise.race([request, timeout]);
|
|
138
|
+
return response !== null && isFinalAnswer(response.status);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
if (timer)
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** Sends the pending logouts. Runs one after the other, so a logout added during a run is sent too. */
|
|
149
|
+
function flush(state) {
|
|
150
|
+
var _a;
|
|
151
|
+
const run = ((_a = state.flushing) !== null && _a !== void 0 ? _a : Promise.resolve()).then(() => sendPending(state));
|
|
152
|
+
state.flushing = run;
|
|
153
|
+
return run.finally(() => {
|
|
154
|
+
if (state.flushing === run)
|
|
155
|
+
state.flushing = null;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
async function sendPending(state) {
|
|
159
|
+
await load(state);
|
|
160
|
+
if (state.pending.length === 0)
|
|
161
|
+
return;
|
|
162
|
+
const answered = [];
|
|
163
|
+
for (const registration of [...state.pending]) {
|
|
164
|
+
if (await sendLogout(registration)) {
|
|
165
|
+
answered.push(registrationKey(registration));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (answered.length > 0) {
|
|
169
|
+
state.pending = state.pending.filter((item) => !answered.includes(registrationKey(item)));
|
|
170
|
+
await persist(state);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Called by `configureCobbaiPush`: loads what the storage holds and retries pending logouts. */
|
|
174
|
+
function setPushLogoutStorage(storage) {
|
|
175
|
+
const state = getState();
|
|
176
|
+
if (state.storage !== storage) {
|
|
177
|
+
state.storage = storage;
|
|
178
|
+
state.loaded = null;
|
|
179
|
+
}
|
|
180
|
+
return flush(state);
|
|
181
|
+
}
|
|
182
|
+
/** A register the server accepted, with its secret (`cobbai:push-token-registered`). */
|
|
183
|
+
function rememberUnregisterSecret(registration) {
|
|
184
|
+
const state = getState();
|
|
185
|
+
const entry = { ...registration, baseUrl: (0, buildHostHtml_1.normalizeBaseUrl)(registration.baseUrl) };
|
|
186
|
+
state.registrations = uniqueLatest([...state.registrations, entry]);
|
|
187
|
+
void load(state)
|
|
188
|
+
.then(() => {
|
|
189
|
+
state.registrations = uniqueLatest([...state.registrations, entry]);
|
|
190
|
+
return persist(state);
|
|
191
|
+
})
|
|
192
|
+
// A register means the device is online: a good time to retry a pending logout.
|
|
193
|
+
.then(() => flush(state));
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Call it when the user logs out of your app: unregisters this device's push token from Cobbai,
|
|
197
|
+
* without the chat. Your app's own notifications are not affected. Then give the chat
|
|
198
|
+
* screen the next user's config (or the anonymous one): on its next ready, the SDK registers the token
|
|
199
|
+
* again for whoever the chat user is then.
|
|
200
|
+
*
|
|
201
|
+
* Resolves once every logout got an answer or was kept to be retried (no network, or no answer
|
|
202
|
+
* within PUSH_LOGOUT_TIMEOUT_MS): it never rejects.
|
|
203
|
+
*/
|
|
204
|
+
async function logoutCobbai() {
|
|
205
|
+
const state = getState();
|
|
206
|
+
await load(state);
|
|
207
|
+
if (state.registrations.length > 0) {
|
|
208
|
+
state.pending = uniqueLatest([...state.pending, ...state.registrations]);
|
|
209
|
+
state.registrations = [];
|
|
210
|
+
await persist(state);
|
|
211
|
+
}
|
|
212
|
+
await flush(state);
|
|
213
|
+
}
|
|
214
|
+
function __resetPushLogoutForTests() {
|
|
215
|
+
const state = getState();
|
|
216
|
+
state.storage = null;
|
|
217
|
+
state.loaded = null;
|
|
218
|
+
state.registrations = [];
|
|
219
|
+
state.pending = [];
|
|
220
|
+
state.flushing = null;
|
|
221
|
+
}
|
|
222
|
+
function __getPushLogoutStateForTests() {
|
|
223
|
+
const state = getState();
|
|
224
|
+
return { registrations: [...state.registrations], pending: [...state.pending] };
|
|
225
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type CobbaiMetadata = Record<string, string | number | boolean | null>;
|
|
2
|
+
export type CobbaiError = {
|
|
3
|
+
code: string;
|
|
4
|
+
message: string;
|
|
5
|
+
};
|
|
6
|
+
/** CSS pixels, integers, non-negative. */
|
|
7
|
+
export type CobbaiSafeAreaInsets = {
|
|
8
|
+
top: number;
|
|
9
|
+
right: number;
|
|
10
|
+
bottom: number;
|
|
11
|
+
left: number;
|
|
12
|
+
};
|
|
13
|
+
export type CobbaiNewMessageEvent = {
|
|
14
|
+
conversationId: number;
|
|
15
|
+
messageId: number;
|
|
16
|
+
speaker: 'agent' | 'auto';
|
|
17
|
+
senderName?: string;
|
|
18
|
+
preview?: string;
|
|
19
|
+
/** True while the chat surface is open (between openChatScreen() and the close request). */
|
|
20
|
+
chatVisible: boolean;
|
|
21
|
+
};
|
|
22
|
+
export type CobbaiChatConfig = {
|
|
23
|
+
baseUrl: string;
|
|
24
|
+
chatId: string;
|
|
25
|
+
authToken?: string;
|
|
26
|
+
userId?: string;
|
|
27
|
+
metadata?: CobbaiMetadata;
|
|
28
|
+
locale?: string;
|
|
29
|
+
onReady?: () => void;
|
|
30
|
+
onClose?: () => void;
|
|
31
|
+
onError?: (error: CobbaiError) => void;
|
|
32
|
+
onNewMessage?: (event: CobbaiNewMessageEvent) => void;
|
|
33
|
+
onUnreadCountChange?: (total: number) => void;
|
|
34
|
+
};
|
|
35
|
+
export type CobbaiChatScreenProps = {
|
|
36
|
+
config: CobbaiChatConfig;
|
|
37
|
+
prewarm?: boolean;
|
|
38
|
+
launcherVisible?: boolean;
|
|
39
|
+
launcherBottomOffset?: number;
|
|
40
|
+
inAppBanner?: boolean;
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,CAAC;AAE9E,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,0CAA0C;AAC1C,MAAM,MAAM,oBAAoB,GAAG;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4FAA4F;IAC5F,WAAW,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,cAAc,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IACvC,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,qBAAqB,KAAK,IAAI,CAAC;IACtD,mBAAmB,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,MAAM,EAAE,gBAAgB,CAAC;IACzB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version of this package, sent to the Cobbai server so that it stays compatible with this release.
|
|
3
|
+
* Must equal `version` in package.json (checked by a test): bump both together.
|
|
4
|
+
*/
|
|
5
|
+
export declare const SDK_VERSION = "1.0.0-beta.0";
|
|
6
|
+
/** Version of the protocol between the SDK and the Cobbai server that this release implements. Internal: not exported. */
|
|
7
|
+
export declare const CONTRACT_VERSION = "1.16";
|
|
8
|
+
//# sourceMappingURL=version.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,WAAW,iBAAiB,CAAC;AAE1C,0HAA0H;AAC1H,eAAO,MAAM,gBAAgB,SAAS,CAAC"}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONTRACT_VERSION = exports.SDK_VERSION = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Version of this package, sent to the Cobbai server so that it stays compatible with this release.
|
|
6
|
+
* Must equal `version` in package.json (checked by a test): bump both together.
|
|
7
|
+
*/
|
|
8
|
+
exports.SDK_VERSION = '1.0.0-beta.0';
|
|
9
|
+
/** Version of the protocol between the SDK and the Cobbai server that this release implements. Internal: not exported. */
|
|
10
|
+
exports.CONTRACT_VERSION = '1.16';
|
package/ios/CobbaiPush.m
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#import <React/RCTBridgeModule.h>
|
|
2
|
+
#import <React/RCTEventEmitter.h>
|
|
3
|
+
#import <UIKit/UIKit.h>
|
|
4
|
+
|
|
5
|
+
#import "CobbaiPushRelay.h"
|
|
6
|
+
|
|
7
|
+
/// JavaScript side of the iPhone relay (`src/adapters/iosRelayAdapter.ts`). A bridge module: it runs
|
|
8
|
+
/// on the New Architecture through the interop layer.
|
|
9
|
+
@interface CobbaiPush : RCTEventEmitter <RCTBridgeModule>
|
|
10
|
+
@end
|
|
11
|
+
|
|
12
|
+
@implementation CobbaiPush {
|
|
13
|
+
BOOL _hasListeners;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
RCT_EXPORT_MODULE(CobbaiPush)
|
|
17
|
+
|
|
18
|
+
+ (BOOL)requiresMainQueueSetup {
|
|
19
|
+
return NO;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
- (dispatch_queue_t)methodQueue {
|
|
23
|
+
return dispatch_get_main_queue();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
- (NSArray<NSString *> *)supportedEvents {
|
|
27
|
+
return @[ CobbaiPushTokenEvent, CobbaiPushTapEvent, CobbaiPushForegroundEvent ];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
- (void)startObserving {
|
|
31
|
+
_hasListeners = YES;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
- (void)stopObserving {
|
|
35
|
+
_hasListeners = NO;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
- (void)invalidate {
|
|
39
|
+
[[CobbaiPushRelay shared] detachSink];
|
|
40
|
+
[super invalidate];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// Attaches JavaScript to the relay. JavaScript adds its listeners first.
|
|
44
|
+
RCT_EXPORT_METHOD(start : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject) {
|
|
45
|
+
__weak CobbaiPush *weakSelf = self;
|
|
46
|
+
NSDictionary *initial = [[CobbaiPushRelay shared] attachSink:^(NSString *name, NSDictionary *body) {
|
|
47
|
+
CobbaiPush *strongSelf = weakSelf;
|
|
48
|
+
if (strongSelf != nil && strongSelf->_hasListeners) {
|
|
49
|
+
[strongSelf sendEventWithName:name body:body];
|
|
50
|
+
}
|
|
51
|
+
}];
|
|
52
|
+
|
|
53
|
+
[[UNUserNotificationCenter currentNotificationCenter]
|
|
54
|
+
getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
|
|
55
|
+
NSMutableDictionary *result = [initial mutableCopy];
|
|
56
|
+
result[@"status"] = [CobbaiPushRelay statusForAuthorization:settings.authorizationStatus];
|
|
57
|
+
resolve(result);
|
|
58
|
+
}];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
RCT_EXPORT_METHOD(setPresentationState : (NSDictionary *)state) {
|
|
62
|
+
id chatId = state[@"mountedChatId"];
|
|
63
|
+
[[CobbaiPushRelay shared] setMountedChatId:[chatId isKindOfClass:[NSString class]] ? chatId : nil
|
|
64
|
+
connected:[state[@"connected"] boolValue]
|
|
65
|
+
bannerEnabled:[state[@"bannerEnabled"] boolValue]];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
RCT_EXPORT_METHOD(getPermissionStatus : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject) {
|
|
69
|
+
[[UNUserNotificationCenter currentNotificationCenter]
|
|
70
|
+
getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
|
|
71
|
+
resolve([CobbaiPushRelay statusForAuthorization:settings.authorizationStatus]);
|
|
72
|
+
}];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/// Asks the user (only when the app asks through the SDK), then registers with APNs if allowed.
|
|
76
|
+
RCT_EXPORT_METHOD(requestPermission : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject) {
|
|
77
|
+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
78
|
+
UNAuthorizationOptions options = UNAuthorizationOptionAlert | UNAuthorizationOptionSound;
|
|
79
|
+
|
|
80
|
+
[center requestAuthorizationWithOptions:options
|
|
81
|
+
completionHandler:^(BOOL granted, NSError *_Nullable error) {
|
|
82
|
+
[[CobbaiPushRelay shared] registerIfAuthorized];
|
|
83
|
+
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
|
|
84
|
+
resolve([CobbaiPushRelay statusForAuthorization:settings.authorizationStatus]);
|
|
85
|
+
}];
|
|
86
|
+
}];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/// Installs the relay again after another library took the delegate without chaining.
|
|
90
|
+
RCT_EXPORT_METHOD(reinstallRelay : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject) {
|
|
91
|
+
CobbaiPushRelay *relay = [CobbaiPushRelay shared];
|
|
92
|
+
[relay install];
|
|
93
|
+
resolve(@([relay isInPlace]));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
#import <UserNotifications/UserNotifications.h>
|
|
3
|
+
|
|
4
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
5
|
+
|
|
6
|
+
/// Event names delivered to JavaScript (see `CobbaiPush`).
|
|
7
|
+
extern NSString *const CobbaiPushTokenEvent;
|
|
8
|
+
extern NSString *const CobbaiPushTapEvent;
|
|
9
|
+
extern NSString *const CobbaiPushForegroundEvent;
|
|
10
|
+
|
|
11
|
+
typedef void (^CobbaiPushRelayEventSink)(NSString *name, NSDictionary *body);
|
|
12
|
+
|
|
13
|
+
/// Native relay of Cobbai pushes on iPhone.
|
|
14
|
+
///
|
|
15
|
+
/// - Becomes the `UNUserNotificationCenter` delegate at launch and keeps the previous one: Cobbai
|
|
16
|
+
/// pushes are handled here, every call about any other notification is forwarded unchanged.
|
|
17
|
+
/// - Hooks the app delegate's APNs registration callbacks, chaining the app's own implementation,
|
|
18
|
+
/// to obtain the device token; registers for remote notifications at launch when notifications
|
|
19
|
+
/// are already authorized (never prompts).
|
|
20
|
+
/// - Decides what the open app shows for a Cobbai push from the state JavaScript pushes in.
|
|
21
|
+
/// - Keeps the taps that arrive before JavaScript listens (the tap that launched the app).
|
|
22
|
+
///
|
|
23
|
+
/// Installed automatically unless the app's Info.plist sets `CobbaiPushAutoInstall` to NO.
|
|
24
|
+
@interface CobbaiPushRelay : NSObject <UNUserNotificationCenterDelegate>
|
|
25
|
+
|
|
26
|
+
+ (instancetype)shared;
|
|
27
|
+
|
|
28
|
+
/// Becomes the notification-centre delegate (unless already in place) and hooks the app delegate.
|
|
29
|
+
/// Idempotent; main thread.
|
|
30
|
+
- (void)install;
|
|
31
|
+
|
|
32
|
+
/// YES when the relay is the centre's delegate, or a library known to chain (Firebase, Expo) is.
|
|
33
|
+
- (BOOL)isInPlace;
|
|
34
|
+
|
|
35
|
+
/// The foreground state: the mounted chat, whether it is connected, whether the in-app banner can show.
|
|
36
|
+
- (void)setMountedChatId:(nullable NSString *)chatId
|
|
37
|
+
connected:(BOOL)connected
|
|
38
|
+
bannerEnabled:(BOOL)bannerEnabled;
|
|
39
|
+
|
|
40
|
+
/// Starts delivering events to `sink` and returns what is known so far:
|
|
41
|
+
/// `{ token: string | null, apnsEnvironment: string, pendingTaps: object[], inPlace: bool }`.
|
|
42
|
+
- (NSDictionary *)attachSink:(CobbaiPushRelayEventSink)sink;
|
|
43
|
+
- (void)detachSink;
|
|
44
|
+
|
|
45
|
+
- (void)didRegisterWithDeviceToken:(NSData *)deviceToken;
|
|
46
|
+
- (void)didFailToRegisterWithError:(NSError *)error;
|
|
47
|
+
|
|
48
|
+
/// Registers for remote notifications when the user has allowed notifications.
|
|
49
|
+
- (void)registerIfAuthorized;
|
|
50
|
+
|
|
51
|
+
/// `sandbox` or `production`, from the app's embedded provisioning profile.
|
|
52
|
+
+ (NSString *)apnsEnvironment;
|
|
53
|
+
|
|
54
|
+
/// `not-asked`, `granted` or `denied`.
|
|
55
|
+
+ (NSString *)statusForAuthorization:(UNAuthorizationStatus)status;
|
|
56
|
+
|
|
57
|
+
@end
|
|
58
|
+
|
|
59
|
+
NS_ASSUME_NONNULL_END
|