mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
parent
bf715c8235
commit
1deeff2336
@ -8,6 +8,7 @@ import { ajax } from "discourse/lib/ajax";
|
|||||||
import Category from "discourse/models/category";
|
import Category from "discourse/models/category";
|
||||||
import GroupHistory from "discourse/models/group-history";
|
import GroupHistory from "discourse/models/group-history";
|
||||||
import RestModel from "discourse/models/rest";
|
import RestModel from "discourse/models/rest";
|
||||||
|
import Site from "discourse/models/site";
|
||||||
import Topic from "discourse/models/topic";
|
import Topic from "discourse/models/topic";
|
||||||
import User from "discourse/models/user";
|
import User from "discourse/models/user";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
@ -463,15 +464,16 @@ export default class Group extends RestModel {
|
|||||||
data.category_id = parseInt(opts.categoryId, 10);
|
data.category_id = parseInt(opts.categoryId, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
const posts = await ajax(`/groups/${this.name}/${type}.json`, { data });
|
const result = await ajax(`/groups/${this.name}/${type}.json`, { data });
|
||||||
const categories = await Category.asyncFindByIds(
|
|
||||||
posts.map((p) => p.category_id)
|
|
||||||
);
|
|
||||||
|
|
||||||
return posts.map((p) => {
|
result.categories?.forEach((category) => {
|
||||||
|
Site.current().updateCategory(category);
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.posts.map((p) => {
|
||||||
p.user = User.create(p.user);
|
p.user = User.create(p.user);
|
||||||
p.topic = Topic.create(p.topic);
|
p.topic = Topic.create(p.topic);
|
||||||
p.category = categories[p.category_id];
|
p.category = Category.findById(p.category_id);
|
||||||
return EmberObject.create(p);
|
return EmberObject.create(p);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -259,7 +259,8 @@ export default {
|
|||||||
offset: 0,
|
offset: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"/groups/discourse/posts.json": [
|
"/groups/discourse/posts.json": {
|
||||||
|
"posts": [
|
||||||
{
|
{
|
||||||
id: 94607,
|
id: 94607,
|
||||||
cooked:
|
cooked:
|
||||||
@ -1318,8 +1319,9 @@ export default {
|
|||||||
avatar_template:
|
avatar_template:
|
||||||
"/user_avatar/meta.discourse.org/codinghorror/{size}/5297.png",
|
"/user_avatar/meta.discourse.org/codinghorror/{size}/5297.png",
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
],
|
|
||||||
"/groups/alternative-group.json": {
|
"/groups/alternative-group.json": {
|
||||||
group: {
|
group: {
|
||||||
id: 57,
|
id: 57,
|
||||||
|
@ -196,7 +196,16 @@ class GroupsController < ApplicationController
|
|||||||
|
|
||||||
posts =
|
posts =
|
||||||
group.posts_for(guardian, params.permit(:before_post_id, :before, :category_id)).limit(20)
|
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
|
end
|
||||||
|
|
||||||
def posts_feed
|
def posts_feed
|
||||||
|
@ -551,7 +551,7 @@ RSpec.describe GroupsController do
|
|||||||
get "/groups/#{group.name}/posts.json"
|
get "/groups/#{group.name}/posts.json"
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
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
|
||||||
|
|
||||||
it "returns moderator actions" do
|
it "returns moderator actions" do
|
||||||
@ -560,7 +560,7 @@ RSpec.describe GroupsController do
|
|||||||
get "/groups/#{group.name}/posts.json"
|
get "/groups/#{group.name}/posts.json"
|
||||||
|
|
||||||
expect(response.status).to eq(200)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user