Skip to content

Commit 53b2822

Browse files
authored
Merge pull request #17869 from wordpress-mobile/fix/17752-restore_comment
Threaded comment moderation: don't show non-approved cached comments
2 parents 7ace517 + 0c0c1d0 commit 53b2822

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

WordPress/Classes/ViewRelated/Reader/Comments/ReaderCommentsViewController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ typedef NS_ENUM(NSUInteger, ReaderCommentsSource) {
3131
/// Navigates to the specified comment when the view appears
3232
@property (nonatomic, strong) NSNumber *navigateToCommentID;
3333

34+
35+
// Comment moderation support.
36+
@property (nonatomic, assign, readwrite) BOOL commentModified;
37+
- (void)refreshAfterCommentModeration;
38+
3439
@end

WordPress/Classes/ViewRelated/Reader/Comments/ReaderCommentsViewController.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ - (void)viewDidLoad
144144
{
145145
[super viewDidLoad];
146146
self.view.backgroundColor = [UIColor murielBasicBackground];
147+
self.commentModified = NO;
147148

148149
[self checkIfLoggedIn];
149150

@@ -195,6 +196,11 @@ - (void)viewWillDisappear:(BOOL)animated
195196
{
196197
[super viewWillDisappear:animated];
197198
[self dismissNotice];
199+
200+
if (self.commentModified) {
201+
// Don't post the notification until the view is being dismissed to avoid purging cached comments prematurely.
202+
[self postCommentModifiedNotification];
203+
}
198204

199205
#pragma clang diagnostic push
200206
#pragma clang diagnostic ignored "-Wunused-result"
@@ -933,6 +939,12 @@ - (void)refreshNoResultsView
933939
[self.noResultsViewController didMoveToParentViewController:self];
934940
}
935941

942+
- (void)refreshAfterCommentModeration
943+
{
944+
[self.tableViewHandler refreshTableView];
945+
[self refreshNoResultsView];
946+
}
947+
936948
- (void)updateTableViewForAttachments
937949
{
938950
[self.tableView performBatchUpdates:nil completion:nil];
@@ -1199,9 +1211,11 @@ - (NSFetchRequest *)fetchRequest
11991211
return nil;
12001212
}
12011213

1202-
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Comment class])];
1203-
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"post = %@", self.post];
1214+
// Moderated comments could still be cached, so filter out non-approved comments.
1215+
NSString *approvedStatus = [Comment descriptionFor:CommentStatusTypeApproved];
12041216

1217+
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([Comment class])];
1218+
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"post = %@ AND status = %@", self.post, approvedStatus];
12051219
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"hierarchy" ascending:YES];
12061220
[fetchRequest setSortDescriptors:@[sortDescriptor]];
12071221

@@ -1225,12 +1239,6 @@ - (void)configureCell:(UITableViewCell *)aCell atIndexPath:(NSIndexPath *)indexP
12251239
}
12261240

12271241
Comment *comment = [self.tableViewHandler.resultsController objectAtIndexPath:indexPath];
1228-
// The user may have moderated a comment, but it is not removed from the post yet.
1229-
// So check the status to be sure only Approved comments are displayed.
1230-
if (![comment.status isEqualToString:[Comment descriptionFor:CommentStatusTypeApproved]]) {
1231-
return;
1232-
}
1233-
12341242
if ([self newCommentThreadEnabled]) {
12351243
CommentContentTableViewCell *cell = (CommentContentTableViewCell *)aCell;
12361244
[self configureContentCell:cell comment:comment indexPath:indexPath handler:self.tableViewHandler];
@@ -1290,8 +1298,6 @@ - (void)configureCell:(UITableViewCell *)aCell atIndexPath:(NSIndexPath *)indexP
12901298
};
12911299
}
12921300

1293-
1294-
12951301
NSAttributedString *attrStr = [self cacheContentForComment:comment];
12961302
[cell configureCellWithComment:comment attributedString:attrStr];
12971303
}

WordPress/Classes/ViewRelated/Reader/Comments/ReaderCommentsViewController.swift

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ extension NSNotification.Name {
142142
WPAnalytics.trackReader(.readerArticleCommentsOpened, properties: properties)
143143
}
144144

145+
// MARK: - Notification
146+
147+
@objc func postCommentModifiedNotification() {
148+
NotificationCenter.default.post(name: .ReaderCommentModifiedNotification, object: nil)
149+
}
150+
145151
}
146152

