Skip to content

Commit 642eb2a

Browse files
committed
Merge pull request #164 from justcool393/distinguish-api-pat
Move distinguish to VoteableThing
2 parents c1086f5 + 9517bed commit 642eb2a

File tree

2 files changed

+93
-91
lines changed

2 files changed

+93
-91
lines changed

RedditSharp/Things/Comment.cs

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace RedditSharp.Things
1313
public class Comment : VotableThing
1414
{
1515
private const string CommentUrl = "/api/comment";
16-
private const string DistinguishUrl = "/api/distinguish";
1716
private const string EditUserTextUrl = "/api/editusertext";
1817
private const string RemoveUrl = "/api/remove";
1918
private const string SetAsReadUrl = "/api/read_message";
@@ -108,9 +107,6 @@ private async Task ParseCommentsAsync(Reddit reddit, JToken data, IWebAgent webA
108107
public string LinkTitle { get; set; }
109108
[JsonProperty("num_reports")]
110109
public int? NumReports { get; set; }
111-
[JsonProperty("distinguished")]
112-
[JsonConverter(typeof(DistinguishConverter))]
113-
public DistinguishType Distinguished { get; set; }
114110

115111
[JsonIgnore]
116112
public IList<Comment> Comments { get; private set; }
@@ -167,42 +163,6 @@ public Comment Reply(string message)
167163
}
168164
}
169165

170-
public void Distinguish(DistinguishType distinguishType)
171-
{
172-
if (Reddit.User == null)
173-
throw new AuthenticationException("No user logged in.");
174-
var request = WebAgent.CreatePost(DistinguishUrl);
175-
var stream = request.GetRequestStream();
176-
string how;
177-
switch (distinguishType)
178-
{
179-
case DistinguishType.Admin:
180-
how = "admin";
181-
break;
182-
case DistinguishType.Moderator:
183-
how = "yes";
184-
break;
185-
case DistinguishType.None:
186-
how = "no";
187-
break;
188-
default:
189-
how = "special";
190-
break;
191-
}
192-
WebAgent.WritePostBody(stream, new
193-
{
194-
how,
195-
id = Id,
196-
uh = Reddit.User.Modhash
197-
});
198-
stream.Close();
199-
var response = request.GetResponse();
200-
var data = WebAgent.GetResponseString(response.GetResponseStream());
201-
var json = JObject.Parse(data);
202-
if (json["jquery"].Count(i => i[0].Value<int>() == 11 && i[1].Value<int>() == 12) == 0)
203-
throw new AuthenticationException("You are not permitted to distinguish this comment.");
204-
}
205-
206166
/// <summary>
207167
/// Replaces the text in this comment with the input text.
208168
/// </summary>
@@ -267,47 +227,4 @@ public void SetAsRead()
267227
var data = WebAgent.GetResponseString(response.GetResponseStream());
268228
}
269229
}
270-
271-
public enum DistinguishType
272-
{
273-
Moderator,
274-
Admin,
275-
Special,
276-
None
277-
}
278-
279-
internal class DistinguishConverter : JsonConverter
280-
{
281-
public override bool CanConvert(Type objectType)
282-
{
283-
return objectType == typeof(DistinguishType) || objectType == typeof(string);
284-
}
285-
286-
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
287-
{
288-
var token = JToken.Load(reader);
289-
var value = token.Value<string>();
290-
if (value == null)
291-
return DistinguishType.None;
292-
switch (value)
293-
{
294-
case "moderator": return DistinguishType.Moderator;
295-
case "admin": return DistinguishType.Admin;
296-
case "special": return DistinguishType.Special;
297-
default: return DistinguishType.None;
298-
}
299-
}
300-
301-
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
302-
{
303-
var d = (DistinguishType)value;
304-
if (d == DistinguishType.None)
305-
{
306-
writer.WriteNull();
307-
return;
308-
}
309-
writer.WriteValue(d.ToString().ToLower());
310-
}
311-
}
312-
313230
}

RedditSharp/Things/VotableThing.cs

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
3+
using System.Linq;
4+
using System.Security.Authentication;
25
using Newtonsoft.Json;
36
using Newtonsoft.Json.Linq;
47

@@ -23,10 +26,19 @@ public enum ReportType
2326
Other = 5
2427
}
2528

29+
public enum DistinguishType
30+
{
31+
Moderator,
32+
Admin,
33+
Special,
34+
None
35+
}
36+
2637
private const string VoteUrl = "/api/vote";
2738
private const string SaveUrl = "/api/save";
2839
private const string UnsaveUrl = "/api/unsave";
2940
private const string ReportUrl = "/api/report";
41+
private const string DistinguishUrl = "/api/distinguish";
3042

