Skip to content

Commit cf9c789

Browse files
committed
Add functionality to read and parse Moderator Toolbox Usernotes from the wiki page.
1 parent 163f4b7 commit cf9c789

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

RedditSharp/RedditSharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<Compile Include="Things\WikiPageRevision.cs" />
9797
<Compile Include="WikiPageSettings.cs" />
9898
<Compile Include="Domain.cs" />
99+
<Compile Include="tbUserNote.cs" />
100+
<Compile Include="ToolBoxUserNotes.cs" />
101+
<Compile Include="ToolBoxUserNotesException.cs" />
99102
</ItemGroup>
100103
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
101104
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

RedditSharp/Things/Subreddit.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ public IEnumerable<ModeratorUser> Moderators
264264
}
265265
}
266266

267+
public IEnumerable<tbUserNote> UserNotes
268+
{
269+
get
270+
{
271+
return ToolBoxUserNotes.GetUserNotes(WebAgent, Name);
272+
}
273+
}
274+
267275
public Subreddit Init(Reddit reddit, JToken json, IWebAgent webAgent)
268276
{
269277
CommonInit(reddit, json, webAgent);

RedditSharp/ToolBoxUserNotes.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json.Linq;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.IO.Compression;
8+
9+
namespace RedditSharp
10+
{
11+
public static class ToolBoxUserNotes
12+
{
13+
private const string ToolBoxUserNotesWiki = "/r/{0}/wiki/usernotes";
14+
public static IEnumerable<tbUserNote> GetUserNotes( IWebAgent webAgent, string subName)
15+
{
16+
var request = webAgent.CreateGet(String.Format(ToolBoxUserNotesWiki, subName));
17+
var reqResponse = webAgent.ExecuteRequest(request);
18+
var response = JObject.Parse(reqResponse["data"]["content_md"].Value<string>());
19+
20+
int version = response["ver"].Value<int>();
21+
string[] mods = response["constants"]["users"].Values<string>().ToArray();
22+
23+
string[] warnings = response["constants"]["warnings"].Values<string>().ToArray();
24+
25+
if (version < 6)
26+
{
27+
throw new ToolBoxUserNotesException("Unsupported ToolBox version");
28+
}
29+
try {
30+
var data = Convert.FromBase64String(response["blob"].Value<string>());
31+
32+
string uncompressed;
33+
using (System.IO.MemoryStream compressedStream = new System.IO.MemoryStream(data))
34+
{
35+
compressedStream.ReadByte();
36+
compressedStream.ReadByte(); //skips first to bytes to fix zlib block size
37+
using (DeflateStream blobStream = new DeflateStream(compressedStream, CompressionMode.Decompress))
38+
{
39+
using (var decompressedReader = new System.IO.StreamReader(blobStream))
40+
{
41+
uncompressed = decompressedReader.ReadToEnd();
42+
}
43+
44+
}
45+
}
46+
47+
JObject users = JObject.Parse(uncompressed);
48+
49+
List<tbUserNote> toReturn = new List<tbUserNote>();
50+
foreach (KeyValuePair<string, JToken> user in users)
51+
{
52+
var x = user.Value;
53+
foreach (JToken note in x["ns"].Children())
54+
{
55+
56+
tbUserNote uNote = new tbUserNote();
57+
uNote.AppliesToUsername = user.Key;
58+
uNote.SubName = subName;
59+
uNote.SubmitterIndex = note["m"].Value<int>();
60+
uNote.Submitter = mods[uNote.SubmitterIndex];
61+
uNote.NoteTypeIndex = note["w"].Value<int>();
62+
uNote.NoteType = warnings[uNote.NoteTypeIndex];
63+
uNote.Message = note["n"].Value<string>();
64+
uNote.Timestamp = UnixTimeStamp.UnixTimeStampToDateTime(note["t"].Value<long>());
65+
uNote.Url = UnsquashLink(subName, note["l"].ValueOrDefault<string>());
66+
67+
toReturn.Add(uNote);
68+
}
69+
}
70+
return toReturn;
71+
}
72+
catch(Exception e)
73+
{
74+
throw new ToolBoxUserNotesException("An error occured while processing Usernotes wiki. See inner exception for details", e);
75+
}
76+
77+
}
78+
public static string UnsquashLink(string subreddit,string permalink)
79+
{
80+
81+
var link = "https://reddit.com/r/" + subreddit + "/";
82+
if (string.IsNullOrEmpty(permalink)) {
83+
return link;
84+
}
85+
var linkParams = permalink.Split(',');
86+
87+
if (linkParams[0] == "l")
88+
{
89+
link += "comments/" + linkParams[1] + "/";
90+
if (linkParams.Length > 2)
91+
link += "-/" + linkParams[2] + "/";
92+
}
93+
else if (linkParams[0] == "m")
94+
{
95+
link += "message/messages/" + linkParams[1];
96+
}
97+
98+
return link;
99+
}
100+
}
101+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace RedditSharp
8+
{
9+
class ToolBoxUserNotesException : Exception
10+
{
11+
public ToolBoxUserNotesException()
12+
{
13+
}
14+
15+
public ToolBoxUserNotesException(string message)
16+
: base(message)
17+
{
18+
}
19+
20+
public ToolBoxUserNotesException(string message, Exception inner)
21+
: base(message, inner)
22+
{
23+
}
24+
}
25+
}

RedditSharp/tbUserNote.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace RedditSharp
8+
{
9+
public class tbUserNote
10+
{
11+
public int NoteTypeIndex { get; set; }
12+
public string NoteType { get; set; }
13+
public string SubName { get; set; }
14+
public string Submitter { get; set; }
15+
public int SubmitterIndex { get; set; }
16+
public string Message { get; set; }
17+
public string AppliesToUsername { get; set; }
18+
public string Url { get; set; }
19+
private DateTime _timestamp;
20+
public DateTime Timestamp
21+
{
22+
get { return _timestamp; }
23+
set
24+
{
25+
_timestamp = DateTime.SpecifyKind(value, DateTimeKind.Utc);
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)