FIX: Serialize categories for group posts (#26663)

This is necessary when "lazy load categories" feature is enabled to
make sure the categories are rendered for group posts.
This commit is contained in:
Bianca Nenciu 2024-04-19 16:33:37 +03:00 committed by GitHub
parent bf715c8235
commit 1deeff2336
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1064 additions and 1051 deletions

View File

@ -8,6 +8,7 @@ import { ajax } from "discourse/lib/ajax";
import Category from "discourse/models/category";
import GroupHistory from "discourse/models/group-history";
import RestModel from "discourse/models/rest";
import Site from "discourse/models/site";
import Topic from "discourse/models/topic";
import User from "discourse/models/user";
import discourseComputed from "discourse-common/utils/decorators";
@ -463,15 +464,16 @@ export default class Group extends RestModel {
data.category_id = parseInt(opts.categoryId, 10);
}
const posts = await ajax(`/groups/${this.name}/${type}.json`, { data });
const categories = await Category.asyncFindByIds(
posts.map((p) => p.category_id)
);
const result = await ajax(`/groups/${this.name}/${type}.json`, { data });
return posts.map((p) => {
result.categories?.forEach((category) => {
Site.current().updateCategory(category);
});
return result.posts.map((p) => {
p.user = User.create(p.user);
p.topic = Topic.create(p.topic);
p.category = categories[p.category_id];
p.category = Category.findById(p.category_id);
return EmberObject.create(p);
});
}

File diff suppressed because it is too large Load Diff

View File

@ -196,7 +196,16 @@ class GroupsController < ApplicationController
posts =
group.posts_for(guardian, params.permit(:before_post_id, :before, :category_id)).limit(20)
render_serialized posts.to_a, GroupPostSerializer
response = { posts: serialize_data(posts, GroupPostSerializer) }
if guardian.can_lazy_load_categories?
category_ids = posts.map { |p| p.topic.category_id }.compact.uniq
categories = Category.secured(guardian).with_parents(category_ids)
response[:categories] = serialize_data(categories, CategoryBadgeSerializer)
end
render json: response
end
def posts_feed

View File

@ -551,7 +551,7 @@ RSpec.describe GroupsController do
get "/groups/#{group.name}/posts.json"
expect(response.status).to eq(200)
expect(response.parsed_body.first["id"]).to eq(post.id)
expect(response.parsed_body["posts"].first["id"]).to eq(post.id)
end
it "returns moderator actions" do
@ -560,7 +560,7 @@ RSpec.describe GroupsController do
get "/groups/#{group.name}/posts.json"
expect(response.status).to eq(200)
expect(response.parsed_body.first["id"]).to eq(post.id)
expect(response.parsed_body["posts"].first["id"]).to eq(post.id)
end
end