<commit_before>﻿using System;
using Elders.Cronus;
using Elders.Cronus.Projections;
using Multitenancy.Delivery;
using PushNotifications.Contracts.Subscriptions.Events;
using PushNotifications.Ports.Logging;
using PushNotifications.Projections.Subscriptions;

namespace PushNotifications.Ports
{
    public class TopicsSubscriptionsPort : IPort,
        IEventHandler<Subscribed>,
        IEventHandler<SubscribedToTopic>,
        IEventHandler<UnSubscribed>,
        IEventHandler<UnsubscribedFromTopic>
    {
        static ILog log = LogProvider.GetLogger(typeof(TopicsSubscriptionsPort));

        public IPublisher<ICommand> CommandPublisher { get; set; }

        public IProjectionLoader Projections { get; set; }

        public IDeliveryProvisioner DeliveryProvisioner { get; set; }

        public ITopicSubscriptionProvisioner TopicSubscriptionProvider { get; set; }

        public void Handle(Subscribed @event)
        {
            var currentUser = @event.SubscriberId;
            var tenant = @event.SubscriberId.Tenant;
            var device = @event.SubscriptionToken.Token;
            var subscriptionType = @event.SubscriptionToken.SubscriptionType;

            var projectionReponse = Projections.Get<TopicsPerSubscriberProjection>(@event.SubscriberId);

            if (projectionReponse.Success == false)
            {
                log.Info(() => $"No topics were found for subscriber {@event.SubscriberId}");
                return;
            }

            foreach (var topic in projectionReponse.Projection.State.Topics)
            {
                var subscriptionManager = TopicSubscriptionProvider.ResolveTopicSubscriptionManager(subscriptionType, tenant);
                subscriptionManager.SubscribeToTopic(@event.SubscriptionToken, topic);
            }
        }

        public void Handle(UnSubscribed @event)
        {
            var currentUser = @event.SubscriberId;
            var tenant = @event.SubscriberId.Tenant;
            var device = @event.SubscriptionToken.Token;
            var subscriptionType = @event.SubscriptionToken.SubscriptionType;

            var projectionReponse = Projections.Get<TopicsPerSubscriberProjection>(@event.SubscriberId);

            if (projectionReponse.Success == false)
            {
                log.Info(() => $"No topics were found for subscriber {@event.SubscriberId}");
                return;
            }

            foreach (var topic in projectionReponse.Projection.State.Topics)
            {
                var subscriptionManager = TopicSubscriptionProvider.ResolveTopicSubscriptionManager(subscriptionType, tenant);
                subscriptionManager.UnsubscribeFromTopic(@event.SubscriptionToken, topic);
            }
        }

        public void Handle(SubscribedToTopic @event)
        {
            if (ReferenceEquals(null, Projections)) throw new ArgumentNullException(nameof(Projections));
            if (ReferenceEquals(null, DeliveryProvisioner)) throw new ArgumentNullException(nameof(DeliveryProvisioner));

            var projectionReponse = Projections.Get<SubscriberTokensProjection>(@event.SubscriberId);
            if (projectionReponse.Success == false)
            {
                log.Info(() => $"No tokens were found for subscriber {@event.SubscriberId}");
                return;
            }

            foreach (var token in projectionReponse.Projection.State.Tokens)
            {
                var subscriptionManager = TopicSubscriptionProvider.ResolveTopicSubscriptionManager(@event.SubscriptionType, @event.SubscriberId.Tenant);
                subscriptionManager.SubscribeToTopic(token, @event.Topic);
            }
        }

        public void Handle(UnsubscribedFromTopic @event)
        {
            if (ReferenceEquals(null, Projections)) throw new ArgumentNullException(nameof(Projections));
            if (ReferenceEquals(null, DeliveryProvisioner)) throw new ArgumentNullException(nameof(DeliveryProvisioner));

            var projectionReponse = Projections.Get<SubscriberTokensProjection>(@event.SubscriberId);
            if (projectionReponse.Success == false)
            {
                log.Info(() => $"No tokens were found for subscriber {@event.SubscriberId}");
                return;
            }

            foreach (var token in projectionReponse.Projection.State.Tokens)<commit_msg>TopicsSubscriptionsPort now uses the token Subscription type<commit_after>﻿using System;
using Elders.Cronus;
using Elders.Cronus.Projections;
using Multitenancy.Delivery;
using PushNotifications.Contracts.Subscriptions.Events;
using PushNotifications.Ports.Logging;
using PushNotifications.Projections.Subscriptions;

namespace PushNotifications.Ports
{
    public class TopicsSubscriptionsPort : IPort,
        IEventHandler<Subscribed>,
        IEventHandler<SubscribedToTopic>,
        IEventHandler<UnSubscribed>,
        IEventHandler<UnsubscribedFromTopic>
    {
        static ILog log = LogProvider.GetLogger(typeof(TopicsSubscriptionsPort));

        public IPublisher<ICommand> CommandPublisher { get; set; }

        public IProjectionLoader Projections { get; set; }

        public IDeliveryProvisioner DeliveryProvisioner { get; set; }

        public ITopicSubscriptionProvisioner TopicSubscriptionProvider { get; set; }

        public void Handle(Subscribed @event)
        {
            var currentUser = @event.SubscriberId;
            var tenant = @event.SubscriberId.Tenant;
            var device = @event.SubscriptionToken.Token;
            var subscriptionType = @event.SubscriptionToken.SubscriptionType;

            var projectionReponse = Projections.Get<TopicsPerSubscriberProjection>(@event.SubscriberId);

            if (projectionReponse.Success == false)
            {
                log.Info(() => $"No topics were found for subscriber {@event.SubscriberId}");
                return;
            }

            foreach (var topic in projectionReponse.Projection.State.Topics)
            {
                var subscriptionManager = TopicSubscriptionProvider.ResolveTopicSubscriptionManager(subscriptionType, tenant);
                subscriptionManager.SubscribeToTopic(@event.SubscriptionToken, topic);
            }
        }

        public void Handle(UnSubscribed @event)
        {
            var currentUser = @event.SubscriberId;
            var tenant = @event.SubscriberId.Tenant;
            var device = @event.SubscriptionToken.Token;
            var subscriptionType = @event.SubscriptionToken.SubscriptionType;

            var projectionReponse = Projections.Get<TopicsPerSubscriberProjection>(@event.SubscriberId);

            if (projectionReponse.Success == false)
            {
                log.Info(() => $"No topics were found for subscriber {@event.SubscriberId}");
                return;
            }

            foreach (var topic in projectionReponse.Projection.State.Topics)
            {
                var subscriptionManager = TopicSubscriptionProvider.ResolveTopicSubscriptionManager(subscriptionType, tenant);
                subscriptionManager.UnsubscribeFromTopic(@event.SubscriptionToken, topic);
            }
        }

        public void Handle(SubscribedToTopic @event)
        {
            if (ReferenceEquals(null, Projections)) throw new ArgumentNullException(nameof(Projections));
            if (ReferenceEquals(null, DeliveryProvisioner)) throw new ArgumentNullException(nameof(DeliveryProvisioner));

            var projectionReponse = Projections.Get<SubscriberTokensProjection>(@event.SubscriberId);
            if (projectionReponse.Success == false)
            {
                log.Info(() => $"No tokens were found for subscriber {@event.SubscriberId}");
                return;
            }

            foreach (var token in projectionReponse.Projection.State.Tokens)
            {
                var subscriptionManager = TopicSubscriptionProvider.ResolveTopicSubscriptionManager(token.SubscriptionType, @event.SubscriberId.Tenant);
                subscriptionManager.SubscribeToTopic(token, @event.Topic);
            }
        }

        public void Handle(UnsubscribedFromTopic @event)
        {
            if (ReferenceEquals(null, Projections)) throw new ArgumentNullException(nameof(Projections));
            if (ReferenceEquals(null, DeliveryProvisioner)) throw new ArgumentNullException(nameof(DeliveryProvisioner));

            var projectionReponse = Projections.Get<SubscriberTokensProjection>(@event.SubscriberId);
            if (projectionReponse.Success == false)
            {
                log.Info(() => $"No tokens were found for subscriber {@event.SubscriberId}");
                return;
            }

            foreach (var token in projectionReponse.Projection.State.Tokens)
{'fraction_non_alphanumeric': 0.08137946733439563, 'fraction_numerical': 0.0, 'mean_word_length': 2.6415250725238293, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6267745', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2136, 'n_words': 425}