react-native-context-center
Version:
React native package for Impekable context center customer app
358 lines (316 loc) • 14.9 kB
JavaScript
import { NativeModules, NativeAppEventEmitter, Platform } from 'react-native';
const {CCClient, CCChannels} = NativeModules
import ChatMessage from './ChatMessage';
import ChatMember from './ChatMember';
import ChatUser from './ChatUser';
import Paginator from './Paginator';
import ClientChannel from './ClientChannel';
class ContextCenter {
constructor(hostname, organization,identity){
// properties
this.hostname = hostname;
this.organization = organization;
this.identity = identity;
this.userInfo = {};
this.constants = {};
this.isReachabilityEnabled = false;
this.getConstants();
this.setupEventHandler();
this._properties = {initialMessageCount: 100};
this.initEventHandler();
}
setupEventHandler(){
this.onClientConnectionStateUpdated = null;
this.onSynchronizationStatusUpdated = null;
this.onChannelAdded = null;
this.onChannelUpdated = null;
this.onChannelSynchronizationStatusUpdated = null;
this.onChannelDeleted = null;
this.onMemberJoined = null;
this.onMemberUpdated = null;
this.onMemberLeft = null;
this.onMessageAdded = null;
this.onMessageUpdated = null;
this.onMessageDeleted = null;
this.onError = null;
this.onTypingStarted = null;
this.onTypingEnded = null;
this.onChannelNotificationNewMessageReceived = null;
this.onNotificationUpdatedBadgeCount = null;
this.onUserUpdated = null;
this.onUserSubscribed = null;
this.onUserUnsubscribed = null;
this.onClientSynchronized = null;
}
initEventHandler(){
this.setupConnectionHandler()
this.setupChannelHandler()
this.setupMemberHandler()
this.setupMessageHandler()
this.setupNotificationHandler()
this.setupUserHandler()
}
setupConnectionHandler(){
this._clientConnectionStateUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:connectionStateUpdated',
(state) => {
console.log('_clientConnectionStateUpdatedSubscription')
if (this.onClientConnectionStateUpdated) this.onClientConnectionStateUpdated(state)
});
this._synchronizationStatusUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:synchronizationStatusUpdated',
(status) => {
console.log('_synchronizationStatusUpdatedSubscription')
this.synchronizationStatus = status
if(status === this.constants.TCHClientSynchronizationStatus.Completed){
CCClient.userInfo()
.then((userInfo) => this.userInfo = new ChatUser(userInfo))
.then(() => {
if(this.onClientSynchronized) this.onClientSynchronized();
});
}
if (this.onSynchronizationStatusUpdated) this.onSynchronizationStatusUpdated(status)
});
}
setupChannelHandler(){
this._channelAddedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channelAdded',
(channel) => {
console.log('_channelAddedSubscription')
if (this.onChannelAdded) this.onChannelAdded(new ClientChannel(channel));
},
);
this._channelUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:updated',
(channelSid,updated,channel) => {
console.log('_channelUpdatedSubscription')
if (this.onChannelUpdated) this.onChannelUpdated({ channelSid, channel: new ClientChannel(channel), updated});
},
);
this._channelSynchronizationStatusUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:synchronizationStatusUpdated',
({ channelSid, status }) => {
console.log('_channelSynchronizationStatusUpdatedSubscription')
if (this.onChannelSynchronizationStatusUpdated) this.onChannelSynchronizationStatusUpdated({channelSid,status});
},
);
this._channelDeletedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channelDeleted',
(channel) => {
console.log('_channelDeletedSubscription')
if (this.onChannelDeleted) this.onChannelDeleted(new ClientChannel(channel));
},
);
}
setupMemberHandler(){
this._channelMemberJoinedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:memberJoined',
({ channelSid, member }) => {
console.log('_channelMemberJoinedSubscription')
if (this.onMemberJoined) this.onMemberJoined({ channelSid, member: new ChatMember(member) });
},
);
this._channelMemberUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:member:updated',
({ channelSid, updated, member }) => {
console.log('_channelMemberUpdatedSubscription')
if (this.onMemberUpdated) this.onMemberUpdated({channelSid, updated, member: new ChatMember(member) });
},
);
this._channelMemberLeftSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:memberLeft',
({ channelSid, member }) => {
console.log('_channelMemberLeftSubscription')
if (this.onMemberLeft) this.onMemberLeft({ channelSid, member: new ChatMember(member) });
},
);
}
setupMessageHandler(){
this._channelMessageAddedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:messageAdded',
({ channelSid, message }) => {
console.log('_channelMessageAddedSubscription')
if (this.onMessageAdded) this.onMessageAdded({ channelSid, message: new ChatMessage(message, channelSid) });
},
);
this._channelMessageUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:message:updated',
({ channelSid, updated, message }) => {
console.log('_channelMessageUpdatedSubscription')
if (this.onMessageUpdated) this.onMessageUpdated({channelSid, updated, message: new ChatMessage(message, channelSid)});
},
);
this._channelMessageDeletedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:messageDeleted',
({ channelSid, message }) => {
console.log('_channelMessageDeletedSubscription')
if (this.onMessageDeleted) this.onMessageDeleted({channelSid, message: new ChatMessage(message, channelSid)});
},
);
this._errorReceivedSubscription = NativeAppEventEmitter.addListener(
'chatClient:errorReceived',
({ error, userInfo }) => {
console.log('_errorReceivedSubscription')
if (this.onError) this.onError({ error, userInfo });
},
);
this._typingChannelStartedSubscription = NativeAppEventEmitter.addListener(
'chatClient:typingStartedOnChannel:member',
({ channelSid, member }) => {
console.log('_typingChannelStartedSubscription')
if (this.onTypingStarted) this.onTypingStarted({ channelSid, member: new ChatMember(member) });
},
);
this._typingChannelEndedSubscription = NativeAppEventEmitter.addListener(
'chatClient:typingEndedOnChannel:member',
({ channelSid, member }) => {
console.log('_typingChannelEndedSubscription')
if (this.onTypingEnded) this.onTypingEnded({ channelSid, member: new ChatMember(member) });
},
);
}
setupNotificationHandler(){
this._channelReceivedNewMessageNotificationSubscription = NativeAppEventEmitter.addListener(
'chatClient:notificationNewMessageReceivedForChannelSid:messageIndex',
({channelSid, index}) => {
console.log('_channelReceivedNewMessageNotificationSubscription')
if (this.onChannelNotificationNewMessageReceived) this.onChannelNotificationNewMessageReceived({channelSid, index});
},
);
this._channelUpdatedBadgeCountNotificationSubscription = NativeAppEventEmitter.addListener(
'chatClient:notificationUpdatedBadgeCount',
({ badgeCount }) => {
console.log('_channelUpdatedBadgeCountNotificationSubscription')
if (this.onNotificationUpdatedBadgeCount) this.onNotificationUpdatedBadgeCount(badgeCount);
},
);
}
setupUserHandler(){
this._userUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:user:updated',
({ user, updated }) => {
console.log('_userUpdatedSubscription')
this.userInfo = new ChatUser(user);
if (this.onUserUpdated) this.onUserUpdated({ userInfo:this.userInfo, updated });
},
);
this._userSubscribedSubscription = NativeAppEventEmitter.addListener(
'chatClient:userSubscribed',
({ user }) => {
console.log('_userSubscribedSubscription')
this.userInfo = new ChatUser(user);
if (this.onUserSubscribed) this.onUserSubscribed({ userInfo: this.userInfo });
},
);
this._userUnsubscribedSubscription = NativeAppEventEmitter.addListener(
'chatClient:userUnsubscribed',
({ user }) => {
console.log('_userUnsubscribedSubscription')
if (this.onUserUnsubscribed) this.onUserUnsubscribed({ userInfo: new ChatUser(user) });
},
);
}
getConstants(){
return CCClient.getConstants().then((constants) => {
this.constants = constants;
return constants;
});
}
fetchToken(){
if (typeof this.hostname != 'string' || this.hostname == '') {
return { hostname:this.hostname, error: 'Invalid hostname, hostname must be a string & non-empty'}
}else if (typeof this.organization != 'string' || this.organization == '') {
return { organization:this.organization, error: 'Invalid organization, organization must be a string & non-empty'}
}else if (typeof this.identity != 'string' || this.identity == '') {
return { identity:this.identity, error: 'Invalid identity / name, organization must be a string & non-empty'}
}else{
// Initalize and fetch user token
const URL = `https://${this.hostname}/organization/${this.organization}/conversation/web-chat/token?identity=${this.identity}`;
return fetch(URL).then((response) => response.json()).then((json) => {
this.token = json.token
return this.token
}).catch((err) => console.log(err))
}
}
// initialize the library with Host name & organization Id
// return {initialized: true} when the initialization started
initialize() {
return CCClient.setupClient(this.token, this._properties).then((client) => {
if(client){
this.version = client.version;
this.synchronizationStatus = client.synchronizationStatus;
this.isReachabilityEnabled = client.isReachabilityEnabled;
}
return true
})
}
getUserChannels() {
return CCChannels.getUserChannels().then(({ channels }) => channels);
}
getPublicChannels() {
return CCChannels.fetchPublicChannels().then(({ sid, type, paginator }) => new Paginator(sid, type, paginator));
}
getChannel(sidOrName) {
return CCChannels.fetchChannel(sidOrName).then(channel => new ClientChannel(channel));
}
createChannel(options) {
const parsedOptions = {};
for (const key in options) {
let newKey = null;
switch(key) {
case 'friendlyName': newKey = this.constants.TCHChannelOption.FriendlyName;
break;
case 'uniqueName': newKey = this.constants.TCHChannelOption.UniqueName;
break;
case 'type': newKey = this.constants.TCHChannelOption.Type;
break;
case 'attributes': newKey = this.constants.TCHChannelOption.Attributes;
break;
}
parsedOptions[newKey] = options[key];
}
return CCChannels.createChannel(parsedOptions).then(channel => new ClientChannel(channel));
}
setLogLevel(logLevel) {
CCClient.enableLogLevel(logLevel);
}
register(token) {
CCClient.registerClient(token);
}
unregister(token) {
CCClient.unregisterClient(token);
}
handleNotification(notification) {
CCClient.handleNotification(notification);
}
close() {
CCClient.close();
if (Platform.OS === 'android') {
CCChannels.close();
}
this._removeListeners();
}
_removeListeners() {
this._clientConnectionStateUpdatedSubscription.remove()
this._synchronizationStatusUpdatedSubscription.remove();
this._channelAddedSubscription.remove();
this._channelUpdatedSubscription.remove();
this._channelDeletedSubscription.remove();
this._channelSynchronizationStatusUpdatedSubscription.remove();
this._channelMemberJoinedSubscription.remove();
this._channelMemberUpdatedSubscription.remove();
this._channelMemberLeftSubscription.remove();
this._channelMessageAddedSubscription.remove();
this._channelMessageUpdatedSubscription.remove();
this._channelMessageDeletedSubscription.remove();
this._errorReceivedSubscription.remove();
this._typingChannelStartedSubscription.remove();
this._typingChannelEndedSubscription.remove();
this._channelReceivedNewMessageNotificationSubscription.remove();
this._channelUpdatedBadgeCountNotificationSubscription.remove();
this._userUpdatedSubscription.remove();
this._userSubscribedSubscription.remove();
this._userUnsubscribedSubscription.remove();
}
}
export default ContextCenter