1
- using System . Threading . Tasks ;
1
+ using System ;
2
+ using System . Threading . Tasks ;
3
+ using System . Linq ;
4
+ using System . Security . Authentication ;
2
5
using Newtonsoft . Json ;
3
6
using Newtonsoft . Json . Linq ;
4
7
@@ -23,10 +26,19 @@ public enum ReportType
23
26
Other = 5
24
27
}
25
28
29
+ public enum DistinguishType
30
+ {
31
+ Moderator ,
32
+ Admin ,
33
+ Special ,
34
+ None
35
+ }
36
+
26
37
private const string VoteUrl = "/api/vote" ;
27
38
private const string SaveUrl = "/api/save" ;
28
39
private const string UnsaveUrl = "/api/unsave" ;
29
40
private const string ReportUrl = "/api/report" ;
41
+ private const string DistinguishUrl = "/api/distinguish" ;
30
42
31
43
[ JsonIgnore ]
32
44
private IWebAgent WebAgent { get ; set ; }
@@ -54,12 +66,16 @@ private void CommonInit(Reddit reddit, IWebAgent webAgent, JToken json)
54
66
WebAgent = webAgent ;
55
67
}
56
68
69
+
57
70
[ JsonProperty ( "downs" ) ]
58
71
public int Downvotes { get ; set ; }
59
72
[ JsonProperty ( "ups" ) ]
60
73
public int Upvotes { get ; set ; }
61
74
[ JsonProperty ( "saved" ) ]
62
75
public bool Saved { get ; set ; }
76
+ [ JsonProperty ( "distinguished" ) ]
77
+ [ JsonConverter ( typeof ( DistinguishConverter ) ) ]
78
+ public DistinguishType Distinguished { get ; set ; }
63
79
64
80
/// <summary>
65
81
/// True if the logged in user has upvoted this.
@@ -169,7 +185,7 @@ public void ClearVote()
169
185
var response = request . GetResponse ( ) ;
170
186
var data = WebAgent . GetResponseString ( response . GetResponseStream ( ) ) ;
171
187
}
172
-
188
+
173
189
public void Report ( ReportType reportType , string otherReason = null )
174
190
{
175
191
var request = WebAgent . CreatePost ( ReportUrl ) ;
@@ -178,17 +194,17 @@ public void Report(ReportType reportType, string otherReason = null)
178
194
string reportReason ;
179
195
switch ( reportType )
180
196
{
181
- case ReportType . Spam :
197
+ case ReportType . Spam :
182
198
reportReason = "spam" ; break ;
183
- case ReportType . VoteManipulation :
199
+ case ReportType . VoteManipulation :
184
200
reportReason = "vote manipulation" ; break ;
185
- case ReportType . PersonalInformation :
201
+ case ReportType . PersonalInformation :
186
202
reportReason = "personal information" ; break ;
187
- case ReportType . BreakingReddit :
203
+ case ReportType . BreakingReddit :
188
204
reportReason = "breaking reddit" ; break ;
189
- case ReportType . SexualizingMinors :
205
+ case ReportType . SexualizingMinors :
190
206
reportReason = "sexualizing minors" ; break ;
191
- default :
207
+ default :
192
208
reportReason = "other" ; break ;
193
209
}
194
210
@@ -205,5 +221,74 @@ public void Report(ReportType reportType, string otherReason = null)
205
221
var data = WebAgent . GetResponseString ( response . GetResponseStream ( ) ) ;
206
222
}
207
223
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
+ }
208
293
}
209
294
}
0 commit comments