147153
// MARK: - Popover Presentation Delegate
@@ -190,13 +196,13 @@ private extension ReaderCommentsViewController {
190196
return [
191197
[
192198
.unapprove { [weak self] in
193-
self?.moderateComment(comment, status: .pending, handler: handler)
199+
self?.moderateComment(comment, status: .pending)
194200
},
195201
.spam { [weak self] in
196-
self?.moderateComment(comment, status: .spam, handler: handler)
202+
self?.moderateComment(comment, status: .spam)
197203
},
198204
.trash { [weak self] in
199-
self?.moderateComment(comment, status: .unapproved, handler: handler)
205+
self?.moderateComment(comment, status: .unapproved)
200206
}
201207
],
202208
[
@@ -223,7 +229,7 @@ private extension ReaderCommentsViewController {
223229
CommentAnalytics.trackCommentEdited(comment: comment)
224230

225231
self?.commentService.uploadComment(comment, success: {
226-
NotificationCenter.default.post(name: .ReaderCommentModifiedNotification, object: nil)
232+
self?.commentModified = true
227233

228234
// update the thread again in case the approval status changed.
229235
tableView.reloadRows(at: [indexPath], with: .automatic)
@@ -237,23 +243,18 @@ private extension ReaderCommentsViewController {
237243
present(navigationControllerToPresent, animated: true)
238244
}
239245

240-
func moderateComment(_ comment: Comment, status: CommentStatusType, handler: WPTableViewHandler) {
246+
func moderateComment(_ comment: Comment, status: CommentStatusType) {
241247
let successBlock: (String) -> Void = { [weak self] noticeText in
242-
NotificationCenter.default.post(name: .ReaderCommentModifiedNotification, object: nil)
243-
244-
// Adjust the ReaderPost's comment count.
245-
if let post = self?.post, let commentCount = post.commentCount?.intValue {
246-
let adjustment = (status == .approved) ? 1 : -1
247-
post.commentCount = NSNumber(value: commentCount + adjustment)
248-
}
248+
self?.commentModified = true
249+
self?.refreshAfterCommentModeration()
249250

250-
// Refresh the UI. The table view handler is needed because the fetched results delegate is set to nil.
251-
handler.refreshTableViewPreservingOffset()
251+
// Dismiss any old notices to avoid stacked Undo notices.
252+
self?.dismissNotice()
252253

253254
// If the status is Approved, the user has undone a comment moderation.
254255
// So don't show the Undo option in this case.
255256
(status == .approved) ? self?.displayNotice(title: noticeText) :
256-
self?.showActionableNotice(title: noticeText, comment: comment, handler: handler)
257+
self?.showActionableNotice(title: noticeText, comment: comment)
257258
}
258259

259260
switch status {
@@ -288,12 +289,12 @@ private extension ReaderCommentsViewController {
288289
}
289290
}
290291

291-
func showActionableNotice(title: String, comment: Comment, handler: WPTableViewHandler) {
292+
func showActionableNotice(title: String, comment: Comment) {
292293
displayActionableNotice(title: title,
293294
actionTitle: .undoActionTitle,
294295
actionHandler: { [weak self] _ in
295296
// Set the Comment's status back to Approved when the user selects Undo on the notice.
296-
self?.moderateComment(comment, status: .approved, handler: handler)
297+
self?.moderateComment(comment, status: .approved)
297298
})
298299
}
299300

WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,16 @@ class ReaderDetailViewController: UIViewController, ReaderDetailView {
382382
return
383383
}
384384

385+
// Moderated comments could still be cached, so filter out non-approved comments.
386+
let approvedStatus = Comment.descriptionFor(.approved)
387+
let approvedComments = comments.filter({ $0.status == approvedStatus})
388+
385389
// Set the delegate here so the table isn't shown until fetching is complete.
386390
commentsTableView.delegate = commentsTableViewDelegate
387391
commentsTableView.dataSource = commentsTableViewDelegate
388392

389393
commentsTableViewDelegate.updateWith(post: post,
390-
comments: comments,
394+
comments: approvedComments,
391395
totalComments: totalComments,
392396
presentingViewController: self,
393397
buttonDelegate: self)

0 commit comments

Comments
 (0)