react-native-context-center
Version:
React native package for Impekable context center customer app
301 lines (253 loc) • 11.6 kB
JavaScript
import {NativeModules,NativeAppEventEmitter,Platform} from 'react-native';
const { CCChannels, CCMessages, CCMembers } = NativeModules;
import ChatMessage from './ChatMessage';
import ChatMember from './ChatMember';
import ChatUser from './ChatUser';
import Paginator from './Paginator';
class ClientChannel{
constructor(props) {
this.initializeValues(props)
this.setupEventHandler();
this.initEventHandler();
}
initializeValues(props){
this.sid = props.sid;
this.friendlyName = props.friendlyName;
this.uniqueName = props.uniqueName;
this.attributes = props.attributes;
this.dateCreated = new Date(props.dateCreated);
this.dateUpdated = new Date(props.dateUpdated);
this.createdBy = props.createdBy;
if (props.synchronizationStatus) this.synchronizationStatus = props.synchronizationStatus;
if (props.status) this.status = props.status;
if (props.type) this.type = props.type;
if (props.membersCount) this.membersCount = props.membersCount;
if (props.messagesCount) this.messageCount = props.messagesCount;
}
setupEventHandler(){
this.onSynchronizationStatusUpdated = null;
this.onUpdated = null;
this.onDeleted = null;
this.onMemberJoined = null;
this.onMemberUpdated = null;
this.onMemberLeft = null;
this.onMemberUserInfoUpdated = null;
this.onMessageAdded = null;
this.onMessageUpdated = null;
this.onMessageDeleted = null;
this.onTypingStarted = null;
this.onTypingEnded = null;
this.onChannelNotificationNewMessageReceived = null;
}
initEventHandler(){
this._channelSynchronizationStatusUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:synchronizationStatusUpdated',
({ channelSid, status }) => {
console.log('_channelSynchronizationStatusUpdatedSubscription')
if (channelSid === this.sid && this.onSynchronizationStatusUpdated) this.onSynchronizationStatusUpdated(status);
},
);
this._channelUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:updated',
({ channelSid, updated ,channel }) => {
console.log('_channelUpdatedSubscription')
if (channelSid === this.sid && this.onUpdated) {
this.sid = channelSid;
this.friendlyName = channel.friendlyName;
this.uniqueName = channel.uniqueName;
this.synchronizationStatus = channel.synchronizationStatus;
this.status = channel.status;
this.type = channel.type;
this.attributes = channel.attributes;
this.dateCreated = new Date(channel.dateCreated);
this.dateUpdated = new Date(channel.dateUpdated);
this.createdBy = props.createdBy;
this.onUpdated();
}
},
);
this._channelDeletedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channelDeleted',
(channel) => {
console.log('_channelDeletedSubscription')
if (channel.sid === this.sid && this.onDeleted) this.onDeleted();
},
);
this._channelMemberJoinedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:memberJoined',
({ channelSid, member }) => {
console.log('_channelMemberJoinedSubscription')
if (channelSid === this.sid && this.onMemberJoined) this.onMemberJoined(new ChatMember(member));
},
);
this._channelMemberUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:member:updated',
({ channelSid,updated, member }) => {
console.log('_channelMemberUpdatedSubscription')
if (channelSid === this.sid && this.onMemberChanged) this.onMemberUpdated(new ChatMember(member));
},
);
this._channelMemberLeftSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:memberLeft',
({ channelSid, member }) => {
console.log('_channelMemberLeftSubscription')
if (channelSid === this.sid && this.onMemberLeft) this.onMemberLeft(new ChatMember(member));
},
);
this._channelMessageAddedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:messageAdded',
({ channelSid, message }) => {
console.log('_channelMessageAddedSubscription')
if (channelSid === this.sid && this.onMessageAdded) this.onMessageAdded(new ChatMessage(message, channelSid));
},
);
this._channelMessageUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:message:updated',
({ channelSid, updated, message }) => {
console.log('_channelMessageUpdatedSubscription')
if (channelSid === this.sid && this.onMessageUpdated) this.onMessageUpdated(new ChatMessage(message, channelSid));
},
);
this._channelMessageDeletedSubscription = NativeAppEventEmitter.addListener(
'chatClient:channel:messageDeleted',
({ channelSid, message }) => {
console.log('_channelMessageDeletedSubscription')
if (channelSid === this.sid && this.onMessageDeleted) this.onMessageDeleted(new ChatMessage(message, channelSid));
},
);
this._typingChannelStartedSubscription = NativeAppEventEmitter.addListener(
'chatClient:typingStartedOnChannel:member',
({ channelSid, member }) => {
console.log('_typingChannelStartedSubscription')
if (channelSid === this.sid && this.onTypingStarted) this.onTypingStarted(new ChatMember(member));
},
);
this._typingChannelEndedSubscription = NativeAppEventEmitter.addListener(
'chatClient:typingEndedOnChannel:member',
({ channelSid, member }) => {
console.log('_typingChannelEndedSubscription')
if (channelSid === this.sid && this.onTypingEnded) this.onTypingEnded(new ChatMember(member));
},
);
this._receivedNewMessageNotificationSubscription = NativeAppEventEmitter.addListener(
'chatClient:notificationNewMessageReceivedForChannelSid:messageIndex',
({ channelSid, index }) => {
console.log('_receivedNewMessageNotificationSubscription')
if (channelSid === this.sid && this.onChannelNotificationNewMessageReceived) this.onChannelNotificationNewMessageReceived();
},
);
this._memberUserInfoUpdatedSubscription = NativeAppEventEmitter.addListener(
'chatClient:user:updated',
({ updated, user }) => {
console.log('_memberUserInfoUpdatedSubscription')
if (channelSid === this.sid && this.onMemberUserInfoUpdated) this.onMemberUserInfoUpdated({ updated, userInfo: new ChatUser(user) });
},
);
}
initialize() {
return CCChannels.synchronize(this.sid);
}
setAttributes(attributes) {
this.attributes = attributes;
return CCChannels.setAttributes(this.sid, attributes);
}
setFriendlyName(friendlyName) {
this.friendlyName = friendlyName;
return CCChannels.setFriendlyName(this.sid, friendlyName);
}
setUniqueName(uniqueName) {
this.uniqueName = uniqueName;
return CCChannels.setUniqueName(this.sid, uniqueName);
}
join() {
return CCChannels.joinChannel(this.sid);
}
declineInvitation() {
return CCChannels.declineInvitation(this.sid);
}
leave() {
return CCChannels.leaveChannel(this.sid);
}
delete() {
return CCChannels.deleteChannel(this.sid);
}
typing() {
CCChannels.isTyping(this.sid);
}
getMember(identity) {
return CCChannels.getMember(this.sid, identity).then(member => new ChatMember(member));
}
getMembers() {
return CCMembers.fetchMembers(this.sid).then(({ sid, type, paginator }) => new Paginator(sid, type, paginator));
}
add(identity) {
return CCMembers.addMember(this.sid, identity);
}
invite(identity) {
return CCMembers.inviteMember(this.sid, identity);
}
remove(identity) {
return CCMembers.removeMember(this.sid, identity);
}
getLastConsumedMessageIndex() {
return CCMessages.getLastConsumedMessageIndex(this.sid);
}
sendMessage(body, attributes = null) {
return CCMessages.sendMessage(this.sid, body, attributes);
}
removeMessage(index) {
return CCMessages.removeMessage(this.sid, index);
}
getMessages(count = 10) {
return CCMessages.getLastMessages(this.sid, count).then(messages => messages.map(message => new ChatMessage(message, this.sid)));
}
getMessagesBefore(index, count) {
return CCMessages.getMessagesBefore(this.sid, index, count).then(messages => messages.map(message => new ChatMessage(message, this.sid)));
}
getMessagesAfter(index, count) {
return CCMessages.getMessagesAfter(this.sid, index, count).then(messages => messages.map(message => new ChatMessage(message, this.sid)));
}
getMessage(index) {
return CCMessages.getMessage(this.sid, index).then(message => new ChatMessage(message, this.sid));
}
getMessageForConsumption(index) {
if (Platform.OS !== 'ios') console.warn('getMessageForConsumption is only available on iOS.');
else {
return CCMessages.messageForConsumptionIndex(this.sid, index).then(message => new ChatMessage(message, this.sid));
}
}
setLastConsumedMessageIndex(index) {
CCMessages.setLastConsumedMessageIndex(this.sid, index);
}
advanceLastConsumedMessageIndex(index) {
CCMessages.advanceLastConsumedMessageIndex(this.sid, index);
}
setAllMessagesConsumed() {
CCMessages.setAllMessagesConsumed(this.sid);
}
getUnconsumedMessagesCount() {
return CCChannels.getUnconsumedMessagesCount(this.sid);
}
getMessagesCount() {
return CCChannels.getMessagesCount(this.sid);
}
getMembersCount() {
return CCChannels.getMembersCount(this.sid);
}
removeListeners() {
this._channelSynchronizationStatusUpdatedSubscription.remove();
this._channelUpdatedSubscription.remove();
this._channelDeletedSubscription.remove();
this._channelMemberJoinedSubscription.remove();
this._channelMemberUpdatedSubscription.remove();
this._channelMemberLeftSubscription.remove();
this._channelMessageAddedSubscription.remove();
this._channelMessageUpdatedSubscription.remove();
this._channelMessageDeletedSubscription.remove();
this._typingChannelStartedSubscription.remove();
this._typingChannelEndedSubscription.remove();
this._receivedNewMessageNotificationSubscription.remove();
this._memberUserInfoUpdatedSubscription.remove();
}
}
export default ClientChannel;