I have a flag setup to capture comments that have been reported as spam. This action triggers an unpublish for the comment in question. When looking at nodes displayed in a view the comment count for nodes with unpublished comments are displaying all comments in the count.

Please trigger a call to _comment_update_node_statistics($nid) when the action for unpublishing comments happen otherwise other modules will show out of date information for those nodes where flagging has occurred.

Comments

joachim’s picture

Status: Active » Postponed (maintainer needs more info)

I don't think the action for unpublishing comments is Flag's responsibility...?

coolestdude1’s picture

joachim, flags can have actions on comments and one of those actions is to set the published status of said comments. The process works great for unpublishing comments but the count of these comments are stored in a different table as aggregate data for a node. Views reads from that statistics table rather than counting the number of comments every single time. Since flag module adds the flag I do believe this issue originates from flag_actions since the process happens correctly except for the statistics update.

Essentially I had to implement this code to get around this issue but it should just be patched from within flag_actions

function MODULE_flag_flag($flag, $entity_id, $account, $flagging) {
  if ($flag->name == 'spam') {
    $comment = comment_load($entity_id);
    _comment_update_node_statistics($comment->nid); // Update statistics when something is being unpublished.
  }
  
}

function MODULE_flag_unflag($flag, $entity_id, $account, $flagging) {
  if ($flag->name == 'spam') {
    $comment = comment_load($entity_id);
    _comment_update_node_statistics($comment->nid); // Update statistics when something is being published.
  }
}

Once implemented comment count numbers return to normal.

joachim’s picture

I'm not sure it's Flag's responsibility to do Comment module's housekeeping! Furthermore, _comment_update_node_statistics() is prefixed with an underscore, which in Drupal convention means it should be considered private to that module. So we shouldn't be calling it.

What I reckon should be happening is that unpublishing a comment should take care of calling _comment_update_node_statistics(), internally to Comment module.

coolestdude1’s picture

Fair enough, do you want to push this into the comments queue? I am not exactly sure how the action gets triggered from the comments module perspective, that would also mean moderating comments from the core perceptive doesn't work unless you are just approving a comment.

joachim’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

Yup, though probably best if you file a new issue.

The problem is the comment action callback:
https://api.drupal.org/api/drupal/modules!comment!comment.module/functio...

It's changing the database directly. It shouldn't do that; it should use Comment module's own API. At a push, if it really has to go direct to the database, it needs to perform its own housekeeping, as we certainly can't call its private functions, and it would be unreasonable of us to call its housekeeping functions in our actions event, on the mere offchance that the site might be configured to unpublish or publish a comment.

joachim’s picture

Status: Closed (won't fix) » Closed (works as designed)

Argh, I meant 'works as designed', since the problem is elsewhere.

coolestdude1’s picture

Thanks joachim I will follow up with them.

coolestdude1’s picture

Link to follow up issue for those with similar issues #2309957