forked from CrustyJew/RedditSharp-DEPRECATED-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPost.cs
More file actions
469 lines (424 loc) · 15.3 KB
/
Post.cs
File metadata and controls
469 lines (424 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RedditSharp.Extensions;
namespace RedditSharp.Things
{
public class Post : VotableThing
{
private const string CommentUrl = "/api/comment";
private const string GetCommentsUrl = "/comments/{0}.json";
private const string EditUserTextUrl = "/api/editusertext";
private const string HideUrl = "/api/hide";
private const string UnhideUrl = "/api/unhide";
private const string SetFlairUrl = "/r/{0}/api/flair";
private const string MarkNSFWUrl = "/api/marknsfw";
private const string UnmarkNSFWUrl = "/api/unmarknsfw";
private const string ContestModeUrl = "/api/set_contest_mode";
private const string StickyModeUrl = "/api/set_subreddit_sticky";
private const string SpoilerUrl = "/api/spoiler";
private const string UnSpoilerUrl = "/api/unspoiler";
/// <summary>
/// Initialize
/// </summary>
/// <param name="reddit"></param>
/// <param name="post"></param>
/// <param name="webAgent"></param>
/// <returns></returns>
public async Task<Post> InitAsync(Reddit reddit, JToken post, IWebAgent webAgent)
{
await CommonInitAsync(reddit, post, webAgent);
JsonConvert.PopulateObject(post["data"].ToString(), this, reddit.JsonSerializerSettings);
return this;
}
/// <summary>
/// Initialize
/// </summary>
/// <param name="reddit"></param>
/// <param name="post"></param>
/// <param name="webAgent"></param>
/// <returns></returns>
public Post Init(Reddit reddit, JToken post, IWebAgent webAgent)
{
CommonInit(reddit, post, webAgent);
JsonConvert.PopulateObject(post["data"].ToString(), this, reddit.JsonSerializerSettings);
return this;
}
private void CommonInit(Reddit reddit, JToken post, IWebAgent webAgent)
{
base.Init(reddit, webAgent, post);
Reddit = reddit;
WebAgent = webAgent;
}
private async Task CommonInitAsync(Reddit reddit, JToken post, IWebAgent webAgent)
{
await base.InitAsync(reddit, webAgent, post);
Reddit = reddit;
WebAgent = webAgent;
}
/// <summary>
/// Author of this post.
/// </summary>
[JsonIgnore]
public RedditUser Author
{
get
{
return Reddit.GetUser(AuthorName);
}
}
/// <summary>
/// An array of comments on this post.
/// </summary>
public Comment[] Comments
{
get
{
return ListComments().ToArray();
}
}
/// <summary>
/// Returns true if post is marekd as spoiler
/// </summary>
[JsonProperty("spoiler")]
public bool IsSpoiler { get; set; }
/// <summary>
/// Domain of this post.
/// </summary>
[JsonProperty("domain")]
public string Domain { get; set; }
/// <summary>
/// Returns true if this is a self post.
/// </summary>
[JsonProperty("is_self")]
public bool IsSelfPost { get; set; }
/// <summary>
/// Css class of the link flair.
/// </summary>
[JsonProperty("link_flair_css_class")]
public string LinkFlairCssClass { get; set; }
/// <summary>
/// Text of the link flair.
/// </summary>
[JsonProperty("link_flair_text")]
public string LinkFlairText { get; set; }
/// <summary>
/// Number of comments on this post.
/// </summary>
[JsonProperty("num_comments")]
public int CommentCount { get; set; }
/// <summary>
/// Returns true if this post is marked not safe for work.
/// </summary>
[JsonProperty("over_18")]
public bool NSFW { get; set; }
/// <summary>
/// Post permalink.
/// </summary>
[JsonProperty("permalink")]
[JsonConverter(typeof(UrlParser))]
public Uri Permalink { get; set; }
/// <summary>
/// Post self text markdown.
/// </summary>
[JsonProperty("selftext")]
public string SelfText { get; set; }
/// <summary>
/// Post self text html.
/// </summary>
[JsonProperty("selftext_html")]
public string SelfTextHtml { get; set; }
/// <summary>
/// Uri to the thumbnail image of this post.
/// </summary>
[JsonProperty("thumbnail")]
[JsonConverter(typeof(UrlParser))]
public Uri Thumbnail { get; set; }
/// <summary>
/// Post title.
/// </summary>
[JsonProperty("title")]
public string Title { get; set; }
/// <summary>
/// Parent subreddit name.
/// </summary>
[JsonProperty("subreddit")]
public string SubredditName { get; set; }
/// <summary>
/// Parent subreddit.
/// </summary>
[JsonIgnore]
public Subreddit Subreddit
{
get
{
return Reddit.GetSubreddit("/r/" + SubredditName);
}
}
/// <summary>
/// Post uri.
/// </summary>
[JsonProperty("url")]
[JsonConverter(typeof(UrlParser))]
public Uri Url { get; set; }
/// <summary>
/// Comment on this post.
/// </summary>
/// <param name="message">Markdown text.</param>
/// <returns></returns>
public Comment Comment(string message)
{
if (Reddit.User == null)
throw new AuthenticationException("No user logged in.");
var request = WebAgent.CreatePost(CommentUrl);
var stream = request.GetRequestStream();
WebAgent.WritePostBody(stream, new
{
text = message,
thing_id = FullName,
uh = Reddit.User.Modhash,
api_type = "json"
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
var json = JObject.Parse(data);
if (json["json"]["ratelimit"] != null)
throw new RateLimitException(TimeSpan.FromSeconds(json["json"]["ratelimit"].ValueOrDefault<double>()));
return new Comment().Init(Reddit, json["json"]["data"]["things"][0], WebAgent, this);
}
/// <summary>
/// Marks post as spoiler
/// </summary>
public void Spoiler()
{
var data = SimpleAction(SpoilerUrl);
}
/// <summary>
/// Unmarks a post as being a spoiler
/// </summary>
public void UnSpoiler()
{
var data = SimpleAction(UnSpoilerUrl);
}
private string SimpleActionToggle(string endpoint, bool value, bool requiresModAction = false)
{
if (Reddit.User == null)
throw new AuthenticationException("No user logged in.");
var modNameList = this.Subreddit.Moderators.Select(b => b.Name).ToList();
if (requiresModAction && !modNameList.Contains(Reddit.User.Name))
throw new AuthenticationException(
string.Format(
@"User {0} is not a moderator of subreddit {1}.",
Reddit.User.Name,
this.Subreddit.Name));
var request = WebAgent.CreatePost(endpoint);
var stream = request.GetRequestStream();
WebAgent.WritePostBody(stream, new
{
id = FullName,
state = value,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
return data;
}
/// <summary>
/// Hide this post.
/// </summary>
public void Hide()
{
var data = SimpleAction(HideUrl);
}
/// <summary>
/// Unhide this post.
/// </summary>
public void Unhide()
{
var data = SimpleAction(UnhideUrl);
}
/// <summary>
/// Mark this post not safe for work.
/// </summary>
public void MarkNSFW()
{
var data = SimpleAction(MarkNSFWUrl);
}
/// <summary>
/// Unmark this post not safe for work.
/// </summary>
public void UnmarkNSFW()
{
var data = SimpleAction(UnmarkNSFWUrl);
}
/// <summary>
/// Set contest mode state. Logged in user must be a moderator of parent subreddit.
/// </summary>
/// <param name="state"></param>
public void ContestMode(bool state)
{
var data = SimpleActionToggle(ContestModeUrl, state);
}
/// <summary>
/// Set sticky state. Logged in user must be a moderator of parent subreddit.
/// </summary>
/// <param name="state"></param>
public void StickyMode(bool state)
{
var data = SimpleActionToggle(StickyModeUrl, state, true);
}
#region Obsolete Getter Methods
[Obsolete("Use Comments property instead")]
public Comment[] GetComments()
{
return Comments;
}
#endregion Obsolete Getter Methods
/// <summary>
/// Replaces the text in this post with the input text.
/// </summary>
/// <param name="newText">The text to replace the post's contents</param>
public void EditText(string newText)
{
if (Reddit.User == null)
throw new Exception("No user logged in.");
if (!IsSelfPost)
throw new Exception("Submission to edit is not a self-post.");
var request = WebAgent.CreatePost(EditUserTextUrl);
WebAgent.WritePostBody(request.GetRequestStream(), new
{
api_type = "json",
text = newText,
thing_id = FullName,
uh = Reddit.User.Modhash
});
var response = request.GetResponse();
var result = WebAgent.GetResponseString(response.GetResponseStream());
JToken json = JToken.Parse(result);
if (json["json"].ToString().Contains("\"errors\": []"))
SelfText = newText;
else
throw new Exception("Error editing text. Error: " + json["json"]["errors"][0][0].ToString());
}
/// <summary>
/// Update this post.
/// </summary>
public void Update()
{
JToken post = Reddit.GetToken(this.Url);
JsonConvert.PopulateObject(post["data"].ToString(), this, Reddit.JsonSerializerSettings);
}
/// <summary>
/// Sets your claim
/// </summary>
/// <param name="flairText">Text to set your flair</param>
/// <param name="flairClass">class of the flair</param>
public void SetFlair(string flairText, string flairClass)
{
if (Reddit.User == null)
throw new Exception("No user logged in.");
var request = WebAgent.CreatePost(string.Format(SetFlairUrl, SubredditName));
WebAgent.WritePostBody(request.GetRequestStream(), new
{
api_type = "json",
css_class = flairClass,
link = FullName,
name = Reddit.User.Name,
text = flairText,
uh = Reddit.User.Modhash
});
var response = request.GetResponse();
var result = WebAgent.GetResponseString(response.GetResponseStream());
var json = JToken.Parse(result);
LinkFlairText = flairText;
}
/// <summary>
/// Get a <see cref="Listing{T}"/> of comments.
/// </summary>
/// <param name="limit"></param>
/// <returns></returns>
public List<Comment> ListComments(int? limit = null)
{
var url = string.Format(GetCommentsUrl, Id);
if (limit.HasValue)
{
var query = HttpUtility.ParseQueryString(string.Empty);
query.Add("limit", limit.Value.ToString());
url = string.Format("{0}?{1}", url, query);
}
var request = WebAgent.CreateGet(url);
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
var json = JArray.Parse(data);
var postJson = json.Last()["data"]["children"];
var comments = new List<Comment>();
foreach (var comment in postJson)
{
Comment newComment = new Comment().Init(Reddit, comment, WebAgent, this);
if (newComment.Kind == "more")
{
}
else
{
comments.Add(newComment);
}
}
return comments;
}
/// <summary>
/// Enumerate more comments.
/// </summary>
/// <returns></returns>
public IEnumerable<Comment> EnumerateComments()
{
var url = string.Format(GetCommentsUrl, Id);
var request = WebAgent.CreateGet(url);
var response = request.GetResponse();
var data = WebAgent.GetResponseString(response.GetResponseStream());
var json = JArray.Parse(data);
var postJson = json.Last()["data"]["children"];
More moreComments = null;
foreach (var comment in postJson)
{
Comment newComment = new Comment().Init(Reddit, comment, WebAgent, this);
if (newComment.Kind == "more")
{
moreComments = new More().Init(Reddit, comment, WebAgent);
}
else
{
yield return newComment;
}
}
if (moreComments != null)
{
IEnumerator<Thing> things = moreComments.Things().GetEnumerator();
things.MoveNext();
Thing currentThing = null;
while (currentThing != things.Current)
{
currentThing = things.Current;
if (things.Current is Comment)
{
Comment next = ((Comment)things.Current).PopulateComments(things);
yield return next;
}
if (things.Current is More)
{
More more = (More)things.Current;
if (more.ParentId != FullName) break;
things = more.Things().GetEnumerator();
things.MoveNext();
}
}
}
}
}
}