Skip to content

Commit

Permalink
Merge pull request #20 from czf/feature-more-pt2
Browse files Browse the repository at this point in the history
More comments
  • Loading branch information
CrustyJew authored Oct 28, 2016
2 parents bbfa328 + 650aca0 commit 1374742
Show file tree
Hide file tree
Showing 14 changed files with 811 additions and 19 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ TestResults/
*.vsp

# StyleCop files
StyleCop.Cache
StyleCop.Cache
.vs/config/applicationhost.config
/.vs
/packages
6 changes: 5 additions & 1 deletion Rebracer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
</ToolsOptionsSubCategory>
</ToolsOptionsCategory>
<ToolsOptionsCategory name="TextEditor">
<ToolsOptionsSubCategory name="CSharp-Specific">
<ToolsOptionsSubCategory name="CSharp">
<PropertyValue name="InsertTabs">false</PropertyValue>
</ToolsOptionsSubCategory>
<ToolsOptionsSubCategory name="CSharp-Specific">
<PropertyValue name="InsertTabs">false</PropertyValue>
<PropertyValue name="AddImport_SuggestForTypesInNuGetPackages">0</PropertyValue>
<PropertyValue name="AddImport_SuggestForTypesInReferenceAssemblies">0</PropertyValue>
<PropertyValue name="AutoComment">1</PropertyValue>
Expand Down
1 change: 1 addition & 0 deletions RedditSharp/RedditSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Things\BannedUser.cs" />
<Compile Include="Things\Contributor.cs" />
<Compile Include="Things\ModAction.cs" />
<Compile Include="Things\More.cs" />
<Compile Include="Extensions\DateTimeExtensions\DateTimeExtensions.cs" />
<Compile Include="SpamFilterSettings.cs" />
<Compile Include="Things\AuthenticatedUser.cs" />
Expand Down
66 changes: 59 additions & 7 deletions RedditSharp/Things/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,55 @@ public Comment Init(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender
return this;
}

public Comment PopulateComments(IEnumerator<Thing> things)
{
Thing first = things.Current;
Dictionary<string, Tuple<Comment, List<Comment>>> comments = new Dictionary<string, Tuple<Comment, List<Comment>>>();
comments[this.FullName] = Tuple.Create<Comment, List<Comment>>(this, new List<Comment>());

while (things.MoveNext() && (first is Comment || first is More))
{
first = things.Current;
if (first is Comment)
{
Comment comment = (Comment)first;
comments[comment.FullName] = Tuple.Create<Comment, List<Comment>>(comment, new List<Comment>());
if (comments.ContainsKey(comment.ParentId))
{
comments[comment.ParentId].Item2.Add(comment);
}
else if (comment.ParentId == this.ParentId)
{
//only want sub comments.
break;
}
}
else if (first is More)
{
More more = (More)first;
if (comments.ContainsKey(more.ParentId))
{
comments[more.ParentId].Item1.More = more;
}
else if (more.ParentId == this.ParentId)
{
// This is more for parent.
// Need to process the comments dictionary.
break;
}
}
//things.MoveNext();

}

foreach (KeyValuePair<string, Tuple<Comment, List<Comment>>> kvp in comments)
{
kvp.Value.Item1.Comments = kvp.Value.Item2.ToArray();
}

return this;
}