3143
[JsonIgnore]
3244
private IWebAgent WebAgent { get; set; }
@@ -54,12 +66,16 @@ private void CommonInit(Reddit reddit, IWebAgent webAgent, JToken json)
5466
WebAgent = webAgent;
5567
}
5668

69+
5770
[JsonProperty("downs")]
5871
public int Downvotes { get; set; }
5972
[JsonProperty("ups")]
6073
public int Upvotes { get; set; }
6174
[JsonProperty("saved")]
6275
public bool Saved { get; set; }
76+
[JsonProperty("distinguished")]
77+
[JsonConverter(typeof(DistinguishConverter))]
78+
public DistinguishType Distinguished { get; set; }
6379

6480
/// <summary>
6581
/// True if the logged in user has upvoted this.
@@ -169,7 +185,7 @@ public void ClearVote()
169185
var response = request.GetResponse();
170186
var data = WebAgent.GetResponseString(response.GetResponseStream());
171187
}
172-
188+
173189
public void Report(ReportType reportType, string otherReason = null)
174190
{
175191
var request = WebAgent.CreatePost(ReportUrl);
@@ -178,17 +194,17 @@ public void Report(ReportType reportType, string otherReason = null)
178194
string reportReason;
179195
switch (reportType)
180196
{
181-
case ReportType.Spam:
197+
case ReportType.Spam:
182198
reportReason = "spam"; break;
183-
case ReportType.VoteManipulation:
199+
case ReportType.VoteManipulation:
184200
reportReason = "vote manipulation"; break;
185-
case ReportType.PersonalInformation:
201+
case ReportType.PersonalInformation:
186202
reportReason = "personal information"; break;
187-
case ReportType.BreakingReddit:
203+
case ReportType.BreakingReddit:
188204
reportReason = "breaking reddit"; break;
189-
case ReportType.SexualizingMinors:
205+
case ReportType.SexualizingMinors:
190206
reportReason = "sexualizing minors"; break;
191-
default:
207+
default:
192208
reportReason = "other"; break;
193209
}
194210

@@ -205,5 +221,74 @@ public void Report(ReportType reportType, string otherReason = null)
205221
var data = WebAgent.GetResponseString(response.GetResponseStream());
206222
}
207223

224+
public void Distinguish(DistinguishType distinguishType)
225+
{
226+
if (Reddit.User == null)
227+
throw new AuthenticationException("No user logged in.");
228+
var request = WebAgent.CreatePost(DistinguishUrl);
229+
var stream = request.GetRequestStream();
230+
string how;
231+
switch (distinguishType)
232+
{
233+
case DistinguishType.Admin:
234+
how = "admin";
235+
break;
236+
case DistinguishType.Moderator:
237+
how = "yes";
238+
break;
239+
case DistinguishType.None:
240+
how = "no";
241+
break;
242+
default:
243+
how = "special";
244+
break;
245+
}
246+
WebAgent.WritePostBody(stream, new
247+
{
248+
how,
249+
id = Id,
250+
uh = Reddit.User.Modhash
251+
});
252+
stream.Close();
253+
var response = request.GetResponse();
254+
var data = WebAgent.GetResponseString(response.GetResponseStream());
255+
var json = JObject.Parse(data);
256+
if (json["jquery"].Count(i => i[0].Value<int>() == 11 && i[1].Value<int>() == 12) == 0)
257+
throw new AuthenticationException("You are not permitted to distinguish this comment.");
258+
}
259+
260+
internal class DistinguishConverter : JsonConverter
261+
{
262+
public override bool CanConvert(Type objectType)
263+
{
264+
return objectType == typeof(DistinguishType) || objectType == typeof(string);
265+
}
266+
267+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
268+
{
269+
var token = JToken.Load(reader);
270+
var value = token.Value<string>();
271+
if (value == null)
272+
return DistinguishType.None;
273+
switch (value)
274+
{
275+
case "moderator": return DistinguishType.Moderator;
276+
case "admin": return DistinguishType.Admin;
277+
case "special": return DistinguishType.Special;
278+
default: return DistinguishType.None;
279+
}
280+
}
281+
282+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
283+
{
284+
var d = (DistinguishType)value;
285+
if (d == DistinguishType.None)
286+
{
287+
writer.WriteNull();
288+
return;
289+
}
290+
writer.WriteValue(d.ToString().ToLower());
291+
}
292+
}
208293
}
209294
}

0 commit comments

Comments
 (0)