Skip to content

Commit 1374742

Browse files
authored
Merge pull request #20 from czf/feature-more-pt2
More comments
2 parents bbfa328 + 650aca0 commit 1374742

File tree

14 files changed

+811
-19
lines changed

14 files changed

+811
-19
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ TestResults/
1111
*.vsp
1212

1313
# StyleCop files
14-
StyleCop.Cache
14+
StyleCop.Cache
15+
.vs/config/applicationhost.config
16+
/.vs
17+
/packages

Rebracer.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
</ToolsOptionsSubCategory>
2222
</ToolsOptionsCategory>
2323
<ToolsOptionsCategory name="TextEditor">
24-
<ToolsOptionsSubCategory name="CSharp-Specific">
24+
<ToolsOptionsSubCategory name="CSharp">
25+
<PropertyValue name="InsertTabs">false</PropertyValue>
26+
</ToolsOptionsSubCategory>
27+
<ToolsOptionsSubCategory name="CSharp-Specific">
28+
<PropertyValue name="InsertTabs">false</PropertyValue>
2529
<PropertyValue name="AddImport_SuggestForTypesInNuGetPackages">0</PropertyValue>
2630
<PropertyValue name="AddImport_SuggestForTypesInReferenceAssemblies">0</PropertyValue>
2731
<PropertyValue name="AutoComment">1</PropertyValue>

RedditSharp/RedditSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="Things\BannedUser.cs" />
5757
<Compile Include="Things\Contributor.cs" />
5858
<Compile Include="Things\ModAction.cs" />
59+
<Compile Include="Things\More.cs" />
5960
<Compile Include="Extensions\DateTimeExtensions\DateTimeExtensions.cs" />
6061
<Compile Include="SpamFilterSettings.cs" />
6162
<Compile Include="Things\AuthenticatedUser.cs" />

RedditSharp/Things/Comment.cs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,55 @@ public Comment Init(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender
4343
return this;
4444
}
4545

