This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathComment.swift
326 lines (311 loc) · 9.67 KB
/
Comment.swift
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
//
// Comment.swift
// reddift
//
// Created by generator.rb via from https://github.com/reddit/reddit/wiki/JSON
// Created at 2015-04-15 11:23:32 +0900
//
import Foundation
import HTMLSpecialCharacters
/**
Expand child comments which are included in Comment objects, recursively.
Returns comment list and their depth list.
- parameter comment: Comment object will be expanded.
- returns: Array contains Comment objects which are expaned from specified Comment object and depth list of them.
*/
public func extendAllReplies(in comment: Thing, current depth: Int) -> ([(Thing, Int)]) {
var buf: [(Thing, Int)] = []
if let comment = comment as? Comment {
buf.append((comment, depth))
for obj in comment.replies.children {
buf.append(contentsOf: extendAllReplies(in: obj, current: depth + 1))
}
} else if let more = comment as? More {
for id in more.children {
let more = More(id: id, name: "t1_\(id)", parentId: more.parentId, child: id)
buf.append((more, depth))
}
}
return buf
}
/**
Comment object.
*/
public struct Comment: Thing, Created, Votable {
/// identifier of Thing like 15bfi0.
public let id: String
/// name of Thing, that is fullname, like t3_15bfi0.
public let name: String
/// type of Thing, like t3.
static public let kind = "t1"
/**
the id of the subreddit in which the thing is located
example: t5_2qizd
*/
public let subredditId: String
/**
example:
*/
public let bannedBy: String
/**
example: t3_32wnhw
*/
public let linkId: String
/**
how the logged-in user has voted on the link - True = upvoted, False = downvoted, null = no vote
example:
*/
public let likes: VoteDirection
/**
example: {"kind"=>"Listing", "data"=>{"modhash"=>nil, "children"=>[{"kind"=>"more", "data"=>{"count"=>0, "parent_id"=>"t1_cqfhkcb", "children"=>["cqfmmpp"], "name"=>"t1_cqfmmpp", "id"=>"cqfmmpp"}}], "after"=>nil, "before"=>nil}}
*/
public let replies: Listing
/**
example: []
*/
public let userReports: [AnyObject]
/**
true if this post is saved by the logged in user
example: false
*/
public let saved: Bool
/**
example: 0
*/
public let gilded: Int
/**
example: false
*/
public let archived: Bool
/**
example:
*/
public let reportReasons: [AnyObject]
/**
the account name of the poster. null if this is a promotional link
example: Icnoyotl
*/
public let author: String
/**
example: t1_cqfh5kz
*/
public let parentId: String
/**
the net-score of the link. note: A submission's score is simply the number of upvotes minus the number of downvotes. If five users like the submission and three users don't it will have a score of 2. Please note that the vote numbers are not "real" numbers, they have been "fuzzed" to prevent spam bots etc. So taking the above example, if five users upvoted the submission, and three users downvote it, the upvote/downvote numbers may say 23 upvotes and 21 downvotes, or 12 upvotes, and 10 downvotes. The points score is correct, but the vote totals are "fuzzed".
example: 1
*/
public let score: Int
/**
example:
*/
public let approvedBy: String
/**
example: 0
*/
public let controversiality: Int
/**
example: The bot has been having this problem for awhile, there have been thousands of new comments since it last worked properly, so it seems like this must be something recurring? Could it have something to do with our AutoModerator?
*/
public let body: String
/**
example: false
*/
public let edited: Bool
/**
the CSS class of the author's flair. subreddit specific
example:
*/
public let authorFlairCssClass: String
/**
example: 0
*/
public let downs: Int
/**
example: <div class="md"><p>The bot has been having this problem for awhile, there have been thousands of new comments since it last worked properly, so it seems like this must be something recurring? Could it have something to do with our AutoModerator?</p>
</div>
*/
public let bodyHtml: String
/**
subreddit of thing excluding the /r/ prefix. "pics"
example: redditdev
*/
public let subreddit: String
/**
example: false
*/
public let scoreHidden: Bool
/**
example: 1429284845
*/
public let created: Int
/**
the text of the author's flair. subreddit specific
example:
*/
public let authorFlairText: String
/**
example: 1429281245
*/
public let createdUtc: Int
/**
example:
*/
public let distinguished: DistinguishType
/**
example: []
*/
public let modReports: [AnyObject]
/**
example:
*/
public let numReports: Int
/**
example: 1
*/
public let ups: Int
/**
if the comment is stickied
*/
public let stickied: Bool
public var isExpandable: Bool {
get {
if replies.children.count == 1 {
if let more = replies.children[0] as? More {
if more.isEmpty {
return true
}
}
}
return false
}
}
public init(id: String) {
self.id = id
self.name = "\(Comment.kind)_\(self.id)"
subredditId = ""
bannedBy = ""
linkId = ""
likes = .none
replies = Listing()
userReports = []
saved = false
gilded = 0
archived = false
reportReasons = []
author = ""
parentId = ""
score = 0
approvedBy = ""
controversiality = 0
body = ""
edited = false
authorFlairCssClass = ""
downs = 0
bodyHtml = ""
subreddit = ""
scoreHidden = false
created = 0
authorFlairText = ""
createdUtc = 0
distinguished = .none
modReports = []
numReports = 0
ups = 0
stickied = false
}
public init(link: Link) {
self.id = link.id
self.name = "\(Comment.kind)_\(self.id)"
subredditId = link.subredditId
bannedBy = link.bannedBy
linkId = link.id
likes = link.likes
replies = Listing()
userReports = link.userReports
saved = link.saved
gilded = link.gilded
archived = link.archived
reportReasons = link.reportReasons
author = link.author
parentId = ""
score = link.score
approvedBy = link.approvedBy
controversiality = 0
body = link.selftext
edited = link.edited
authorFlairCssClass = link.authorFlairCssClass
downs = link.downs
bodyHtml = link.selftextHtml
subreddit = link.subreddit
scoreHidden = false
created = link.created
authorFlairText = link.authorFlairText
createdUtc = link.createdUtc
distinguished = link.distinguished
modReports = link.modReports
numReports = link.numReports
ups = link.ups
stickied = false
}
/**
Parse t1 Thing.
- parameter data: Dictionary, must be generated parsing t1 Thing.
- returns: Comment object as Thing.
*/
public init(json data: JSONDictionary) {
id = data["id"] as? String ?? ""
subredditId = data["subreddit_id"] as? String ?? ""
bannedBy = data["banned_by"] as? String ?? ""
linkId = data["link_id"] as? String ?? ""
if let temp = data["likes"] as? Bool {
likes = temp ? .up : .down
} else {
likes = .none
}
userReports = []
saved = data["saved"] as? Bool ?? false
gilded = data["gilded"] as? Int ?? 0
archived = data["archived"] as? Bool ?? false
reportReasons = []
author = data["author"] as? String ?? ""
parentId = data["parent_id"] as? String ?? ""
score = data["score"] as? Int ?? 0
approvedBy = data["approved_by"] as? String ?? ""
controversiality = data["controversiality"] as? Int ?? 0
body = data["body"] as? String ?? ""
edited = data["edited"] as? Bool ?? false
authorFlairCssClass = data["author_flair_css_class"] as? String ?? ""
downs = data["downs"] as? Int ?? 0
let tempBodyHtml = data["body_html"] as? String ?? ""
bodyHtml = tempBodyHtml.unescapeHTML
subreddit = data["subreddit"] as? String ?? ""
scoreHidden = data["score_hidden"] as? Bool ?? false
name = data["name"] as? String ?? ""
created = data["created"] as? Int ?? 0
authorFlairText = data["author_flair_text"] as? String ?? ""
createdUtc = data["created_utc"] as? Int ?? 0
if let distinguishedString = data["distinguished"] as? String {
switch distinguishedString {
case "admin": distinguished = .admin
case "moderator": distinguished = .moderator
case "special": distinguished = .special
default: distinguished = .none
}
} else {
distinguished = .none
}
modReports = []
numReports = data["num_reports"] as? Int ?? 0
ups = data["ups"] as? Int ?? 0
stickied = data["stickied"] as? Bool ?? false
if let temp = data["replies"] as? JSONDictionary {
if let obj = Parser.redditAny(from: temp) as? Listing {
replies = obj
} else {
replies = Listing()
}
} else {
replies = Listing()
}
}
}