Change the way post count metadata is stored

We care about the number of “comment” posts, not the number of posts in
total.
This commit is contained in:
Toby Zerner 2015-01-16 17:10:54 +10:30
parent 67afcbf641
commit 1f54876705
7 changed files with 17 additions and 17 deletions

View File

@ -15,11 +15,11 @@ export default Ember.Handlebars.makeBoundHelper(function(time) {
var diff = Math.abs(m.diff(moment()));
if (diff < 60 * minute) {
ago = moment.duration(diff).minutes()+'m';
ago = moment.duration(diff).minutes()+'m ago';
} else if (diff < 24 * hour) {
ago = moment.duration(diff).hours()+'h';
ago = moment.duration(diff).hours()+'h ago';
} else if (diff < 30 * day) {
ago = moment.duration(diff).days()+'d';
ago = moment.duration(diff).days()+'d ago';
} else if (m.year() == moment().year()) {
ago = m.format('D MMM');
} else {

View File

@ -24,10 +24,10 @@ var Discussion = DS.Model.extend({
relevantPosts: DS.hasMany('post'),
postsCount: DS.attr('number'),
commentsCount: DS.attr('number'),
repliesCount: function() {
return Math.max(0, this.get('postsCount') - 1);
}.property('postsCount'),
return Math.max(0, this.get('commentsCount') - 1);
}.property('commentsCount'),
posts: DS.attr('string'),
postIds: function() {

View File

@ -28,7 +28,7 @@ class DiscussionSerializer extends DiscussionBasicSerializer
$attributes = parent::attributes($discussion);
$attributes += [
'postsCount' => (int) $discussion->posts_count,
'commentsCount' => (int) $discussion->comments_count,
'startTime' => $discussion->start_time->toRFC3339String(),
'lastTime' => $discussion->last_time ? $discussion->last_time->toRFC3339String() : null,
'lastPostNumber' => $discussion->last_post_number,

View File

@ -19,7 +19,7 @@ class Discussion extends Entity
protected static $rules = [
'title' => 'required',
'start_time' => 'required|date',
'posts_count' => 'integer',
'comments_count' => 'integer',
'start_user_id' => 'integer',
'start_post_id' => 'integer',
'last_time' => 'date',
@ -79,13 +79,13 @@ class Discussion extends Entity
public function refreshLastPost()
{
$lastPost = $this->dialog()->orderBy('time', 'desc')->first();
$lastPost = $this->comments()->orderBy('time', 'desc')->first();
$this->setLastPost($lastPost);
}
public function refreshPostsCount()
public function refreshCommentsCount()
{
$this->posts_count = $this->dialog()->count();
$this->comments_count = $this->comments()->count();
}
public function rename($title, $user)
@ -109,7 +109,7 @@ class Discussion extends Entity
return $this->hasMany('Flarum\Core\Posts\Post')->orderBy('time', 'asc');
}
public function dialog()
public function comments()
{
return $this->posts()->where('type', 'comment')->whereNull('delete_time');
}

View File

@ -16,7 +16,7 @@ class DiscussionFinder
protected $sortMap = [
'lastPost' => ['last_time', 'desc'],
'replies' => ['posts_count', 'desc'],
'replies' => ['comments_count', 'desc'],
'created' => ['start_time', 'desc']
];

View File

@ -29,7 +29,7 @@ class DiscussionTableSeeder extends Seeder
'start_time' => $faker->dateTimeThisYear,
'start_user_id' => rand(1, $users)
]);
$discussion->posts_count = $posts_count;
$discussion->comments_count = $posts_count;
$post = Post::create([
'discussion_id' => $discussion->id,
@ -57,7 +57,7 @@ class DiscussionTableSeeder extends Seeder
for ($j = 0; $j < $count - 1; $j++) {
if (rand(1, 100) == 1) {
$discussion->posts_count--;
$discussion->comments_count--;
$post = Post::create([
'discussion_id' => $discussion->id,
@ -71,7 +71,7 @@ class DiscussionTableSeeder extends Seeder
$deleted = rand(1, 100) == 1;
if ($deleted) {
$discussion->posts_count--;
$discussion->comments_count--;
}
$post = Post::create([

View File

@ -18,7 +18,7 @@ class CreateDiscussionsTable extends Migration {
$table->increments('id');
$table->string('title');
$table->integer('posts_count')->unsigned()->default(0);
$table->integer('comments_count')->unsigned()->default(0);
$table->integer('number_index')->unsigned()->default(0);
$table->dateTime('start_time');