private JToken CommonInit(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
{
Init(reddit, webAgent, json);
Expand All @@ -57,7 +106,7 @@ private JToken CommonInit(Reddit reddit, JToken json, IWebAgent webAgent, Thing
var context = data["context"].Value<string>();
LinkId = context.Split('/')[4];
}

return data;
}
private async Task<JToken> CommonInitAsync(Reddit reddit, JToken json, IWebAgent webAgent, Thing sender)
Expand Down Expand Up @@ -86,7 +135,7 @@ private void ParseComments(Reddit reddit, JToken data, IWebAgent webAgent, Thing
if (replies != null && replies.Count() > 0)
{
foreach (var comment in replies["data"]["children"])
subComments.Add( new Comment().Init(reddit, comment, webAgent, sender));
subComments.Add(new Comment().Init(reddit, comment, webAgent, sender));
}
Comments = subComments.ToArray();
}
Expand Down Expand Up @@ -133,6 +182,9 @@ private async Task ParseCommentsAsync(Reddit reddit, JToken data, IWebAgent webA
[JsonProperty("stickied")]
public bool IsStickied { get; set; }

[JsonIgnore]
public More More { get; set; }

[JsonIgnore]
public IList<Comment> Comments { get; private set; }

Expand Down Expand Up @@ -280,11 +332,11 @@ public void SetAsRead()
{
var request = WebAgent.CreatePost(SetAsReadUrl);
WebAgent.WritePostBody(request.GetRequestStream(), new
{
id = FullName,
uh = Reddit.User.Modhash,
api_type = "json"
});
{
id = FullName,
uh = Reddit.User.Modhash,
api_type = "json"
});
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
}
Expand Down
68 changes: 68 additions & 0 deletions RedditSharp/Things/More.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace RedditSharp.Things
{
public class More : Thing
{
private const string MoreUrl = "/api/morechildren.json?link_id={0}&children={1}&api_type=json";

[JsonIgnore]
private Reddit Reddit { get; set; }

[JsonIgnore]
private IWebAgent WebAgent { get; set; }

[JsonProperty("children")]
public string[] Children { get; set; }

[JsonProperty("parent_id")]
public string ParentId { get; set; }

public More Init(Reddit reddit, JToken more, IWebAgent webAgent)
{
CommonInit(reddit, more, webAgent);
JsonConvert.PopulateObject(more["data"].ToString(), this, reddit.JsonSerializerSettings);
return this;
}
private void CommonInit(Reddit reddit, JToken more, IWebAgent webAgent)
{
base.Init(more);
Reddit = reddit;
WebAgent = webAgent;
}

public IEnumerable<Thing> Things()
{
var url = string.Format(MoreUrl, ParentId, string.Join(",", Children));
var request = WebAgent.CreateGet(url);
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
var json = JObject.Parse(data)["json"];
if (json["errors"].Count() != 0)
throw new AuthenticationException("Incorrect login.");
var moreJson = json["data"]["things"];

foreach (JToken token in moreJson)
{
Thing parsed = Thing.Parse(Reddit, token, WebAgent);

yield return parsed;
}

}

internal async Task<Thing> InitAsync(Reddit reddit, JToken json, IWebAgent webAgent)
{
CommonInit(reddit, json, webAgent);
await JsonConvert.PopulateObjectAsync(json["data"].ToString(), this, reddit.JsonSerializerSettings);
return this;
}
}
}
72 changes: 63 additions & 9 deletions RedditSharp/Things/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ public Comment Comment(string message)
var request = WebAgent.CreatePost(CommentUrl);
var stream = request.GetRequestStream();
WebAgent.WritePostBody(stream, new
{
text = message,
thing_id = FullName,
uh = Reddit.User.Modhash,
api_type = "json"
});
{
text = message,
thing_id = FullName,
uh = Reddit.User.Modhash,
api_type = "json"
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
Expand Down Expand Up @@ -209,7 +209,7 @@ private string SimpleActionToggle(string endpoint, bool value, bool requiresModA
if (requiresModAction && !modNameList.Contains(Reddit.User.Name))
throw new AuthenticationException(
string.Format(
@"User {0} is not a moderator of subreddit {1}.",
@"User {0} is not a moderator of subreddit {1}.",
Reddit.User.Name,
this.Subreddit.Name));

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

var request = WebAgent.CreatePost(string.Format(SetFlairUrl,SubredditName));
var request = WebAgent.CreatePost(string.Format(SetFlairUrl, SubredditName));
WebAgent.WritePostBody(request.GetRequestStream(), new
{
api_type = "json",
Expand Down Expand Up @@ -390,10 +390,64 @@ public List<Comment> ListComments(int? limit = null)
var comments = new List<Comment>();
foreach (var comment in postJson)
{
comments.Add(new Comment().Init(Reddit, comment, WebAgent, this));
Comment newComment = new Comment().Init(Reddit, comment, WebAgent, this);
if (newComment.Kind == "more")
{
}
else
{
comments.Add(newComment);
}
}

return comments;
}

public IEnumerable<Comment> EnumerateComments()
{
var url = string.Format(GetCommentsUrl, Id);
var request = WebAgent.CreateGet(url);
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
var json = JArray.Parse(data);
var postJson = json.Last()["data"]["children"];
More moreComments = null;
foreach (var comment in postJson)
{
Comment newComment = new Comment().Init(Reddit, comment, WebAgent, this);
if (newComment.Kind == "more")
{
moreComments = new More().Init(Reddit, comment, WebAgent);
}
else
{
yield return newComment;
}
}


if (moreComments != null)
{
IEnumerator<Thing> things = moreComments.Things().GetEnumerator();
things.MoveNext();
Thing currentThing = null;
while (currentThing != things.Current)
{
currentThing = things.Current;
if (things.Current is Comment)
{
Comment next = ((Comment)things.Current).PopulateComments(things);
yield return next;
}
if (things.Current is More)
{
More more = (More)things.Current;
if (more.ParentId != FullName) break;
things = more.Things().GetEnumerator();
things.MoveNext();
}
}
}
}
}
}
12 changes: 12 additions & 0 deletions RedditSharp/Things/Thing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public static async Task<Thing> ParseAsync(Reddit reddit, JToken json, IWebAgent
return await new Subreddit().InitAsync(reddit, json, webAgent);
case "modaction":
return await new ModAction().InitAsync(reddit, json, webAgent);
case "more":
return await new More().InitAsync(reddit, json, webAgent);
default:
return null;
}
Expand Down Expand Up @@ -97,6 +99,8 @@ public static Thing Parse(Reddit reddit, JToken json, IWebAgent webAgent)
return new Subreddit().Init(reddit, json, webAgent);
case "modaction":
return new ModAction().Init(reddit, json, webAgent);
case "more":
return new More().Init(reddit, json, webAgent);
default:
return null;
}
Expand Down Expand Up @@ -130,6 +134,10 @@ public static async Task<Thing> ParseAsync<T>(Reddit reddit, JToken json, IWebAg
{
return await new BannedUser().InitAsync(reddit, json, webAgent);
}
else if (typeof(T) == typeof(More))
{
return await new More().InitAsync(reddit, json, webAgent);
}
}
return result;
}
Expand All @@ -154,6 +162,10 @@ public static Thing Parse<T>(Reddit reddit, JToken json, IWebAgent webAgent) whe
{
return new BannedUser().Init(reddit, json, webAgent);
}
else if (typeof(T) == typeof(More))
{
return new More().Init(reddit, json, webAgent);
}
}
return result;
}
Expand Down
Loading

0 comments on commit 1374742

Please sign in to comment.