@@ -36,7 +36,7 @@ public class Reddit
36
36
private const string PopularSubredditsUrl = "/subreddits/popular.json" ;
37
37
private const string GoldSubredditsUrl = "/subreddits/gold.json" ;
38
38
private const string DefaultSubredditsUrl = "/subreddits/default.json" ;
39
- private const string SearchSubredditsUrl = "/subreddits/search.json?q={0}" ;
39
+ private const string SearchSubredditsUrl = "/subreddits/search.json?q={0}" ;
40
40
41
41
42
42
#endregion
@@ -129,35 +129,35 @@ public Reddit(string accessToken)
129
129
WebAgent . AccessToken = accessToken ;
130
130
InitOrUpdateUser ( ) ;
131
131
}
132
- /// <summary>
133
- /// Creates a Reddit instance with the given WebAgent implementation
134
- /// </summary>
132
+ /// <summary>
133
+ /// Creates a Reddit instance with the given WebAgent implementation
134
+ /// </summary>
135
135
/// <param name="agent">Implementation of IWebAgent interface. Used to generate requests.</param>
136
- public Reddit ( IWebAgent agent )
137
- {
138
- WebAgent = agent ;
139
- JsonSerializerSettings = new JsonSerializerSettings
140
- {
141
- CheckAdditionalContent = false ,
142
- DefaultValueHandling = DefaultValueHandling . Ignore
136
+ public Reddit ( IWebAgent agent )
137
+ {
138
+ WebAgent = agent ;
139
+ JsonSerializerSettings = new JsonSerializerSettings
140
+ {
141
+ CheckAdditionalContent = false ,
142
+ DefaultValueHandling = DefaultValueHandling . Ignore
143
143
} ;
144
- CaptchaSolver = new ConsoleCaptchaSolver ( ) ;
144
+ CaptchaSolver = new ConsoleCaptchaSolver ( ) ;
145
145
}
146
- /// <summary>
147
- /// Creates a Reddit instance with the given WebAgent implementation
148
- /// </summary>
146
+ /// <summary>
147
+ /// Creates a Reddit instance with the given WebAgent implementation
148
+ /// </summary>
149
149
/// <param name="agent">Implementation of IWebAgent interface. Used to generate requests.</param>
150
150
/// <param name="initUser">Whether to run InitOrUpdateUser, requires <paramref name="agent"/> to have credentials first.</param>
151
- public Reddit ( IWebAgent agent , bool initUser )
152
- {
153
- WebAgent = agent ;
154
- JsonSerializerSettings = new JsonSerializerSettings
155
- {
156
- CheckAdditionalContent = false ,
157
- DefaultValueHandling = DefaultValueHandling . Ignore
151
+ public Reddit ( IWebAgent agent , bool initUser )
152
+ {
153
+ WebAgent = agent ;
154
+ JsonSerializerSettings = new JsonSerializerSettings
155
+ {
156
+ CheckAdditionalContent = false ,
157
+ DefaultValueHandling = DefaultValueHandling . Ignore
158
158
} ;
159
- CaptchaSolver = new ConsoleCaptchaSolver ( ) ;
160
- if ( initUser ) InitOrUpdateUser ( ) ;
159
+ CaptchaSolver = new ConsoleCaptchaSolver ( ) ;
160
+ if ( initUser ) InitOrUpdateUser ( ) ;
161
161
}
162
162
163
163
/// <summary>
@@ -295,17 +295,43 @@ public Post GetPost(Uri uri)
295
295
return new Post ( ) . Init ( this , GetToken ( uri ) , WebAgent ) ;
296
296
}
297
297
298
- public void ComposePrivateMessage ( string subject , string body , string to , string captchaId = "" , string captchaAnswer = "" )
298
+ /// <summary>
299
+ ///
300
+ /// </summary>
301
+ /// <param name="subject"></param>
302
+ /// <param name="body"></param>
303
+ /// <param name="to"></param>
304
+ /// <param name="fromSubReddit">The subreddit to send the message as (optional).</param>
305
+ /// <param name="captchaId"></param>
306
+ /// <param name="captchaAnswer"></param>
307
+ /// <remarks>If <paramref name="fromSubReddit"/> is passed in then the message is sent from the subreddit. the sender must be a mod of the specified subreddit.</remarks>
308
+ /// <exception cref="AuthenticationException">Thrown when a subreddit is passed in and the user is not a mod of that sub.</exception>
309
+ public void ComposePrivateMessage ( string subject , string body , string to , string fromSubReddit = "" , string captchaId = "" , string captchaAnswer = "" )
299
310
{
300
311
if ( User == null )
301
312
throw new Exception ( "User can not be null." ) ;
313
+
314
+ if ( ! String . IsNullOrWhiteSpace ( fromSubReddit ) )
315
+ {
316
+ var subReddit = this . GetSubreddit ( fromSubReddit ) ;
317
+ var modNameList = subReddit . Moderators . Select ( b => b . Name ) . ToList ( ) ;
318
+
319
+ if ( ! modNameList . Contains ( User . Name ) )
320
+ throw new AuthenticationException (
321
+ String . Format (
322
+ @"User {0} is not a moderator of subreddit {1}." ,
323
+ User . Name ,
324
+ subReddit . Name ) ) ;
325
+ }
326
+
302
327
var request = WebAgent . CreatePost ( ComposeMessageUrl ) ;
303
328
WebAgent . WritePostBody ( request . GetRequestStream ( ) , new
304
329
{
305
330
api_type = "json" ,
306
331
subject ,
307
332
text = body ,
308
333
to ,
334
+ from_sr = fromSubReddit ,
309
335
uh = User . Modhash ,
310
336
iden = captchaId ,
311
337
captcha = captchaAnswer
@@ -403,6 +429,17 @@ public Listing<T> Search<T>(string query, Sorting sortE = Sorting.Relevance, Tim
403
429
return new Listing < T > ( this , string . Format ( SearchUrl , query , sort , time ) , WebAgent ) ;
404
430
}
405
431
432
+ public Listing < T > SearchByTimestamp < T > ( DateTime from , DateTime to , string query = "" , string subreddit = "" , Sorting sortE = Sorting . Relevance , TimeSorting timeE = TimeSorting . All ) where T : Thing
433
+ {
434
+ string sort = sortE . ToString ( ) . ToLower ( ) ;
435
+ string time = timeE . ToString ( ) . ToLower ( ) ;
436
+
437
+ var fromUnix = ( from - new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 ) ) . TotalSeconds ;
438
+ var toUnix = ( to - new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 ) ) . TotalSeconds ;
439
+
440
+ string searchQuery = "(and+timestamp:" + fromUnix + ".." + toUnix + "+'" + query + "'+" + "subreddit:'" + subreddit + "')&syntax=cloudsearch" ;
441
+ return new Listing < T > ( this , string . Format ( SearchUrl , searchQuery , sort , time ) , WebAgent ) ;
442
+ }
406
443
407
444
408
445
#region SubredditSearching
@@ -441,15 +478,15 @@ public Listing<Subreddit> GetGoldSubreddits()
441
478
public Listing < Subreddit > GetDefaultSubreddits ( )
442
479
{
443
480
return new Listing < Subreddit > ( this , DefaultSubredditsUrl , WebAgent ) ;
444
- }
445
-
446
- /// <summary>
447
- /// Returns the Listing of subreddits related to a query.
448
- /// </summary>
449
- /// <returns></returns>
450
- public Listing < Subreddit > SearchSubreddits ( string query )
451
- {
452
- return new Listing < Subreddit > ( this , string . Format ( SearchSubredditsUrl , query ) , WebAgent ) ;
481
+ }
482
+
483
+ /// <summary>
484
+ /// Returns the Listing of subreddits related to a query.
485
+ /// </summary>
486
+ /// <returns></returns>
487
+ public Listing < Subreddit > SearchSubreddits ( string query )
488
+ {
489
+ return new Listing < Subreddit > ( this , string . Format ( SearchSubredditsUrl , query ) , WebAgent ) ;
453
490
}
454
491
455
492
#endregion SubredditSearching
0 commit comments