Skip to content

Commit 88d83cf

Browse files
Banned user additions
* Add params for ban notes, duration, and PM message * Add UnBanUser * Add BannedUsers listing to Subreddit * Re-add missing parsing/init for Contributor
1 parent 570cc70 commit 88d83cf

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

RedditSharp/RedditSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<ItemGroup>
5454
<Compile Include="BotWebAgent.cs" />
5555
<Compile Include="Moderator Actions\ModActionType.cs" />
56+
<Compile Include="Things\BannedUser.cs" />
5657
<Compile Include="Things\Contributor.cs" />
5758
<Compile Include="Things\ModAction.cs" />
5859
<Compile Include="Extensions\DateTimeExtensions\DateTimeExtensions.cs" />

RedditSharp/Things/BannedUser.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
5+
namespace RedditSharp.Things
6+
{
7+
public class BannedUser : Thing
8+
{
9+
[JsonProperty("date")]
10+
[JsonConverter(typeof(UnixTimestampConverter))]
11+
public DateTime? TimeStamp { get; set; }
12+
13+
[JsonProperty("note")]
14+
public string Note { get; set; }
15+
16+
[JsonProperty("name")]
17+
public string UserName { get; set; }
18+
19+
[JsonProperty("id")]
20+
public string UserId { get; set; }
21+
22+
public BannedUser Init(Reddit reddit, JToken json, IWebAgent webAgent)
23+
{
24+
CommonInit(json);
25+
JsonConvert.PopulateObject(json.ToString(), this, reddit.JsonSerializerSettings);
26+
return this;
27+
}
28+
29+
private void CommonInit(JToken json)
30+
{
31+
base.Init(json);
32+
}
33+
}
34+
}

RedditSharp/Things/Subreddit.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Subreddit : Thing
3131
private const string AcceptModeratorInviteUrl = "/api/accept_moderator_invite";
3232
private const string LeaveModerationUrl = "/api/unfriend";
3333
private const string BanUserUrl = "/api/friend";
34+
private const string UnBanUserUrl = "/api/unfriend";
3435
private const string AddModeratorUrl = "/api/friend";
3536
private const string AddContributorUrl = "/api/friend";
3637
private const string ModeratorsUrl = "/r/{0}/about/moderators.json";
@@ -42,6 +43,7 @@ public class Subreddit : Thing
4243
private const string SearchUrlDate = "/r/{0}/search.json?q=timestamp:{1}..{2}&restrict_sr=on&sort={3}&syntax=cloudsearch";
4344
private const string ModLogUrl = "/r/{0}/about/log.json";
4445
private const string ContributorsUrl = "/r/{0}/about/contributors.json";
46+
private const string BannedUsersUrl = "/r/{0}/about/banned.json";
4547

4648
[JsonIgnore]
4749
private Reddit Reddit { get; set; }
@@ -346,6 +348,13 @@ public Listing<Contributor> Contributors
346348
}
347349
}
348350

351+
public Listing<BannedUser> BannedUsers
352+
{
353+
get
354+
{
355+
return new Listing<BannedUser>(Reddit, string.Format(BannedUsersUrl, Name), WebAgent);
356+
}
357+
}
349358

