Example #1
0
        /// <summary>
        /// This method should be call right after a user with Agent profile logs in
        /// </summary>
        /// <param name="userDetails">Current user details</param>
        /// <returns>A async task</returns>
        public async Task OnAgentConnectedAsync(UserDetails userDetails)
        {
            if (userDetails == null)
            {
                throw new System.ArgumentNullException(nameof(userDetails));
            }

            var currentUserDetails = userDetails;

            // Assign to a new session
            var livechatSession = new LivechatUserSession
            {
                ConnectionId   = currentUserDetails.ConnectionId,
                LivechatUserId = currentUserDetails.LivechatUserId
            };

            await livechatRules.CreateNewSession(livechatSession);

            var channels = await conversationHandler.SendChannelListToAgent(currentUserDetails.ConnectionId, livechatSession.LivechatUserId);

            currentUserDetails.Channels = channels;

            // Assign all agent channels to the new connectionId
            await AddAgentChannels(currentUserDetails.ConnectionId, channels);
        }
Example #2
0
        /// <summary>
        /// It returns bunch of messages to clients
        /// </summary>
        /// <param name="channelId"></param>
        /// <returns></returns>
        public async Task GetLastMessages(string channelId)
        {
            var currentSession = await CurrentUserDetails();

            // Creates a new session for whatever this user is
            var livechatSession = new LivechatUserSession
            {
                ConnectionId   = currentSession.ConnectionId,
                LivechatUserId = currentSession.LivechatUserId
            };

            var channel = await livechatRules.GetCurrentUserChannel(livechatSession.LivechatUserId, channelId);

            if (channel == null)
            {
                await CurrentUser.SendAsync(HubMessages.INVALID_CHANNEL);

                return;
            }

            // Gets and sets pack of messages
            var lastMessages = await livechatRules.LoadLastMessages(livechatSession, channel);

            await CurrentUser.SendAsync("SendBulk", lastMessages);
        }
Example #3
0
        private async Task AddAgentToGroupAndCreateChannel(LivechatUserSession livechatSession, LivechatChannel userCurrentChannel)
        {
            await Groups.AddAsync(livechatSession.ConnectionId, userCurrentChannel.ChannelId);

            await livechatRules.AddUserToChannel(new LivechatChannelUser
            {
                LivechatChannelId = userCurrentChannel.ChannelId,
                LivechatUserId    = livechatSession.LivechatUserId
            });

            // Sends the channel to livechat customer
            await CurrentUser.SendAsync("NewChannelRegistry", userCurrentChannel.ChannelId);
        }
Example #4
0
        public async Task TryingFindAgent(string channelId, int currentNum)
        {
            var currentSession = await CurrentUserDetails();

            // Creates a new session for whatever this user is
            var livechatSession = new LivechatUserSession
            {
                ConnectionId   = currentSession.ConnectionId,
                LivechatUserId = currentSession.LivechatUserId
            };

            var channel = await livechatRules.GetCurrentUserChannel(livechatSession.LivechatUserId, channelId);

            var agent = await livechatRules.GetAgentsOnChannels(channel.Id, currentSession.LivechatUserId);

            if (agent != null)
            {
                return;
            }

            if (channel == null)
            {
                await CurrentUser.SendAsync(HubMessages.INVALID_CHANNEL);

                return;
            }

            var chatConversationHandler = CreateHumanAgentConversation();

            await chatConversationHandler.FindNewAgent(new LivechatChannel
            {
                Id      = channelId,
                StoreId = 1
            },
                                                       clientCallbackMethod : HubMessages.LAST_TRYING_FIND_AGENT,
                                                       currentNumber : currentNum
                                                       );
        }
        /// <summary>
        /// This method should be call right after a user with Customer profile logs in
        /// </summary>
        /// <param name="userDetails"></param>
        /// <returns></returns>
        public async Task OnCustomerConnectedAsync(UserDetails userDetails)
        {
            if (userDetails == null)
            {
                throw new ArgumentNullException(nameof(userDetails));
            }

            var userStoreId = userDetails.CustomerStoreId;

            if (userStoreId <= 0)
            {
                context.Connection.Abort();
                return;
            }

            // CAUTION
            userDetails.AddStore(userStoreId);

            // Assign to a new session
            var livechatSession = new LivechatUserSession
            {
                ConnectionId   = userDetails.ConnectionId,
                LivechatUserId = userDetails.LivechatUserId
            };

            // Include connected user inside Nop Customer table
            await livechatRules.CloneUserToAStore(userDetails.CustomerId, userDetails.CustomerStoreId);

            await livechatRules.CreateNewSession(livechatSession);

            // Check if LivechatId is already registered in some chat with this current store
            // Gets only active stores (IsFinished is false)
            var userCurrentChannel = await livechatRules.GetCustomerChannel(
                userStoreId,
                livechatSession.LivechatUserId
                );

            // If there is no channel for Customer x Store, it creates a new one and deal with it
            if (userCurrentChannel == null)
            {
                userCurrentChannel = await AssignLivechatUserToChannel(userDetails);

                // Persists current user channel on details so it can be recovery later on online/offline status
                userDetails.Channels.Add(userCurrentChannel);

                // Adds user payload
                await conversationHandler.PersistPayload(
                    livechatUserId : livechatSession.LivechatUserId,
                    channelId : userCurrentChannel.ChannelId,
                    userPayload : userDetails.Payload,
                    overwritePayload : false
                    );

                // This is the right time to call a new agent on IChatConversationHandler instance
                await conversationHandler.OnChannelCreated(userCurrentChannel);

                return;
            }

            // If it went this far, is probably because the currentUser's connection went down and its connecting again
            // so basically we should provide its current channel and check if there is some available agent to continue this call
            //
            // Adds current connectionId to correct user group
            // After that it sends the new channel registration to the user
            await NotifyAndAddUserToGroup(
                userDetails.ConnectionId,
                userCurrentChannel.ChannelId
                );

            // Persists current user channel on details so it can be recovery later on online/offline status
            userDetails.Channels.Add(userCurrentChannel);

            // Adds user payload
            await conversationHandler.PersistPayload(
                livechatUserId : livechatSession.LivechatUserId,
                channelId : userCurrentChannel.ChannelId,
                userPayload : userDetails.Payload,
                overwritePayload : false
                );

            // Verifies if user is alone. If it is try to find a new agent again
            var agentsOnChannel = await conversationHandler.GetAgentsOnChannel(
                userCurrentChannel.ChannelId,
                userDetails.LivechatUserId
                );

            if (agentsOnChannel.Count == 0)
            {
                await conversationHandler.FindNewAgent(userCurrentChannel);
            }
            else
            {
                // TODO: Send an warning telling agent about new users online on this channel
                //foreach (var agent in agentsOnChannel)
                //{
                //    await conversationHandler.SendChannelListToAgent(string.Empty, agent.LivechatUserId);
                //}
            }
        }