Skip to content

Commit 5b2a2b6

Browse files
authored
Merge pull request #9 from absurdlyobfuscated/master
User ban additions, approve for comments
2 parents 570cc70 + 78cdea0 commit 5b2a2b6

File tree

5 files changed

+141
-10
lines changed

5 files changed

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

RedditSharp/Things/Comment.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Comment : VotableThing
1717
private const string EditUserTextUrl = "/api/editusertext";
1818
private const string RemoveUrl = "/api/remove";
1919
private const string DelUrl = "/api/del";
20+
private const string ApproveUrl = "/api/approve";
2021
private const string SetAsReadUrl = "/api/read_message";
2122

2223
[JsonIgnore]
@@ -228,6 +229,11 @@ private string SimpleAction(string endpoint)
228229
return data;
229230
}
230231

232+
public void Approve()
233+
{
234+
var data = SimpleAction(ApproveUrl);
235+
}
236+
231237
public void Del()
232238
{
233239
var data = SimpleAction(DelUrl);

RedditSharp/Things/Subreddit.cs

Lines changed: 79 additions & 10 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
{
@@ -691,6 +700,7 @@ public void RemoveContributor(string id)
691700
var response = request.GetResponse();
692701
var result = WebAgent.GetResponseString(response.GetResponseStream());
693702
}
703+
694704
public async Task RemoveContributorAsync(string id)
695705
{
696706
var request = WebAgent.CreatePost(LeaveModerationUrl);
@@ -705,42 +715,101 @@ public async Task RemoveContributorAsync(string id)
705715
var response = request.GetResponse();
706716
var result = WebAgent.GetResponseString(response.GetResponseStream());
707717
}
708-
public void BanUser(string user, string reason)
718+
719+
/// <summary>
720+
/// Bans a user
721+
/// </summary>
722+
/// <param name="user">User to ban, by username</param>
723+
/// <param name="reason">Reason for ban, shows in ban note as 'reason: note' or just 'note' if blank</param>
724+
/// <param name="note">Mod notes about ban, shows in ban note as 'reason: note'</param>
725+
/// <param name="duration">Number of days to ban user, 0 for permanent</param>
726+
/// <param name="message">Message to include in ban PM</param>
727+
public void BanUser(string user, string reason, string note, int duration, string message)
709728
{
710729
var request = WebAgent.CreatePost(BanUserUrl);
711730
WebAgent.WritePostBody(request.GetRequestStream(), new
712731
{
713732
api_type = "json",
714733
uh = Reddit.User.Modhash,
715734
r = Name,
735+
container = FullName,
716736
type = "banned",
717-
id = "#banned",
718737
name = user,
719-
note = reason,
720-
action = "add",
721-
container = FullName
738+
ban_reason = reason,
739+
note = note,
740+
duration = duration <= 0 ? "" : duration.ToString(),
741+
ban_message = message
722742
});
723743
var response = request.GetResponse();
724744
var result = WebAgent.GetResponseString(response.GetResponseStream());
725745
}
726-
public async Task BanUserAsync(string user, string reason)
746+
747+
public async Task BanUserAsync(string user, string reason, string note, int duration, string message)
727748
{
728749
var request = WebAgent.CreatePost(BanUserUrl);
729750
WebAgent.WritePostBody(await request.GetRequestStreamAsync(), new
730751
{
731752
api_type = "json",
732753
uh = Reddit.User.Modhash,
733754
r = Name,
755+
container = FullName,
734756
type = "banned",
735-
id = "#banned",
736757
name = user,
737-
note = reason,
738-
action = "add",
739-
container = FullName
758+
ban_reason = reason,
759+
note = note,
760+
duration = duration <= 0 ? "" : duration.ToString(),
761+
ban_message = message
740762
});
741763
var response = await request.GetResponseAsync();
742764
var result = WebAgent.GetResponseString(response.GetResponseStream());
743765
}
766+
767+
public void BanUser(string user, string note)
768+
{
769+
BanUser(user, "", note, 0, "");
770+
}
771+
772+
public async Task BanUserAsync(string user, string note)
773+
{
774+
await BanUserAsync(user, "", note, 0, "");
775+
}
776+
777+
/// <summary>
778+
/// Unbans a user
779+
/// </summary>
780+
/// <param name="user">User to unban, by username</param>
781+
public void UnBanUser(string user)
782+
{
783+
var request = WebAgent.CreatePost(UnBanUserUrl);
784+
WebAgent.WritePostBody(request.GetRequestStream(), new
785+
{
786+
uh = Reddit.User.Modhash,
787+
r = Name,
788+
type = "banned",
789+
container = FullName,
790+
executed = "removed",
791+
name = user,
792+
});
793+
var response = request.GetResponse();
794+
var result = WebAgent.GetResponseString(response.GetResponseStream());
795+
}
796+
797+
public async Task UnBanUserAsync(string user)
798+
{
799+
var request = WebAgent.CreatePost(UnBanUserUrl);
800+
WebAgent.WritePostBody(await request.GetRequestStreamAsync(), new
801+
{
802+
uh = Reddit.User.Modhash,
803+
r = Name,
804+
type = "banned",
805+
container = FullName,
806+
executed = "removed",
807+
name = user,
808+
});
809+
var response = await request.GetResponseAsync();
810+
var result = WebAgent.GetResponseString(response.GetResponseStream());
811+
}
812+
744813
private Post Submit(SubmitData data)
745814
{
746815
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 await new Contributor().InitAsync(reddit, json, webAgent);
128+
}
129+
else if (typeof(T) == typeof(BannedUser))
130+
{
131+
return await new BannedUser().InitAsync(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)