Skip to content

Commit e4bb363

Browse files
Fix ban async, replace old ban methods with new
1 parent c5ed093 commit e4bb363

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

RedditSharp/Things/BannedUser.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
33
using System;
4+
using System.Threading.Tasks;
45

56
namespace RedditSharp.Things
67
{
@@ -26,6 +27,13 @@ public BannedUser Init(Reddit reddit, JToken json, IWebAgent webAgent)
2627
return this;
2728
}
2829

30+
public async Task<BannedUser> InitAsync(Reddit reddit, JToken json, IWebAgent webAgent)
31+
{
32+
CommonInit(json);
33+
await JsonConvert.PopulateObjectAsync(json.ToString(), this, reddit.JsonSerializerSettings);
34+
return this;
35+
}
36+
2937
private void CommonInit(JToken json)
3038
{
3139
base.Init(json);

RedditSharp/Things/Subreddit.cs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ public void RemoveContributor(string id)
700700
var response = request.GetResponse();
701701
var result = WebAgent.GetResponseString(response.GetResponseStream());
702702
}
703+
703704
public async Task RemoveContributorAsync(string id)
704705
{
705706
var request = WebAgent.CreatePost(LeaveModerationUrl);
@@ -714,42 +715,6 @@ public async Task RemoveContributorAsync(string id)
714715
var response = request.GetResponse();
715716
var result = WebAgent.GetResponseString(response.GetResponseStream());
716717
}
717-
public void BanUser(string user, string reason)
718-
{
719-
var request = WebAgent.CreatePost(BanUserUrl);
720-
WebAgent.WritePostBody(request.GetRequestStream(), new
721-
{
722-
api_type = "json",
723-
uh = Reddit.User.Modhash,
724-
r = Name,
725-
type = "banned",
726-
id = "#banned",
727-
name = user,
728-
note = reason,
729-
action = "add",
730-
container = FullName
731-
});
732-
var response = request.GetResponse();
733-
var result = WebAgent.GetResponseString(response.GetResponseStream());
734-
}
735-
public async Task BanUserAsync(string user, string reason)
736-
{
737-
var request = WebAgent.CreatePost(BanUserUrl);
738-
WebAgent.WritePostBody(await request.GetRequestStreamAsync(), new
739-
{
740-
api_type = "json",
741-
uh = Reddit.User.Modhash,
742-
r = Name,
743-
type = "banned",
744-
id = "#banned",
745-
name = user,
746-
note = reason,
747-
action = "add",
748-
container = FullName
749-
});
750-
var response = await request.GetResponseAsync();
751-
var result = WebAgent.GetResponseString(response.GetResponseStream());
752-
}
753718

754719
/// <summary>
755720
/// Bans a user
@@ -799,6 +764,16 @@ public async Task BanUserAsync(string user, string reason, string note, int dura
799764
var result = WebAgent.GetResponseString(response.GetResponseStream());
800765
}
801766

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+
802777
/// <summary>
803778
/// Unbans a user
804779
/// </summary>

RedditSharp/Things/Thing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ public static async Task<Thing> ParseAsync<T>(Reddit reddit, JToken json, IWebAg
124124
}
125125
else if (typeof(T) == typeof(Contributor))
126126
{
127-
return new Contributor().Init(reddit, json, webAgent);
127+
return await new Contributor().InitAsync(reddit, json, webAgent);
128128
}
129129
else if (typeof(T) == typeof(BannedUser))
130130
{
131-
return new BannedUser().Init(reddit, json, webAgent);
131+
return await new BannedUser().InitAsync(reddit, json, webAgent);
132132
}
133133
}
134134
return result;

0 commit comments

Comments
 (0)