示例#1
0
        /// <summary>
        /// Tries to accept/reject a pending request.
        /// </summary>
        /// <param name="messageRouterManager">The message router manager.</param>
        /// <param name="messageRouterResultHandler">The message router result handler.</param>
        /// <param name="senderParty">The sender party (accepter/rejecter).</param>
        /// <param name="doAccept">If true, will try to accept the request. If false, will reject.</param>
        /// <param name="channelAccountIdOfPartyToAcceptOrReject">The channel account ID of the party whose request to accep/reject.</param>
        /// <returns>Null, if an accept/reject operation was executed successfully.
        /// A user friendly error message otherwise.</returns>
        public async Task <string> AcceptOrRejectRequestAsync(
            MessageRouterManager messageRouterManager, MessageRouterResultHandler messageRouterResultHandler,
            Party senderParty, bool doAccept, string channelAccountIdOfPartyToAcceptOrReject)
        {
            string errorMessage = null;

            IRoutingDataManager routingDataManager = messageRouterManager.RoutingDataManager;
            Party partyToAcceptOrReject            = null;

            if (routingDataManager.GetPendingRequests().Count > 0)
            {
                try
                {
                    partyToAcceptOrReject = routingDataManager.GetPendingRequests().Single(
                        party => (party.ChannelAccount != null &&
                                  !string.IsNullOrEmpty(party.ChannelAccount.Id) &&
                                  party.ChannelAccount.Id.Equals(channelAccountIdOfPartyToAcceptOrReject)));
                }
                catch (InvalidOperationException e)
                {
                    errorMessage = string.Format(
                        ConversationText.FailedToFindPendingRequestForUserWithErrorMessage,
                        channelAccountIdOfPartyToAcceptOrReject,
                        e.Message);
                }
            }

            if (partyToAcceptOrReject != null)
            {
                Party connectedSenderParty =
                    routingDataManager.FindConnectedPartyByChannel(
                        senderParty.ChannelId, senderParty.ChannelAccount);

                bool senderIsConnected =
                    (connectedSenderParty != null &&
                     routingDataManager.IsConnected(connectedSenderParty, ConnectionProfile.Owner));

                MessageRouterResult messageRouterResult = null;

                if (doAccept)
                {
                    if (senderIsConnected)
                    {
                        // The sender (accepter/rejecter) is ALREADY connected with another party
                        Party otherParty = routingDataManager.GetConnectedCounterpart(connectedSenderParty);

                        if (otherParty != null)
                        {
                            errorMessage = string.Format(
                                ConversationText.AlreadyConnectedWithUser, otherParty.ChannelAccount?.Name);
                        }
                        else
                        {
                            errorMessage = ConversationText.ErrorOccured;
                        }
                    }
                    else
                    {
                        bool createNewDirectConversation =
                            !(NoDirectConversationsWithChannels.Contains(senderParty.ChannelId.ToLower()));

                        // Try to accept
                        messageRouterResult = await messageRouterManager.ConnectAsync(
                            senderParty,
                            partyToAcceptOrReject,
                            createNewDirectConversation);
                    }
                }
                else
                {
                    // Note: Rejecting is OK even if the sender is alreay connected
                    messageRouterResult = messageRouterManager.RejectPendingRequest(partyToAcceptOrReject, senderParty);
                }

                if (messageRouterResult != null)
                {
                    await messageRouterResultHandler.HandleResultAsync(messageRouterResult);
                }
            }
            else
            {
                errorMessage = ConversationText.FailedToFindPendingRequest;
            }

            return(errorMessage);
        }
示例#2
0
        /// <summary>
        /// Tries to accept/reject a pending request.
        /// </summary>
        /// <param name="senderParty">The sender party (accepter/rejecter).</param>
        /// <param name="commandMessage">The command message. Required for resolving the party to accept/reject.</param>
        /// <param name="doAccept">If true, will try to accept the request. If false, will reject.</param>
        /// <returns>Null, if successful. A user friendly error message otherwise.</returns>
        private async Task <string> AcceptOrRejectRequestAsync(Party senderParty, string commandMessage, bool doAccept)
        {
            string errorMessage = null;
            IRoutingDataManager routingDataManager = _messageRouterManager.RoutingDataManager;
            Party connectedSenderParty             = routingDataManager.FindConnectedPartyByChannel(senderParty.ChannelId, senderParty.ChannelAccount);

            if (connectedSenderParty == null || !routingDataManager.IsConnected(senderParty, ConnectionProfile.Owner))
            {
                // The sender (accepter/rejecter) is NOT connected with another party
                if (routingDataManager.GetPendingRequests().Count > 0)
                {
                    // The name of the user to accept should be the second word
                    string[] splitMessage = commandMessage.Split(' ');

                    if (splitMessage.Count() > 1 && !string.IsNullOrEmpty(splitMessage[1]))
                    {
                        Party partyToAcceptOrReject = null;

                        try
                        {
                            partyToAcceptOrReject = routingDataManager.GetPendingRequests().Single(
                                party => (party.ChannelAccount != null &&
                                          !string.IsNullOrEmpty(party.ChannelAccount.Id) &&
                                          party.ChannelAccount.Id.Equals(splitMessage[1])));
                        }
                        catch (InvalidOperationException e)
                        {
                            errorMessage = $"Failed to find a pending request for user \"{splitMessage[1]}\": {e.Message}";
                        }

                        if (partyToAcceptOrReject != null)
                        {
                            MessageRouterResult messageRouterResult = null;

                            if (doAccept)
                            {
                                messageRouterResult = await _messageRouterManager.ConnectAsync(
                                    senderParty, partyToAcceptOrReject, !partyToAcceptOrReject.ChannelId.Contains("skype"));
                            }
                            else
                            {
                                messageRouterResult = _messageRouterManager.RejectPendingRequest(partyToAcceptOrReject, senderParty);
                            }

                            await _messageRouterResultHandler.HandleResultAsync(messageRouterResult);
                        }
                    }
                    else
                    {
                        errorMessage = "User name missing";
                    }
                }
                else
                {
                    errorMessage = "No pending requests";
                }
            }
            else
            {
                // The sender (accepter/rejecter) is ALREADY connected with another party
                Party otherParty = routingDataManager.GetConnectedCounterpart(connectedSenderParty);

                if (otherParty != null)
                {
                    errorMessage = $"You are already connected with user \"{otherParty.ChannelAccount.Name}\"";
                }
                else
                {
                    errorMessage = "An error occured";
                }
            }

            return(errorMessage);
        }