350359
public async Task<Subreddit> InitAsync(Reddit reddit, JToken json, IWebAgent webAgent)
351360
{
@@ -741,6 +750,91 @@ public async Task BanUserAsync(string user, string reason)
741750
var response = await request.GetResponseAsync();
742751
var result = WebAgent.GetResponseString(response.GetResponseStream());
743752
}
753+
754+
/// <summary>
755+
/// Bans a user
756+
/// </summary>
757+
/// <param name="user">User to ban, by username</param>
758+
/// <param name="reason">Reason for ban, shows in ban note as 'reason: note' or just 'note' if blank</param>
759+
/// <param name="note">Mod notes about ban, shows in ban note as 'reason: note'</param>
760+
/// <param name="duration">Number of days to ban user, 0 for permanent</param>
761+
/// <param name="message">Message to include in ban PM</param>
762+
public void BanUser(string user, string reason, string note, int duration, string message)
763+
{
764+
var request = WebAgent.CreatePost(BanUserUrl);
765+
WebAgent.WritePostBody(request.GetRequestStream(), new
766+
{
767+
api_type = "json",
768+
uh = Reddit.User.Modhash,
769+
r = Name,
770+
container = FullName,
771+
type = "banned",
772+
name = user,
773+
ban_reason = reason,
774+
note = note,
775+
duration = duration <= 0 ? "" : duration.ToString(),
776+
ban_message = message
777+
});
778+
var response = request.GetResponse();
779+
var result = WebAgent.GetResponseString(response.GetResponseStream());
780+
}
781+
782+
public async Task BanUserAsync(string user, string reason, string note, int duration, string message)
783+
{
784+
var request = WebAgent.CreatePost(BanUserUrl);
785+
WebAgent.WritePostBody(await request.GetRequestStreamAsync(), new
786+
{
787+
api_type = "json",
788+
uh = Reddit.User.Modhash,
789+
r = Name,
790+
container = FullName,
791+
type = "banned",
792+
name = user,
793+
ban_reason = reason,
794+
note = note,
795+
duration = duration <= 0 ? "" : duration.ToString(),
796+
ban_message = message
797+
});
798+
var response = await request.GetResponseAsync();
799+
var result = WebAgent.GetResponseString(response.GetResponseStream());
800+
}
801+
802+
/// <summary>
803+
/// Unbans a user
804+
/// </summary>
805+
/// <param name="user">User to unban, by username</param>
806+
public void UnBanUser(string user)
807+
{
808+
var request = WebAgent.CreatePost(UnBanUserUrl);
809+
WebAgent.WritePostBody(request.GetRequestStream(), new
810+
{
811+
uh = Reddit.User.Modhash,
812+
r = Name,
813+
type = "banned",
814+
container = FullName,
815+
executed = "removed",
816+
name = user,
817+
});
818+
var response = request.GetResponse();
819+
var result = WebAgent.GetResponseString(response.GetResponseStream());
820+
}
821+
822+
public async Task UnBanUserAsync(string user)
823+
{
824+
var request = WebAgent.CreatePost(UnBanUserUrl);
825+
WebAgent.WritePostBody(await request.GetRequestStreamAsync(), new
826+
{
827+
uh = Reddit.User.Modhash,
828+
r = Name,
829+
type = "banned",
830+
container = FullName,
831+
executed = "removed",
832+
name = user,
833+
});
834+
var response = await request.GetResponseAsync();
835+
var result = WebAgent.GetResponseString(response.GetResponseStream());
836+
}
837+
744838
private Post Submit(SubmitData data)
745839
{
746840
if (Reddit.User == null)

RedditSharp/Things/Thing.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ public static async Task<Thing> ParseAsync<T>(Reddit reddit, JToken json, IWebAg
122122
{
123123
return await new ModAction().InitAsync(reddit, json, webAgent);
124124
}
125+
else if (typeof(T) == typeof(Contributor))
126+
{
127+
return new Contributor().Init(reddit, json, webAgent);
128+
}
129+
else if (typeof(T) == typeof(BannedUser))
130+
{
131+
return new BannedUser().Init(reddit, json, webAgent);
132+
}
125133
}
126134
return result;
127135
}
@@ -138,6 +146,14 @@ public static Thing Parse<T>(Reddit reddit, JToken json, IWebAgent webAgent) whe
138146
{
139147
return new ModAction().Init(reddit, json, webAgent);
140148
}
149+
else if (typeof(T) == typeof(Contributor))
150+
{
151+
return new Contributor().Init(reddit, json, webAgent);
152+
}
153+
else if (typeof(T) == typeof(BannedUser))
154+
{
155+
return new BannedUser().Init(reddit, json, webAgent);
156+
}
141157
}
142158
return result;
143159
}

0 commit comments

Comments
 (0)