<commit_before>﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Lime.Protocol;
using Newtonsoft.Json.Linq;
using Take.Blip.Client;
using Take.Blip.Client.Extensions.EventTracker;

namespace Take.Blip.Builder.Actions.TrackEvent
{
    public class TrackEventAction : IAction
    {
        private readonly IEventTrackExtension _eventTrackExtension;

        private const string CATEGORY_KEY = "category";
        private const string ACTION_KEY = "action";
        private const string MESSAGE_ID_KEY = "#messageId";

        public string Type => nameof(TrackEvent);

        public TrackEventAction(IEventTrackExtension eventTrackExtension)
        {
            _eventTrackExtension = eventTrackExtension;
        }

        public async Task ExecuteAsync(IContext context, JObject settings, CancellationToken cancellationToken)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (settings == null) throw new ArgumentNullException(nameof(settings), $"The settings are required for '{nameof(TrackEventAction)}' action");

            var category = (string)settings[CATEGORY_KEY];
            var action = (string)settings[ACTION_KEY];

            if (string.IsNullOrEmpty(category)) throw new ArgumentException($"The '{nameof(category)}' settings value is required for '{nameof(TrackEventAction)}' action");
            if (string.IsNullOrEmpty(action)) throw new ArgumentException($"The '{nameof(action)}' settings value is required for '{nameof(TrackEventAction)}' action");

            var messageId = EnvelopeReceiverContext<Message>.Envelope?.Id;

            Dictionary<string, string> extras = null;
            if (settings.TryGetValue(nameof(extras), out var extrasToken))
            {
                extras = extrasToken.ToObject<Dictionary<string, string>>();
            }

            extras.Add(MESSAGE_ID_KEY, messageId);

            await _eventTrackExtension.AddAsync(category, action, extras, cancellationToken, context.User);
        }
    }<commit_msg>Initialize extras properly<commit_after>﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Lime.Protocol;
using Newtonsoft.Json.Linq;
using Take.Blip.Client;
using Take.Blip.Client.Extensions.EventTracker;

namespace Take.Blip.Builder.Actions.TrackEvent
{
    public class TrackEventAction : IAction
    {
        private readonly IEventTrackExtension _eventTrackExtension;

        private const string CATEGORY_KEY = "category";
        private const string ACTION_KEY = "action";
        private const string MESSAGE_ID_KEY = "#messageId";

        public string Type => nameof(TrackEvent);

        public TrackEventAction(IEventTrackExtension eventTrackExtension)
        {
            _eventTrackExtension = eventTrackExtension;
        }

        public async Task ExecuteAsync(IContext context, JObject settings, CancellationToken cancellationToken)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (settings == null) throw new ArgumentNullException(nameof(settings), $"The settings are required for '{nameof(TrackEventAction)}' action");

            var category = (string)settings[CATEGORY_KEY];
            var action = (string)settings[ACTION_KEY];

            if (string.IsNullOrEmpty(category)) throw new ArgumentException($"The '{nameof(category)}' settings value is required for '{nameof(TrackEventAction)}' action");
            if (string.IsNullOrEmpty(action)) throw new ArgumentException($"The '{nameof(action)}' settings value is required for '{nameof(TrackEventAction)}' action");

            var messageId = EnvelopeReceiverContext<Message>.Envelope?.Id;

            Dictionary<string, string> extras;
            if (settings.TryGetValue(nameof(extras), out var extrasToken))
            {
                extras = extrasToken.ToObject<Dictionary<string, string>>();
            }
            else
            {
                extras = new Dictionary<string, string>();
            }

            extras[MESSAGE_ID_KEY] = messageId;

            await _eventTrackExtension.AddAsync(category, action, extras, cancellationToken, context.User);
        }
    }
{'fraction_non_alphanumeric': 0.09327750639683648, 'fraction_numerical': 0.0, 'mean_word_length': 3.244817374136229, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '3832987', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1076, 'n_words': 272}