1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 15:37:51 +02:00

Improve post stream

- Return all discussion post IDs from API requests which add/remove
posts, so the post stream updates appropriately. Related to #146
- Always unload posts that are two pages away, no matter how fast
you’re scrolling
- Retrieve posts from cache instead of reloading them
- Fix various bugs. Maybe #152, needs confirmation
This commit is contained in:
Toby Zerner
2015-07-06 16:26:27 +09:30
parent a70fff881d
commit eee5133d6e
11 changed files with 93 additions and 128 deletions

View File

@@ -85,6 +85,17 @@ class UpdateAction extends SerializeResourceAction
$discussion = $state->discussion;
}
if ($posts = $discussion->getModifiedPosts()) {
$discussion->posts_ids = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id');
$discussion->posts = array_filter($posts, function ($post) {
return $post->exists;
});
$request->include = array_merge($request->include, ['posts']);
$request->link = array_merge($request->include, ['posts', 'posts.discussion', 'posts.user']);
}
return $discussion;
}
}

View File

@@ -22,13 +22,14 @@ class CreateAction extends BaseCreateAction
* @inheritdoc
*/
public static $include = [
'user' => true
'user' => true,
'discussion' => true
];
/**
* @inheritdoc
*/
public static $link = [];
public static $link = ['discussion.posts'];
/**
* @inheritdoc
@@ -84,6 +85,9 @@ class CreateAction extends BaseCreateAction
);
}
$discussion = $post->discussion;
$discussion->posts_ids = $discussion->postsVisibleTo($actor)->orderBy('time')->lists('id');
return $post;
}
}

View File

@@ -46,6 +46,13 @@ class Discussion extends Model
'last_post_number' => 'integer'
];
/**
* An array of posts that have been modified during this request.
*
* @var array
*/
protected $modifiedPosts = [];
/**
* The attributes that should be mutated to dates.
*
@@ -140,7 +147,7 @@ class Discussion extends Model
/**
* Set the discussion's start post details.
*
* @param \Flarum\Core\Posts\Post $post
* @param Post $post
* @return $this
*/
public function setStartPost(Post $post)
@@ -155,7 +162,7 @@ class Discussion extends Model
/**
* Set the discussion's last post details.
*
* @param \Flarum\Core\Posts\Post $post
* @param Post $post
* @return $this
*/
public function setLastPost(Post $post)
@@ -214,16 +221,28 @@ class Discussion extends Model
* DiscussionRenamedPost, and delete if the title has been reverted
* completely.)
*
* @param \Flarum\Core\Posts\Post $post The post to save.
* @return \Flarum\Core\Posts\Post The resulting post. It may or may not be
* the same post as was originally intended to be saved. It also may not
* exist, if the merge logic resulted in deletion.
* @param MergeablePost $post The post to save.
* @return Post The resulting post. It may or may not be the same post as
* was originally intended to be saved. It also may not exist, if the
* merge logic resulted in deletion.
*/
public function mergePost(MergeablePost $post)
{
$lastPost = $this->posts()->latest('time')->first();
return $post->saveAfter($lastPost);
$post = $post->saveAfter($lastPost);
return $this->modifiedPosts[] = $post;
}
/**
* Get the posts that have been modified during this request.
*
* @return array
*/
public function getModifiedPosts()
{
return $this->modifiedPosts;
}
/**

View File

@@ -129,26 +129,6 @@ class CommentPost extends Post
return $value;
}
/**
* Define the relationship with the user who edited the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function editUser()
{
return $this->belongsTo('Flarum\Core\Users\User', 'edit_user_id');
}
/**
* Define the relationship with the user who hid the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function hideUser()
{
return $this->belongsTo('Flarum\Core\Users\User', 'hide_user_id');
}
/**
* Get text formatter instance.
*

View File

@@ -111,6 +111,26 @@ class Post extends Model
return $this->belongsTo('Flarum\Core\Users\User', 'user_id');
}
/**
* Define the relationship with the user who edited the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function editUser()
{
return $this->belongsTo('Flarum\Core\Users\User', 'edit_user_id');
}
/**
* Define the relationship with the user who hid the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function hideUser()
{
return $this->belongsTo('Flarum\Core\Users\User', 'hide_user_id');
}
/**
* Get all posts, regardless of their type, by removing the
* `RegisteredTypesScope` global scope constraints applied on this model.

View File

@@ -8,7 +8,7 @@ class DiscussionAction extends IndexAction
{
$response = $this->apiClient->send(app('flarum.actor'), 'Flarum\Api\Actions\Discussions\ShowAction', [
'id' => $routeParams['id'],
'near' => $routeParams['near']
'page.near' => $routeParams['near']
]);
// TODO: return an object instead of an array?