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 pathSession+links.swift
289 lines (261 loc) · 15.2 KB
/
Session+links.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
//
// Session+links.swift
// reddift
//
// Created by sonson on 2015/05/19.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import Foundation
extension Session {
/**
Submit a new comment or reply to a message, whose parent is the fullname of the thing being replied to.
Its value changes the kind of object created by this request:
- the fullname of a Link: a top-level comment in that Link's thread.
- the fullname of a Comment: a comment reply to that comment.
- the fullname of a Message: a message reply to that message.
Response is JSON whose type is t1 Thing.
- parameter text: The body of comment, should be the raw markdown body of the comment or message.
- parameter parentName: Name of Thing is commented or replied to.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func postComment(_ text: String, parentName: String, completion: @escaping (Result<Comment>) -> Void) throws -> URLSessionDataTask {
let parameter = ["thing_id": parentName, "api_type": "json", "text": text]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/comment", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<Comment> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2Comment)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Delete a Link or Comment.
- parameter thing: Thing object to be deleted.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func deleteCommentOrLink(_ name: String, completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let parameter = ["id": name]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/del", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2JSON, completion: completion)
}
/**
Vote specified thing.
- parameter direction: The type of voting direction as VoteDirection.
- parameter thing: Thing will be voted.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func setVote(_ direction: VoteDirection, name: String, completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let parameter = ["dir": String(direction.rawValue), "id": name]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/vote", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2JSON, completion: completion)
}
/**
Save a specified content.
- parameter save: If you want to save the content, set to "true". On the other, if you want to remove the content from saved content, set to "false".
- parameter name: Name of Thing will be saved/unsaved.
- parameter category: Name of category into which you want to saved the content
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func setSave(_ save: Bool, name: String, category: String = "", completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
var parameter = ["id": name]
if !category.isEmpty {
parameter["category"] = category
}
let path = save ? "/api/save" : "/api/unsave"
guard let request = URLRequest.requestForOAuth(with: baseURL, path: path, parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2JSON, completion: completion)
}
/**
Set hide/show a specified content.
- parameter save: If you want to hide the content, set to "true". On the other, if you want to show the content, set to "false".
- parameter name: Name of Thing will be hide/show.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func setHide(_ hide: Bool, name: String, completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let parameter = ["id": name]
let path = hide ? "/api/hide" : "/api/unhide"
guard let request = URLRequest.requestForOAuth(with: baseURL, path: path, parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2JSON, completion: completion)
}
/**
Return a listing of things specified by their fullnames.
Only Links, Comments, and Subreddits are allowed.
- parameter names: Array of contents' fullnames.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getInfo(_ names: [String], completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
let commaSeparatedNameString = names.joined(separator: ",")
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/info", parameter: ["id": commaSeparatedNameString], method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<Listing> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap({
(redditAny: RedditAny) -> Result<Listing> in
if let listing = redditAny as? Listing {
return Result(value: listing)
}
return Result(error: ReddiftError.jsonObjectIsNotListingThing as NSError)
})
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Mark or unmark a link NSFW.
- parameter thing: Thing object, to set fullname of a thing.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func setNSFW(_ mark: Bool, thing: Thing, completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let path = mark ? "/api/marknsfw" : "/api/unmarknsfw"
guard let request = URLRequest.requestForOAuth(with: baseURL, path: path, parameter: ["id": thing.name], method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2JSON, completion: completion)
}
// MARK: BDT does not cover following methods.
/**
Get a list of categories in which things are currently saved.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getSavedCategories(_ completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/saved_categories", method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2JSON, completion: completion)
}
/**
Report a link, comment or message.
Reporting a thing brings it to the attention of the subreddit's moderators. Reporting a message sends it to a system for admin review.
For links and comments, the thing is implicitly hidden as well.
- parameter thing: Thing object, to set fullname of a thing.
- parameter reason: Reason of a string no longer than 100 characters.
- parameter otherReason: The other reason of a string no longer than 100 characters.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func report(_ thing: Thing, reason: String, otherReason: String, completion: @escaping (Result<RedditAny>) -> Void) throws -> URLSessionDataTask {
let parameter = [
"api_type": "json",
"reason": reason,
"other_reason": otherReason,
"thing_id": thing.name
]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/report", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2RedditAny, completion: completion)
}
/**
Submit a link to a subreddit.
- parameter subreddit: The subreddit to which is submitted a link.
- parameter title: The title of the submission. up to 300 characters long.
- parameter URL: A valid URL
- parameter captcha: The user's response to the CAPTCHA challenge
- parameter captchaIden: The identifier of the CAPTCHA challenge
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func submitLink(_ subreddit: Subreddit, title: String, URL: String, captcha: String, captchaIden: String, completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let parameter = [
"api_type": "json",
"captcha": captcha,
"iden": captchaIden,
"kind": "link",
"resubmit": "true",
"sendreplies": "true",
"sr": subreddit.displayName,
"title": title,
"url": URL
]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/submit", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2JSON, completion: completion)
}
/**
Submit a text to a subreddit.
Response JSON is, {"json":{"data":{"id":"35ljt6","name":"t3_35ljt6","url":"https://www.reddit.com/r/sandboxtest/comments/35ljt6/this_is_test/"},"errors":[]}}
- parameter subreddit: The subreddit to which is submitted a link.
- parameter title: The title of the submission. up to 300 characters long.
- parameter text: Raw markdown text
- parameter captcha: The user's response to the CAPTCHA challenge
- parameter captchaIden: The identifier of the CAPTCHA challenge
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func submitText(_ subreddit: Subreddit, title: String, text: String, captcha: String, captchaIden: String, completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let parameter = [
"api_type": "json",
"captcha": captcha,
"iden": captchaIden,
"kind": "self",
"resubmit": "true",
"sendreplies": "true",
"sr": subreddit.displayName,
"text": text,
"title": title
]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/submit", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
return executeTask(request, handleResponse: handleResponse2JSON, completion: completion)
}
/**
Retrieve additional comments omitted from a base comment tree. When a comment tree is rendered, the most relevant comments are selected for display first.
Remaining comments are stubbed out with "MoreComments" links. This API call is used to retrieve the additional comments represented by those stubs, up to 20 at a time.
The two core parameters required are link and children. link is the fullname of the link whose comments are being fetched.
children is a comma-delimited list of comment ID36s that need to be fetched. If id is passed, it should be the ID of the MoreComments object this call is replacing.
This is needed only for the HTML UI's purposes and is optional otherwise.
NOTE: you may only make one request at a time to this API endpoint. Higher concurrency will result in an error being returned.
- parameter children: A comma-delimited list of comment ID36s.
- parameter link: Thing object from which you get more children.
- parameter sort: The type of sorting children.
- parameter id: (optional) id of the associated MoreChildren object.
- parameter completion: The completion handler to call when the load request is complete.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func getMoreChildren(_ children: [String], link: Link, sort: CommentSort, id: String? = nil, completion: @escaping (Result<[Thing]>) -> Void) throws -> URLSessionDataTask {
let commaSeparatedChildren = children.joined(separator: ",")
var parameter = [
"children": commaSeparatedChildren,
"link_id": link.name,
"sort": sort.type,
"api_type": "json"
]
if let id = id {
parameter["id"] = id
}
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/morechildren", parameter: parameter, method: "GET", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<[Thing]> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2CommentAndMore)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
}