Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit e93e791

Browse files
committed
Translate PostServiceRemoteREST.getLikesForPostID to Swift
1 parent 388b86c commit e93e791

File tree

3 files changed

+56
-66
lines changed

3 files changed

+56
-66
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import WordPressKitObjC
2+
import NSObject_SafeExpectations
3+
4+
extension PostServiceRemoteREST {
5+
6+
/// Requests a list of users that liked the post with the specified ID.
7+
///
8+
/// Due to the API limitation, up to 90 users will be returned from the endpoint.
9+
///
10+
/// - Parameters:
11+
/// - postID: The ID for the post. Cannot be nil.
12+
/// - count: Number of records to retrieve. Cannot be nil. If 0, will default to endpoint max.
13+
/// - before: Filter results to Likes before this date/time string. Can be nil.
14+
/// - excludeUserIDs: Array of user IDs to exclude from response. Can be nil.
15+
/// - success: The block that will be executed on success. Can be nil.
16+
/// - failure: The block that will be executed on failure. Can be nil.
17+
@objc(getLikesForPostID:count:before:excludeUserIDs:success:failure:)
18+
public func getLikesForPostID(
19+
_ postID: NSNumber,
20+
count: NSNumber,
21+
before: String?,
22+
excludeUserIDs: [NSNumber]?,
23+
success: (([RemoteLikeUser], NSNumber) -> Void)?,
24+
failure: ((Error?) -> Void)?
25+
) {
26+
let path = "sites/\(siteID)/posts/\(postID)/likes"
27+
let requestUrl = self.path(forEndpoint: path, withVersion: ._1_2)
28+
let siteID = self.siteID
29+
30+
// If no count provided, default to endpoint max.
31+
var parameters: [String: Any] = ["number": count == 0 ? 90 : count]
32+
33+
if let before {
34+
parameters["before"] = before
35+
}
36+
37+
if let excludeUserIDs {
38+
parameters["exclude"] = excludeUserIDs
39+
}
40+
41+
wordPressComRestAPI.get(requestUrl,
42+
parameters: parameters,
43+
success: { (responseObject, httpResponse) in
44+
if let success {
45+
let responseDict = responseObject as? [String: Any] ?? [:]
46+
let jsonUsers = responseDict["likes"] as? [[String: Any]] ?? []
47+
let users = jsonUsers.map { RemoteLikeUser(dictionary: $0, postID: postID, siteID: siteID) }
48+
let found = responseDict["found"] as? NSNumber ?? 0
49+
success(users, found)
50+
}
51+
}, failure: { (error, _) in
52+
failure?(error)
53+
})
54+
}
55+
56+
}

Sources/WordPressKitObjC/PostServiceRemoteREST.m

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -314,51 +314,6 @@ - (void)restorePost:(RemotePost *)post
314314
}];
315315
}
316316

317-
- (void)getLikesForPostID:(NSNumber *)postID
318-
count:(NSNumber *)count
319-
before:(NSString *)before
320-
excludeUserIDs:(NSArray<NSNumber *> *)excludeUserIDs
321-
success:(void (^)(NSArray<RemoteLikeUser *> * _Nonnull users, NSNumber *found))success
322-
failure:(void (^)(NSError * _Nullable))failure
323-
{
324-
NSParameterAssert(postID);
325-
326-
NSString *path = [NSString stringWithFormat:@"sites/%@/posts/%@/likes", self.siteID, postID];
327-
NSString *requestUrl = [self pathForEndpoint:path
328-
withVersion:WordPressComRESTAPIVersion_1_2];
329-
NSNumber *siteID = self.siteID;
330-
331-
// If no count provided, default to endpoint max.
332-
if (count == 0) {
333-
count = @90;
334-
}
335-
336-
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{ @"number": count }];
337-
338-
if (before) {
339-
parameters[@"before"] = before;
340-
}
341-
342-
if (excludeUserIDs) {
343-
parameters[@"exclude"] = excludeUserIDs;
344-
}
345-
346-
[self.wordPressComRESTAPI get:requestUrl
347-
parameters:parameters
348-
success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
349-
if (success) {
350-
NSArray *jsonUsers = responseObject[@"likes"] ?: @[];
351-
NSArray<RemoteLikeUser *> *users = [self remoteUsersFromJSONArray:jsonUsers postID:postID siteID:siteID];
352-
NSNumber *found = [responseObject numberForKey:@"found"] ?: @0;
353-
success(users, found);
354-
}
355-
} failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
356-
if (failure) {
357-
failure(error);
358-
}
359-
}];
360-
}
361-
362317
- (NSDictionary *)dictionaryWithRemoteOptions:(id <PostServiceRemoteOptions>)options
363318
{
364319
NSMutableDictionary *remoteParams = [NSMutableDictionary dictionary];

Sources/WordPressKitObjC/include/PostServiceRemoteREST.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#import "RemoteMedia.h"
55

66
@class RemoteUser;
7-
@class RemoteLikeUser;
87

98
@interface PostServiceRemoteREST : SiteServiceRemoteWordPressComREST <PostServiceRemote>
109

@@ -56,26 +55,6 @@
5655
success:(void (^ _Nullable)(RemotePost * _Nullable))success
5756
failure:(void (^ _Nullable)(NSError * _Nullable error))failure;
5857

59-
/**
60-
* @brief Requests a list of users that liked the post with the specified ID.
61-
*
62-
* @discussion Due to the API limitation, up to 90 users will be returned from the
63-
* endpoint.
64-
*
65-
* @param postID The ID for the post. Cannot be nil.
66-
* @param count Number of records to retrieve. Cannot be nil. If 0, will default to endpoint max.
67-
* @param before Filter results to Likes before this date/time string. Can be nil.
68-
* @param excludeUserIDs Array of user IDs to exclude from response. Can be nil.
69-
* @param success The block that will be executed on success. Can be nil.
70-
* @param failure The block that will be executed on failure. Can be nil.
71-
*/
72-
- (void)getLikesForPostID:(NSNumber * _Nonnull)postID
73-
count:(NSNumber * _Nonnull)count
74-
before:(NSString * _Nullable)before
75-
excludeUserIDs:(NSArray<NSNumber *> * _Nullable)excludeUserIDs
76-
success:(void (^ _Nullable)(NSArray<RemoteLikeUser *> * _Nonnull users, NSNumber * _Nonnull found))success
77-
failure:(void (^ _Nullable)(NSError * _Nullable))failure;
78-
7958
/// Returns a remote post with the given data.
8059
+ (nonnull RemotePost *)remotePostFromJSONDictionary:(nonnull NSDictionary *)jsonPost;
8160

0 commit comments

Comments
 (0)