/WordPress/Classes/Networking/CommentServiceRemoteREST.m

https://gitlab.com/jslee1/WordPress-iOS · Objective C · 419 lines · 363 code · 52 blank · 4 comment · 35 complexity · acd64be8873b319f17286bb98718f0ad MD5 · raw file

  1. #import "CommentServiceRemoteREST.h"
  2. #import "WordPress-Swift.h"
  3. #import "RemoteComment.h"
  4. #import "NSDate+WordPressJSON.h"
  5. #import <NSObject_SafeExpectations/NSObject+SafeExpectations.h>
  6. @implementation CommentServiceRemoteREST
  7. #pragma mark Public methods
  8. #pragma mark - Blog-centric methods
  9. - (void)getCommentsWithMaximumCount:(NSInteger)maximumComments
  10. success:(void (^)(NSArray *comments))success
  11. failure:(void (^)(NSError *error))failure
  12. {
  13. [self getCommentsWithMaximumCount:maximumComments options:nil success:success failure:failure];
  14. }
  15. - (void)getCommentsWithMaximumCount:(NSInteger)maximumComments
  16. options:(NSDictionary *)options
  17. success:(void (^)(NSArray *posts))success
  18. failure:(void (^)(NSError *error))failure
  19. {
  20. NSString *path = [NSString stringWithFormat:@"sites/%@/comments", self.siteID];
  21. NSString *requestUrl = [self pathForEndpoint:path
  22. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  23. NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{
  24. @"status": @"all",
  25. @"context": @"edit",
  26. @"number": @(maximumComments)
  27. }];
  28. if (options) {
  29. [parameters addEntriesFromDictionary:options];
  30. }
  31. [self.wordPressComRestApi GET:requestUrl
  32. parameters:parameters
  33. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  34. if (success) {
  35. success([self remoteCommentsFromJSONArray:responseObject[@"comments"]]);
  36. }
  37. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  38. if (failure) {
  39. failure(error);
  40. }
  41. }];
  42. }
  43. - (void)createComment:(RemoteComment *)comment
  44. success:(void (^)(RemoteComment *comment))success
  45. failure:(void (^)(NSError *))failure
  46. {
  47. NSString *path;
  48. if (comment.parentID) {
  49. path = [NSString stringWithFormat:@"sites/%@/comments/%@/replies/new", self.siteID, comment.parentID];
  50. } else {
  51. path = [NSString stringWithFormat:@"sites/%@/posts/%@/replies/new", self.siteID, comment.postID];
  52. }
  53. NSString *requestUrl = [self pathForEndpoint:path
  54. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  55. NSDictionary *parameters = @{
  56. @"content": comment.content,
  57. @"context": @"edit",
  58. };
  59. [self.wordPressComRestApi POST:requestUrl
  60. parameters:parameters
  61. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  62. // TODO: validate response
  63. RemoteComment *comment = [self remoteCommentFromJSONDictionary:responseObject];
  64. if (success) {
  65. success(comment);
  66. }
  67. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  68. if (failure) {
  69. failure(error);
  70. }
  71. }];
  72. }
  73. - (void)updateComment:(RemoteComment *)comment
  74. success:(void (^)(RemoteComment *comment))success
  75. failure:(void (^)(NSError *))failure
  76. {
  77. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@", self.siteID, comment.commentID];
  78. NSString *requestUrl = [self pathForEndpoint:path
  79. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  80. NSDictionary *parameters = @{
  81. @"content": comment.content,
  82. @"context": @"edit",
  83. };
  84. [self.wordPressComRestApi POST:requestUrl
  85. parameters:parameters
  86. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  87. // TODO: validate response
  88. RemoteComment *comment = [self remoteCommentFromJSONDictionary:responseObject];
  89. if (success) {
  90. success(comment);
  91. }
  92. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  93. if (failure) {
  94. failure(error);
  95. }
  96. }];
  97. }
  98. - (void)moderateComment:(RemoteComment *)comment
  99. success:(void (^)(RemoteComment *))success
  100. failure:(void (^)(NSError *))failure
  101. {
  102. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@", self.siteID, comment.commentID];
  103. NSString *requestUrl = [self pathForEndpoint:path
  104. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  105. NSDictionary *parameters = @{
  106. @"status": [self remoteStatusWithStatus:comment.status],
  107. @"context": @"edit",
  108. };
  109. [self.wordPressComRestApi POST:requestUrl
  110. parameters:parameters
  111. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  112. // TODO: validate response
  113. RemoteComment *comment = [self remoteCommentFromJSONDictionary:responseObject];
  114. if (success) {
  115. success(comment);
  116. }
  117. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  118. if (failure) {
  119. failure(error);
  120. }
  121. }];
  122. }
  123. - (void)trashComment:(RemoteComment *)comment
  124. success:(void (^)())success
  125. failure:(void (^)(NSError *error))failure
  126. {
  127. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@/delete", self.siteID, comment.commentID];
  128. NSString *requestUrl = [self pathForEndpoint:path
  129. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  130. [self.wordPressComRestApi POST:requestUrl
  131. parameters:nil
  132. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  133. if (success) {
  134. success();
  135. }
  136. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  137. if (failure) {
  138. failure(error);
  139. }
  140. }];
  141. }
  142. #pragma mark Post-centric methods
  143. - (void)syncHierarchicalCommentsForPost:(NSNumber *)postID
  144. page:(NSUInteger)page
  145. number:(NSUInteger)number
  146. success:(void (^)(NSArray *comments))success
  147. failure:(void (^)(NSError *error))failure
  148. {
  149. NSString *path = [NSString stringWithFormat:@"sites/%@/posts/%@/replies?order=ASC&hierarchical=1&page=%d&number=%d", self.siteID, postID, page, number];
  150. NSString *requestUrl = [self pathForEndpoint:path
  151. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  152. [self.wordPressComRestApi GET:requestUrl parameters:nil success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  153. if (success) {
  154. NSDictionary *dict = (NSDictionary *)responseObject;
  155. NSArray *comments = [self remoteCommentsFromJSONArray:[dict arrayForKey:@"comments"]];
  156. success(comments);
  157. }
  158. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  159. if (failure) {
  160. failure(error);
  161. }
  162. }];
  163. }
  164. #pragma mark - Public Methods
  165. - (void)updateCommentWithID:(NSNumber *)commentID
  166. content:(NSString *)content
  167. success:(void (^)())success
  168. failure:(void (^)(NSError *error))failure
  169. {
  170. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@", self.siteID, commentID];
  171. NSString *requestUrl = [self pathForEndpoint:path
  172. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  173. NSDictionary *parameters = @{
  174. @"content": content,
  175. @"context": @"edit",
  176. };
  177. [self.wordPressComRestApi POST:requestUrl
  178. parameters:parameters
  179. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  180. if (success) {
  181. success();
  182. }
  183. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  184. if (failure) {
  185. failure(error);
  186. }
  187. }];
  188. }
  189. - (void)replyToPostWithID:(NSNumber *)postID
  190. content:(NSString *)content
  191. success:(void (^)(RemoteComment *comment))success
  192. failure:(void (^)(NSError *error))failure
  193. {
  194. NSString *path = [NSString stringWithFormat:@"sites/%@/posts/%@/replies/new", self.siteID, postID];
  195. NSString *requestUrl = [self pathForEndpoint:path
  196. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  197. NSDictionary *parameters = @{@"content": content};
  198. [self.wordPressComRestApi POST:requestUrl
  199. parameters:parameters
  200. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  201. if (success) {
  202. NSDictionary *commentDict = (NSDictionary *)responseObject;
  203. RemoteComment *comment = [self remoteCommentFromJSONDictionary:commentDict];
  204. success(comment);
  205. }
  206. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  207. if (failure) {
  208. failure(error);
  209. }
  210. }];
  211. }
  212. - (void)replyToCommentWithID:(NSNumber *)commentID
  213. content:(NSString *)content
  214. success:(void (^)(RemoteComment *comment))success
  215. failure:(void (^)(NSError *error))failure
  216. {
  217. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@/replies/new", self.siteID, commentID];
  218. NSString *requestUrl = [self pathForEndpoint:path
  219. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  220. NSDictionary *parameters = @{
  221. @"content": content,
  222. @"context": @"edit",
  223. };
  224. [self.wordPressComRestApi POST:requestUrl
  225. parameters:parameters
  226. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  227. if (success) {
  228. NSDictionary *commentDict = (NSDictionary *)responseObject;
  229. RemoteComment *comment = [self remoteCommentFromJSONDictionary:commentDict];
  230. success(comment);
  231. }
  232. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  233. if (failure) {
  234. failure(error);
  235. }
  236. }];
  237. }
  238. - (void)moderateCommentWithID:(NSNumber *)commentID
  239. status:(NSString *)status
  240. success:(void (^)())success
  241. failure:(void (^)(NSError *error))failure
  242. {
  243. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@", self.siteID, commentID];
  244. NSString *requestUrl = [self pathForEndpoint:path
  245. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  246. NSDictionary *parameters = @{
  247. @"status" : status,
  248. @"context" : @"edit",
  249. };
  250. [self.wordPressComRestApi POST:requestUrl
  251. parameters:parameters
  252. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  253. if (success) {
  254. success();
  255. }
  256. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  257. if (failure) {
  258. failure(error);
  259. }
  260. }];
  261. }
  262. - (void)trashCommentWithID:(NSNumber *)commentID
  263. success:(void (^)())success
  264. failure:(void (^)(NSError *error))failure
  265. {
  266. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@/delete", self.siteID, commentID];
  267. NSString *requestUrl = [self pathForEndpoint:path
  268. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  269. [self.wordPressComRestApi POST:requestUrl
  270. parameters:nil
  271. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  272. if (success) {
  273. success();
  274. }
  275. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  276. if (failure) {
  277. failure(error);
  278. }
  279. }];
  280. }
  281. - (void)likeCommentWithID:(NSNumber *)commentID
  282. success:(void (^)())success
  283. failure:(void (^)(NSError *error))failure
  284. {
  285. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@/likes/new", self.siteID, commentID];
  286. NSString *requestUrl = [self pathForEndpoint:path
  287. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  288. [self.wordPressComRestApi POST:requestUrl
  289. parameters:nil
  290. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  291. if (success) {
  292. success();
  293. }
  294. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  295. if (failure) {
  296. failure(error);
  297. }
  298. }];
  299. }
  300. - (void)unlikeCommentWithID:(NSNumber *)commentID
  301. success:(void (^)())success
  302. failure:(void (^)(NSError *error))failure
  303. {
  304. NSString *path = [NSString stringWithFormat:@"sites/%@/comments/%@/likes/mine/delete", self.siteID, commentID];
  305. NSString *requestUrl = [self pathForEndpoint:path
  306. withVersion:ServiceRemoteWordPressComRESTApiVersion_1_1];
  307. [self.wordPressComRestApi POST:requestUrl
  308. parameters:nil
  309. success:^(id responseObject, NSHTTPURLResponse *httpResponse) {
  310. if (success) {
  311. success();
  312. }
  313. } failure:^(NSError *error, NSHTTPURLResponse *httpResponse) {
  314. if (failure) {
  315. failure(error);
  316. }
  317. }];
  318. }
  319. #pragma mark - Private methods
  320. - (NSArray *)remoteCommentsFromJSONArray:(NSArray *)jsonComments
  321. {
  322. return [jsonComments wp_map:^id(NSDictionary *jsonComment) {
  323. return [self remoteCommentFromJSONDictionary:jsonComment];
  324. }];
  325. }
  326. - (RemoteComment *)remoteCommentFromJSONDictionary:(NSDictionary *)jsonDictionary
  327. {
  328. RemoteComment *comment = [RemoteComment new];
  329. comment.author = jsonDictionary[@"author"][@"name"];
  330. // Email might be `false`, turn into `nil`
  331. comment.authorEmail = [jsonDictionary[@"author"] stringForKey:@"email"];
  332. comment.authorUrl = jsonDictionary[@"author"][@"URL"];
  333. comment.authorAvatarURL = [jsonDictionary stringForKeyPath:@"author.avatar_URL"];
  334. comment.commentID = jsonDictionary[@"ID"];
  335. comment.content = jsonDictionary[@"content"];
  336. comment.date = [NSDate dateWithWordPressComJSONString:jsonDictionary[@"date"]];
  337. comment.link = jsonDictionary[@"URL"];
  338. comment.parentID = [jsonDictionary numberForKeyPath:@"parent.ID"];
  339. comment.postID = [jsonDictionary numberForKeyPath:@"post.ID"];
  340. comment.postTitle = [jsonDictionary stringForKeyPath:@"post.title"];
  341. comment.status = [self statusWithRemoteStatus:jsonDictionary[@"status"]];
  342. comment.type = jsonDictionary[@"type"];
  343. comment.isLiked = [[jsonDictionary numberForKey:@"i_like"] boolValue];
  344. comment.likeCount = [jsonDictionary numberForKey:@"like_count"];
  345. return comment;
  346. }
  347. - (NSString *)statusWithRemoteStatus:(NSString *)remoteStatus
  348. {
  349. NSString *status = remoteStatus;
  350. if ([status isEqualToString:@"unapproved"]) {
  351. status = @"hold";
  352. } else if ([status isEqualToString:@"approved"]) {
  353. status = @"approve";
  354. }
  355. return status;
  356. }
  357. - (NSString *)remoteStatusWithStatus:(NSString *)status
  358. {
  359. NSString *remoteStatus = status;
  360. if ([remoteStatus isEqualToString:@"hold"]) {
  361. remoteStatus = @"unapproved";
  362. } else if ([remoteStatus isEqualToString:@"approve"]) {
  363. remoteStatus = @"approved";
  364. }
  365. return remoteStatus;
  366. }
  367. @end