Removing Obscenities from Your Content

August 09, 2017 Digital Experience

When many people are publishing content on a website, it leads to variety of opinions which is always good. But working with a large content team also brings complexity, like inconsistencies in the general understanding of what is appropriate for your audience and what is a harsh tone. With that in mind, we should avoid publishing offensive content at any cost. Keep in mind that sometimes the standard autocorrect creates more problems than it solves. With Sitefinity 10.1 you have the option to sanitize your content easily and remove any obscene words, such as those we can only refer to here by their first letter – let’s call them collectively the “X-word“.

 

How to sanitize your content?

With Sitefinity 10.1 you can create your own DataSanitizeProcessor and check all the content that is going into your Database. You need to inherit the ProcessorBase class and implement the IDataProcessor interface. The data processor is activated for each content type and its fields. If you want to apply a check to a field, return true in the ShouldProcess method. Here is a sample illustrating the idea:

using System;
using System.Collections.Specialized;
using System.ComponentModel;
using Telerik.Sitefinity.Data.DataProcessing.Processors;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Processors;
 
 
namespace SitefinityWebApp
{
    public class DataSanitizer : ProcessorBase, IDataProcessor
    {
        public virtual void Process(ref object value)
        {
            if (value != null && value is string)
            {
                value = ((string)value).Replace("X-word", "CENSORED");
            }
        }
 
        public virtual bool ShouldProcess(PropertyDescriptor prop, Type type)
        {
            if (type == typeof(NewsItem) && (prop.Name == "Content" || prop.Name == "Title"))
            {
                return true;
 
            }           
 
            return false;
 
        }
 
        protected override void Initialize(NameValueCollection config)
        {
 
        }
 
    }
}

 

Handling offensive comments

When it comes to comments, the approach is different since the input comes from the directly from the frontend of the website. To deal with inappropriate comments and censor them you only need to override one method in comments.js. The JS file tells the client-side how to visualize the comments. To get the current implementation of the file, open Telerik.Sitefinity.Frontend.Comments.dll with JustDecompile and look in the resources folder for Telerik.Sitefinity.Frontend.Comments.MVC.Scripts.comments-list.js.

Copy the content into a new comments-list.js file and put it in the MVC folder under the Bootstrap resource package. 

The last step is to change the implementation of the attachCommentMessage function. E.g.:

/*
            Comments listing
        */
        attachCommentMessage: function (element, message) {
            if (element && message) {
                message = message.replace("X-word", "CENSORED");
                var rawText = message.replace(/<[^>]*>/ig, ' ');
                 
                if (rawText.length < this.settings.commentsTextMaxLength) {
                    element.html(message);
                }
                else {
                    element.append($('<p data-sf-role="comments-read-substr-comment-header" />').html(rawText.substr(0, this.settings.commentsTextMaxLength)));
                    element.append($('<span />').hide().html(message));
                    element.append($('<a href="#" data-sf-role="comments-read-full-comment-button" />').text(this.settings.useReviews ? this.resources.readFullReview : this.resources.readFullComment));
                }
            }
        }

To avoid discrepancies after upgrading, the best practice is to get the latest version of the JS file and merge it with your version.

 

Code implementation or a swear jar

Even if you apply a basic replacement mechanism to handle the obscene words, the experience on your website will greatly benefit from it. If you don’t want to use this word sanitizer, you can always consider putting a jar in your office and collecting $1 for each bad word instead :). 

Peter Filipov

Peter Filipov (Pepi) is a Product Builder focused on building the future of Sitefinity, relying on the newest technologies such as .NET 6 (and up), React and Angular. His previous experience as a Developer Advocate and Manager of Engineering helps him to understand the customers’ and market needs of Sitefinity. He also is passionate about being active and healthy.