Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: discussion posts not always properly loaded #4156

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 2 additions & 29 deletions framework/core/js/src/forum/components/DiscussionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,40 +165,13 @@ export default class DiscussionPage<CustomAttrs extends IDiscussionPageAttrs = I
app.setTitle(discussion.title());
app.setTitleCount(0);

// When the API responds with a discussion, it will also include a number of
// posts. Some of these posts are included because they are on the first
// page of posts we want to display (determined by the `near` parameter) –
// others may be included because due to other relationships introduced by
// extensions. We need to distinguish the two so we don't end up displaying
// the wrong posts. We do so by filtering out the posts that don't have
// the 'discussion' relationship linked, then sorting and splicing.
let includedPosts: Post[] = [];
if (discussion.payload && discussion.payload.included) {
const discussionId = discussion.id();

includedPosts = discussion.payload.included
.filter(
(record) =>
record.type === 'posts' &&
record.relationships &&
record.relationships.discussion &&
!Array.isArray(record.relationships.discussion.data) &&
record.relationships.discussion.data.id === discussionId
)
// We can make this assertion because posts should be in the store,
// since they were in the discussion's payload.
.map((record) => app.store.getById<Post>('posts', record.id) as Post)
.sort((a: Post, b: Post) => a.number() - b.number())
.slice(0, 20);
}

// Set up the post stream for this discussion, along with the first page of
// posts we want to display. Tell the stream to scroll down and highlight
// the specific post that was routed to.
this.stream = new PostStreamState(discussion, includedPosts);
this.stream = new PostStreamState(discussion);
const rawNearParam = m.route.param('near');
const nearParam = rawNearParam === 'reply' ? 'reply' : parseInt(rawNearParam);
this.stream.goToNumber(nearParam || (includedPosts[0]?.number() ?? 0), true).then(() => {
this.stream.goToNumber(nearParam || 0, true).then(() => {
this.discussion = discussion;

app.current.set('discussion', discussion);
Expand Down
3 changes: 2 additions & 1 deletion framework/core/src/Api/Resource/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ public function endpoints(): array
Endpoint\Index::make()
->extractOffset(function (Context $context, array $defaultExtracts): int {
$queryParams = $context->request->getQueryParams();
$near = intval(Arr::get($queryParams, 'page.near'));

if (($near = Arr::get($queryParams, 'page.near')) > 1) {
if ($near > 1) {
$sort = $defaultExtracts['sort'];
$filter = $defaultExtracts['filter'];

Expand Down
Loading