Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
Closed
56 changes: 56 additions & 0 deletions Sources/WordPressKit/PostServiceRemoteREST.swift
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to translate this function to Swift, because it's used in a Swift module. I'll explain a little bit more of why this is needed in the upcoming app PR (which would provide a full context).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. We'll just need to make sure we test the features that needed rewrite.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import WordPressKitObjC
import NSObject_SafeExpectations

extension PostServiceRemoteREST {

/// Requests a list of users that liked the post with the specified ID.
///
/// Due to the API limitation, up to 90 users will be returned from the endpoint.
///
/// - Parameters:
/// - postID: The ID for the post. Cannot be nil.
/// - count: Number of records to retrieve. Cannot be nil. If 0, will default to endpoint max.
/// - before: Filter results to Likes before this date/time string. Can be nil.
/// - excludeUserIDs: Array of user IDs to exclude from response. Can be nil.
/// - success: The block that will be executed on success. Can be nil.
/// - failure: The block that will be executed on failure. Can be nil.
@objc(getLikesForPostID:count:before:excludeUserIDs:success:failure:)
public func getLikesForPostID(
_ postID: NSNumber,
count: NSNumber,
before: String?,
excludeUserIDs: [NSNumber]?,
success: (([RemoteLikeUser], NSNumber) -> Void)?,
failure: ((Error?) -> Void)?
) {
let path = "sites/\(siteID)/posts/\(postID)/likes"
let requestUrl = self.path(forEndpoint: path, withVersion: ._1_2)
let siteID = self.siteID

// If no count provided, default to endpoint max.
var parameters: [String: Any] = ["number": count == 0 ? 90 : count]

if let before {
parameters["before"] = before
}

if let excludeUserIDs {
parameters["exclude"] = excludeUserIDs
}

wordPressComRestAPI.get(requestUrl,
parameters: parameters,
success: { (responseObject, httpResponse) in
if let success {
let responseDict = responseObject as? [String: Any] ?? [:]
let jsonUsers = responseDict["likes"] as? [[String: Any]] ?? []
let users = jsonUsers.map { RemoteLikeUser(dictionary: $0, postID: postID, siteID: siteID) }
let found = responseDict["found"] as? NSNumber ?? 0
success(users, found)
}
}, failure: { (error, _) in
failure?(error)
})
}

}
45 changes: 0 additions & 45 deletions Sources/WordPressKitObjC/PostServiceRemoteREST.m
Original file line number Diff line number Diff line change
Expand Up @@ -314,51 +314,6 @@ - (void)restorePost:(RemotePost *)post
}];
}

- (void)getLikesForPostID:(NSNumber *)postID
count:(NSNumber *)count
before:(NSString *)before
excludeUserIDs:(NSArray<NSNumber *> *)excludeUserIDs
success:(void (^)(NSArray<RemoteLikeUser *> * _Nonnull users, NSNumber *found))success
failure:(void (^)(NSError * _Nullable))failure
{
NSParameterAssert(postID);

NSString *path = [NSString stringWithFormat:@"sites/%@/posts/%@/likes", self.siteID, postID];
NSString *requestUrl = [self pathForEndpoint:path
withVersion:WordPressComRESTAPIVersion_1_2];
NSNumber *siteID = self.siteID;

// If no count provided, default to endpoint max.
if (count == 0) {
count = @90;
}

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{ @"number": count }];

if (before) {
parameters[@"before"] = before;
}

if (excludeUserIDs) {
parameters[@"exclude"] = excludeUserIDs;
}

[self.wordPressComRESTAPI get:requestUrl
parameters:parameters
success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
if (success) {
NSArray *jsonUsers = responseObject[@"likes"] ?: @[];
NSArray<RemoteLikeUser *> *users = [self remoteUsersFromJSONArray:jsonUsers postID:postID siteID:siteID];
NSNumber *found = [responseObject numberForKey:@"found"] ?: @0;
success(users, found);
}
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
if (failure) {
failure(error);
}
}];
}

- (NSDictionary *)dictionaryWithRemoteOptions:(id <PostServiceRemoteOptions>)options
{
NSMutableDictionary *remoteParams = [NSMutableDictionary dictionary];
Expand Down
21 changes: 0 additions & 21 deletions Sources/WordPressKitObjC/include/PostServiceRemoteREST.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#import "RemoteMedia.h"

@class RemoteUser;
@class RemoteLikeUser;

@interface PostServiceRemoteREST : SiteServiceRemoteWordPressComREST <PostServiceRemote>

Expand Down Expand Up @@ -56,26 +55,6 @@
success:(void (^ _Nullable)(RemotePost * _Nullable))success
failure:(void (^ _Nullable)(NSError * _Nullable error))failure;

/**
* @brief Requests a list of users that liked the post with the specified ID.
*
* @discussion Due to the API limitation, up to 90 users will be returned from the
* endpoint.
*
* @param postID The ID for the post. Cannot be nil.
* @param count Number of records to retrieve. Cannot be nil. If 0, will default to endpoint max.
* @param before Filter results to Likes before this date/time string. Can be nil.
* @param excludeUserIDs Array of user IDs to exclude from response. Can be nil.
* @param success The block that will be executed on success. Can be nil.
* @param failure The block that will be executed on failure. Can be nil.
*/
- (void)getLikesForPostID:(NSNumber * _Nonnull)postID
count:(NSNumber * _Nonnull)count
before:(NSString * _Nullable)before
excludeUserIDs:(NSArray<NSNumber *> * _Nullable)excludeUserIDs
success:(void (^ _Nullable)(NSArray<RemoteLikeUser *> * _Nonnull users, NSNumber * _Nonnull found))success
failure:(void (^ _Nullable)(NSError * _Nullable))failure;

/// Returns a remote post with the given data.
+ (nonnull RemotePost *)remotePostFromJSONDictionary:(nonnull NSDictionary *)jsonPost;

Expand Down