Last active
April 30, 2018 07:57
-
-
Save avastamin/5baef083bb48131124334d28cc88356c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const authenticateWithTwilio = () => (dispatch) => { | |
dispatch(fetchTwilioRequest()); | |
return API.getTwilioToken() | |
.then((response) => { | |
dispatch(authenticateWithTwilioSuccess(response)); | |
Client.create(response.token).then((chatClient) => { | |
twilioChat.chatClient = chatClient; | |
dispatch(initChat(chatClient)); | |
chatClient.on('messageAdded', message => dispatch(sendNewMessageSuccess(message))); | |
}); | |
}) | |
.catch(() => { | |
dispatch(fetchTwilioError()); | |
}); | |
}; | |
const initChat = chatClient => (dispatch) => { | |
dispatch(chatInitializeSuccess()); | |
dispatch(fetchChannelList(chatClient)); | |
}; | |
/* | |
// Currenlty using (moved join to backend ) | |
const fetchChannelList = chatClient => (dispatch) => { | |
fetchAllChannels(chatClient).then((channels) => { | |
dispatch(fetchChannelListSuccess(normalizeChannelSidToChannel(channels))); | |
channels.map(channelDescriptor => channelDescriptor.getChannel() | |
.then(channel => dispatch(fetchChannelMessages(channel, chatClient)))); | |
dispatch(fetchAllMessagesCompleteSuccess()); | |
}); | |
}; | |
*/ | |
// Not in use | |
const fetchChannelList = chatClient => (dispatch) => { | |
fetchAllChannels(chatClient).then((channels) => { | |
dispatch(fetchChannelListSuccess(normalizeChannelSidToChannel(channels))); | |
// TODO: Remove in future to avoid session error for already joined user. | |
channels.map((channelDescriptor) => { | |
channelDescriptor.getChannel() | |
.then((channel) => { | |
channel.join() | |
.then(() => { | |
dispatch(fetchChannelMessages(channel, chatClient)); | |
}) | |
.catch((error) => { | |
dispatch(fetchChannelMessages(channel, chatClient)); | |
console.error('Channel join error:', error); | |
}); | |
}); | |
dispatch(fetchAllMessagesCompleteSuccess()); | |
}); | |
}); | |
}; | |
const fetchChannelMessages = (channel, chatClient) => dispatch => chatClient.getChannelBySid(channel.sid) | |
.then((activeChannel) => { | |
activeChannel.getMessages().then((response) => { | |
dispatch(getAllChannelMessagesSuccess({ data: response, sid: channel.sid })); | |
}); | |
}); | |
export const fetchAllChannels = async (chatClient) => { | |
const channels = []; | |
const fetchChannels = async (paginator) => { | |
channels.push(...paginator.items); | |
const restPaginator = await paginator.nextPage(); | |
return (restPaginator.hasNextPage) ? fetchChannels(restPaginator) : channels; | |
}; | |
const paginator = await chatClient.getPublicChannelDescriptors(); | |
return paginator.items; | |
const allChannels = await fetchChannels(paginator); | |
return allChannels; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment