Skip to content

Commit 5c830e4

Browse files
authored
Merge pull request #170 from cwhitman/2.0-.Net-Core
Bug Fixes related to submitting and editing various things.
2 parents dc00d86 + d1e118d commit 5c830e4

File tree

5 files changed

+62
-43
lines changed

5 files changed

+62
-43
lines changed

RedditSharp/Reddit.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ public async Task<LiveUpdateEvent> CreateLiveEventAsync(string title, string des
229229
nsfw = nsfw
230230
}).ConfigureAwait(false);
231231

232-
if (json["json"]["errors"].Any())
233-
throw new Exception(json["json"]["errors"][0][0].ToString());
232+
if (json["errors"].Any())
233+
throw new Exception(json["errors"][0][0].ToString());
234234

235-
var id = json["json"]["data"]["id"].ToString();
235+
var id = json["data"]["id"].ToString();
236236

237237
return await GetLiveEvent(new Uri(String.Format(GetLiveEventUrl, id))).ConfigureAwait(false);
238238
}
@@ -247,7 +247,7 @@ public async Task<LiveUpdateEvent> GetLiveEvent(Uri uri)
247247
if (!uri.AbsoluteUri.EndsWith("about"))
248248
uri = new Uri(uri.AbsoluteUri + "/about");
249249

250-
var token = await Helpers.GetTokenAsync(WebAgent, uri).ConfigureAwait(false);
250+
var token = await Helpers.GetTokenAsync(WebAgent, uri,true).ConfigureAwait(false);
251251
return new LiveUpdateEvent(WebAgent, token);
252252
}
253253

RedditSharp/Things/Comment.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89

910
namespace RedditSharp.Things
@@ -176,16 +177,26 @@ public async Task<Comment> ReplyAsync(string message)
176177
// TODO actual error handling. This just hides the error and returns null
177178
//try
178179
//{
179-
var json = await WebAgent.Post(CommentUrl, new
180+
var json = await WebAgent.Post(CommentUrl, new
181+
{
182+
text = message,
183+
thing_id = FullName,
184+
api_type = "json"
185+
//r = Subreddit
186+
}).ConfigureAwait(false);
187+
if (json["errors"].Any())
188+
{
189+
if (json["errors"][0].Any(x => x.ToString() == "RATELIMIT" || x.ToString() == "ratelimit"))
180190
{
181-
text = message,
182-
thing_id = FullName,
183-
api_type = "json"
184-
//r = Subreddit
185-
}).ConfigureAwait(false);
186-
if (json["json"]["ratelimit"] != null)
187-
throw new RateLimitException(TimeSpan.FromSeconds(json["json"]["ratelimit"].ValueOrDefault<double>()));
188-
return new Comment(WebAgent, json["json"]["data"]["things"][0], this);
191+
var timeToReset = TimeSpan.FromMinutes(Convert.ToDouble(Regex.Match(json["errors"][0].ElementAt(1).ToString(), @"\d+").Value));
192+
throw new RateLimitException(timeToReset);
193+
}
194+
else
195+
{
196+
throw new Exception(json["errors"][0][0].ToString());
197+
}
198+
}
199+
return new Comment(WebAgent, json["data"]["things"][0], this);
189200
//}
190201
//catch (HttpRequestException ex)
191202
//{
@@ -206,10 +217,10 @@ public async Task EditTextAsync(string newText)
206217
text = newText,
207218
thing_id = FullName
208219
}).ConfigureAwait(false);
209-
if (!json["json"]["errors"].Any())
220+
if (!json["errors"].Any())
210221
Body = newText;
211222
else
212-
throw new Exception($"Errors editing text {json["json"]["errors"][0][0].ToString()}");
223+
throw new Exception($"Errors editing text {json["errors"][0][0].ToString()}");
213224
}
214225

215226
/// <inheritdoc />

RedditSharp/Things/LiveUpdateEvent.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,41 +166,36 @@ public async Task<bool> DeleteUpdateAsync(LiveUpdate update)
166166
/// <summary>
167167
/// Edit a live thread. Set parameters to empty string to clear those fields. Or null to ignore them on update.
168168
/// </summary>
169-
/// <param name="title">New Title.</param>
169+
/// <param name="title">New Title. Cannot be empty string.</param>
170170
/// <param name="description">New Description</param>
171171
/// <param name="resources">new Resources</param>
172172
/// <param name="nsfw">NSFW flag</param>
173173
public async Task<bool> EditAsync(string title, string description, string resources, bool? nsfw)
174174
{
175-
var expando = (IDictionary<string, object>)new ExpandoObject();
176-
177-
if (title != null)
178-
expando.Add(new KeyValuePair<string, object>("title", title));
179-
180-
if (description != null)
181-
expando.Add(new KeyValuePair<string, object>("description", description));
182-
183-
if (resources != null)
184-
expando.Add(new KeyValuePair<string, object>("resources", resources));
185-
186-
if (nsfw.HasValue)
187-
expando.Add(new KeyValuePair<string, object>("nsfw", nsfw.Value));
188-
175+
if (title == null)
176+
title = Title;
177+
if (description == null)
178+
description = Description;
179+
if (resources == null)
180+
resources = Resources;
181+
if (!nsfw.HasValue)
182+
nsfw = NSFW;
183+
184+
dynamic properties = new { title=title,description =description, resources = resources, nsfw = nsfw};
189185
var request = WebAgent.CreateRequest(EditUrl, "POST");
190-
WebAgent.WritePostBody(request, expando);
191-
186+
WebAgent.WritePostBody(request, properties);
192187
var response = await WebAgent.GetResponseAsync(request).ConfigureAwait(false);
193188

194189
if (!response.IsSuccessStatusCode)
195190
return false;
196191

197192
var data = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
198193
JToken json = JToken.Parse(data);
199-
if (!json["json"]["errors"].Any())
194+
if (json["success"].Value<Boolean>())
200195
{
201-
Title = title ?? "";
202-
Description = description ?? "";
203-
Resources = resources ?? "";
196+
Title = title ?? Title;
197+
Description = description ?? Description;
198+
Resources = resources ?? Resources;
204199

205200
if (nsfw.HasValue)
206201
NSFW = nsfw.Value;

RedditSharp/Things/Post.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Reactive.Linq;
88
using System.Threading.Tasks;
9+
using System.Text.RegularExpressions;
910

1011
namespace RedditSharp.Things
1112
{
@@ -143,9 +144,20 @@ public async Task<Comment> CommentAsync(string message)
143144
thing_id = FullName,
144145
api_type = "json"
145146
}).ConfigureAwait(false);
146-
if (json["json"]["ratelimit"] != null)
147-
throw new RateLimitException(TimeSpan.FromSeconds(json["json"]["ratelimit"].ValueOrDefault<double>()));
148-
return new Comment(WebAgent, json["json"]["data"]["things"][0], this);
147+
if (json["errors"].Any())
148+
{
149+
if (json["errors"][0].Any(x => x.ToString() == "RATELIMIT" || x.ToString() == "ratelimit"))
150+
{
151+
var timeToReset = TimeSpan.FromMinutes(Convert.ToDouble(Regex.Match(json["errors"][0].ElementAt(1).ToString(), @"\d+").Value));
152+
throw new RateLimitException(timeToReset);
153+
}
154+
else
155+
{
156+
throw new Exception(json["errors"][0][0].ToString());
157+
}
158+
}
159+
160+
return new Comment(WebAgent, json["data"]["things"][0], this);
149161
}
150162

151163
private async Task<JToken> SimpleActionToggleAsync(string endpoint, bool value, bool requiresModAction = false)
@@ -204,7 +216,7 @@ public async Task EditTextAsync(string newText)
204216
text = newText,
205217
thing_id = FullName
206218
}).ConfigureAwait(false);
207-
if (json["json"].ToString().Contains("\"errors\": []"))
219+
if (!json["errors"].Any())
208220
SelfText = newText;
209221
else
210222
throw new Exception("Error editing text.");
@@ -319,7 +331,7 @@ public IAsyncEnumerable<Comment> EnumerateCommentTreeAsync(int limitPerRequest =
319331
{
320332
return new CommentsEnumarable(WebAgent, this, limitPerRequest);
321333
}
322-
#region Static Operations
334+
#region Static Operations
323335
/// <summary>
324336
/// Sets flair of given post by <paramref name="fullname"/>
325337
/// </summary>

RedditSharp/Things/Subreddit.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ private async Task<Post> SubmitAsync(SubmitData data, ICaptchaSolver solver = nu
832832
throw new CaptchaFailedException("Captcha required but not ICaptchaSolver provided");
833833
}
834834

835-
data.Iden = json["json"]["captcha"].ToString();
835+
data.Iden = json["captcha"].ToString();
836836
CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden));
837837

838838
// We throw exception due to this method being expected to return a valid Post object, but we cannot
@@ -853,7 +853,8 @@ private async Task<Post> SubmitAsync(SubmitData data, ICaptchaSolver solver = nu
853853
{
854854
throw new Exception(json["errors"][0][0].ToString());
855855
}
856-
return new Post(WebAgent, json["data"]);
856+
857+
return new Post(WebAgent, await Helpers.GetTokenAsync(WebAgent, new Uri(json["data"]["url"].ToString())).ConfigureAwait(false));
857858
}
858859
/// <summary>
859860
/// Submits a link post in the current subreddit using the logged-in user

0 commit comments

Comments
 (0)