-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
496 lines (452 loc) · 16.1 KB
/
index.js
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import {
NativeModules,
Platform,
NativeEventEmitter,
DeviceEventEmitter,
} from 'react-native';
const ChannelModule = NativeModules.RNChannelIO;
const ChannelEventEmitter = Platform.select({
ios: new NativeEventEmitter(NativeModules.RNChannelIO),
android: DeviceEventEmitter,
});
var subscribers = {};
const replaceSubscriber = (type, newSubscriber) => {
const oldSubscriber = subscribers[type];
if (oldSubscriber && typeof oldSubscriber.remove === 'function') {
oldSubscriber.remove();
}
subscribers[type] = newSubscriber;
}
const hasSubscriber = (type) => {
return !!subscribers[type]
}
ChannelEventEmitter.addListener(ChannelModule.Event.ON_PRE_URL_CLICKED, (data) => {
if (!hasSubscriber(ChannelModule.Event.ON_URL_CLICKED)) {
ChannelModule.handleUrlClicked(data.url);
} else {
const subscriber = subscribers[ChannelModule.Event.ON_URL_CLICKED];
if (typeof subscriber === 'function') {
subscriber(data);
}
}
});
export const ChannelIO = {
/**
* Boot `ChannelIO`
* Note that in order to use any methods from `ChannelIO`, you have to use this method beforehand
* @param bootConfig BootConfig object contains information for booting
* @returns A promise that returns status and guest info
*/
boot: async (bootConfig) => {
return ChannelModule.boot(bootConfig);
},
/**
* Sleep `ChannelIO`
*/
sleep: () => {
ChannelModule.sleep();
},
/**
* Shutdown `ChannelIO`
*/
shutdown: () => {
ChannelModule.shutdown();
},
/**
* @deprecated
* Show `ChannelIO` button
* @param {Boolean} aniamted Animate the launcher if true
*/
show: (animated) => {
console.log('ChannelIO', 'ChannelIO.show(animated) is deprecated. Please use ChannelIO.showChannelButton()')
ChannelModule.showChannelButton()
},
/**
* Show `ChannelIO` button
*/
showChannelButton: () => ChannelModule.showChannelButton(),
/**
* @deprecated
* Hide `ChannelIO` button
* @param {Boolean} aniamted Animate the launcher if true
*/
hide: (animated) => {
console.log('ChannelIO', 'ChannelIO.hide(animated) is deprecated. Please use ChannelIO.hideChannelButton()')
ChannelModule.hideChannelButton()
},
/**
* Hide `ChannelIO` button
*/
hideChannelButton: () => ChannelModule.hideChannelButton(),
/**
* @deprecated
* Open `ChannelIO` messenger
* @param {Boolean} aniamted Animate messenger if true
*/
open: (animated) => {
console.log('ChannelIO', 'ChannelIO.open(animated) is deprecated. Please use ChannelIO.showMessenger()')
ChannelModule.showMessenger()
},
/**
* Show `ChannelIO` messenger
*/
showMessenger: () => ChannelModule.showMessenger(),
/**
* @deprecated
* Close `ChannelIO` messenger
* @param {Boolean} Animate messenger if true
*/
close: (animated) => {
console.log('ChannelIO', 'ChannelIO.close(animated) is deprecated. Please use ChannelIO.hideMessenger()')
ChannelModule.hideMessenger()
},
/**
* Hide `ChannelIO` messenger
*/
hideMessenger: () => ChannelModule.hideMessenger(),
/**
* Open user chat with given chat id
* @param {String} chatId user chat id
* @param {String} payload auto fill message
*/
openChat: (chatId, payload) => {
if (typeof payload === 'string') {
ChannelModule.openChat(chatId, payload);
} else {
if (typeof payload === 'boolean') {
console.log('ChannelIO', 'ChannelIO.openChat(chatId, animated) is deprecated. Please use ChannelIO.openChat(chatId, message)')
}
ChannelModule.openChat(chatId, undefined);
}
},
/**
* Opens a user chat and starts the specified workflow.
* - If a corresponded workflow with the provided workflowId is exists, it will be executed. if workflowId is invalid, an error page is displayed.
* - If you don't pass workflowId, no action is taken.
* @param {String} workflowId The ID of workflow to start with. An error page will be shown if such workflow does not exist.
*/
openWorkflow: (workflowId) => {
ChannelModule.openWorkflow(workflowId);
},
/**
* Send a event
* @param {String} eventName event name
* @param {Object} properties a json object contains information
*/
track: (eventName, properties) => ChannelModule.track(eventName, properties),
/**
* Update user
* @param userData userData object contains user data information for updating
* @returns A promise that returns exception or user info
*/
updateUser: async (userData) => {
return ChannelModule.updateUser(userData)
},
/**
* Add user tags
* @param tags tags object contains tags information for adding
* @returns A promise that returns exception or user data
*/
addTags: async (tags) => {
return ChannelModule.addTags(tags)
},
/**
* Remove user tags
* @param tags tags object contains tags information for removing
* @returns A promise that returns exception or user data
*/
removeTags: async (tags) => {
return ChannelModule.removeTags(tags)
},
/**
* Check is channel booted
* @returns {Boolean} true if the channel is booted, otherwise false
*/
isBooted: async () => {
return ChannelModule.isBooted()
},
/**
* Set debug mode for native module
* @param {Boolean} enable True if you want to set debug mode, otherwise false
*/
setDebugMode: (enable) => ChannelModule.setDebugMode(enable),
/**
* Initialize push token
* @param {String} token a push token
*/
initPushToken: (token) => ChannelModule.initPushToken(token),
/**
* Check whether a push data is for channel
* @param {Object} userInfo userInfo part from push data
* @returns {Boolean} true if the userInfo indicates `ChannelIO'`s push, otherwise false
*/
isChannelPushNotification: async (userInfo) => ChannelModule.isChannelPushNotification(userInfo),
/**
* @deprecated
* Handle `ChannelIO` push notification
* @param {Object} userInfo userInfo part from push data
*/
handlePushNotification: async (userInfo) => {
console.log('ChannelIO', 'ChannelIO.handlePushNotification(userInfo) is deprecated. Please use ChannelIO.receivePushNotification(userInfo)')
ChannelModule.receivePushNotification(userInfo)
},
/**
* Receive `ChannelIO` push notification
* @param {Object} userInfo userInfo part from push data
*/
receivePushNotification: async (userInfo) => ChannelModule.receivePushNotification(userInfo),
/**
* Check whether a push data has stored
* @returns {Boolean} true if the `ChannelIO'`s push has stored, otherwise false
*/
hasStoredPushNotification: async () => ChannelModule.hasStoredPushNotification(),
/**
* Open stored ChannelIO'` push notification
*/
openStoredPushNotification: () => ChannelModule.openStoredPushNotification(),
/**
* Sets the name of the screen along with user chat profile. If track is called before setPage, the event will not reflect the page information.
* @param {String} page This is the screen name when track is called. When calling .track(), the event's page is set to null.
* @param {String} profile The user chat profile value.
* - When nil is assigned to a specific field within the profile object, only the value of that field is cleared.
* - The user chat profile value is applied when a user chat is created.
*/
setPage: (page, profile) => {
if (typeof page === "string") {
ChannelModule.setPage(page, profile)
} else if (page === null || page === undefined) {
ChannelModule.setPage(null, profile)
} else {
console.error('ChannelIO', '"page" must be type of "string", null or undefined.')
}
},
/**
* Reset page data customized by developer.
*/
resetPage: () => ChannelModule.resetPage(),
/**
* Sets the appearance of the SDK.
* @param {String} appearance system | light | dark
*/
setAppearance: (appearance) => {
if (typeof appearance === "string") {
ChannelModule.setAppearance(appearance)
} else {
console.error('ChannelIO', '"appearance" must be type of "string". ex) "system", "light", "dark"')
}
},
/**
* Hides the Channel popup on the global screen.
*/
hidePopup: () => ChannelModule.hidePopup(),
/**
* @deprecated
* Event listener that triggers when badge count has been changed
* @param {Function} cb a callback function that takes a integer badge count as parameter
*/
onChangeBadge: (cb) => {
console.log('ChannelIO', 'ChannelIO.onChangeBadge(cb) is deprecated. Please use ChannelIO.onBadgeChanged(cb)')
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_BADGE_CHANGED, (data) => {
cb(data.unread, data.alert);
});
replaceSubscriber(ChannelModule.Event.ON_BADGE_CHANGED, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_BADGE_CHANGED, null);
}
},
/**
* Event listener that triggers when badge count has been changed
* @param {Function} cb a callback function that takes a integer badge count as parameter
*/
onBadgeChanged: (cb) => {
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_BADGE_CHANGED, (data) => {
cb(data.unread, data.alert);
});
replaceSubscriber(ChannelModule.Event.ON_BADGE_CHANGED, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_BADGE_CHANGED, null);
}
},
/**
* @deprecated
* Event listener that triggers when in-app popup has been arrived
* @param {Function} cb a callback function that takes a object popup data as parameter
*/
onReceivePush: (cb) => {
console.log('ChannelIO', 'ChannelIO.onReceivePush(cb) is deprecated. Please use ChannelIO.onPopupDataReceived(cb)')
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_POPUP_DATA_RECEIVED, (data) => {
cb(data.popup);
});
replaceSubscriber(ChannelModule.Event.ON_POPUP_DATA_RECEIVED, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_POPUP_DATA_RECEIVED, null);
}
},
/**
* Event listener that triggers when in-app popup has been arrived
* @param {Function} cb a callback function that takes a object popup data as parameter
*/
onPopupDataReceived: (cb) => {
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_POPUP_DATA_RECEIVED, (data) => {
cb(data.popup);
});
replaceSubscriber(ChannelModule.Event.ON_POPUP_DATA_RECEIVED, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_POPUP_DATA_RECEIVED, null);
}
},
/**
* @deprecated
* Event listener that triggers when a link has been clicked by a user
* @param {Boolean} handle True if you want to handle a link, otherwise false
* @param {Function} cb a callback function that takes a string url as parameter
*/
onClickChatLink: (handle, cb) => {
console.log('ChannelIO', 'ChannelIO.onClickChatLink(handle, cb) is deprecated. Please use ChannelIO.onUrlClicked(cb)')
if (cb) {
replaceSubscriber(ChannelModule.Event.ON_URL_CLICKED, (data) => {
if (!handle) {
ChannelModule.handleUrlClicked(data.url);
}
cb(data.url);
});
} else {
replaceSubscriber(ChannelModule.Event.ON_URL_CLICKED, null);
}
},
/**
* Event listener that triggers when a url has been clicked by a user
* @param {Function} cb a callback function that takes a string url as parameter
*/
onUrlClicked: (cb) => {
if (cb) {
replaceSubscriber(ChannelModule.Event.ON_URL_CLICKED, (data) => {
const next = () => {
ChannelModule.handleUrlClicked(data.url);
}
cb(data.url, next);
});
} else {
replaceSubscriber(ChannelModule.Event.ON_URL_CLICKED, null);
}
},
/**
* @deprecated
* 'onChangeProfile' is deprecated. Please use 'onFollowUpChanged'.
*/
onChangeProfile: (cb) => {
console.warn("'onChangeProfile' is deprecated. Please use 'onFollowUpChanged'.")
},
/**
* @deprecated
* 'onProfileChanged' is deprecated. Please use 'onFollowUpChanged'.
*/
onProfileChanged: (cb) => {
console.warn("'onProfileChanged' is deprecated. Please use 'onFollowUpChanged'.")
},
/**
* Event listener that triggers when guest profile is updated
* @param {Function} cb a callback function that takes a map
*/
onFollowUpChanged: (cb) => {
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_FOLLOW_UP_CHANGED, (data) => {
cb(data);
});
replaceSubscriber(ChannelModule.Event.ON_FOLLOW_UP_CHANGED, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_FOLLOW_UP_CHANGED, null);
}
},
/**
* Event listener that triggers when user clicks a system push notification.
* Note that the callback only works on Android. A call to this method on an iOS
* environment will be silently ignored.
*
* @param {Function} cb a callback function
*/
onPushNotificationClicked: (cb) => {
if (Platform.OS !== 'android') { return }
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_PUSH_NOTIFICATION_CLICKED, data => {
const next = () => ChannelModule.performDefaultPushNotificationClickAction(data.userId, data.chatId);
cb(data.chatId, next);
});
ChannelModule.notifyPushNotificationClickSubscriberExistence(true);
replaceSubscriber(ChannelModule.Event.ON_PUSH_NOTIFICATION_CLICKED, subscription);
} else {
ChannelModule.notifyPushNotificationClickSubscriberExistence(false);
replaceSubscriber(ChannelModule.Event.ON_PUSH_NOTIFICATION_CLICKED, null);
}
},
/**
* @deprecated
* Event listener that triggers when `ChannelIO` messenger is about to display
* @param {Function} cb a callback function
*/
willShowMessenger: (cb) => {
console.log('ChannelIO', 'ChannelIO.willShowMessenger(cb) is deprecated. Please use ChannelIO.onShowMessenger(cb)')
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_SHOW_MESSENGER, cb);
replaceSubscriber(ChannelModule.Event.ON_SHOW_MESSENGER, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_SHOW_MESSENGER, null);
}
},
/**
* Event listener that triggers when `ChannelIO` messenger is about to display
* @param {Function} cb a callback function
*/
onShowMessenger: (cb) => {
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_SHOW_MESSENGER, cb);
replaceSubscriber(ChannelModule.Event.ON_SHOW_MESSENGER, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_SHOW_MESSENGER, null);
}
},
/**
* @deprecated
* Event listener that triggers when `ChannelIO` messenger is about to dismiss
* @param {Function} cb a callback function
*/
willHideMessenger: (cb) => {
console.log('ChannelIO', 'ChannelIO.willHideMessenger(cb) is deprecated. Please use ChannelIO.onHideMessenger(cb)')
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_HIDE_MESSENGER, cb);
replaceSubscriber(ChannelModule.Event.ON_HIDE_MESSENGER, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_HIDE_MESSENGER, null);
}
},
/**
* Event listener that triggers when `ChannelIO` messenger is about to dismiss
* @param {Function} cb a callback function
*/
onHideMessenger: (cb) => {
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_HIDE_MESSENGER, cb);
replaceSubscriber(ChannelModule.Event.ON_HIDE_MESSENGER, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_HIDE_MESSENGER, null);
}
},
/**
* Event listener that triggers when a chat has been created by a user
* @param {Function} cb a callback function that takes a string chat id as parameter
*/
onChatCreated: (cb) => {
if (cb) {
const subscription = ChannelEventEmitter.addListener(ChannelModule.Event.ON_CHAT_CREATED, (data) => {
cb(data.chatId);
});
replaceSubscriber(ChannelModule.Event.ON_CHAT_CREATED, subscription);
} else {
replaceSubscriber(ChannelModule.Event.ON_CHAT_CREATED, null);
}
},
}