From 3eeb9a9e9bad425e1b79dc0c3ea47e0b074a95e1 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Thu, 8 Oct 2020 15:47:39 -0400 Subject: [PATCH 1/3] Set request URL to /posts/disperse if collectionAlias is nil --- Sources/WriteFreely/WFClient.swift | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Sources/WriteFreely/WFClient.swift b/Sources/WriteFreely/WFClient.swift index cb141e7..6ceeccf 100644 --- a/Sources/WriteFreely/WFClient.swift +++ b/Sources/WriteFreely/WFClient.swift @@ -328,19 +328,25 @@ public class WFClient { /// - token: The access token for the user moving the post to a collection. /// - postId: The ID of the post to add to the collection. /// - modifyToken: The post's modify token; required if the post doesn't belong to the requesting user. - /// - collectionAlias: The alias of the collection to which the post should be added. + /// - collectionAlias: The alias of the collection to which the post should be added; if `nil`, this removes the post from any collection. /// - completion: A handler for the returned `Bool` on success, or `Error` on failure. public func movePost( token: String? = nil, postId: String, with modifyToken: String? = nil, - to collectionAlias: String, + to collectionAlias: String?, completion: @escaping (Result) -> Void ) { if token == nil && user == nil { return } guard let tokenToVerify = token ?? user?.token else { return } - guard let url = URL(string: "collections/\(collectionAlias)/collect", relativeTo: requestURL) else { return } + var urlString = "" + if let collectionAlias = collectionAlias { + urlString = "collections/\(collectionAlias)/collect" + } else { + urlString = "posts/disperse" + } + guard let url = URL(string: urlString, relativeTo: requestURL) else { return } var request = URLRequest(url: url) request.httpMethod = "POST" From 6e94615d82412009498a8e0de8082a02164ea926 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Thu, 8 Oct 2020 16:13:08 -0400 Subject: [PATCH 2/3] Set bodyObject based on collectionAlias --- Sources/WriteFreely/WFClient.swift | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Sources/WriteFreely/WFClient.swift b/Sources/WriteFreely/WFClient.swift index 6ceeccf..ada676d 100644 --- a/Sources/WriteFreely/WFClient.swift +++ b/Sources/WriteFreely/WFClient.swift @@ -353,20 +353,11 @@ public class WFClient { request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") request.addValue(tokenToVerify, forHTTPHeaderField: "Authorization") - var bodyObject: [[String: Any]] + var bodyObject: [Any] if let modifyToken = modifyToken { - bodyObject = [ - [ - "id": postId, - "token": modifyToken - ] - ] + bodyObject = collectionAlias == nil ? [ postId ] : [ [ "id": postId, "token": modifyToken ] ] } else { - bodyObject = [ - [ - "id": postId - ] - ] + bodyObject = collectionAlias == nil ? [ postId ] : [ [ "id": postId ] ] } do { From 43271316d5c3a0389cdc5e0d0676a643e839f7c6 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Thu, 8 Oct 2020 16:58:57 -0400 Subject: [PATCH 3/3] Fail early if collectionAlias is nil and modifyToken is not nil --- Sources/WriteFreely/WFClient.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/WriteFreely/WFClient.swift b/Sources/WriteFreely/WFClient.swift index ada676d..2d673fe 100644 --- a/Sources/WriteFreely/WFClient.swift +++ b/Sources/WriteFreely/WFClient.swift @@ -327,7 +327,7 @@ public class WFClient { /// - Parameters: /// - token: The access token for the user moving the post to a collection. /// - postId: The ID of the post to add to the collection. - /// - modifyToken: The post's modify token; required if the post doesn't belong to the requesting user. + /// - 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`. /// - collectionAlias: The alias of the collection to which the post should be added; if `nil`, this removes the post from any collection. /// - completion: A handler for the returned `Bool` on success, or `Error` on failure. public func movePost( @@ -340,6 +340,8 @@ public class WFClient { if token == nil && user == nil { return } guard let tokenToVerify = token ?? user?.token else { return } + if collectionAlias == nil && modifyToken != nil { completion(.failure(WFError.badRequest)) } + var urlString = "" if let collectionAlias = collectionAlias { urlString = "collections/\(collectionAlias)/collect" @@ -355,7 +357,7 @@ public class WFClient { var bodyObject: [Any] if let modifyToken = modifyToken { - bodyObject = collectionAlias == nil ? [ postId ] : [ [ "id": postId, "token": modifyToken ] ] + bodyObject = [ [ "id": postId, "token": modifyToken ] ] } else { bodyObject = collectionAlias == nil ? [ postId ] : [ [ "id": postId ] ] }