Skip to content

Commit

Permalink
Merge pull request #13 from pimanac/reports
Browse files Browse the repository at this point in the history
add user reports / mod reports to VotableThing
  • Loading branch information
CrustyJew authored Sep 21, 2016
2 parents ba390a4 + 1c3d17a commit b8a488f
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions RedditSharp/Things/VotableThing.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Linq;
using System.Security.Authentication;
Expand Down Expand Up @@ -92,6 +94,14 @@ private async Task CommonInitAsync(Reddit reddit, IWebAgent webAgent, JToken jso
[JsonProperty("likes")]
public bool? Liked { get; set; }

[JsonProperty("mod_reports")]
[JsonConverter(typeof(ReportCollectionConverter))]
public ICollection<Report> ModReports { get; set; }

[JsonProperty("user_reports")]
[JsonConverter(typeof(ReportCollectionConverter))]
public ICollection<Report> UserReports { get; set; }

/// <summary>
/// Gets or sets the vote for the current VotableThing.
/// </summary>
Expand Down Expand Up @@ -306,5 +316,94 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
writer.WriteValue(d.ToString().ToLower());
}
}

internal class ReportCollectionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ICollection<Report>) || objectType == typeof(object);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
if (token.Type != JTokenType.Array || token.Children().Count() == 0)
return new Collection<Report>();

var result = new Collection<Report>();
foreach (var child in token.Children())
{
// always tuples
// https://github.com/reddit/reddit/blob/master/r2/r2/models/report.py#L165
if (child.Type != JTokenType.Array || child.Children().Count() != 2)
continue;

var report = new Report();
report.Reason = child.First.Value<string>();

if (child.Last.Type == JTokenType.String)
{
report.ModeratorName = child.Last.Value<string>();
report.Count = 1;
}
else
{
report.ModeratorName = "";
report.Count = child.Last.Value<int>();
}
result.Add(report);
}
return result;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var reports = value as ICollection<Report>;

if (reports == null || reports.Count == 0)
{
writer.WriteStartArray();
writer.WriteEndArray();
return;
}

writer.WriteStartArray();

foreach (var report in reports)
{
writer.WriteStartArray();

writer.WriteValue(report.Reason);

if (String.IsNullOrEmpty(report.ModeratorName))
writer.WriteValue(report.Count);
else
writer.WriteValue(report.ModeratorName);

writer.WriteEndArray();
}

writer.WriteEndArray();
}
}
}

public class Report
{
/// <summary>
/// Report reason
/// </summary>
public string Reason { get; set; }

/// <summary>
/// Moderator who made the report. Empty if report was made by
/// a regular user.
/// </summary>
public string ModeratorName { get; set; }

/// <summary>
/// Number of reports matching <see cref="Reason"/>
/// </summary>
public int Count { get; set; }
}
}

0 comments on commit b8a488f

Please sign in to comment.