Example #1
0
        /// <summary>
        /// Remove a conversation from a subscription.
        /// </summary>
        public async Task <bool> RemoveSubscription(
            ITurnContext context,
            string subscriptionKey,
            ConversationReference conversationReference,
            CancellationToken cancellationToken)
        {
            bool isEmpty = false;

            ProactiveSubscription subscription = await this.GetSubscriptionByKey(context, subscriptionKey, cancellationToken);

            // Get SubscriptionMap
            var proactiveSubscriptionMap = await this._proactiveStateSubscription.GetAsync(context, () => new ProactiveSubscriptionMap()).ConfigureAwait(false);

            // remove thread message id
            string conversationId = conversationReference.Conversation.Id.Split(';')[0];

            List <ConversationReference> subscribedConversations = subscription.ConversationReferences?.ToList();

            subscribedConversations?.RemoveAll(it => it.Conversation.Id == conversationId);

            if (subscribedConversations == null || subscribedConversations.Count == 0)
            {
                await this._proactiveStateSubscription.SetAsync(context, null, cancellationToken);

                isEmpty = true;
            }
            else
            {
                subscription.ConversationReferences       = subscribedConversations;
                proactiveSubscriptionMap[subscriptionKey] = subscription;
                await this._proactiveStateSubscription.SetAsync(context, proactiveSubscriptionMap, cancellationToken);
            }

            return(isEmpty);
        }
Example #2
0
        /// <summary>
        /// Add a conversation to a subscription.
        /// </summary>
        /// <param name="context">The turn context.</param>
        /// <param name="subscriptionKey">The Subscription key.</param>
        /// <param name="conversationReference">Conversation reference.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task <bool> AddSubscription(
            ITurnContext context,
            string subscriptionKey,
            ConversationReference conversationReference,
            CancellationToken cancellationToken)
        {
            bool isNewSubscription             = false;
            ProactiveSubscription subscription = await this.GetSubscriptionByKey(context, subscriptionKey, cancellationToken);

            var proactiveSubscriptionMap = await this._proactiveStateSubscription.GetAsync(context, () => new ProactiveSubscriptionMap()).ConfigureAwait(false);

            if (subscription != null)
            {
                proactiveSubscriptionMap[subscriptionKey] = new ProactiveSubscription
                {
                    ConversationReferences = new List <ConversationReference>(subscription.ConversationReferences.Append(conversationReference))
                };
            }
            else
            {
                proactiveSubscriptionMap[subscriptionKey] = new ProactiveSubscription
                {
                    ConversationReferences = new List <ConversationReference> {
                        conversationReference
                    }
                };

                isNewSubscription = true;
            }

            await this._proactiveStateSubscription.SetAsync(context, proactiveSubscriptionMap, cancellationToken);

            // Save ProactiveState
            await _proactiveState.SaveChangesAsync(context, false, cancellationToken);

            return(isNewSubscription);
        }