diff --git a/Sources/WriteFreely/WFClient.swift b/Sources/WriteFreely/WFClient.swift index cb141e7..2d673fe 100644 --- a/Sources/WriteFreely/WFClient.swift +++ b/Sources/WriteFreely/WFClient.swift @@ -327,40 +327,39 @@ 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. - /// - collectionAlias: The alias of the collection to which the post should be added. + /// - 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( 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 } + if collectionAlias == nil && modifyToken != nil { completion(.failure(WFError.badRequest)) } + + 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" 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 = [ [ "id": postId, "token": modifyToken ] ] } else { - bodyObject = [ - [ - "id": postId - ] - ] + bodyObject = collectionAlias == nil ? [ postId ] : [ [ "id": postId ] ] } do {