Core: Post events increasing post count

Created on 11 Dec 2018  路  8Comments  路  Source: flarum/core

Bug Report

Current Behavior
Post count increases by one for Post Events, counted after post is made following the post event. See this thread on discuss for more information and demonstration links.

Steps to Reproduce

  1. Check profile for post count
  2. Perform an action on a discussion that generates a Post Event
  3. Check profile, post count should reflect previous count
  4. Make a new reply
  5. Check profile, post count should reflect previous count + 2

Expected Behavior
In step 5, post count should only reflect previous count + 1, post events should not count

Screenshots
If applicable, add screenshots to help explain your problem.

Environment

  • Flarum version: Beta 8.1
typbug

Most helpful comment

This should be an easy fix. Just change

$this->comment_count = $this->posts()->count();

to

$this->comment_count = $this->posts()->where('type', 'comment')->count();

All 8 comments

The issue seems to be that comments_count includes all types of posts, not only comments.
Thus actions like renaming a discussion are included in the count.

https://github.com/flarum/core/blob/master/src/User/User.php#L756

Honestly why is there comment_count and discussion_count while you can get the count from the relationships 馃

Performance and DB load, basically. Sometimes a little bit of extra work when inserting pays off greatly when reading.

You just have to be extra careful to sync correctly when inserting, as this issue shows. :wink:

This is probably one of the hardest things to implement correctly. Because we'll never be sure which Post type (CommentPost - default, EventPost - relabelling & locking for instance, PollPost or anything else) is valid to be included in this count without actually going over each entry in the relationship and checking the Type class implementation.

This should be an easy fix. Just change

$this->comment_count = $this->posts()->count();

to

$this->comment_count = $this->posts()->where('type', 'comment')->count();

What about is_private?

True, didn't think about that.

What if we had a UserMeta collection/class that would be extensible. In addition the default values, like comment_count and discussion_count are retrieved while dispatching an event. That logic could re-use the same logic used with the Policies perhaps..

I'm not too fond of more complexity, but it might be interesting to be able to create such statistical meta information on models.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franzliedke picture franzliedke  路  4Comments

tobyzerner picture tobyzerner  路  4Comments

luceos picture luceos  路  3Comments

luceos picture luceos  路  4Comments

jordanjay29 picture jordanjay29  路  3Comments