Validate User Generated content

With the new EPiServer Social Platform you can allow your visitors to “generate” content on your site. Of course you will need to moderate the content, with all the trolling and spamming these days. To save your moderator some work, you could (should?) do some validation before the comment enters the moderation workflow.

You may have noticed that I am quite a fan of the Cognitive services from Microsoft. They also have a text moderation API. So I thought to use this API for validating the input from the user for profanity, unsafe URL’s, etc. By using the RemoteAttribute on the property that contains your user input and pointing it to a controller that validates the input, this can be quite easily accomplished.

For the code sample you will need to download and compile the SDK, as it’s not on NuGet yet.

I created some methods in the controller that can be called in your attribute, they all use this piece of code, which basically calls the API, gets the result and checks if there is profanity,  unsafe URL’s (if you allow URL’s), unsafe email addresses (if you allow that to be entered). It returns a JsonResult with either true or a message.

public class CommentViewModel
    {
        [Remote("IsSafeTextContent", "ModeratorValidation")]
        public virtual string Body { get; set; }
    }
private JsonResult CheckContent(string body, Constants.MediaType mediaType, bool allowUrls, bool allowPii)
        {
            Task<ScreenTextResult> moderate = this.client.ScreenTextAsync(
                body,
                mediaType,
                ContentLanguage.PreferredCulture.Name,
                true,
                true,
                true,
                string.Empty);

            ScreenTextResult result = moderate.Result;

            if (!ResultSuccess(result))
            {
                return this.Json(this.Translate("/validation/moderatornotavailable"), JsonRequestBehavior.AllowGet);
            }

            bool safeText = IsSafeText(result);
            bool safeUrl = allowUrls ? IsSafeUrl(result) : ContainsNoUrl(result);
            bool safePii = allowPii ? IsSafePii(result) : ContainsNoPii(result);
            bool noMisrepresentation = !IsMisrepresentation(result);

            if (safeText && safeUrl && safePii && noMisrepresentation)
            {
                return this.Json(true, JsonRequestBehavior.AllowGet);
            }

            return this.Json(this.Translate("/validation/moderation"), JsonRequestBehavior.AllowGet);
        }

 

Not much more to tell about it as you just call the API and let the machines make your moderators happy. You will of course need to handle the validation errors, if any, when you submit the content.

See gist for full code example.

Advertisements

One thought on “Validate User Generated content

  1. Great to see Cognitive services in action! Just 2 cents from my side: I would prefer to refactor method to be truly async -and skipping Task.Result and other cobstructions. Helps if I want to call it from async action..

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s