Skip to content

Commit e8fc77d

Browse files
Merge pull request #24 from writeas/extend-move-api-to-drafts
Extend move API to drafts
2 parents accd9c4 + 4327131 commit e8fc77d

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

Sources/WriteFreely/WFClient.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -327,40 +327,39 @@ public class WFClient {
327327
/// - Parameters:
328328
/// - token: The access token for the user moving the post to a collection.
329329
/// - postId: The ID of the post to add to the collection.
330-
/// - modifyToken: The post's modify token; required if the post doesn't belong to the requesting user.
331-
/// - collectionAlias: The alias of the collection to which the post should be added.
330+
/// - modifyToken: The post's modify token; required if the post doesn't belong to the requesting user. If `collectionAlias` is `nil`, do not include a `modifyToken`.
331+
/// - collectionAlias: The alias of the collection to which the post should be added; if `nil`, this removes the post from any collection.
332332
/// - completion: A handler for the returned `Bool` on success, or `Error` on failure.
333333
public func movePost(
334334
token: String? = nil,
335335
postId: String,
336336
with modifyToken: String? = nil,
337-
to collectionAlias: String,
337+
to collectionAlias: String?,
338338
completion: @escaping (Result<Bool, Error>) -> Void
339339
) {
340340
if token == nil && user == nil { return }
341341
guard let tokenToVerify = token ?? user?.token else { return }
342342

343-
guard let url = URL(string: "collections/\(collectionAlias)/collect", relativeTo: requestURL) else { return }
343+
if collectionAlias == nil && modifyToken != nil { completion(.failure(WFError.badRequest)) }
344+
345+
var urlString = ""
346+
if let collectionAlias = collectionAlias {
347+
urlString = "collections/\(collectionAlias)/collect"
348+
} else {
349+
urlString = "posts/disperse"
350+
}
351+
guard let url = URL(string: urlString, relativeTo: requestURL) else { return }
344352
var request = URLRequest(url: url)
345353

346354
request.httpMethod = "POST"
347355
request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
348356
request.addValue(tokenToVerify, forHTTPHeaderField: "Authorization")
349357

350-
var bodyObject: [[String: Any]]
358+
var bodyObject: [Any]
351359
if let modifyToken = modifyToken {
352-
bodyObject = [
353-
[
354-
"id": postId,
355-
"token": modifyToken
356-
]
357-
]
360+
bodyObject = [ [ "id": postId, "token": modifyToken ] ]
358361
} else {
359-
bodyObject = [
360-
[
361-
"id": postId
362-
]
363-
]
362+
bodyObject = collectionAlias == nil ? [ postId ] : [ [ "id": postId ] ]
364363
}
365364

366365
do {

0 commit comments

Comments
 (0)