This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBlogJetpackSettingsServiceRemote.swift
More file actions
259 lines (214 loc) · 12.2 KB
/
Copy pathBlogJetpackSettingsServiceRemote.swift
File metadata and controls
259 lines (214 loc) · 12.2 KB
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
import Foundation
public class BlogJetpackSettingsServiceRemote: ServiceRemoteWordPressComREST {
public enum ResponseError: Error {
case decodingFailure
}
/// Fetches the Jetpack settings for the specified site
///
public func getJetpackSettingsForSite(_ siteID: Int, success: @escaping (RemoteBlogJetpackSettings) -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "jetpack-blogs/\(siteID)/rest-api"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters: [String: Any] = ["path": "/jetpack/v4/settings"]
wordPressComRESTAPI.get(path,
parameters: parameters,
success: { response, _ in
guard let responseDict = response as? [String: Any],
let results = responseDict["data"] as? [String: AnyObject],
let remoteSettings = try? self.remoteJetpackSettingsFromDictionary(results) else {
failure(ResponseError.decodingFailure)
return
}
success(remoteSettings)
}, failure: {
error, _ in
failure(error)
})
}
/// Fetches the Jetpack Monitor settings for the specified site
///
public func getJetpackMonitorSettingsForSite(_ siteID: Int, success: @escaping (RemoteBlogJetpackMonitorSettings) -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "jetpack-blogs/\(siteID)"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
wordPressComRESTAPI.get(path,
parameters: nil,
success: { response, _ in
guard let responseDict = response as? [String: Any],
let results = responseDict["settings"] as? [String: AnyObject],
let remoteMonitorSettings = try? self.remoteJetpackMonitorSettingsFromDictionary(results) else {
failure(ResponseError.decodingFailure)
return
}
success(remoteMonitorSettings)
}, failure: {
error, _ in
failure(error)
})
}
/// Fetches the Jetpack Modules settings for the specified site
///
public func getJetpackModulesSettingsForSite(_ siteID: Int, success: @escaping (RemoteBlogJetpackModulesSettings) -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "sites/\(siteID)/jetpack/modules"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
wordPressComRESTAPI.get(path,
parameters: nil,
success: { response, _ in
guard let responseDict = response as? [String: Any],
let modules = responseDict["modules"] as? [[String: AnyObject]],
let remoteModulesSettings = try? self.remoteJetpackModulesSettingsFromArray(modules) else {
failure(ResponseError.decodingFailure)
return
}
success(remoteModulesSettings)
}, failure: {
error, _ in
failure(error)
})
}
/// Saves the Jetpack settings for the specified site
///
public func updateJetpackSettingsForSite(_ siteID: Int, settings: RemoteBlogJetpackSettings, success: @escaping () -> Void, failure: @escaping (Error?) -> Void) {
let dictionary = dictionaryFromJetpackSettings(settings)
guard let jSONData = try? JSONSerialization.data(withJSONObject: dictionary, options: []),
let jSONBody = String(data: jSONData, encoding: .ascii) else {
failure(nil)
return
}
let endpoint = "jetpack-blogs/\(siteID)/rest-api"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters = ["path": "/jetpack/v4/settings",
"body": jSONBody,
"json": true] as [String: AnyObject]
wordPressComRESTAPI.post(path,
parameters: parameters,
success: {
_, _ in
success()
}, failure: {
error, _ in
failure(error)
})
}
/// Saves the Jetpack Monitor settings for the specified site
///
public func updateJetpackMonitorSettingsForSite(_ siteID: Int, settings: RemoteBlogJetpackMonitorSettings, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "jetpack-blogs/\(siteID)"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters = dictionaryFromJetpackMonitorSettings(settings)
wordPressComRESTAPI.post(path,
parameters: parameters,
success: {
_, _ in
success()
}, failure: {
error, _ in
failure(error)
})
}
/// Saves the Jetpack Module active setting for the specified site
///
public func updateJetpackModuleActiveSettingForSite(_ siteID: Int, module: String, active: Bool, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "sites/\(siteID)/jetpack/modules/\(module)"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
let parameters = [ModuleOptionKeys.active: active]
wordPressComRESTAPI.post(path,
parameters: parameters as [String: AnyObject],
success: {
_, _ in
success()
}, failure: {
error, _ in
failure(error)
})
}
/// Disconnects Jetpack from a site
///
@objc public func disconnectJetpackFromSite(_ siteID: Int, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
let endpoint = "jetpack-blogs/\(siteID)/mine/delete"
let path = self.path(forEndpoint: endpoint, withVersion: ._1_1)
wordPressComRESTAPI.post(path,
parameters: nil,
success: {
_, _ in
success()
}, failure: {
error, _ in
failure(error)
})
}
}
private extension BlogJetpackSettingsServiceRemote {
func remoteJetpackSettingsFromDictionary(_ dictionary: [String: AnyObject]) throws -> RemoteBlogJetpackSettings {
guard let monitorEnabled = dictionary[Keys.monitorEnabled] as? Bool,
let blockMaliciousLoginAttempts = dictionary[Keys.blockMaliciousLoginAttempts] as? Bool,
let allowlistedIPs = dictionary[Keys.allowListedIPAddresses]?[Keys.allowListedIPsLocal] as? [String],
let ssoEnabled = dictionary[Keys.ssoEnabled] as? Bool,
let ssoMatchAccountsByEmail = dictionary[Keys.ssoMatchAccountsByEmail] as? Bool,
let ssoRequireTwoStepAuthentication = dictionary[Keys.ssoRequireTwoStepAuthentication] as? Bool else {
throw ResponseError.decodingFailure
}
return RemoteBlogJetpackSettings(monitorEnabled: monitorEnabled,
blockMaliciousLoginAttempts: blockMaliciousLoginAttempts,
loginAllowListedIPAddresses: Set(allowlistedIPs),
ssoEnabled: ssoEnabled,
ssoMatchAccountsByEmail: ssoMatchAccountsByEmail,
ssoRequireTwoStepAuthentication: ssoRequireTwoStepAuthentication)
}
func remoteJetpackMonitorSettingsFromDictionary(_ dictionary: [String: AnyObject]) throws -> RemoteBlogJetpackMonitorSettings {
guard let monitorEmailNotifications = dictionary[Keys.monitorEmailNotifications] as? Bool,
let monitorPushNotifications = dictionary[Keys.monitorPushNotifications] as? Bool else {
throw ResponseError.decodingFailure
}
return RemoteBlogJetpackMonitorSettings(monitorEmailNotifications: monitorEmailNotifications,
monitorPushNotifications: monitorPushNotifications)
}
func remoteJetpackModulesSettingsFromArray(_ modules: [[String: AnyObject]]) throws -> RemoteBlogJetpackModulesSettings {
let dictionary = modules.reduce(into: [String: [String: AnyObject]]()) {
guard let key = $1.valueAsString(forKey: "id") else {
return
}
$0[key] = $1
}
guard let serveImagesFromOurServersValue = dictionary[Keys.serveImagesFromOurServers]?[ModuleOptionKeys.active] as? Bool else {
throw ResponseError.decodingFailure
}
return RemoteBlogJetpackModulesSettings(serveImagesFromOurServers: serveImagesFromOurServersValue)
}
func dictionaryFromJetpackSettings(_ settings: RemoteBlogJetpackSettings) -> [String: Any] {
let joinedIPs = settings.loginAllowListedIPAddresses.joined(separator: ", ")
let shouldSendAllowlist = settings.blockMaliciousLoginAttempts
let settingsDictionary: [String: Any?] = [
Keys.monitorEnabled: settings.monitorEnabled,
Keys.blockMaliciousLoginAttempts: settings.blockMaliciousLoginAttempts,
Keys.allowListedIPAddresses: shouldSendAllowlist ? joinedIPs : nil,
Keys.ssoEnabled: settings.ssoEnabled,
Keys.ssoMatchAccountsByEmail: settings.ssoMatchAccountsByEmail,
Keys.ssoRequireTwoStepAuthentication: settings.ssoRequireTwoStepAuthentication
]
return settingsDictionary.compactMapValues { $0 }
}
func dictionaryFromJetpackMonitorSettings(_ settings: RemoteBlogJetpackMonitorSettings) -> [String: AnyObject] {
return [Keys.monitorEmailNotifications: settings.monitorEmailNotifications as AnyObject,
Keys.monitorPushNotifications: settings.monitorPushNotifications as AnyObject]
}
}
public extension BlogJetpackSettingsServiceRemote {
enum Keys {
// RemoteBlogJetpackSettings keys
public static let monitorEnabled = "monitor"
public static let blockMaliciousLoginAttempts = "protect"
public static let allowListedIPAddresses = "jetpack_protect_global_whitelist"
public static let allowListedIPsLocal = "local"
public static let ssoEnabled = "sso"
public static let ssoMatchAccountsByEmail = "jetpack_sso_match_by_email"
public static let ssoRequireTwoStepAuthentication = "jetpack_sso_require_two_step"
// RemoteBlogJetpackMonitorSettings keys
static let monitorEmailNotifications = "email_notifications"
static let monitorPushNotifications = "wp_note_notifications"
// RemoteBlogJetpackModuleSettings keys
public static let serveImagesFromOurServers = "photon"
}
enum ModuleOptionKeys {
// Whether or not the module is currently active
public static let active = "active"
}
}