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+messages.swift
212 lines (196 loc) · 10.9 KB
/
Session+messages.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
//
// Session+messages.swift
// reddift
//
// Created by sonson on 2015/05/19.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import Foundation
extension Session {
// MARK: Update message status
/**
Mark messages as "unread"
- parameter id: A comma-separated list of thing fullnames
- parameter modhash: A modhash, default is blank string not nil.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func markMessagesAsUnread(_ fullnames: [String], modhash: String = "", completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let commaSeparatedFullameString = fullnames.joined(separator: ",")
let parameter = ["id": commaSeparatedFullameString]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/unread_message", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<JSONAny> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Mark messages as "read"
- parameter id: A comma-separated list of thing fullnames
- parameter modhash: A modhash, default is blank string not nil.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func markMessagesAsRead(_ fullnames: [String], modhash: String = "", completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let commaSeparatedFullameString = fullnames.joined(separator: ",")
let parameter = ["id": commaSeparatedFullameString]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/read_message", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<JSONAny> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Mark all messages as "read"
Queue up marking all messages for a user as read.
This may take some time, and returns 202 to acknowledge acceptance of the request.
- parameter modhash: A modhash, default is blank string not nil.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func markAllMessagesAsRead(_ modhash: String = "", completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/read_all_messages", parameter: nil, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<JSONAny> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Collapse messages
- parameter id: A comma-separated list of thing fullnames
- parameter modhash: A modhash, default is blank string not nil.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func collapseMessages(_ fullnames: [String], modhash: String = "", completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let commaSeparatedFullameString = fullnames.joined(separator: ",")
let parameter = ["id": commaSeparatedFullameString]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/collapse_message", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<JSONAny> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
Uncollapse messages
- parameter id: A comma-separated list of thing fullnames
- parameter modhash: A modhash, default is blank string not nil.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func uncollapseMessages(_ fullnames: [String], modhash: String = "", completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let commaSeparatedFullameString = fullnames.joined(separator: ",")
let parameter = ["id": commaSeparatedFullameString]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/uncollapse_message", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<JSONAny> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
For blocking via inbox.
- parameter id: fullname of a thing
- parameter modhash: A modhash, default is blank string not nil.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func blockViaInbox(_ fullname: String, modhash: String = "", completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let parameter = ["id": fullname]
guard let request = URLRequest.requestForOAuth(with: Session.OAuthEndpointURL, path: "/api/block", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<JSONAny> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
/**
For unblocking via inbox.
- parameter id: fullname of a thing
- parameter modhash: A modhash, default is blank string not nil.
- returns: Data task which requests search to reddit.com.
*/
@discardableResult
public func unblockViaInbox(_ fullname: String, modhash: String = "", completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let parameter = ["id": fullname]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/unblock_subreddit", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<JSONAny> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
// MARK: Get messages
/**
Get the message from the specified box.
- parameter messageWhere: The box from which you want to get your messages.
- parameter limit: The maximum number of comments to return. Default is 100.
- 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 getMessage(_ messageWhere: MessageWhere, limit: Int = 100, completion: @escaping (Result<Listing>) -> Void) throws -> URLSessionDataTask {
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/message" + messageWhere.path, 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(redditAny2Object)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
// MARK: Compose a message
/**
Compose new message to specified user.
- parameter to: Account object of user to who you want to send a message.
- parameter subject: A string no longer than 100 characters
- parameter text: Raw markdown text
- parameter fromSubreddit: Subreddit name?
- 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 composeMessage(_ to: Account, subject: String, text: String, fromSubreddit: Subreddit, captcha: String, captchaIden: String, completion: @escaping (Result<JSONAny>) -> Void) throws -> URLSessionDataTask {
let parameter = [
"api_type": "json",
"captcha": captcha,
"iden": captchaIden,
"from_sr": fromSubreddit.displayName,
"text": text,
"subject": subject,
"to": to.id
]
guard let request = URLRequest.requestForOAuth(with: baseURL, path: "/api/submit", parameter: parameter, method: "POST", token: token)
else { throw ReddiftError.canNotCreateURLRequest as NSError }
let closure = {(data: Data?, response: URLResponse?, error: NSError?) -> Result<JSONAny> in
return Result(from: Response(data: data, urlResponse: response), optional: error)
.flatMap(response2Data)
.flatMap(data2Json)
.flatMap(json2RedditAny)
.flatMap(redditAny2Object)
}
return executeTask(request, handleResponse: closure, completion: completion)
}
}