Skip to content

Commit b7cd786

Browse files
authored
Merge pull request #34 from pimanac/listing-fixes
yield new replies to modmails in a ListingStream
2 parents 19def22 + 9b09d54 commit b7cd786

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

RedditSharp/Listing.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ private void PageForward()
255255
{
256256
int limit = LimitPerRequest;
257257

258-
if (limit > MaximumLimit)
258+
if (limit > MaximumLimit && MaximumLimit != -1)
259259
{
260260
// If the limit is more than the maximum number of listings, adjust
261261
limit = MaximumLimit;
262262
}
263-
else if (Count + limit > MaximumLimit)
263+
else if (Count + limit > MaximumLimit && MaximumLimit != -1)
264264
{
265265
// If a smaller subset of listings are needed, adjust the limit
266266
limit = MaximumLimit - Count;
@@ -289,7 +289,6 @@ private void PageForward()
289289
Parse(json);
290290
}
291291

292-
293292
private void Parse(JToken json)
294293
{
295294
var children = json["data"]["children"] as JArray;
@@ -301,8 +300,24 @@ private void Parse(JToken json)
301300
things.Add(Thing.Parse<T>(Listing.Reddit, children[i], Listing.WebAgent));
302301
else
303302
{
304-
// we only want to see new items.
303+
var kind = children[i]["kind"].ValueOrDefault<string>();
305304
var id = children[i]["data"]["id"].ValueOrDefault<string>();
305+
306+
// check for new replies to pm / modmail
307+
if (kind == "t4" && children[i]["data"]["replies"].HasValues)
308+
{
309+
var replies = children[i]["data"]["replies"]["data"]["children"] as JArray;
310+
foreach (var reply in replies)
311+
{
312+
var replyId = reply["data"]["id"].ValueOrDefault<string>();
313+
if (done.Contains(replyId))
314+
continue;
315+
316+
things.Add(Thing.Parse<T>(Listing.Reddit, reply, Listing.WebAgent));
317+
done.Add(replyId);
318+
}
319+
}
320+
306321
if (String.IsNullOrEmpty(id) || done.Contains(id))
307322
continue;
308323

@@ -311,6 +326,7 @@ private void Parse(JToken json)
311326
}
312327
}
313328

329+
// this doesn't really work when we're processing messages with replies.
314330
if (stream)
315331
things.Reverse();
316332

@@ -390,8 +406,6 @@ private bool MoveNextForward()
390406
catch (Exception ex)
391407
{
392408
// sleep for a while to see if we can recover
393-
// Sleep() will rethrow after waiting a bit
394-
// todo: make this smarter
395409
Sleep(tries,ex);
396410
}
397411

RedditSharp/Things/Subreddit.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class Subreddit : Thing
4343
private const string ModLogUrl = "/r/{0}/about/log.json";
4444
private const string ContributorsUrl = "/r/{0}/about/contributors.json";
4545
private const string BannedUsersUrl = "/r/{0}/about/banned.json";
46+
private const string ModmailUrl = "/r/{0}/message/moderator/inbox.json";
4647

4748
[JsonIgnore]
4849
private Reddit Reddit { get; set; }
@@ -349,6 +350,21 @@ public Listing<BannedUser> BannedUsers
349350
}
350351
}
351352

353+
/// <summary>
354+
/// Subreddit modmail.
355+
/// <para/>
356+
/// When calling <see cref="System.Linq.Enumerable.Take{T}"/> make sure to take replies into account!
357+
/// </summary>
358+
public Listing<PrivateMessage> Modmail
359+
{
360+
get
361+
{
362+
if (Reddit.User == null)
363+
throw new AuthenticationException("No user logged in.");
364+
return new Listing<PrivateMessage>(Reddit, string.Format(ModmailUrl, Name), WebAgent);
365+
}
366+
}
367+
352368
public async Task<Subreddit> InitAsync(Reddit reddit, JToken json, IWebAgent webAgent)
353369
{
354370
CommonInit(reddit, json, webAgent);

0 commit comments

Comments
 (0)