forked from CrustyJew/RedditSharp-DEPRECATED-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComment.cs
More file actions
250 lines (225 loc) · 8.27 KB
/
Comment.cs
File metadata and controls
250 lines (225 loc) · 8.27 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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using System.Threading.Tasks;
using RedditSharp.Extensions;
namespace RedditSharp.Things
{
/// <summary>
/// A comment.
/// </summary>
public class Comment : VotableThing
{
private const string CommentUrl = "/api/comment";
private const string EditUserTextUrl = "/api/editusertext";
private const string SetAsReadUrl = "/api/read_message";
#pragma warning disable 1591
public Comment(IWebAgent agent, JToken json, Thing sender) : base(agent, json) {
var data = json["data"];
Parent = sender;
// Handle Reddit's API being horrible
if (data["context"] != null)
{
var context = data["context"].Value<string>();
LinkId = context.Split('/')[4];
}
ParseComments(json, sender);
}
#pragma warning restore 1591
/// <inheritdoc />
internal override JToken GetJsonData(JToken json) => json["data"];
/// <summary>
/// Prefix for fullname. Includes trailing underscore
/// </summary>
public static string KindPrefix { get { return "t1_"; } }
/// <summary>
/// Fill the object with comments.
/// </summary>
/// <param name="things"></param>
/// <returns></returns>
public Comment PopulateComments(IEnumerator<Thing> things)
{
Thing first = things.Current;
Dictionary<string, Tuple<Comment, List<Comment>>> comments = new Dictionary<string, Tuple<Comment, List<Comment>>>
{
[this.FullName] = Tuple.Create<Comment, List<Comment>>(this, new List<Comment>())
};
while (things.MoveNext() && (first is Comment || first is More))
{
first = things.Current;
if (first is Comment comment)
{
comments[comment.FullName] = Tuple.Create<Comment, List<Comment>>(comment, new List<Comment>());
if (comments.ContainsKey(comment.ParentId))
{
comments[comment.ParentId].Item2.Add(comment);
}
else if (comment.ParentId == this.ParentId)
{
//only want sub comments.
break;
}
}
else if (first is More more)
{
if (comments.ContainsKey(more.ParentId))
{
comments[more.ParentId].Item1.More = more;
}
else if (more.ParentId == this.ParentId)
{
// This is more for parent.
// Need to process the comments dictionary.
break;
}
}
//things.MoveNext();
}
foreach (KeyValuePair<string, Tuple<Comment, List<Comment>>> kvp in comments)
{
kvp.Value.Item1.Comments = kvp.Value.Item2.ToArray();
}
return this;
}
private void ParseComments(JToken data, Thing sender)
{
// Parse sub comments
var replies = data["data"]["replies"];
var subComments = new List<Comment>();
if (replies != null && replies.Count() > 0)
{
foreach (var comment in replies["data"]["children"])
subComments.Add(new Comment(WebAgent, comment, sender));
}
Comments = subComments.ToArray();
}
/// <summary>
/// Comment body markdown.
/// </summary>
[JsonProperty("body")]
public string Body { get; private set; }
/// <summary>
/// Comment body html.
/// </summary>
[JsonProperty("body_html")]
public string BodyHtml { get; private set; }
/// <summary>
/// Id of the parent <see cref="VotableThing"/>.
/// </summary>
[JsonProperty("parent_id")]
public string ParentId { get; private set; }
/// <summary>
/// Parent subreddit name.
/// </summary>
[JsonProperty("subreddit")]
public string Subreddit { get; private set; }
/// <summary>
/// Link id.
/// </summary>
[JsonProperty("link_id")]
public string LinkId { get; private set; }
/// <summary>
/// Parent link title.
/// </summary>
[JsonProperty("link_title")]
public string LinkTitle { get; private set; }
/// <summary>
/// More comments.
/// </summary>
[JsonIgnore]
public More More { get; private set; }
/// <summary>
/// Replies to this comment.
/// </summary>
[JsonIgnore]
public IList<Comment> Comments { get; private set; }
/// <summary>
/// Parent <see cref="VotableThing"/>
/// </summary>
[JsonIgnore]
public Thing Parent { get; internal set; }
/// <inheritdoc/>
public override string Shortlink
{
get
{
// Not really a "short" link, but you can't actually use short links for comments
string linkId = "";
int index = this.LinkId.IndexOf('_');
if (index > -1)
{
linkId = this.LinkId.Substring(index + 1);
}
return string.Format("{0}://{1}/r/{2}/comments/{3}/_/{4}",
RedditSharp.WebAgent.Protocol, RedditSharp.WebAgent.RootDomain,
this.Subreddit, this.Parent != null ? this.Parent.Id : linkId, this.Id);
}
}
/// <summary>
/// Reply to this comment.
/// </summary>
/// <param name="message">markdown text of the reply.</param>
/// <returns></returns>
public async Task<Comment> ReplyAsync(string message)
{
// TODO actual error handling. This just hides the error and returns null
//try
//{
var json = await WebAgent.Post(CommentUrl, new
{
text = message,
thing_id = FullName,
api_type = "json"
//r = Subreddit
}).ConfigureAwait(false);
if (json["json"]["ratelimit"] != null)
throw new RateLimitException(TimeSpan.FromSeconds(json["json"]["ratelimit"].ValueOrDefault<double>()));
return new Comment(WebAgent, json["json"]["data"]["things"][0], this);
//}
//catch (HttpRequestException ex)
//{
// var error = new StreamReader(ex..GetResponseStream()).ReadToEnd();
// return null;
//}
}
/// <summary>
/// Replaces the text in this comment with the input text.
/// </summary>
/// <param name="newText">The text to replace the comment's contents</param>
public async Task EditTextAsync(string newText)
{
var json = await WebAgent.Post(EditUserTextUrl, new
{
api_type = "json",
text = newText,
thing_id = FullName
}).ConfigureAwait(false);
if (json["json"].ToString().Contains("\"errors\": []"))
Body = newText;
else
throw new Exception("Error editing text.");
}
/// <inheritdoc />
protected override async Task<JToken> SimpleActionAsync(string endpoint)
{
return await WebAgent.Post(endpoint, new
{
id = FullName
}).ConfigureAwait(false);
}
/// <summary>
/// Mark this comment as read.
/// </summary>
public async Task SetAsReadAsync()
{
await WebAgent.Post(SetAsReadUrl, new
{
id = FullName,
api_type = "json"
}).ConfigureAwait(false);
}
}
}