FIX: Load category info for about page (#26519)

This commit is contained in:
Bianca Nenciu 2024-04-05 09:38:54 +03:00 committed by GitHub
parent d6c30a54d7
commit 19eb0a7055
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 32 deletions

View File

@ -1,35 +1,17 @@
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import Category from "discourse/models/category";
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
import I18n from "discourse-i18n"; import I18n from "discourse-i18n";
export default DiscourseRoute.extend({ export default DiscourseRoute.extend({
model() { model() {
return ajax("/about.json").then((result) => { return ajax("/about.json").then((result) => {
let activeAdmins = [];
let activeModerators = [];
const yearAgo = moment().locale("en").utc().subtract(1, "year"); const yearAgo = moment().locale("en").utc().subtract(1, "year");
result.about.admins.forEach((r) => { result.about.admins = result.about.admins.filter(
if (moment(r.last_seen_at) > yearAgo) { (r) => moment(r.last_seen_at) > yearAgo
activeAdmins.push(r); );
} result.about.moderators = result.about.moderators.filter(
}); (r) => moment(r.last_seen_at) > yearAgo
result.about.moderators.forEach((r) => { );
if (moment(r.last_seen_at) > yearAgo) {
activeModerators.push(r);
}
});
result.about.admins = activeAdmins;
result.about.moderators = activeModerators;
const { category_moderators: categoryModerators } = result.about;
if (categoryModerators && categoryModerators.length) {
categoryModerators.forEach((obj, index) => {
const category = Category.findById(obj.category_id);
result.about.category_moderators[index].category = category;
});
}
return result.about; return result.about;
}); });
}, },

View File

@ -7,10 +7,10 @@ class About
class CategoryMods class CategoryMods
include ActiveModel::Serialization include ActiveModel::Serialization
attr_reader :category_id, :moderators attr_reader :category, :moderators
def initialize(category_id, moderators) def initialize(category, moderators)
@category_id = category_id @category = category
@moderators = moderators @moderators = moderators
end end
end end
@ -85,9 +85,10 @@ class About
ORDER BY c.position ORDER BY c.position
SQL SQL
cats = Category.where(id: results.map(&:category_id)).index_by(&:id)
mods = User.where(id: results.map(&:user_ids).flatten.uniq).index_by(&:id) mods = User.where(id: results.map(&:user_ids).flatten.uniq).index_by(&:id)
results.map { |row| CategoryMods.new(row.category_id, mods.values_at(*row.user_ids)) } results.map { |row| CategoryMods.new(cats[row.category_id], mods.values_at(*row.user_ids)) }
end end
def category_mods_limit def category_mods_limit

View File

@ -1,13 +1,16 @@
# frozen_string_literal: true # frozen_string_literal: true
class AboutSerializer < ApplicationSerializer class AboutSerializer < ApplicationSerializer
class CategoryAboutSerializer < ApplicationSerializer
attributes :id, :name, :color, :slug, :parent_category_id
end
class UserAboutSerializer < BasicUserSerializer class UserAboutSerializer < BasicUserSerializer
attributes :title, :last_seen_at attributes :title, :last_seen_at
end end
class AboutCategoryModsSerializer < ApplicationSerializer class AboutCategoryModsSerializer < ApplicationSerializer
attributes :category_id has_one :category, serializer: CategoryAboutSerializer, embed: :objects
has_many :moderators, serializer: UserAboutSerializer, embed: :objects has_many :moderators, serializer: UserAboutSerializer, embed: :objects
end end

View File

@ -87,7 +87,7 @@ RSpec.describe About do
it "lists moderators of the category that the current user can see" do it "lists moderators of the category that the current user can see" do
results = About.new(private_cat_moderator).category_moderators results = About.new(private_cat_moderator).category_moderators
expect(results.map(&:category_id)).to contain_exactly(public_cat.id, private_cat.id) expect(results.map(&:category).map(&:id)).to contain_exactly(public_cat.id, private_cat.id)
expect(results.map(&:moderators).flatten.map(&:id).uniq).to contain_exactly( expect(results.map(&:moderators).flatten.map(&:id).uniq).to contain_exactly(
public_cat_moderator.id, public_cat_moderator.id,
common_moderator.id, common_moderator.id,
@ -97,7 +97,7 @@ RSpec.describe About do
[public_cat_moderator, user, nil].each do |u| [public_cat_moderator, user, nil].each do |u|
results = About.new(u).category_moderators results = About.new(u).category_moderators
expect(results.map(&:category_id)).to contain_exactly(public_cat.id) expect(results.map(&:category).map(&:id)).to contain_exactly(public_cat.id)
expect(results.map(&:moderators).flatten.map(&:id)).to eq( expect(results.map(&:moderators).flatten.map(&:id)).to eq(
[public_cat_moderator.id, common_moderator.id, common_moderator_2.id], [public_cat_moderator.id, common_moderator.id, common_moderator_2.id],
) )