public static string[] DetermineFieldsToSend(Dictionary<string, string> tweetUpdate, SearchDefinition[] searchDefinitions)
        {
            List<string> fields = new List<string>();
            foreach (SearchDefinition definition in searchDefinitions)
            {
                if (definition.ShouldIgnoreTweet(tweetUpdate))
                {
                    continue;
                }

                if (definition.FieldsToSend != null)
                {
                    AddFieldIfAbsent(definition.FieldsToSend.Split(','), fields);
                }
                else
                {
                    // add all
                    AddFieldIfAbsent(tweetUpdate.Keys.ToArray<string>(), fields);
                }
            }
            return fields.ToArray();
        }
        public void Only_count_field_should_be_present_if_all_but_one_SearchDefinition_ignore_tweet()
        {
            var text = "He's right > RT @leggetter No way!";
            var ignoreDef = CreateSearchDefinition("/TEST", "leggetter", text, "Text", @"(\s|\W|^)RT(\s|\W|$)", false);

            var countOnlyDef = new SearchDefinition();
            countOnlyDef.FieldsToSend = "RetweetCount";
            countOnlyDef.CountFields = new CountField[1];
            countOnlyDef.CountFields[0] = new CountField();
            countOnlyDef.CountFields[0].Match = new MatchExpression();
            countOnlyDef.CountFields[0].Match.FieldToMatch = "Text";
            countOnlyDef.CountFields[0].Match.Expression = @"(\s|\W|^)RT(\s|\W|$)";

            var update = new Dictionary<string, string>();
            update["Text"] = text;
            update["RetweetCount"] = "1";

            string[] fields = Publisher.DetermineFieldsToSend(update, new SearchDefinition[] { ignoreDef, countOnlyDef });

            Assert.AreEqual(1, fields.Length);
            Assert.AreEqual("RetweetCount", fields[0]);
        }
        public static string[] DetermineTopicFromTwitterMessage(IDictionary<string,string> tweetUpdate, IStatusMessage statusMessage, SearchDefinition[] searchDefinitions)
        {
            List<string> topics = new List<string>();
            foreach (SearchDefinition definition in searchDefinitions)
            {
                if (definition.ShouldIgnoreTweet(tweetUpdate))
                {
                    continue;
                }

                if (definition.IsTrackingFor(statusMessage) &&
                    topics.Contains(definition.PublishTo) == false)
                {
                    topics.Add(definition.PublishTo);
                }

                if (definition.IsFollowingUser(statusMessage.User) &&
                    topics.Contains(definition.PublishTo) == false)
                {
                    topics.Add(definition.PublishTo);
                }
            }
            return topics.ToArray();
        }
 private void SendIntialCountFieldValues(SearchDefinition[] searchDefinitions)
 {
     foreach (SearchDefinition def in searchDefinitions)
     {
         if (def.CountFields != null)
         {
             Dictionary<string, string> tweetUpdate = new Dictionary<string, string>();
             foreach (CountField field in def.CountFields)
             {
                 _countFields[field.Name] = 0;
                 tweetUpdate[field.Name] = "0";
             }
             Publish(def.PublishTo, tweetUpdate);
         }
     }
 }
        private string BuildTwitterParams(SearchDefinition[] searchDefinitions)
        {
            List<string> tracks = new List<string>();
            List<string> userIds = new List<string>();

            // TODO: other types of search e.g. follow
            foreach (SearchDefinition def in searchDefinitions)
            {
                if (def.Request == true)
                {
                    if (string.IsNullOrEmpty(def.TrackFor) == false)
                    {
                        string[] configuredTracks = def.TrackFor.Split(',');
                        foreach (string track in configuredTracks)
                        {
                            if (tracks.Contains(track) == false)
                            {
                                tracks.Add(track);
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(def.FollowUsersWithId) == false)
                    {
                        string[] configuredIds = def.FollowUsersWithId.Split(',');
                        foreach (string id in configuredIds)
                        {
                            if (configuredIds.Contains(id) == false)
                            {
                                userIds.Add(id);
                            }
                        }
                        userIds.AddRange(configuredIds);
                    }
                }
            }
            string twitterParameters = String.Join(",", tracks.ToArray());
            if(string.Empty != twitterParameters)
            {
                twitterParameters = "track=" + twitterParameters;
            }
            string userIdsToFollow = String.Join(",", userIds.ToArray());
            if (string.Empty != userIdsToFollow)
            {
                if (twitterParameters != string.Empty)
                {
                    twitterParameters += "&";
                }
                twitterParameters += "follow=" + userIdsToFollow;
            }
            return twitterParameters;
        }
 private void AddCountFieldsToUpdate(string[] topics, Dictionary<string, string> tweetUpdate, SearchDefinition[] searchDefinitions)
 {
     foreach (SearchDefinition def in searchDefinitions)
     {
         if (topics.Contains(def.PublishTo) && def.CountFields != null)
         {
             foreach (CountField field in def.CountFields)
             {
                 if (FieldAlreadyAddedToUpdate(tweetUpdate, field.Name) == false)
                 {
                     long currentValue = (_countFields.ContainsKey(field.Name) == false ? 0 : _countFields[field.Name]);
                     if (field.MatchAll == true)
                     {
                         currentValue++;
                         tweetUpdate.Add(field.Name, currentValue.ToString());
                     }
                     else if (string.IsNullOrEmpty(field.Match.Expression) == false &&
                         string.IsNullOrEmpty(field.Match.FieldToMatch) == false &&
                         tweetUpdate.ContainsKey(field.Match.FieldToMatch) == true)
                     {
                         RegexOptions options = (field.Match.IgnoreCase?RegexOptions.IgnoreCase:RegexOptions.None);
                         Regex regex = new Regex(field.Match.Expression, options);
                         string fieldToMatch = tweetUpdate[field.Match.FieldToMatch];
                         if (regex.IsMatch(fieldToMatch) == true)
                         {
                             currentValue++;
                             tweetUpdate.Add(field.Name, currentValue.ToString());
                         }
                     }
                     _countFields[field.Name] = currentValue;
                 }
             }
         }
     }
 }
        private SearchDefinition CreateSearchDefinition(string publishTo, string trackFor, string tweetMsg, string fieldToIgnore, string ignoreExpression, bool ignoreCase)
        {
            SearchDefinition def = new SearchDefinition();
            def.PublishTo = publishTo;
            def.TrackFor = trackFor;

            if (string.IsNullOrEmpty(fieldToIgnore) == false)
            {
                def.Ignore = new MatchExpression[1];
                def.Ignore[0] = new MatchExpression();
                def.Ignore[0].FieldToMatch = fieldToIgnore;
                def.Ignore[0].Expression = ignoreExpression;
                def.Ignore[0].IgnoreCase = ignoreCase;
            }
            return def;
        }