Ejemplo n.º 1
0
        /// <summary>
        /// The get cleaned topic message. Caches cleaned topic message by TopicID.
        /// </summary>
        /// <param name="topicMessage">
        /// The message to clean.
        /// </param>
        /// <param name="topicId">
        ///   The topic id.
        /// </param>
        /// <returns>
        /// The get cleaned topic message.
        /// </returns>
        public MessageCleaned GetCleanedTopicMessage([NotNull] object topicMessage, [NotNull] object topicId)
        {
            CodeContracts.VerifyNotNull(topicMessage, "topicMessage");
            CodeContracts.VerifyNotNull(topicId, "topicId");

            // get the common words for the language -- should be all lower case.
            var commonWords = this.Get <ILocalization>().GetText("COMMON", "COMMON_WORDS").StringToList(',');

            var cacheKey = string.Format(Constants.Cache.FirstPostCleaned, BoardContext.Current.PageBoardID, topicId);
            var message  = new MessageCleaned();

            if (!topicMessage.IsNullOrEmptyDBField())
            {
                message = this.Get <IDataCache>().GetOrSet(
                    cacheKey,
                    () =>
                {
                    var returnMsg   = topicMessage.ToString();
                    var keywordList = new List <string>();

                    if (returnMsg.IsNotSet())
                    {
                        return(new MessageCleaned(returnMsg.Truncate(200), keywordList));
                    }

                    // process message... clean html, strip html, remove bbcode, etc...
                    returnMsg = BBCodeHelper
                                .StripBBCode(HtmlHelper.StripHtml(HtmlHelper.CleanHtmlString(returnMsg)))
                                .RemoveMultipleWhitespace();

                    // encode Message For Security Reasons
                    returnMsg = this.HttpServer.HtmlEncode(returnMsg);

                    if (returnMsg.IsNotSet())
                    {
                        returnMsg = string.Empty;
                    }
                    else
                    {
                        // get string without punctuation
                        var keywordCleaned = new string(
                            returnMsg.Where(c => !char.IsPunctuation(c) || char.IsWhiteSpace(c))
                            .ToArray())
                                             .Trim().ToLower();

                        if (keywordCleaned.Length <= 5)
                        {
                            return(new MessageCleaned(returnMsg.Truncate(200), keywordList));
                        }

                        // create keywords...
                        keywordList = keywordCleaned.StringToList(' ', commonWords);

                        // clean up the list a bit...
                        keywordList = keywordList.GetNewNoEmptyStrings().GetNewNoSmallStrings(5)
                                      .Where(x => !char.IsNumber(x[0])).Distinct().ToList();

                        // sort...
                        keywordList.Sort();

                        // get maximum of 20 keywords...
                        if (keywordList.Count > 20)
                        {
                            keywordList = keywordList.GetRange(0, 20);
                        }
                    }

                    return(new MessageCleaned(returnMsg.Truncate(200), keywordList));
                },
                    TimeSpan.FromMinutes(this.Get <BoardSettings>().FirstPostCacheTimeout));
            }

            return(message);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The get cleaned topic message. Caches cleaned topic message by TopicID.
        /// </summary>
        /// <param name="topicMessage">
        /// The message to clean.
        /// </param>
        /// <param name="topicId">
        ///   The topic id.
        /// </param>
        /// <returns>
        /// The get cleaned topic message.
        /// </returns>
        public MessageCleaned GetCleanedTopicMessage([NotNull] object topicMessage, [NotNull] object topicId)
        {
            CodeContracts.VerifyNotNull(topicMessage, "topicMessage");
            CodeContracts.VerifyNotNull(topicId, "topicId");

            // get the common words for the language -- should be all lower case.
            List<string> commonWords = this.Get<ILocalization>().GetText("COMMON", "COMMON_WORDS").StringToList(',');

            string cacheKey = Constants.Cache.FirstPostCleaned.FormatWith(YafContext.Current.PageBoardID, topicId);
            var message = new MessageCleaned();

            if (!topicMessage.IsNullOrEmptyDBField())
            {
                message = this.Get<IDataCache>().GetOrSet(
                    cacheKey,
                    () =>
                        {
                            string returnMsg = topicMessage.ToString();
                            var keywordList = new List<string>();

                            if (returnMsg.IsSet())
                            {
                                // process message... clean html, strip html, remove bbcode, etc...
                                returnMsg =
                                    BBCodeHelper.StripBBCode(
                                        HtmlHelper.StripHtml(HtmlHelper.CleanHtmlString(returnMsg))).RemoveMultipleWhitespace();

                                // encode Message For Security Reasons
                                returnMsg = this.HttpServer.HtmlEncode(returnMsg);

                                if (returnMsg.IsNotSet())
                                {
                                    returnMsg = string.Empty;
                                }
                                else
                                {
                                    // get string without punctuation
                                    string keywordCleaned =
                                        new string(
                                            returnMsg.Where(c => !char.IsPunctuation(c) || char.IsWhiteSpace(c)).ToArray()).Trim().ToLower();

                                    if (keywordCleaned.Length > 5)
                                    {
                                        // create keywords...
                                        keywordList = keywordCleaned.StringToList(' ', commonWords);

                                        // clean up the list a bit...
                                        keywordList =
                                            keywordList.GetNewNoEmptyStrings().GetNewNoSmallStrings(5).Where(x => !char.IsNumber(x[0])).Distinct().ToList();

                                        // sort...
                                        keywordList.Sort();

                                        // get maximum of 50 keywords...
                                        if (keywordList.Count > 50)
                                        {
                                            keywordList = keywordList.GetRange(0, 50);
                                        }
                                    }
                                }
                            }

                            return new MessageCleaned(returnMsg.Truncate(255), keywordList);
                        },
                    TimeSpan.FromMinutes(this.Get<YafBoardSettings>().FirstPostCacheTimeout));
            }

            return message;
        }