Skip to content

Commit

Permalink
Sanitize review text
Browse files Browse the repository at this point in the history
  • Loading branch information
btalma committed Dec 13, 2019
1 parent 654ca43 commit 5fa0395
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .vtexignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules/
README.md
dotnet/obj/
dotnet/bin/
dotnet-wrapper/
dotnet-wrapper/
.vs
5 changes: 5 additions & 0 deletions dotnet/DataSources/ProductReviewRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ReviewsRatings.Models;
using ReviewsRatings.Services;

Expand Down Expand Up @@ -71,7 +72,11 @@ public async Task<IList<Review>> GetProductReviewsAsync(string productId)

response.EnsureSuccessStatusCode();

responseContent = Utils.ReviewSanitizer.SantizeReviewText(responseContent);

Console.WriteLine($"Before DeserializeObject");
IList<Review> productReviews = JsonConvert.DeserializeObject<IList<Review>>(responseContent);
Console.WriteLine($"productReviews.Count = {productReviews.Count}");
return productReviews;
}

Expand Down
6 changes: 5 additions & 1 deletion dotnet/Models/Review.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace ReviewsRatings.Models
using Newtonsoft.Json;
using ReviewsRatings.Utils;

namespace ReviewsRatings.Models
{
/// <summary>
/// Id: ID!
Expand All @@ -15,6 +18,7 @@
/// Approved : Boolean
/// Location : String
/// </summary>
//[JsonConverter(typeof(ReviewJsonConverter))]
public class Review
{
/// <summary>
Expand Down
72 changes: 72 additions & 0 deletions dotnet/Utils/ReviewJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace ReviewsRatings.Utils
{
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ReviewsRatings.Models;
using System;
using System.Collections.Generic;

class ReviewJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Review));
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
Review review = new Review();
if (jo["Approved"] != null)
{
review.VerifiedPurchaser = (bool)jo["Approved"];
}

review.CacheId = (int)jo["CacheId"];
review.Id = (int)jo["Id"];
review.Location = jo["Location"] != null ? (string)jo["Location"] : null;
review.ProductId = (string)jo["ProductId"];
review.Rating = (int)jo["Rating"];
review.ReviewDateTime = jo["ReviewDateTime"] != null ? (string)jo["ReviewDateTime"] : null;
review.ReviewerName = jo["ReviewerName"] != null ? (string)jo["ReviewerName"] : null;
review.ShopperId = jo["ShopperId"] != null ? (string)jo["ShopperId"] : null;
review.Sku = jo["Sku"] != null ? (string)jo["Sku"] : null;

if (jo["Text"] != null)
{
JToken reviewText = jo["Text"];
review.Text = EscapeForJson(reviewText.ToString());
Console.WriteLine($"review.Text ======== {review.Text}");
}

if (jo["Title"] != null)
{
JToken titleText = jo["Title"];
review.Title = EscapeForJson(titleText.ToString());
}

if (jo["VerifiedPurchaser"] != null)
{
review.VerifiedPurchaser = (bool)jo["VerifiedPurchaser"];
}

return review;
}

public override bool CanWrite
{
get { return false; }
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}

static string EscapeForJson(string s)
{
string quoted = JsonConvert.ToString(s);
return quoted.Substring(1, quoted.Length - 2);
}
}
}
40 changes: 40 additions & 0 deletions dotnet/Utils/ReviewSanitizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ReviewsRatings.Utils
{
public static class ReviewSanitizer
{
public static string SantizeReviewText(string rawReviewsText)
{
int startPos = rawReviewsText.IndexOf("\"Text\":", StringComparison.OrdinalIgnoreCase);
int i = 0; // just to keep it from somehow getting stuck
while (startPos > 0 && i < 999)
{
int endPos = rawReviewsText.IndexOf(",\"", startPos, StringComparison.OrdinalIgnoreCase);
startPos = startPos + 7;
string rawText = rawReviewsText.Substring(startPos, endPos+ - startPos);
//Console.WriteLine($"rawtext is -[{rawText}]-");
if(!rawText.Equals("null"))
{
// Remove quotes around the text field
rawText = rawText.Substring(1, rawText.Length - 2);
string replacementText = JsonConvert.ToString(rawText);
//Console.WriteLine($"REPTEXT1 = {replacementText}");
// JsonConvert will add surrounding quotes - remove them
replacementText = replacementText.Substring(1, replacementText.Length - 2);
var regex = new Regex(Regex.Escape(rawText));
rawReviewsText = regex.Replace(rawReviewsText, replacementText, 1);
}

startPos = rawReviewsText.IndexOf("\"Text\":", endPos);
i++;
}

return rawReviewsText;
}
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "reviews-and-ratings",
"vendor": "vtex",
"version": "1.0.8",
"version": "1.1.0",
"title": "Reviews and Ratings",
"description": "An app for saving and retrieving product reviews and ratings.",
"scripts": {
Expand Down

0 comments on commit 5fa0395

Please sign in to comment.