Skip to content

Commit a5e1e6a

Browse files
artofdarkness123CrustyJew
authored andcommitted
added mod actions: ability to send PM via subreddit & ability to sticky a post (#227)
* added ability to send PM via subreddit (mod action) added ability to sticky a post (mod action)
1 parent b4af995 commit a5e1e6a

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

RedditSharp/Reddit.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,17 +295,43 @@ public Post GetPost(Uri uri)
295295
return new Post().Init(this, GetToken(uri), WebAgent);
296296
}
297297

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 = "")
299310
{
300311
if (User == null)
301312
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+
302327
var request = WebAgent.CreatePost(ComposeMessageUrl);
303328
WebAgent.WritePostBody(request.GetRequestStream(), new
304329
{
305330
api_type = "json",
306331
subject,
307332
text = body,
308333
to,
334+
from_sr = fromSubReddit,
309335
uh = User.Modhash,
310336
iden = captchaId,
311337
captcha = captchaAnswer
@@ -415,6 +441,7 @@ public Listing<T> SearchByTimestamp<T>(DateTime from, DateTime to, string query
415441
return new Listing<T>(this, string.Format(SearchUrl, searchQuery, sort, time), WebAgent);
416442
}
417443

444+
418445
#region SubredditSearching
419446

420447
/// <summary>

RedditSharp/Things/Post.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class Post : VotableThing
2323
private const string MarkNSFWUrl = "/api/marknsfw";
2424
private const string UnmarkNSFWUrl = "/api/unmarknsfw";
2525
private const string ContestModeUrl = "/api/set_contest_mode";
26+
private const string StickyModeUrl = "/api/set_subreddit_sticky";
2627

2728
[JsonIgnore]
2829
private Reddit Reddit { get; set; }
@@ -181,10 +182,20 @@ private string SimpleAction(string endpoint)
181182
return data;
182183
}
183184

184-
private string SimpleActionToggle(string endpoint, bool value)
185+
private string SimpleActionToggle(string endpoint, bool value, bool requiresModAction = false)
185186
{
186187
if (Reddit.User == null)
187188
throw new AuthenticationException("No user logged in.");
189+
190+
var modNameList = this.Subreddit.Moderators.Select(b => b.Name).ToList();
191+
192+
if (requiresModAction && !modNameList.Contains(Reddit.User.Name))
193+
throw new AuthenticationException(
194+
String.Format(
195+
@"User {0} is not a moderator of subreddit {1}.",
196+
Reddit.User.Name,
197+
this.Subreddit.Name));
198+
188199
var request = WebAgent.CreatePost(endpoint);
189200
var stream = request.GetRequestStream();
190201
WebAgent.WritePostBody(stream, new
@@ -259,6 +270,11 @@ public void ContestMode(bool state)
259270
var data = SimpleActionToggle(ContestModeUrl, state);
260271
}
261272

273+
public void StickyMode(bool state)
274+
{
275+
var data = SimpleActionToggle(StickyModeUrl, state, true);
276+
}
277+
262278
#region Obsolete Getter Methods
263279

264280
[Obsolete("Use Comments property instead")]

0 commit comments

Comments
 (0)