Skip to content

Commit b8a488f

Browse files
authored
Merge pull request #13 from pimanac/reports
add user reports / mod reports to VotableThing
2 parents ba390a4 + 1c3d17a commit b8a488f

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

RedditSharp/Things/VotableThing.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
24
using System.Threading.Tasks;
35
using System.Linq;
46
using System.Security.Authentication;
@@ -92,6 +94,14 @@ private async Task CommonInitAsync(Reddit reddit, IWebAgent webAgent, JToken jso
9294
[JsonProperty("likes")]
9395
public bool? Liked { get; set; }
9496

97+
[JsonProperty("mod_reports")]
98+
[JsonConverter(typeof(ReportCollectionConverter))]
99+
public ICollection<Report> ModReports { get; set; }
100+
101+
[JsonProperty("user_reports")]
102+
[JsonConverter(typeof(ReportCollectionConverter))]
103+
public ICollection<Report> UserReports { get; set; }
104+
95105
/// <summary>
96106
/// Gets or sets the vote for the current VotableThing.
97107
/// </summary>
@@ -306,5 +316,94 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
306316
writer.WriteValue(d.ToString().ToLower());
307317
}
308318
}
319+
320+
internal class ReportCollectionConverter : JsonConverter
321+
{
322+
public override bool CanConvert(Type objectType)
323+
{
324+
return objectType == typeof(ICollection<Report>) || objectType == typeof(object);
325+
}
326+
327+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
328+
{
329+
var token = JToken.Load(reader);
330+
if (token.Type != JTokenType.Array || token.Children().Count() == 0)
331+
return new Collection<Report>();
332+
333+
var result = new Collection<Report>();
334+
foreach (var child in token.Children())
335+
{
336+
// always tuples
337+
// https://github.com/reddit/reddit/blob/master/r2/r2/models/report.py#L165
338+
if (child.Type != JTokenType.Array || child.Children().Count() != 2)
339+
continue;
340+
341+
var report = new Report();
342+
report.Reason = child.First.Value<string>();
343+
344+
if (child.Last.Type == JTokenType.String)
345+
{
346+
report.ModeratorName = child.Last.Value<string>();
347+
report.Count = 1;
348+
}
349+
else
350+
{
351+
report.ModeratorName = "";
352+
report.Count = child.Last.Value<int>();
353+
}
354+
result.Add(report);
355+
}
356+
return result;
357+
}
358+
359+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
360+
{
361+
var reports = value as ICollection<Report>;
362+
363+
if (reports == null || reports.Count == 0)
364+
{
365+
writer.WriteStartArray();
366+
writer.WriteEndArray();
367+
return;
368+
}
369+
370+
writer.WriteStartArray();
371+
372+
foreach (var report in reports)
373+
{
374+
writer.WriteStartArray();
375+
376+
writer.WriteValue(report.Reason);
377+
378+
if (String.IsNullOrEmpty(report.ModeratorName))
379+
writer.WriteValue(report.Count);
380+
else
381+
writer.WriteValue(report.ModeratorName);
382+
383+
writer.WriteEndArray();
384+
}
385+
386+
writer.WriteEndArray();
387+
}
388+
}
389+
}
390+
391+
public class Report
392+
{
393+
/// <summary>
394+
/// Report reason
395+
/// </summary>
396+
public string Reason { get; set; }
397+
398+
/// <summary>
399+
/// Moderator who made the report. Empty if report was made by
400+
/// a regular user.
401+
/// </summary>
402+
public string ModeratorName { get; set; }
403+
404+
/// <summary>
405+
/// Number of reports matching <see cref="Reason"/>
406+
/// </summary>
407+
public int Count { get; set; }
309408
}
310409
}

0 commit comments

Comments
 (0)