Example #1
0
        /// <inheritdoc />
        public Task StoreAsync(AlertsSubscription subscription, CancellationToken cancellation = default)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            _lock.EnterUpgradeableReadLock();
            try
            {
                var existingItem = _subscriptions.FirstOrDefault(s => s.Equals(subscription));
                if (existingItem == null)
                {
                    _lock.EnterWriteLock();
                    try
                    {
                        _subscriptions.Add(subscription);
                    }
                    finally
                    {
                        _lock.ExitWriteLock();
                    }
                }
            }
            finally
            {
                _lock.ExitUpgradeableReadLock();
            }

            return(Task.CompletedTask);
        }
Example #2
0
 public SendAlertRequest(AlertsSubscription subscription, string text, ParseMode parseMode, AlertManagerWebhookUpdate alertManagerUpdate)
 {
     Subscription       = subscription;
     Text               = text;
     ParseMode          = parseMode;
     AlertManagerUpdate = alertManagerUpdate;
 }
Example #3
0
        /// <inheritdoc />
        public async Task RemoveAsync(AlertsSubscription subscription, CancellationToken cancellation = default)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            var key = GetSubscriptionKey(subscription.Channel, subscription.ChatId);
            await _consulClient.KV.Delete(key, cancellation);
        }
Example #4
0
        /// <inheritdoc />
        public async Task RemoveAsync(AlertsSubscription subscription, CancellationToken cancellation = default)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            var key = new AlertsSubscriptionKey(subscription.ChatId, subscription.Channel);

            await _store.RemoveAsync(key, cancellation);

            await _store.CommitAsync(cancellation);
        }
Example #5
0
        /// <inheritdoc />
        public async Task StoreAsync(AlertsSubscription subscription, CancellationToken cancellation = default)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            var key     = GetSubscriptionKey(subscription.Channel, subscription.ChatId);
            var options = _optionsProvider.Value;

            var result = await _consulClient.KV.PutAsync(key, subscription, options.SerializerSettings, cancellation);

            if (!result.Response)
            {
                throw new ConsulRequestException(
                          $"Unable to store subscription (chat: {subscription.ChatId}; channel: {subscription.Channel})",
                          result.StatusCode);
            }
        }
Example #6
0
        /// <inheritdoc />
        protected override async Task Handle(BotActionRequest <SubscribeAction> request, CancellationToken cancellationToken)
        {
            var chat = request.Update.Message.Chat;

            var channel = await _channelStore.GetAsync(request.Action.Channel, cancellationToken);

            if (channel == null)
            {
                var message = Localizer.GetString(BotResources.SubscribeFailedChannelNotFound);
                await Reply.SendAsync(new SendTelegramReply(chat.Id, message),
                                      cancellationToken);

                return;
            }

            var subscriptionExists = await _subscriptionsStore.ExistsAsync(request.Action.Channel, chat.Id, cancellationToken);

            if (subscriptionExists)
            {
                var message = Localizer.GetString(BotResources.SubscribeFailedAlreadySubscribed);
                await Reply.SendAsync(new SendTelegramReply(chat.Id, message), cancellationToken);

                return;
            }

            var subscription = new AlertsSubscription
            {
                Channel  = request.Action.Channel,
                ChatId   = chat.Id,
                ChatName = chat.Username != null
                    ? $"@{chat.Username}"
                    : chat.Title
            };

            await _subscriptionsStore.StoreAsync(subscription, cancellationToken);

            var replyMessage = Localizer.GetString(BotResources.SubscribeSucceed);
            await Reply.SendAsync(new SendTelegramReply(chat.Id, replyMessage), cancellationToken);
        }
Example #7
0
        /// <inheritdoc />
        public Task RemoveAsync(AlertsSubscription subscription, CancellationToken cancellation = default)
        {
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            _lock.EnterWriteLock();
            try
            {
                var existing = _subscriptions.FirstOrDefault(s => s.Equals(subscription));
                if (existing != null)
                {
                    _subscriptions.Remove(existing);
                }
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            return(Task.CompletedTask);
        }