46+
public Comment PopulateComments(IEnumerator<Thing> things)
47+
{
48+
Thing first = things.Current;
49+
Dictionary<string, Tuple<Comment, List<Comment>>> comments = new Dictionary<string, Tuple<Comment, List<Comment>>>();
50+
comments[this.FullName] = Tuple.Create<Comment, List<Comment>>(this, new List<Comment>());
51+
52+
while (things.MoveNext() && (first is Comment || first is More))
53+
{
54+
first = things.Current;
55+
if (first is Comment)
56+
{
57+
Comment comment = (Comment)first;
58+
comments[comment.FullName] = Tuple.Create<Comment, List<Comment>>(comment, new List<Comment>());
59+
if (comments.ContainsKey(comment.ParentId))
60+
{
61+
comments[comment.ParentId].Item2.Add(comment);
62+
}
63+
else if (comment.ParentId == this.ParentId)
64+
{
65+
//only want sub comments.
66+
break;
67+
}
68+
}
69+
else if (first is More)
70+
{
71+
More more = (More)first;
72+
if (comments.ContainsKey(more.ParentId))
73+
{
74+
comments[more.ParentId].Item1.More = more;
75+
}
76+
else if (more.ParentId == this.ParentId)
77+
{
78+
// This is more for parent.
79+
// Need to process the comments dictionary.
80+
break;
81+
}
82+
}
83+
//things.MoveNext();
84+
85+
}
86+
87+
foreach (KeyValuePair<string, Tuple<Comment, List<Comment>>> kvp in comments)
88+
{
89+
kvp.Value.Item1.Comments = kvp.Value.Item2.ToArray();
90+
}
91+
92+
return this;
93+
}
94+
4695
private JToken CommonInit(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
4796
{
4897
Init(reddit, webAgent, json);
@@ -57,7 +106,7 @@ private JToken CommonInit(Reddit reddit, JToken json, IWebAgent webAgent, Thing
57106
var context = data["context"].Value<string>();
58107
LinkId = context.Split('/')[4];
59108
}
60-
109+
61110
return data;
62111
}
63112
private async Task<JToken> CommonInitAsync(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
@@ -86,7 +135,7 @@ private void ParseComments(Reddit reddit, JToken data, IWebAgent webAgent, Thing
86135
if (replies != null && replies.Count() > 0)
87136
{
88137
foreach (var comment in replies["data"]["children"])
89-
subComments.Add( new Comment().Init(reddit, comment, webAgent, sender));
138+
subComments.Add(new Comment().Init(reddit, comment, webAgent, sender));
90139
}
91140
Comments = subComments.ToArray();
92141
}
@@ -133,6 +182,9 @@ private async Task ParseCommentsAsync(Reddit reddit, JToken data, IWebAgent webA
133182
[JsonProperty("stickied")]
134183
public bool IsStickied { get; set; }
135184

185+
[JsonIgnore]
186+
public More More { get; set; }
187+
136188
[JsonIgnore]
137189
public IList<Comment> Comments { get; private set; }
138190

@@ -280,11 +332,11 @@ public void SetAsRead()
280332
{
281333
var request = WebAgent.CreatePost(SetAsReadUrl);
282334
WebAgent.WritePostBody(request.GetRequestStream(), new
283-
{
284-
id = FullName,
285-
uh = Reddit.User.Modhash,
286-
api_type = "json"
287-
});
335+
{
336+
id = FullName,
337+
uh = Reddit.User.Modhash,
338+
api_type = "json"
339+
});
288340
var response = request.GetResponse();
289341
var data = WebAgent.GetResponseString(response.GetResponseStream());
290342
}

RedditSharp/Things/More.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Authentication;
5+
using System.Threading.Tasks;
6+
using System.Web;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Linq;
9+
10+
namespace RedditSharp.Things
11+
{
12+
public class More : Thing
13+
{
14+
private const string MoreUrl = "/api/morechildren.json?link_id={0}&children={1}&api_type=json";
15+
16+
[JsonIgnore]
17+
private Reddit Reddit { get; set; }
18+
19+
[JsonIgnore]
20+
private IWebAgent WebAgent { get; set; }
21+
22+
[JsonProperty("children")]
23+
public string[] Children { get; set; }
24+
25+
[JsonProperty("parent_id")]
26+
public string ParentId { get; set; }
27+
28+
public More Init(Reddit reddit, JToken more, IWebAgent webAgent)
29+
{
30+
CommonInit(reddit, more, webAgent);
31+
JsonConvert.PopulateObject(more["data"].ToString(), this, reddit.JsonSerializerSettings);
32+
return this;
33+
}
34+
private void CommonInit(Reddit reddit, JToken more, IWebAgent webAgent)
35+
{
36+
base.Init(more);
37+
Reddit = reddit;
38+
WebAgent = webAgent;
39+
}
40+
41+
public IEnumerable<Thing> Things()
42+
{
43+
var url = string.Format(MoreUrl, ParentId, string.Join(",", Children));
44+
var request = WebAgent.CreateGet(url);
45+
var response = request.GetResponse();
46+
var data = WebAgent.GetResponseString(response.GetResponseStream());
47+
var json = JObject.Parse(data)["json"];
48+
if (json["errors"].Count() != 0)
49+
throw new AuthenticationException("Incorrect login.");
50+
var moreJson = json["data"]["things"];
51+
52+
foreach (JToken token in moreJson)
53+
{
54+
Thing parsed = Thing.Parse(Reddit, token, WebAgent);
55+
56+
yield return parsed;
57+
}
58+
59+
}
60+
61+
internal async Task<Thing> InitAsync(Reddit reddit, JToken json, IWebAgent webAgent)
62+
{
63+
CommonInit(reddit, json, webAgent);
64+
await JsonConvert.PopulateObjectAsync(json["data"].ToString(), this, reddit.JsonSerializerSettings);
65+
return this;
66+
}
67+
}
68+
}

RedditSharp/Things/Post.cs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ public Comment Comment(string message)
167167
var request = WebAgent.CreatePost(CommentUrl);
168168
var stream = request.GetRequestStream();
169169
WebAgent.WritePostBody(stream, new
170-
{
171-
text = message,
172-
thing_id = FullName,
173-
uh = Reddit.User.Modhash,
174-
api_type = "json"
175-
});
170+
{
171+
text = message,
172+
thing_id = FullName,
173+
uh = Reddit.User.Modhash,
174+
api_type = "json"
175+
});
176176
stream.Close();
177177
var response = request.GetResponse();
178178
var data = WebAgent.GetResponseString(response.GetResponseStream());
@@ -209,7 +209,7 @@ private string SimpleActionToggle(string endpoint, bool value, bool requiresModA
209209
if (requiresModAction && !modNameList.Contains(Reddit.User.Name))
210210
throw new AuthenticationException(
211211
string.Format(
212-
@"User {0} is not a moderator of subreddit {1}.",
212+
@"User {0} is not a moderator of subreddit {1}.",
213213
Reddit.User.Name,
214214
this.Subreddit.Name));
215215

@@ -354,7 +354,7 @@ public void SetFlair(string flairText, string flairClass)
354354
if (Reddit.User == null)
355355
throw new Exception("No user logged in.");
356356

357-
var request = WebAgent.CreatePost(string.Format(SetFlairUrl,SubredditName));
357+
var request = WebAgent.CreatePost(string.Format(SetFlairUrl, SubredditName));
358358
WebAgent.WritePostBody(request.GetRequestStream(), new
359359
{
360360
api_type = "json",
@@ -390,10 +390,64 @@ public List<Comment> ListComments(int? limit = null)
390390
var comments = new List<Comment>();
391391
foreach (var comment in postJson)
392392
{
393-
comments.Add(new Comment().Init(Reddit, comment, WebAgent, this));
393+
Comment newComment = new Comment().Init(Reddit, comment, WebAgent, this);
394+
if (newComment.Kind == "more")
395+
{
396+
}
397+
else
398+
{
399+
comments.Add(newComment);
400+
}
394401
}
395402

396403
return comments;
397404
}
405+
406+
public IEnumerable<Comment> EnumerateComments()
407+
{
408+
var url = string.Format(GetCommentsUrl, Id);
409+
var request = WebAgent.CreateGet(url);
410+
var response = request.GetResponse();
411+
var data = WebAgent.GetResponseString(response.GetResponseStream());
412+
var json = JArray.Parse(data);
413+
var postJson = json.Last()["data"]["children"];
414+
More moreComments = null;
415+
foreach (var comment in postJson)
416+
{
417+
Comment newComment = new Comment().Init(Reddit, comment, WebAgent, this);
418+
if (newComment.Kind == "more")
419+
{
420+
moreComments = new More().Init(Reddit, comment, WebAgent);
421+
}
422+
else
423+
{
424+
yield return newComment;
425+
}
426+
}
427+
428+
429+
if (moreComments != null)
430+
{
431+
IEnumerator<Thing> things = moreComments.Things().GetEnumerator();
432+
things.MoveNext();
433+
Thing currentThing = null;
434+
while (currentThing != things.Current)
435+
{
436+
currentThing = things.Current;
437+
if (things.Current is Comment)
438+
{
439+
Comment next = ((Comment)things.Current).PopulateComments(things);
440+
yield return next;
441+
}
442+
if (things.Current is More)
443+
{
444+
More more = (More)things.Current;
445+
if (more.ParentId != FullName) break;
446+
things = more.Things().GetEnumerator();
447+
things.MoveNext();
448+
}
449+
}
450+
}
451+
}
398452
}
399453
}

RedditSharp/Things/Thing.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public static async Task<Thing> ParseAsync(Reddit reddit, JToken json, IWebAgent
6969
return await new Subreddit().InitAsync(reddit, json, webAgent);
7070
case "modaction":
7171
return await new ModAction().InitAsync(reddit, json, webAgent);
72+
case "more":
73+
return await new More().InitAsync(reddit, json, webAgent);
7274
default:
7375
return null;
7476
}
@@ -97,6 +99,8 @@ public static Thing Parse(Reddit reddit, JToken json, IWebAgent webAgent)
9799
return new Subreddit().Init(reddit, json, webAgent);
98100
case "modaction":
99101
return new ModAction().Init(reddit, json, webAgent);
102+
case "more":
103+
return new More().Init(reddit, json, webAgent);
100104
default:
101105
return null;
102106
}
@@ -130,6 +134,10 @@ public static async Task<Thing> ParseAsync<T>(Reddit reddit, JToken json, IWebAg
130134
{
131135
return await new BannedUser().InitAsync(reddit, json, webAgent);
132136
}
137+
else if (typeof(T) == typeof(More))
138+
{
139+
return await new More().InitAsync(reddit, json, webAgent);
140+
}
133141
}
134142
return result;
135143
}
@@ -154,6 +162,10 @@ public static Thing Parse<T>(Reddit reddit, JToken json, IWebAgent webAgent) whe
154162
{
155163
return new BannedUser().Init(reddit, json, webAgent);
156164
}
165+
else if (typeof(T) == typeof(More))
166+
{
167+
return new More().Init(reddit, json, webAgent);
168+
}
157169
}
158170
return result;
159171
}

0 commit comments

Comments
 (0)