A platform for community discussion. Free, open, simple.
FIX: Localize the user activity stream
Régis Hanol211a46b54996
diff --git a/app/controllers/user_actions_controller.rb b/app/controllers/user_actions_controller.rbindex 1d601d274ff..8a4f161f527 100644--- a/app/controllers/user_actions_controller.rb+++ b/app/controllers/user_actions_controller.rb@@ -32,7 +32,9 @@ class UserActionsController < ApplicationControllerstream = UserAction.stream(opts).to_a- response = { user_actions: serialize_data(stream, UserActionSerializer) }+ response = {+ user_actions: serialize_data(stream, UserActionSerializer, localization_opts(stream)),+ }if guardian.can_lazy_load_categories?category_ids = stream.map(&:category_id).compact.uniq@@ -53,11 +55,26 @@ class UserActionsController < ApplicationControllerensure_user_actions_visible!(user, [stream_item.action_type])- render_serialized(stream_item, UserActionSerializer)+ render_serialized(stream_item, UserActionSerializer, localization_opts([stream_item]))endprivate+ def localization_opts(stream)+ return {} if !SiteSetting.content_localization_enabled++ {+ localized_topics:+ Topic.where(id: stream.map(&:topic_id)).includes(:localizations).index_by(&:id),+ localized_posts:+ Post+ .with_deleted+ .where(id: stream.map(&:cooked_post_id))+ .includes(:localizations)+ .index_by(&:id),+ }+ end+def ensure_user_actions_visible!(user, action_types)raise Discourse::NotFound unless guardian.can_see_profile?(user)raise Discourse::NotFound unless guardian.can_see_user_actions?(user, action_types)diff --git a/app/models/user_action.rb b/app/models/user_action.rbindex 9cdbe1f11ec..b248b74171c 100644--- a/app/models/user_action.rb+++ b/app/models/user_action.rb@@ -249,6 +249,7 @@ class UserAction < ActiveRecord::Basepu.uploaded_avatar_id,#{acting_cols.join(", ")},coalesce(p.cooked, p2.cooked) cooked,+ coalesce(p.id, p2.id) cooked_post_id,CASE WHEN coalesce(p.deleted_at, p2.deleted_at, t.deleted_at) IS NULL THEN false ELSE true END deleted,p.hidden,p.post_type,diff --git a/app/serializers/user_action_serializer.rb b/app/serializers/user_action_serializer.rbindex 0958de3d9dd..b702dbc69ce 100644--- a/app/serializers/user_action_serializer.rb+++ b/app/serializers/user_action_serializer.rb@@ -35,6 +35,10 @@ class UserActionSerializer < ApplicationSerializer:closed,:archived+ def title+ ContentLocalization.translated_topic_title(source_topic, scope) || object.title+ end+def avatar_templateUser.avatar_template(object.username, object.uploaded_avatar_id)end@@ -98,4 +102,14 @@ class UserActionSerializer < ApplicationSerializerdef action_code_pathobject.action_code_pathend++ private++ def source_topic+ @options[:localized_topics].to_h[object.topic_id]+ end++ def excerpt_source_post+ @options[:localized_posts].to_h[object.cooked_post_id]+ endenddiff --git a/spec/requests/user_actions_controller_spec.rb b/spec/requests/user_actions_controller_spec.rbindex d4fa4a2b16f..08339c7cf00 100644--- a/spec/requests/user_actions_controller_spec.rb+++ b/spec/requests/user_actions_controller_spec.rb@@ -393,4 +393,83 @@ RSpec.describe UserActionsController doexpect(response).to have_http_status :not_foundendend++ describe "content localization" do+ fab!(:author) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:viewer) { Fabricate(:user, locale: "ja") }+ fab!(:staff_viewer) { Fabricate(:moderator, locale: "ja") }++ let!(:topic_post) do+ UserActionManager.enable+ create_post(+ user: author,+ title: "Original english title",+ raw: "Original topic body",+ locale: "en",+ )+ end+ let(:topic) { topic_post.topic }+ let!(:reply) do+ create_post(user: author, topic: topic, raw: "Original reply body", locale: "en")+ end++ let(:new_topic_action) do+ response.parsed_body["user_actions"].find { |a| a["action_type"] == UserAction::NEW_TOPIC }+ end+ let(:reply_action) do+ response.parsed_body["user_actions"].find { |a| a["action_type"] == UserAction::REPLY }+ end++ before do+ SiteSetting.content_localization_enabled = true++ Fabricate(:topic_localization, topic: topic, locale: "ja", title: "翻訳されたタイトル")+ Fabricate(:post_localization, post: topic_post, locale: "ja", cooked: "<p>翻訳された本文</p>")+ Fabricate(:post_localization, post: reply, locale: "ja", cooked: "<p>翻訳された返信</p>")++ sign_in(viewer)+ end++ it "returns the translated title and excerpt" do+ get "/user_actions.json", params: { username: author.username }++ expect(response).to have_http_status :ok+ expect(new_topic_action["title"]).to eq("翻訳されたタイトル")+ expect(new_topic_action["excerpt"]).to eq("翻訳された本文")+ expect(reply_action["excerpt"]).to eq("翻訳された返信")+ expect(new_topic_action["slug"]).to eq(topic.slug)+ end++ it "returns the original content without preloading when localization is disabled" do+ SiteSetting.content_localization_enabled = false++ queries =+ track_sql_queries { get "/user_actions.json", params: { username: author.username } }++ expect(new_topic_action["title"]).to eq("Original english title")+ expect(queries.grep(/FROM "?(topic|post)_localizations"?/)).to be_empty+ end++ it "returns the translated excerpt of a deleted post to staff" do+ reply.trash!(staff_viewer)+ sign_in(staff_viewer)++ get "/user_actions.json", params: { username: author.username }++ expect(reply_action["excerpt"]).to eq("翻訳された返信")+ end++ it "returns the translated title and excerpt for a single action" do+ user_action =+ UserAction.find_by!(+ user_id: author.id,+ action_type: UserAction::NEW_TOPIC,+ target_topic_id: topic.id,+ )++ get "/user_actions/#{user_action.id}.json"++ expect(response.parsed_body["user_action"]["excerpt"]).to eq("翻訳された本文")+ end+ endend